|
|
@ -3,19 +3,24 @@ package com.bnyer.img.service.impl; |
|
|
import cn.hutool.core.util.IdUtil; |
|
|
import cn.hutool.core.util.IdUtil; |
|
|
import com.alibaba.fastjson.JSON; |
|
|
import com.alibaba.fastjson.JSON; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
import com.bnyer.common.core.client.TmtClient; |
|
|
import com.bnyer.common.core.constant.RedisKeyConstant; |
|
|
import com.bnyer.common.core.constant.RedisKeyConstant; |
|
|
import com.bnyer.common.core.domain.AiPaint; |
|
|
import com.bnyer.common.core.domain.AiPaint; |
|
|
import com.bnyer.common.core.domain.R; |
|
|
|
|
|
import com.bnyer.common.core.dto.TextToImgDto; |
|
|
import com.bnyer.common.core.dto.TextToImgDto; |
|
|
|
|
|
import com.bnyer.common.core.dto.TextTranslateDto; |
|
|
import com.bnyer.common.core.utils.TranslateUtils; |
|
|
import com.bnyer.common.core.utils.TranslateUtils; |
|
|
import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils; |
|
|
import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils; |
|
|
import com.bnyer.common.core.utils.file.ImageUtils; |
|
|
|
|
|
import com.bnyer.common.core.vo.TextToImgVo; |
|
|
import com.bnyer.common.core.vo.TextToImgVo; |
|
|
|
|
|
import com.bnyer.common.core.vo.TextTranslateVo; |
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
import com.bnyer.file.api.RemoteFileService; |
|
|
import com.bnyer.file.api.RemoteFileService; |
|
|
|
|
|
import com.bnyer.img.config.TencentTranslateConfig; |
|
|
import com.bnyer.img.service.AiPaintService; |
|
|
import com.bnyer.img.service.AiPaintService; |
|
|
import com.bnyer.img.service.StableDiffusionService; |
|
|
import com.bnyer.img.service.StableDiffusionService; |
|
|
import com.bnyer.system.api.RemoteSystemFileService; |
|
|
import com.tencentcloudapi.common.Credential; |
|
|
|
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException; |
|
|
|
|
|
import com.tencentcloudapi.common.profile.ClientProfile; |
|
|
|
|
|
import com.tencentcloudapi.common.profile.HttpProfile; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
@ -44,25 +49,54 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private RedisService redisService; |
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TencentTranslateConfig tencentTranslateConfig; |
|
|
|
|
|
|
|
|
|
|
|
private String translate(String text){ |
|
|
|
|
|
try{ |
|
|
|
|
|
Credential cred = new Credential(tencentTranslateConfig.getSecretId(), tencentTranslateConfig.getSecretKey()); |
|
|
|
|
|
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
|
HttpProfile httpProfile = new HttpProfile(); |
|
|
|
|
|
httpProfile.setEndpoint(tencentTranslateConfig.getUrl()); |
|
|
|
|
|
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
|
ClientProfile clientProfile = new ClientProfile(); |
|
|
|
|
|
clientProfile.setHttpProfile(httpProfile); |
|
|
|
|
|
// 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
|
|
|
TmtClient client = new TmtClient(cred, tencentTranslateConfig.getRegion(), clientProfile); |
|
|
|
|
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
|
|
|
TextTranslateDto req = new TextTranslateDto(); |
|
|
|
|
|
req.setSourceText(text); |
|
|
|
|
|
req.setSource("zh"); |
|
|
|
|
|
req.setTarget("en"); |
|
|
|
|
|
req.setProjectId(Long.parseLong(tencentTranslateConfig.getProjectId())); |
|
|
|
|
|
// 返回的resp是一个TextTranslateResponse的实例,与请求对象对应
|
|
|
|
|
|
TextTranslateVo resp = client.TextTranslate(req); |
|
|
|
|
|
return resp.getTargetText(); |
|
|
|
|
|
} catch (TencentCloudSDKException e) { |
|
|
|
|
|
log.error("调用腾讯api失败"+e.toString()); |
|
|
|
|
|
} |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public TextToImgVo textToImg(TextToImgDto param) { |
|
|
public TextToImgVo textToImg(TextToImgDto param) { |
|
|
try{ |
|
|
try{ |
|
|
//String prompt = "";
|
|
|
String prompt = ""; |
|
|
//判断prompt是否包含中文,中文则翻译,否则跳过
|
|
|
//判断prompt是否包含中文,中文则翻译,否则跳过
|
|
|
// if(TranslateUtils.isContainChinese(param.getPrompt())){
|
|
|
if(TranslateUtils.isContainChinese(param.getPrompt())){ |
|
|
// //调用翻译api
|
|
|
//调用翻译api
|
|
|
// //prompt =
|
|
|
prompt = translate(param.getPrompt()); |
|
|
// prompt = param.getPrompt();
|
|
|
}else{ |
|
|
// }else{
|
|
|
prompt = param.getPrompt(); |
|
|
// prompt = param.getPrompt();
|
|
|
} |
|
|
// }
|
|
|
System.out.println(prompt); |
|
|
|
|
|
|
|
|
//TODO 根据选择的风格来选择模型
|
|
|
//TODO 根据选择的风格来选择模型
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
map.put("width",param.getWidth() == null ? 512 : param.getWidth()); |
|
|
map.put("width",param.getWidth() == null ? 512 : param.getWidth()); |
|
|
map.put("height",param.getHeight() == null ? 512 : param.getHeight()); |
|
|
map.put("height",param.getHeight() == null ? 512 : param.getHeight()); |
|
|
//map.put("prompt", prompt);
|
|
|
map.put("prompt", prompt); |
|
|
map.put("prompt", param.getPrompt()); |
|
|
//map.put("prompt", param.getPrompt());
|
|
|
map.put("seed",-1); |
|
|
map.put("seed",-1); |
|
|
map.put("batch_size",1); |
|
|
map.put("batch_size",1); |
|
|
map.put("cfg_scale",7); |
|
|
map.put("cfg_scale",7); |
|
|
|