|
|
|
@ -16,6 +16,7 @@ import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils; |
|
|
|
import com.bnyer.common.core.vo.TextToImgVo; |
|
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
|
import com.bnyer.file.api.RemoteFileService; |
|
|
|
import com.bnyer.img.config.FlagStudioConfig; |
|
|
|
import com.bnyer.img.config.StableDiffusionConfig; |
|
|
|
import com.bnyer.img.config.TencentTranslateConfig; |
|
|
|
import com.bnyer.img.enums.AiPaintButtonEnum; |
|
|
|
@ -33,6 +34,8 @@ import com.tencentcloudapi.tmt.v20180321.models.TextTranslateRequest; |
|
|
|
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateResponse; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.HttpEntity; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
@ -61,6 +64,9 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { |
|
|
|
@Autowired |
|
|
|
private StableDiffusionConfig stableDiffusionConfig; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private FlagStudioConfig flagStudioConfig; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private TiktokUserService tiktokUserService; |
|
|
|
|
|
|
|
@ -230,7 +236,7 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { |
|
|
|
log.error("腾讯文生图调用错误!"+e.getMessage()); |
|
|
|
throw new ServiceException(e.getMessage(),500); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
}else if(button == AiPaintButtonEnum.SD.getCode()){ |
|
|
|
//采用sd
|
|
|
|
try{ |
|
|
|
//内容提示词
|
|
|
|
@ -343,6 +349,111 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("文本翻译错误!"+e); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
//FlagStudio
|
|
|
|
try{ |
|
|
|
//获取token
|
|
|
|
JSONObject tokenObject = restTemplate.getForObject(flagStudioConfig.getTokenUrl() + "?apikey=" + flagStudioConfig.getApiKey(), JSONObject.class); |
|
|
|
String token = ""; |
|
|
|
if(tokenObject != null && tokenObject.getString("code").equals("200")){ |
|
|
|
token = tokenObject.getJSONObject("data").getString("token"); |
|
|
|
} |
|
|
|
//描述词处理
|
|
|
|
//负面提示词
|
|
|
|
String negaPrompt = "lowres,bad anatomy,bad hands,text,error,missing fingers,extra digit,fewer digits,cropped,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,username,blurry,lowres,text, cropped,worst quality,low quality,normal quality,jpeg artifacts,signature, watermark,username,blurry,text,signature,watermark,simple background,toony,dated,low res,line art,flat colors,nsfw,easynegative,naked,nsfw"; |
|
|
|
//内容提示词
|
|
|
|
String contentPrompt = param.getPrompt(); |
|
|
|
//积极预设词
|
|
|
|
String activePrompt = "8k,masterpiece,best quality,ultra high res,"; |
|
|
|
String resultPrompt = activePrompt + contentPrompt; |
|
|
|
//画布大小
|
|
|
|
Integer width = 512; |
|
|
|
Integer height = 512; |
|
|
|
//1:1正方形
|
|
|
|
if(param.getHeight() == 512 && param.getWidth() == 512){ |
|
|
|
width = 512; |
|
|
|
height = 512; |
|
|
|
//16:9宽屏
|
|
|
|
}else if(param.getHeight() == 512 && param.getWidth() == 1024){ |
|
|
|
width = 768; |
|
|
|
height = 432; |
|
|
|
//9:16竖屏
|
|
|
|
}else{ |
|
|
|
width = 432; |
|
|
|
height = 768; |
|
|
|
} |
|
|
|
|
|
|
|
// 返回的resp是一个TextToImageResponse的实例,与请求对象对应
|
|
|
|
TextToImgVo img = new TextToImgVo(); |
|
|
|
//组装参数并发送
|
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("prompt", resultPrompt); //存预设词+用户输入词
|
|
|
|
map.put("guidance_scale",7.5); //精细度可调
|
|
|
|
map.put("height",height); |
|
|
|
map.put("width",width); |
|
|
|
map.put("negative_prompts",negaPrompt); |
|
|
|
map.put("sampler","ddim"); //采样风格可调
|
|
|
|
map.put("seed",0); //种子
|
|
|
|
map.put("steps",50); //采样步数可调
|
|
|
|
map.put("style",param.getStyleName()); //风格
|
|
|
|
map.put("upsample",1); |
|
|
|
log.info("请求flagStudio请求体为:【{}】", JSON.toJSONString(map)); |
|
|
|
// 构建你的请求头
|
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.set("Content-Type", "application/json"); |
|
|
|
headers.set("Accept", "application/json"); |
|
|
|
headers.set("token",token); |
|
|
|
// 组合请求头与请求体参数
|
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<>(JSONObject.toJSONString(map), headers); |
|
|
|
JSONObject jsonObject = restTemplate.postForObject(flagStudioConfig.getTxt2ImgUrl(), requestEntity, JSONObject.class); |
|
|
|
if(jsonObject != null && jsonObject.getString("data") != null && jsonObject.getString("code").equals("200") && jsonObject.getString("nsfw").equals("0")) { |
|
|
|
String imgUrl = jsonObject.getString("data"); |
|
|
|
List<String> list = new ArrayList<>(); |
|
|
|
list.add(imgUrl); |
|
|
|
img.setImages(list); |
|
|
|
String paintId = null; |
|
|
|
Date paintTime = null; |
|
|
|
for (String image : list) { |
|
|
|
//base64转file
|
|
|
|
MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg"); |
|
|
|
//上传图片到七牛云/minio
|
|
|
|
//String imgStr = remoteFileService.uploadBanner(file).getData();
|
|
|
|
//上传图片到七牛云并存入sd文件夹
|
|
|
|
String imgStr = remoteFileService.uploadQiNiu(file,7).getData(); |
|
|
|
//保存生辰该图片到ai绘画表
|
|
|
|
AiPaint paint = new AiPaint(); |
|
|
|
paintId = IdUtil.getSnowflakeNextIdStr(); |
|
|
|
paintTime = new Date(); |
|
|
|
paint.setPaintId(paintId); |
|
|
|
paint.setCreateTime(paintTime); |
|
|
|
paint.setImgUrl(imgStr); |
|
|
|
paint.setPrompt(contentPrompt); //只存储用户输入的内容
|
|
|
|
paint.setNegativePrompt(negaPrompt); |
|
|
|
paint.setModel(param.getModelName()); |
|
|
|
paint.setStyleName(param.getStyleName()); |
|
|
|
paint.setHeight(param.getHeight() == null ? "512" : String.valueOf(param.getHeight())); |
|
|
|
paint.setWidth(param.getWidth() == null ? "512" : String.valueOf(param.getWidth())); |
|
|
|
paint.setIsShow("1"); |
|
|
|
paint.setSource(param.getPlatform()); |
|
|
|
paint.setPainterId(param.getPainterId()); |
|
|
|
paint.setPainterName(param.getPainterName()); |
|
|
|
aiPaintService.insert(paint); |
|
|
|
//写入画意值消耗记录
|
|
|
|
GoldLog goldLog = new GoldLog(); |
|
|
|
goldLog.setGoldNum(param.getGoldNum()); |
|
|
|
goldLog.setUserId(param.getPainterId()); |
|
|
|
goldLog.setSource(param.getPlatform()); |
|
|
|
goldLog.setReason(GoldEnum.PAINT.getValue()); |
|
|
|
goldLogService.insert(goldLog); |
|
|
|
} |
|
|
|
img.setPaintId(paintId); |
|
|
|
img.setPaintTime(paintTime); |
|
|
|
} |
|
|
|
return img; |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("FlagStudio文生图调用错误!"+e.getMessage()); |
|
|
|
throw new ServiceException(e.getMessage(),500); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|