diff --git a/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java b/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java index 782a2b7..f96cdc5 100644 --- a/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java +++ b/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/RemoteFileService.java @@ -34,7 +34,15 @@ public interface RemoteFileService { * @param files 文件 * @return - */ - @PostMapping("/uploadBatch") - R> uploadBatch(MultipartFile[] files); + @PostMapping(value = "/uploadBatch",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + R> uploadBatch(@RequestPart(name = "files") MultipartFile[] files,@RequestPart(name = "fileType") Integer fileType); + + /** + * 上传文件到七牛云 + * @param file 文件 + * @return - + */ + @PostMapping(value = "/uploadQiNiu",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + R uploadQiNiu(@RequestPart(name = "file") MultipartFile file, @RequestPart(name = "fileType") Integer fileType); } diff --git a/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java b/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java index 4ac58a7..c4f5d8a 100644 --- a/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java +++ b/bnyer-api/bnyer-api-file/src/main/java/com/bnyer/file/api/factory/RemoteFileFallbackFactory.java @@ -32,9 +32,14 @@ public class RemoteFileFallbackFactory implements FallbackFactory> uploadBatch(MultipartFile[] files) { + public R> uploadBatch(MultipartFile[] files,Integer fileType) { return R.fail("远程调用七牛云批量文件上传失败:" + throwable.getMessage()); } + + @Override + public R uploadQiNiu(MultipartFile file, Integer fileType) { + return R.fail("远程调用七牛云文件上传失败:" + throwable.getMessage()); + } }; } } diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java index 9c1f801..ce55cda 100644 --- a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java +++ b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/domain/AiPaint.java @@ -5,17 +5,12 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import lombok.*; + import java.io.Serializable; import java.util.Date; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.ToString; /** * ai绘画内容表 @@ -70,6 +65,56 @@ public class AiPaint implements Serializable { @ApiModelProperty(value="关键词") private String prompt; + + /** + * 反向关键词 + */ + @TableField(value = "negative_prompt") + @ApiModelProperty(value="反向关键词") + private String negativePrompt; + + /** + * 采样步数 + */ + @TableField(value = "steps") + @ApiModelProperty(value="采样步数") + private Integer steps; + + /** + * 种子值 + */ + @TableField(value = "seed") + @ApiModelProperty(value="种子值") + private Integer seed; + + /** + * 批量数量 + */ + @TableField(value = "batch_size") + @ApiModelProperty(value="批量数量") + private Integer batchSize; + + /** + * 精细度 + */ + @TableField(value = "cfg_scale") + @ApiModelProperty(value="精细度") + private Double cfgScale; + + /** + * eta值 + */ + @TableField(value = "eta") + @ApiModelProperty(value="eta值") + private Double eta; + + /** + * 采样器 + */ + @TableField(value = "sampler_index") + @ApiModelProperty(value="采样器") + private String samplerIndex; + /** * 模型名称 */ 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 index 511c6cc..37ceb51 100644 --- 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 @@ -25,9 +25,30 @@ public class TextToImgDto implements Serializable { @ApiModelProperty(value="提示词") private String prompt; + @ApiModelProperty(value="反向提示词") + private String negativePrompt; + @ApiModelProperty(value="模型") private String modelName; + @ApiModelProperty(value="采样步数") + private Integer steps; + + @ApiModelProperty(value="种子值") + private Integer seed; + + @ApiModelProperty(value="精细度") + private Double cfgScale; + + @ApiModelProperty(value="批量数量") + private Integer batchSize; + + @ApiModelProperty(value="eta值") + private Double eta; + + @ApiModelProperty(value="采样器") + private String samplerIndex; + @ApiModelProperty(value="风格名称") private String styleName; diff --git a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java index 120b940..01b5464 100644 --- a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java +++ b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/controller/SysFileController.java @@ -62,6 +62,22 @@ public class SysFileController { } } + /** + * 上传图片到七牛云 + */ + @ApiOperation(value="上传图片到七牛云") + @PostMapping("/uploadQiNiu") + public R uploadQiNiu(@RequestParam MultipartFile file,@RequestParam Integer fileType) { + try { + String url = qiniuService.userUpload(file,fileType); + // 上传并返回访问地址 + return R.ok(url); + } catch (Exception e) { + log.error("上传文件失败", e); + return R.fail("上传文件失败"); + } + } + /** * 批量上传压缩图片 * @param files diff --git a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/enumeration/AddressEnum.java b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/enumeration/AddressEnum.java index d8873ef..a64d4cc 100644 --- a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/enumeration/AddressEnum.java +++ b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/enumeration/AddressEnum.java @@ -14,7 +14,8 @@ public enum AddressEnum { PHONEBACK(3,"phoneBack/"), FRIEND(4,"friend/"), EMOJI(5,"emoji/"), - WATCH(6,"iwatch/"); + WATCH(6,"iwatch/"), + SD(7,"sd/"); private Integer businessType; diff --git a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/utils/StringUtil.java b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/utils/StringUtil.java index 9db8dd7..8e8483a 100644 --- a/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/utils/StringUtil.java +++ b/bnyer-services/bnyer-file/src/main/java/com/bnyer/file/utils/StringUtil.java @@ -146,6 +146,13 @@ public class StringUtil { */ public static String getRandomImgName(String fileName) { + String[] split = fileName.split("\\."); + if (split.length > 1) { + fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1]; + } else { + fileName = fileName + System.currentTimeMillis()+".png"; + } + int index = fileName.lastIndexOf("."); if ((fileName == null || fileName.isEmpty()) || index == -1){ diff --git a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/RestTemplateConfiguration.java b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/RestTemplateConfiguration.java index d6205ab..76c6de3 100644 --- a/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/RestTemplateConfiguration.java +++ b/bnyer-services/bnyer-img/src/main/java/com/bnyer/img/config/RestTemplateConfiguration.java @@ -21,7 +21,7 @@ public class RestTemplateConfiguration { public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); - factory.setReadTimeout(10000); + factory.setReadTimeout(15000); return factory; } } 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 index 6eca136..b4c2383 100644 --- 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 @@ -7,6 +7,7 @@ import com.bnyer.common.core.constant.RedisKeyConstant; import com.bnyer.common.core.domain.AiPaint; import com.bnyer.common.core.dto.TextToImgDto; import com.bnyer.common.core.exception.ServiceException; +import com.bnyer.common.core.utils.StringUtils; import com.bnyer.common.core.utils.TranslateUtils; import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils; import com.bnyer.common.core.vo.TextToImgVo; @@ -148,7 +149,9 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { //base64转file MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg"); //上传图片到七牛云/minio - String imgStr = remoteFileService.uploadBanner(file).getData(); + //String imgStr = remoteFileService.uploadBanner(file).getData(); + //上传图片到七牛云并存入sd文件夹 + String imgStr = remoteFileService.uploadQiNiu(file,7).getData(); //保存生辰该图片到ai绘画表 AiPaint paint = new AiPaint(); //paint.setId(); 主键改成雪花算法后启用 @@ -188,25 +191,47 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { }else{ prompt = param.getPrompt(); } - System.out.println(prompt); + log.info("正向提示词为:【{}】",prompt); + + String negaPromptText = ""; + //判断反向prompt是否包含中文,中文则翻译,否则跳过 + if(StringUtils.isNotBlank(param.getNegativePrompt())){ + if(TranslateUtils.isContainChinese(param.getNegativePrompt())){ + //调用翻译api + negaPromptText = translate(param.getNegativePrompt()); + }else{ + negaPromptText = param.getNegativePrompt(); + } + }else{ + negaPromptText = param.getNegativePrompt(); + } + + log.info("反向提示词为:【{}】",negaPromptText); - //TODO 根据选择的风格来选择模型 Map map = new HashMap<>(); + String negaPrompt = "easynegative,nsfw,naked"; + if(StringUtils.isNotBlank(param.getNegativePrompt())){ + negaPrompt = negaPrompt + "," + negaPromptText; + } + Double eta = param.getEta() == null ? 0 : param.getEta(); + Integer batchSize = param.getBatchSize() == null ? 1 :param.getBatchSize(); + Integer seed = param.getSeed() == null ? -1 : param.getSeed(); + Double cfgScale = param.getCfgScale() == null ? 7 : param.getCfgScale(); + Integer steps = param.getSteps() == null ? 25 : param.getSteps(); + String samplerIndex = StringUtils.isEmpty(param.getSamplerIndex()) ? "DPM++ 2S a Karras" : param.getSamplerIndex(); + map.put("prompt", prompt); + map.put("restore_faces",false); //面部修复,卡通模型不支持,只适合真人模型 + map.put("tiling",false); //生成可平铺的周期性图片 + map.put("eta",eta); //取值为0-1 + map.put("batch_size",batchSize); //生成图片数可调 + map.put("seed",seed); //种子 TODO 加个提取种子的功能 + map.put("cfg_scale",cfgScale); //精细度可调 + map.put("steps",steps); //采样步数可调 + map.put("sampler_index",samplerIndex); //采样风格可调 + map.put("negative_prompt",negaPrompt); map.put("width",param.getWidth() == null ? 512 : param.getWidth()); map.put("height",param.getHeight() == null ? 512 : param.getHeight()); - map.put("prompt", prompt); - //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","DPM++ 2S a Karras"); - //map.put("sampler_index",param.getSamplerIndex()); - map.put("steps",25); - map.put("negative_prompt","easynegative,nsfw,naked"); - //log.info("请求stable_diffusion请求体为:【{}】", JSON.toJSONString(map)); + log.info("请求stable_diffusion请求体为:【{}】", JSON.toJSONString(map)); JSONObject jsonObject = restTemplate.postForObject(stableDiffusionConfig.getTxt2ImgUrl(), map, JSONObject.class); //log.info("请求stable_diffusion响应体的为:【{}】", JSON.toJSONString(jsonObject)); TextToImgVo img = new TextToImgVo(); @@ -219,7 +244,9 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { //base64转file MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg"); //上传图片到七牛云/minio - String imgStr = remoteFileService.uploadBanner(file).getData(); + //String imgStr = remoteFileService.uploadBanner(file).getData(); + //上传图片到七牛云并存入sd文件夹 + String imgStr = remoteFileService.uploadQiNiu(file,7).getData(); //保存生辰该图片到ai绘画表 AiPaint paint = new AiPaint(); //paint.setId(); 主键改成雪花算法后启用 @@ -229,6 +256,13 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { paint.setCreateTime(paintTime); paint.setImgUrl(imgStr); paint.setPrompt(param.getPrompt()); + paint.setNegativePrompt(negaPrompt); + paint.setEta(eta); + paint.setBatchSize(batchSize); + paint.setSeed(seed); + paint.setCfgScale(cfgScale); + paint.setSteps(steps); + paint.setSamplerIndex(samplerIndex); paint.setModel(param.getModelName()); paint.setStyleName(param.getStyleName()); paint.setHeight(param.getHeight() == null ? "512" : String.valueOf(param.getHeight()));