将generate输入的文本进行翻译或微处理

This commit is contained in:
2024-05-07 18:27:32 +08:00
parent ffeaac8c46
commit ae937dbf65
3 changed files with 63 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ public class MQConfig {
public static final String GENERATE_EXCHANGE_FANOUT = "generate-exchange";
// public static final String GENERATE_QUEUE = "generate-queue-prod";
// public static final String GENERATE_QUEUE = "generate-queue-test";
// ==================================================================
// public static final String GENERATE_QUEUE = "generate-queue-local";
public static final String GENERATE_QUEUE = "generate-queue-dev";

View File

@@ -3151,4 +3151,59 @@ public class PythonService {
return Boolean.TRUE;
}
public String promptTranslate(String text) throws BusinessException {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
HashMap<String, String> content = new HashMap<>();
content.put("text", text);
String jsonString = JSON.toJSONString(content, SerializerFeature.WriteNullStringAsEmpty);
RequestBody body = RequestBody.create(mediaType, jsonString);
Request request = new Request.Builder()
.url(accessPythonIp + ":" + accessPythonPort + "/api/translateToEN")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
try {
log.info("promptTranslation请求入参content###{}", jsonString);
response = client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##promptTranslation异常###{}", ExceptionUtil.getThrowableList(ioException));
return text;
}
int responseCode = response.code();
String bodyString;
try {
bodyString = response.body().string();
if (responseCode != HttpURLConnection.HTTP_OK) {
// 基本不会有除200以外的code
log.info("promptTranslation 用户输入翻译失败。 Response code " + responseCode);
throw new BusinessException("promptTranslation 用户输入翻译失败。 Response code " + responseCode);
}
JSONObject jsonObject = JSON.parseObject(bodyString);
Boolean result = JSON.parseObject(JSON.toJSONString(response)).getBoolean("successful");
if (result && jsonObject.get("msg").equals("OK!")) {
String translated = jsonObject.get("data").toString();
log.info("翻译或处理后的文本 {}", translated);
return translated;
}
} catch (IOException e) {
log.error("promptTranslation 用户输入翻译失败; error message => " + e.getMessage());
throw new RuntimeException(e);
} finally {
response.close();
}
log.info("promptTranslation 用户输入翻译失败,返回用户输入");
return text;
}
}

View File

@@ -121,7 +121,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
String text = generateThroughImageTextDTO.getText();
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
validateGeneraType(generate, text, elementId, generateType);
if (generateType.equals("text") || generateType.equals("text-image")) {
if (!StringUtil.isNullOrEmpty(text)) {
text = modifyPrompt(text, generate, generateThroughImageTextDTO.getLevel1Type());
}
@@ -255,24 +255,25 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
private String modifyPrompt(String userInput, Generate generate, String level1Type) {
String text = "";
String translated = pythonService.promptTranslate(userInput);
switch (level1Type) {
case "Moodboard":
text = userInput + ",high quality";
text = translated + ",high quality";
generate.setText(text);
break;
case "Printboard":
if (userInput.contains("Painting Style")) {
userInput = "Picasso,increased color saturation,increased glossiness," + userInput;
userInput = "Picasso,increased color saturation,increased glossiness," + translated;
} else if (userInput.contains("Illustration Style")) {
userInput = "Flat coating,romantic,soft,pencil strokes,accentuating and widening the depth of pencil strokes,paper patterns,block colors,crayons,reducing image contrast,and hand drawn painting marks," + userInput;
userInput = "Flat coating,romantic,soft,pencil strokes,accentuating and widening the depth of pencil strokes,paper patterns,block colors,crayons,reducing image contrast,and hand drawn painting marks," + translated;
} else if (userInput.contains("Real Style")) {
userInput = "Still life photography,hyper realism,3d,deepened projection,increased permutation value,increased concavity and convexity value," + userInput;
userInput = "Still life photography,hyper realism,3d,deepened projection,increased permutation value,increased concavity and convexity value," + translated;
}
text = userInput + ", fabric print, high quality";
generate.setText(text);
break;
case "Sketchboard":
text = "clear lines, simple outlines monochrome white vector image of " + userInput + ", no background, sketch flat, front view display, best quality, ultra-high resolution 8k";
text = "clear lines, simple outlines monochrome white vector image of " + translated + ", no background, sketch flat, front view display, best quality, ultra-high resolution 8k";
generate.setText(text);
default:
}