Skip to content

Commit

Permalink
add ExtraImage (#8)
Browse files Browse the repository at this point in the history
* feat: add ExtraImg api
  • Loading branch information
1328411791 authored Mar 5, 2024
1 parent 92c99a5 commit b4c2116
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/io/github/robothy/sdwebui/sdk/ExtraImage.java
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);
}
2 changes: 1 addition & 1 deletion src/main/java/io/github/robothy/sdwebui/sdk/SdWebui.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.lang.reflect.Proxy;

public interface SdWebui extends SystemInfoFetcher, Txt2Image, Image2Image, GetSdModels, GetFaceRestorers {
public interface SdWebui extends SystemInfoFetcher, Txt2Image, Image2Image, ExtraImage, GetSdModels, GetFaceRestorers {

static SdWebui create(String endpoint) {
SdWebuiOptions options = new SdWebuiOptions(endpoint);
Expand Down
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;
}
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;
}
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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void init() {
register(CommonGetService.class, new CommonGetService(this));
register(GetSdModels.class, new DefaultGetSdModelService(this));
register(GetFaceRestorers.class, new DefaultGetFaceRestorersService(this));
register(ExtraImage.class, new DefaultExtraImageService(this));
}

}
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);

}
}

0 comments on commit b4c2116

Please sign in to comment.