From 51b34f42276dd4ad4cd6d7fdecd83acc4be73ca3 Mon Sep 17 00:00:00 2001 From: Penny <2500338766@qq.com> Date: Fri, 24 Mar 2023 01:43:55 +0800 Subject: [PATCH] =?UTF-8?q?feature-img-1.0:=E4=BF=AE=E5=A4=8DBUG=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E7=94=9F=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bnyer/common/core/dto/TextToImgDto.java | 32 +++++++++++ .../com/bnyer/common/core/vo/TextToImgVo.java | 21 ++++++++ .../img/controller/CreatorMiniController.java | 9 ++++ .../img/controller/TiktokMiniController.java | 2 - .../img/service/StableDiffusionService.java | 12 +++++ .../impl/StableDiffusionServiceImpl.java | 54 +++++++++++++++++++ .../img/mapper/TiktokCollectionMapper.xml | 2 +- 7 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java create mode 100644 bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java create mode 100644 bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/StableDiffusionService.java create mode 100644 bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java new file mode 100644 index 0000000..dd825b1 --- /dev/null +++ b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/dto/TextToImgDto.java @@ -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; +} diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java new file mode 100644 index 0000000..04c408e --- /dev/null +++ b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/vo/TextToImgVo.java @@ -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 images; + + private static final long serialVersionUID = 1L; +} diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/CreatorMiniController.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/CreatorMiniController.java index eb6b0ba..88a15a0 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/CreatorMiniController.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/CreatorMiniController.java @@ -67,6 +67,9 @@ public class CreatorMiniController extends BaseController { @Autowired private FollowService followService; + @Autowired + private StableDiffusionService stableDiffusionService; + @ApiOperation(value="检查手机号是否重复") @PostMapping(value = "/checkPhone") public AjaxResult checkPhone(@Validated @RequestBody @ApiParam("手机号对象") CheckPhoneDto dto){ @@ -400,4 +403,10 @@ public class CreatorMiniController extends BaseController { public AjaxResult queryCreatorFansLikeNum(@PathVariable @ApiParam("艺术家图片标签对象") Long creatorId){ return AjaxResult.success(creatorService.queryCreatorFansLikeNum(creatorId)); } + + @ApiOperation(value="文生图") + @PostMapping(value = "/textToImg") + public AjaxResult textToImg(@Validated @RequestBody @ApiParam("文生图对象") TextToImgDto param){ + return AjaxResult.success(stableDiffusionService.textToImg(param)); + } } diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java index f590807..da2802a 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/controller/TiktokMiniController.java @@ -346,7 +346,6 @@ public class TiktokMiniController extends BaseController { return AjaxResult.success(); } - @ApiOperation(value="取消关注") @PostMapping(value = "/tiktokUnFollow") public AjaxResult tiktokUnFollow(@Validated @RequestBody @ApiParam("关注对象") FollowDto dto){ @@ -355,7 +354,6 @@ public class TiktokMiniController extends BaseController { return AjaxResult.success(); } - @ApiOperation(value="查询是否关注过") @PostMapping(value = "/judgeTiktokFollow") public AjaxResult judgeTiktokFollow(@Validated @RequestBody @ApiParam("关注对象") FollowDto dto){ diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/StableDiffusionService.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/StableDiffusionService.java new file mode 100644 index 0000000..6b386b6 --- /dev/null +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/StableDiffusionService.java @@ -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(); +} diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java new file mode 100644 index 0000000..6b588b8 --- /dev/null +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java @@ -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 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() { + + } +} diff --git a/bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokCollectionMapper.xml b/bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokCollectionMapper.xml index 4040dff..aca16c3 100644 --- a/bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokCollectionMapper.xml +++ b/bnyer-services/bnyer-img/src/main/resources/com/bnyer/img/mapper/TiktokCollectionMapper.xml @@ -27,7 +27,7 @@ from img_collection itc join img_tiktok_img iti on itc.img_id = iti.id join img_type it on iti.type_id = it.id - where iti.is_show = '1' and it.is_show = '1' and iti.status = '3' and itc.is_show = '1' + where iti.is_show = '1' and it.is_show = '1' and iti.status = '2' and itc.is_show = '1' and itc.user_id = #{userId} and itc.platform = #{platform} order by itc.sort desc