-
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.
Showing
7 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/io/github/robothy/sdwebui/sdk/ExtraImage.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,10 @@ | ||
package io.github.robothy.sdwebui.sdk; | ||
|
||
|
||
import io.github.robothy.sdwebui.sdk.models.options.ExtraImageOptions; | ||
import io.github.robothy.sdwebui.sdk.models.results.ExtraImageResult; | ||
|
||
public interface ExtraImage { | ||
|
||
ExtraImageResult extraImage(ExtraImageOptions options); | ||
} |
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
123 changes: 123 additions & 0 deletions
123
src/main/java/io/github/robothy/sdwebui/sdk/models/options/ExtraImageOptions.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,123 @@ | ||
package io.github.robothy.sdwebui.sdk.models.options; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/* | ||
{ | ||
"resize_mode": 0, | ||
"show_extras_results": true, | ||
"gfpgan_visibility": 0.07745869113225834, | ||
"codeformer_visibility": 0.47970707311691596, | ||
"codeformer_weight": 0.3154859812234816, | ||
"upscaling_resize": 5.823972728481651, | ||
"upscaling_resize_w": 78222408, | ||
"upscaling_resize_h": 11201482, | ||
"upscaling_crop": false, | ||
"upscaler_1": "sed ipsum anim proident dolor", | ||
"upscaler_2": "proident occaecat", | ||
"extras_upscaler_2_visibility": 0.6957613714482722, | ||
"upscale_first": true, | ||
"image": "http://dummyimage.com/400x400" | ||
} | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ExtraImageOptions { | ||
/** | ||
* CodeFormer Visibility,Sets the visibility of CodeFormer, values should be between 0 and 1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("codeformer_visibility") | ||
private Double codeformerVisibility = 0.0; | ||
/** | ||
* CodeFormer Weight,Sets the weight of CodeFormer, values should be between 0 and 1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("codeformer_weight") | ||
private Double codeformerWeight = 0.0; | ||
/** | ||
* Secondary upscaler visibility,Sets the visibility of secondary upscaler, values should be | ||
* between 0 and 1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("extras_upscaler_2_visibility") | ||
private Double extrasUpscaler2Visibility = 0.0; | ||
/** | ||
* GFPGAN Visibility,Sets the visibility of GFPGAN, values should be between 0 and 1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("gfpgan_visibility") | ||
private Double gfpganVisibility = 0.0; | ||
/** | ||
* Image,Image to work on, must be a Base64 string containing the image's data. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("image") | ||
private String image = null; | ||
/** | ||
* Resize Mode,Sets the resize mode: 0 to upscale by upscaling_resize amount, 1 to upscale | ||
* up to upscaling_resize_h x upscaling_resize_w. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("resize_mode") | ||
private Long resizeMode = 0L; | ||
/** | ||
* Show results,Should the backend return the generated image? | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("show_results") | ||
private Boolean showExtrasResults = false; | ||
/** | ||
* Upscale first,Should the upscaler run before restoring faces? | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscale_first") | ||
private Boolean upscaleFirst = false; | ||
/** | ||
* Main upscaler,The name of the main upscaler to use, it has to be one of this list: | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaler_1") | ||
private String upscaler1 = ""; | ||
/** | ||
* Secondary upscaler,The name of the secondary upscaler to use, it has to be one of this | ||
* list: | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaler_2") | ||
private String upscaler2 = ""; | ||
/** | ||
* Crop to fit,Should the upscaler crop the image to fit in the chosen size? | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaling_crop") | ||
private Boolean upscalingCrop = false; | ||
/** | ||
* Upscaling Factor,By how much to upscale the image, only used when resize_mode=0. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaling_resize") | ||
private Double upscalingResize = 0.0; | ||
/** | ||
* Target Height,Target height for the upscaler to hit. Only used when resize_mode=1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaling_resize_h") | ||
private Long upscalingResizeH = 512L; | ||
/** | ||
* Target Width,Target width for the upscaler to hit. Only used when resize_mode=1. | ||
*/ | ||
@Builder.Default | ||
@JsonProperty("upscaling_resize_w") | ||
private Long upscalingResizeW = 512L; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/io/github/robothy/sdwebui/sdk/models/results/ExtraImageResult.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,18 @@ | ||
package io.github.robothy.sdwebui.sdk.models.results; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ExtraImageResult { | ||
/** | ||
* HTML info,A series of HTML tags containing the process info. | ||
*/ | ||
@JsonProperty("html_info") | ||
private String htmlInfo; | ||
/** | ||
* Image,The generated image in base64 format. | ||
*/ | ||
@JsonProperty("image") | ||
private String image; | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/io/github/robothy/sdwebui/sdk/services/DefaultExtraImageService.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,72 @@ | ||
package io.github.robothy.sdwebui.sdk.services; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.robothy.sdwebui.sdk.ExtraImage; | ||
import io.github.robothy.sdwebui.sdk.SdWebuiBeanContainer; | ||
import io.github.robothy.sdwebui.sdk.models.SdWebuiOptions; | ||
import io.github.robothy.sdwebui.sdk.models.options.ExtraImageOptions; | ||
import io.github.robothy.sdwebui.sdk.models.results.ExtraImageResult; | ||
import io.github.robothy.sdwebui.sdk.utils.SdWebuiResponseUtils; | ||
import org.apache.hc.client5.http.classic.HttpClient; | ||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.core5.http.ClassicHttpRequest; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.io.entity.StringEntity; | ||
|
||
import java.io.IOException; | ||
|
||
public class DefaultExtraImageService implements ExtraImage { | ||
|
||
private static final String ExtraImage_PATH = "/sdapi/v1/extra-single-image"; | ||
|
||
private final SdWebuiBeanContainer beanContainer; | ||
|
||
public DefaultExtraImageService(SdWebuiBeanContainer beanContainer) { | ||
this.beanContainer = beanContainer; | ||
} | ||
|
||
@Override | ||
public ExtraImageResult extraImage(ExtraImageOptions options) { | ||
HttpClient httpClient = this.beanContainer.getBean(HttpClient.class); | ||
ClassicHttpRequest extraImageRequest = buildTxt2ImageRequest(options); | ||
try { | ||
return httpClient.execute(extraImageRequest, this::parseExtraImageResult); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
ClassicHttpRequest buildTxt2ImageRequest(ExtraImageOptions options) { | ||
SdWebuiOptions sdWebuiOptions = this.beanContainer.getBean(SdWebuiOptions.class); | ||
HttpPost httpPost = new HttpPost(sdWebuiOptions.getEndpoint() + ExtraImage_PATH); | ||
HttpEntity entity = buildExtraImageRequestEntity(options); | ||
httpPost.setEntity(entity); | ||
httpPost.addHeader("Content-Type", "application/json"); | ||
return httpPost; | ||
} | ||
|
||
HttpEntity buildExtraImageRequestEntity(ExtraImageOptions options) { | ||
try { | ||
String payload = this.beanContainer.getBean(ObjectMapper.class) | ||
.writeValueAsString(options); | ||
return new StringEntity(payload); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
ExtraImageResult parseExtraImageResult(ClassicHttpResponse response) { | ||
|
||
SdWebuiResponseUtils.checkResponseStatus(response); | ||
|
||
try { | ||
return this.beanContainer.getBean(ObjectMapper.class) | ||
.readValue(response.getEntity().getContent(), ExtraImageResult.class); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/test/java/io/github/robothy/sdwebui/sdk/models/options/ExtraImageOptionsTest.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,35 @@ | ||
package io.github.robothy.sdwebui.sdk.models.options; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ExtraImageOptionsTest { | ||
|
||
@Test | ||
void testSerialization() throws JsonProcessingException{ | ||
ExtraImageOptions options = ExtraImageOptions.builder() | ||
.image("") | ||
.codeformerWeight(0.1) | ||
.upscalingResizeW(1L) | ||
.upscalingResizeH(2L) | ||
.codeformerVisibility(0.2) | ||
.extrasUpscaler2Visibility(0.3) | ||
.gfpganVisibility(0.4) | ||
.showExtrasResults(true) | ||
.upscaleFirst(true) | ||
.upscaler2("upscaler2") | ||
.upscalingCrop(true) | ||
.upscalingResize(0.0) | ||
.upscaler1("upscaler1") | ||
.resizeMode(0L) | ||
.build(); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ExtraImageOptions deserializedTxt2ImageOptions = | ||
objectMapper.readValue(objectMapper.writeValueAsString(options), ExtraImageOptions.class); | ||
assertEquals(options, deserializedTxt2ImageOptions); | ||
|
||
} | ||
} |