7 changed files with 129 additions and 3 deletions
@ -0,0 +1,32 @@ |
|||
package com.bnyer.common.core.dto; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Getter; |
|||
import lombok.Setter; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import javax.validation.constraints.NotEmpty; |
|||
import javax.validation.constraints.NotNull; |
|||
import java.io.Serializable; |
|||
|
|||
|
|||
@Getter |
|||
@Setter |
|||
@ApiModel("文生图接收类") |
|||
public class TextToImgDto implements Serializable { |
|||
|
|||
@NotNull(message = "宽度不能为空!") |
|||
@ApiModelProperty(value="图片宽度") |
|||
private Integer width; |
|||
|
|||
@NotNull(message = "高度不能为空!") |
|||
@ApiModelProperty(value="图片高度") |
|||
private Integer height; |
|||
|
|||
@ApiModelProperty(value="提示词") |
|||
private String prompt; |
|||
|
|||
@ApiModelProperty(value="风格") |
|||
private String samplerIndex; |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
package com.bnyer.common.core.vo; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Getter; |
|||
import lombok.Setter; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
|
|||
@Getter |
|||
@Setter |
|||
@ApiModel("文生图响应体") |
|||
public class TextToImgVo implements Serializable { |
|||
|
|||
@ApiModelProperty(value="图片base64集合") |
|||
private List<String> images; |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
package com.bnyer.img.service; |
|||
|
|||
import com.bnyer.common.core.dto.TextToImgDto; |
|||
import com.bnyer.common.core.vo.TextToImgVo; |
|||
|
|||
public interface StableDiffusionService { |
|||
|
|||
|
|||
TextToImgVo textToImg(TextToImgDto param); |
|||
|
|||
void imgToImg(); |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
package com.bnyer.img.service.impl; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.bnyer.common.core.dto.TextToImgDto; |
|||
import com.bnyer.common.core.vo.TextToImgVo; |
|||
import com.bnyer.img.service.StableDiffusionService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class StableDiffusionServiceImpl implements StableDiffusionService { |
|||
|
|||
@Autowired |
|||
private RestTemplate restTemplate; |
|||
|
|||
@Override |
|||
public TextToImgVo textToImg(TextToImgDto param) { |
|||
Map<String, Object> map = new HashMap<>(); |
|||
map.put("width",param.getWidth()); |
|||
map.put("height",param.getHeight()); |
|||
map.put("prompt", param.getPrompt()); |
|||
map.put("seed",-1); |
|||
map.put("batch_size",1); |
|||
map.put("cfg_scale",7); |
|||
map.put("restore_faces",false); |
|||
map.put("tiling",false); |
|||
map.put("eta",0); |
|||
map.put("sampler_index","Euler"); |
|||
//map.put("sampler_index",param.getSamplerIndex());
|
|||
map.put("steps",20); |
|||
map.put("negative_prompt","nsfw"); |
|||
JSONObject jsonObject = restTemplate.postForObject("http://localhost:7860/sdapi/v1/txt2img", map, JSONObject.class); |
|||
log.info("请求stable_diffusion响应体的为:【{}】", JSON.toJSONString(jsonObject)); |
|||
TextToImgVo img = new TextToImgVo(); |
|||
if(jsonObject != null && jsonObject.getJSONArray("images").size() > 0){ |
|||
img.setImages(jsonObject.getJSONArray("images").toJavaList(String.class)); |
|||
} |
|||
|
|||
return img; |
|||
} |
|||
|
|||
@Override |
|||
public void imgToImg() { |
|||
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue