-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
209 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/io/github/robothy/sdwebui/sdk/GetSdModels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.robothy.sdwebui.sdk; | ||
|
||
import io.github.robothy.sdwebui.sdk.models.results.SdModel; | ||
|
||
import java.util.List; | ||
|
||
public interface GetSdModels { | ||
|
||
List<SdModel> getSdModels(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/io/github/robothy/sdwebui/sdk/models/results/SdModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.robothy.sdwebui.sdk.models.results; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SdModel { | ||
|
||
@JsonProperty("title") | ||
private String title; | ||
|
||
@JsonProperty("model_name") | ||
private String modelName; | ||
|
||
@JsonProperty("hash") | ||
private String hash; | ||
|
||
@JsonProperty("sha256") | ||
private String sha256; | ||
|
||
@JsonProperty("filename") | ||
private String filename; | ||
|
||
@JsonProperty("config") | ||
private String config; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/robothy/sdwebui/sdk/services/DefaultGetSdModelService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.github.robothy.sdwebui.sdk.services; | ||
|
||
import io.github.robothy.sdwebui.sdk.GetSdModels; | ||
import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer; | ||
import io.github.robothy.sdwebui.sdk.models.results.SdModel; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class DefaultGetSdModelService implements GetSdModels { | ||
|
||
private final SdWebuiBeanContainer container; | ||
|
||
public DefaultGetSdModelService(SdWebuiBeanContainer container) { | ||
this.container = container; | ||
} | ||
|
||
@Override | ||
public List<SdModel> getSdModels() { | ||
return Arrays.asList(this.container.getBean(CommonGetService.class).getData("/sdapi/v1/sd-models", SdModel[].class)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/io/github/robothy/sdwebui/sdk/MockSdServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.robothy.sdwebui.sdk; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.netty.MockServer; | ||
|
||
import java.net.http.HttpRequest; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
public class MockSdServer implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback { | ||
|
||
private int port; | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
|
||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/io/github/robothy/sdwebui/sdk/models/results/SdModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.github.robothy.sdwebui.sdk.models.results; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.robothy.sdwebui.sdk.SdWebui; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.junit.jupiter.MockServerExtension; | ||
import org.mockserver.model.HttpRequest; | ||
import org.mockserver.model.HttpResponse; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@ExtendWith(MockServerExtension.class) | ||
class SdModelTest { | ||
|
||
private static final String JSON = "{\n" + | ||
" \"title\": \"v1-5-pruned-emaonly.ckpt [cc6cb27103]\",\n" + | ||
" \"model_name\": \"v1-5-pruned-emaonly\",\n" + | ||
" \"hash\": \"cc6cb27103\",\n" + | ||
" \"sha256\": \"cc6cb27103417325ff94f52b7a5d2dde45a7515b25c255d8e396c90014281516\",\n" + | ||
" \"filename\": \"C:\\\\Users\\\\admin\\\\PythonProjects\\\\stable-diffusion-webui\\\\models\\\\Stable-diffusion\\\\v1-5-pruned-emaonly.ckpt\",\n" + | ||
" \"config\": null\n" + | ||
"}"; | ||
|
||
@Test | ||
void testSerialization() throws JsonProcessingException { | ||
SdModel sdModel = new ObjectMapper().readValue(JSON, SdModel.class); | ||
assertEquals("v1-5-pruned-emaonly.ckpt [cc6cb27103]", sdModel.getTitle()); | ||
assertEquals("v1-5-pruned-emaonly", sdModel.getModelName()); | ||
assertEquals("cc6cb27103", sdModel.getHash()); | ||
assertEquals("cc6cb27103417325ff94f52b7a5d2dde45a7515b25c255d8e396c90014281516", sdModel.getSha256()); | ||
assertEquals("C:\\Users\\admin\\PythonProjects\\stable-diffusion-webui\\models\\Stable-diffusion\\v1-5-pruned-emaonly.ckpt", sdModel.getFilename()); | ||
assertNull(sdModel.getConfig()); | ||
} | ||
|
||
@Test | ||
void getGetSdModels(MockServerClient client) { | ||
client.when(new HttpRequest().withMethod("GET").withPath("/sdapi/v1/sd-models")) | ||
.respond(new HttpResponse().withStatusCode(200).withBody(" [" + | ||
"{\n" + | ||
" \"title\": \"MoyouArtificial_v10502g.safetensors [b6c1edcbe9]\",\n" + | ||
" \"model_name\": \"MoyouArtificial_v10502g\",\n" + | ||
" \"hash\": \"b6c1edcbe9\",\n" + | ||
" \"sha256\": \"b6c1edcbe9ef9fa3d38c3787d351211a775e6254b832234d97042800f33345d1\",\n" + | ||
" \"filename\": \"C:\\\\Users\\\\admin\\\\PythonProjects\\\\stable-diffusion-webui\\\\models\\\\Stable-diffusion\\\\MoyouArtificial_v10502g.safetensors\",\n" + | ||
" \"config\": null\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"title\": \"v1-5-pruned-emaonly.ckpt [cc6cb27103]\",\n" + | ||
" \"model_name\": \"v1-5-pruned-emaonly\",\n" + | ||
" \"hash\": \"cc6cb27103\",\n" + | ||
" \"sha256\": \"cc6cb27103417325ff94f52b7a5d2dde45a7515b25c255d8e396c90014281516\",\n" + | ||
" \"filename\": \"C:\\\\Users\\\\admin\\\\PythonProjects\\\\stable-diffusion-webui\\\\models\\\\Stable-diffusion\\\\v1-5-pruned-emaonly.ckpt\",\n" + | ||
" \"config\": null\n" + | ||
" }\n" + | ||
"]")); | ||
List<SdModel> sdModels = SdWebui.create("http://localhost:" + client.remoteAddress().getPort()).getSdModels(); | ||
assertEquals(2, sdModels.size()); | ||
assertEquals("MoyouArtificial_v10502g.safetensors [b6c1edcbe9]", sdModels.get(0).getTitle()); | ||
assertEquals("v1-5-pruned-emaonly.ckpt [cc6cb27103]", sdModels.get(1).getTitle()); | ||
assertEquals("MoyouArtificial_v10502g", sdModels.get(0).getModelName()); | ||
assertEquals("v1-5-pruned-emaonly", sdModels.get(1).getModelName()); | ||
assertEquals("b6c1edcbe9", sdModels.get(0).getHash()); | ||
assertEquals("cc6cb27103", sdModels.get(1).getHash()); | ||
assertEquals("b6c1edcbe9ef9fa3d38c3787d351211a775e6254b832234d97042800f33345d1", sdModels.get(0).getSha256()); | ||
assertEquals("cc6cb27103417325ff94f52b7a5d2dde45a7515b25c255d8e396c90014281516", sdModels.get(1).getSha256()); | ||
assertEquals("C:\\Users\\admin\\PythonProjects\\stable-diffusion-webui\\models\\Stable-diffusion\\MoyouArtificial_v10502g.safetensors", sdModels.get(0).getFilename()); | ||
assertEquals("C:\\Users\\admin\\PythonProjects\\stable-diffusion-webui\\models\\Stable-diffusion\\v1-5-pruned-emaonly.ckpt", sdModels.get(1).getFilename()); | ||
assertNull(sdModels.get(0).getConfig()); | ||
assertNull(sdModels.get(1).getConfig()); | ||
} | ||
|
||
} |