|
|
|
@ -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<String, Object> 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())); |
|
|
|
|