diff --git a/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/TranslateUtils.java b/bnyer-common/bnyer-common-core/src/main/java/com/bnyer/common/core/utils/TranslateUtils.java new file mode 100644 index 0000000..bba49a1 --- /dev/null +++ b/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(); + } +} 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 3edf36a..d881e5b 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 @@ -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,59 +40,72 @@ public class StableDiffusionServiceImpl implements StableDiffusionService { @Override public TextToImgVo textToImg(TextToImgDto param) { - //TODO 根据选择的风格来选择模型 - Map 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("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",20); - 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); - log.info("请求stable_diffusion响应体的为:【{}】", JSON.toJSONString(jsonObject)); - TextToImgVo img = new TextToImgVo(); - if(jsonObject != null && jsonObject.getJSONArray("images").size() > 0){ - List images = jsonObject.getJSONArray("images").toJavaList(String.class); - img.setImages(images); - String paintId = null; - Date paintTime = null; - for (String image : images) { - //base64转file - MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg"); - //上传图片到七牛云/minio - String imgStr = remoteFileService.uploadBanner(file).getData(); - //保存生辰该图片到ai绘画表 - AiPaint paint = new AiPaint(); - //paint.setId(); 主键改成雪花算法后启用 - paintId = IdUtil.getSnowflakeNextIdStr(); - paintTime = new Date(); - paint.setPaintId(paintId); - paint.setCreateTime(paintTime); - paint.setImgUrl(imgStr); - paint.setPrompt(param.getPrompt()); - 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("1"); - paint.setPainterId(param.getPainterId()); - paint.setPainterName(param.getPainterName()); - aiPaintService.insert(paint); + try{ + String prompt = ""; + //判断prompt是否包含中文,中文则翻译,否则跳过 + if(TranslateUtils.isContainChinese(param.getPrompt())){ + //调用翻译api + //prompt = + }else{ + prompt = param.getPrompt(); } - img.setPaintId(paintId); - img.setPaintTime(paintTime); - } - return img; + //TODO 根据选择的风格来选择模型 + Map map = new HashMap<>(); + map.put("width",param.getWidth() == null ? 512 : param.getWidth()); + map.put("height",param.getHeight() == null ? 512 : param.getHeight()); + map.put("prompt", prompt); + 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","nsfw"); + log.info("请求stable_diffusion请求体为:【{}】", JSON.toJSONString(map)); + JSONObject jsonObject = restTemplate.postForObject("http://localhost:7860/sdapi/v1/txt2img", map, JSONObject.class); + log.info("请求stable_diffusion响应体的为:【{}】", JSON.toJSONString(jsonObject)); + TextToImgVo img = new TextToImgVo(); + if(jsonObject != null && jsonObject.getJSONArray("images").size() > 0){ + List images = jsonObject.getJSONArray("images").toJavaList(String.class); + img.setImages(images); + String paintId = null; + Date paintTime = null; + for (String image : images) { + //base64转file + MultipartFile file = new Base64ToMultipartFileUtils(image, "data:image/png;base64", "file", "tempSDImg"); + //上传图片到七牛云/minio + String imgStr = remoteFileService.uploadBanner(file).getData(); + //保存生辰该图片到ai绘画表 + AiPaint paint = new AiPaint(); + //paint.setId(); 主键改成雪花算法后启用 + paintId = IdUtil.getSnowflakeNextIdStr(); + paintTime = new Date(); + paint.setPaintId(paintId); + paint.setCreateTime(paintTime); + paint.setImgUrl(imgStr); + paint.setPrompt(param.getPrompt()); + 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("1"); + paint.setPainterId(param.getPainterId()); + paint.setPainterName(param.getPainterName()); + aiPaintService.insert(paint); + } + img.setPaintId(paintId); + img.setPaintTime(paintTime); + } + return img; + }catch (Exception e){ + log.error("文本翻译错误!"+e); + } + return null; } @Override