Browse Source

feature-img-1.0:待加入翻译api

feature-1.0-img
Penny 3 years ago
parent
commit
17a5233ca2
  1. 27
      bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/TranslateUtils.java
  2. 20
      bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java

27
bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/TranslateUtils.java

@ -0,0 +1,27 @@
package com.bnyer.common.core.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 汉译英工具类
*/
public class TranslateUtils {
/**
* 字符串是否包含中文
*
* @param str 待校验字符串
* @return true 包含中文字符 false 不包含中文字符
* @throws Exception
*/
public static boolean isContainChinese(String str) throws Exception {
if (StringUtils.isEmpty(str)) {
throw new Exception("文本内容为空!");
}
Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
Matcher m = p.matcher(str);
return m.find();
}
}

20
bnyer-services/bnyer-img/src/main/java/com/bnyer/img/service/impl/StableDiffusionServiceImpl.java

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
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.utils.TranslateUtils;
import com.bnyer.common.core.utils.file.Base64ToMultipartFileUtils;
import com.bnyer.common.core.utils.file.ImageUtils;
import com.bnyer.common.core.vo.TextToImgVo;
@ -39,11 +40,21 @@ public class StableDiffusionServiceImpl implements StableDiffusionService {
@Override
public TextToImgVo textToImg(TextToImgDto param) {
try{
String prompt = "";
//判断prompt是否包含中文,中文则翻译,否则跳过
if(TranslateUtils.isContainChinese(param.getPrompt())){
//调用翻译api
//prompt =
}else{
prompt = param.getPrompt();
}
//TODO 根据选择的风格来选择模型
Map<String, Object> map = new HashMap<>();
map.put("width",param.getWidth() == null ? 512 : param.getWidth());
map.put("height",param.getHeight() == null ? 512 : param.getHeight());
map.put("prompt", param.getPrompt());
map.put("prompt", prompt);
map.put("seed",-1);
map.put("batch_size",1);
map.put("cfg_scale",7);
@ -52,7 +63,7 @@ public class StableDiffusionServiceImpl implements StableDiffusionService {
map.put("eta",0);
map.put("sampler_index","DPM++ 2S a Karras");
//map.put("sampler_index",param.getSamplerIndex());
map.put("steps",20);
map.put("steps",25);
map.put("negative_prompt","nsfw");
log.info("请求stable_diffusion请求体为:【{}】", JSON.toJSONString(map));
JSONObject jsonObject = restTemplate.postForObject("http://localhost:7860/sdapi/v1/txt2img", map, JSONObject.class);
@ -90,8 +101,11 @@ public class StableDiffusionServiceImpl implements StableDiffusionService {
img.setPaintId(paintId);
img.setPaintTime(paintTime);
}
return img;
}catch (Exception e){
log.error("文本翻译错误!"+e);
}
return null;
}
@Override

Loading…
Cancel
Save