|
|
@@ -3,6 +3,7 @@ package com.cyksj.service.midjourney.impl;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
@@ -10,11 +11,13 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.mapper.MidjourneyUserConversationMapper;
|
|
|
import com.cyksj.model.dto.BlendDimensions;
|
|
|
import com.cyksj.model.entity.MidjourneyAccount;
|
|
|
import com.cyksj.model.entity.MidjourneyUser;
|
|
|
import com.cyksj.model.entity.MidjourneyUserConversation;
|
|
|
+import com.cyksj.model.response.SubmitResult;
|
|
|
import com.cyksj.service.midjourney.MidjourneyService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -37,7 +40,9 @@ import java.util.regex.Pattern;
|
|
|
@Slf4j
|
|
|
@RequiredArgsConstructor
|
|
|
public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
- private static final String HOST = "http://43.154.230.104:8080";
|
|
|
+ private static final String RELAX_HOST = "http://43.154.230.104:8080";
|
|
|
+ private static final String FAST_HOST = "https://aigc.api4midjourney.com/api";
|
|
|
+ private static final String FAST_TOKEN = "14a6b469-7cb2-4b99-9238-676c7c5cffef";
|
|
|
|
|
|
private final MidjourneyUserConversationMapper conversationMapper;
|
|
|
|
|
|
@@ -47,146 +52,107 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt, List<String> base64Array) {
|
|
|
- JSONObject imagine = imagine(user.getId(), prompt, base64Array);
|
|
|
- JSONObject properties = imagine.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), imagine.getLong("result"), properties,"IMAGINE", prompt);
|
|
|
+ public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt, List<String> base64Array) throws Exception {
|
|
|
+ Map<String, Object> imagineParam = MapUtil.builder(new HashMap<String,Object>())
|
|
|
+ .put("prompt", prompt).put("state", user.getId()).build();
|
|
|
+ if (CollectionUtil.isNotEmpty(base64Array)) {
|
|
|
+ imagineParam.put("base64Array", base64Array);
|
|
|
+ }
|
|
|
+ SubmitResult result = submit(user.getMode(),"imagine", imagineParam);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"IMAGINE", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
- public MidjourneyUserConversation saveConversation(Long userId,Long taskId,JSONObject properties, String action,String prompt) {
|
|
|
+ public MidjourneyUserConversation saveConversation(Long userId,Long taskId,Map<String,Object> properties, String action,String prompt) {
|
|
|
MidjourneyUserConversation conversation = new MidjourneyUserConversation()
|
|
|
.setUserId(userId).setTaskId(taskId).setAction(action).setPrompt(prompt)
|
|
|
.setMode(1).setStartTime(System.currentTimeMillis()).setProgress("0%")
|
|
|
- .setStatus("IN_PROGRESS").setChannelId(properties.getLong("discordChannelId"))
|
|
|
- .setInstanceId(properties.getLong("discordInstanceId")).setTaskId(taskId);
|
|
|
+ .setStatus("IN_PROGRESS").setChannelId(Long.parseLong(properties.get("discordChannelId").toString()))
|
|
|
+ .setInstanceId(Long.parseLong(properties.get("discordInstanceId").toString())).setTaskId(taskId);
|
|
|
conversationMapper.insert(conversation);
|
|
|
return conversation;
|
|
|
}
|
|
|
|
|
|
- private JSONObject imagine(Long userId,String prompt, List<String> base64Array) {
|
|
|
- Map<String, Object> imagineParam = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("prompt", prompt).put("state", userId).build();
|
|
|
- if (CollectionUtil.isNotEmpty(base64Array)) {
|
|
|
- imagineParam.put("base64Array", base64Array);
|
|
|
- }
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/imagine", imagineParam);
|
|
|
- log.info("imagine body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("imagine error message:" + jsonObject.getStr("description"));
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitDescribe(MidjourneyUser user, String base64) {
|
|
|
- JSONObject describe = describe(user.getId(), base64);
|
|
|
- JSONObject properties = describe.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), describe.getLong("result"), properties,"DESCRIBE", StringUtils.EMPTY);
|
|
|
+ public MidjourneyUserConversation submitDescribe(MidjourneyUser user, String base64) throws Exception {
|
|
|
+ Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>()).put("state", user.getId()).put("base64", base64).build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"describe", param);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"DESCRIBE", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
- private JSONObject describe(Long userId,String base64) {
|
|
|
- Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>()).put("state", userId).put("base64", base64).build();
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/describe", param);
|
|
|
- log.info("describe body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("describe error message:" + jsonObject.getStr("description"));
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitBlend(MidjourneyUser user, BlendDimensions dimensions, List<String> base64Array) {
|
|
|
- JSONObject blend = blend(user.getId(), dimensions, base64Array);
|
|
|
- JSONObject properties = blend.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), blend.getLong("result"), properties,"BLEND", StringUtils.EMPTY);
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject blend(Long userId,BlendDimensions dimensions, List<String> base64Array) {
|
|
|
+ public MidjourneyUserConversation submitBlend(MidjourneyUser user, BlendDimensions dimensions, List<String> base64Array) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("base64Array", base64Array).put("state", userId).build();
|
|
|
+ .put("base64Array", base64Array).put("state", user.getId()).build();
|
|
|
if (dimensions != null) {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/blend", param);
|
|
|
- log.info("blend body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("blend error message:" + jsonObject.getStr("description"));
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
- }
|
|
|
- @Override
|
|
|
- public MidjourneyUserConversation submitModal(MidjourneyUser user, Long taskId, String prompt, String maskBase64) {
|
|
|
- JSONObject modal = modal(user.getId(),taskId, prompt, maskBase64);
|
|
|
- JSONObject properties = modal.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), modal.getLong("result"), properties,"MODAL", prompt);
|
|
|
+ SubmitResult result = submit(user.getMode(),"blend", param);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"BLEND", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
- private JSONObject modal(Long userId,Long taskId,String prompt, String maskBase64) {
|
|
|
+ @Override
|
|
|
+ public MidjourneyUserConversation submitModal(MidjourneyUser user, Long taskId, String prompt, String maskBase64) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("taskId", taskId).put("state", userId).build();
|
|
|
+ .put("taskId", taskId).put("state", user.getId()).build();
|
|
|
if (StringUtils.isNotBlank(prompt)) {
|
|
|
param.put("prompt", prompt);
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(maskBase64)) {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/modal", param);
|
|
|
- log.info("shorten body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("shorten error message:" + jsonObject.getStr("description"));
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
+ SubmitResult result = submit(user.getMode(),"modal", param);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"MODAL", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitShorten(MidjourneyUser user, String prompt) {
|
|
|
- JSONObject shorten = shorten(user.getId(), prompt);
|
|
|
- JSONObject properties = shorten.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), shorten.getLong("result"), properties,"SHORTEN", prompt);
|
|
|
- }
|
|
|
-
|
|
|
- private JSONObject shorten(Long userId,String prompt) {
|
|
|
+ public MidjourneyUserConversation submitShorten(MidjourneyUser user, String prompt) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("prompt", prompt).put("state", userId).build();
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/shorten", param);
|
|
|
- log.info("shorten body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("shorten error message:" + jsonObject.getStr("description"));
|
|
|
- }
|
|
|
- return jsonObject;
|
|
|
+ .put("prompt", prompt).put("state", user.getId()).build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"shorten", param);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"SHORTEN", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitAction(MidjourneyUser user, Long taskId, String customId) {
|
|
|
- JSONObject action = action(user.getId(), taskId, customId);
|
|
|
- JSONObject properties = action.getJSONObject("properties");
|
|
|
- return saveConversation(user.getId(), action.getLong("result"), properties,"ACTION", StringUtils.EMPTY);
|
|
|
+ public MidjourneyUserConversation submitAction(MidjourneyUser user, Long taskId, String customId) throws Exception {
|
|
|
+ Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
+ .put("taskId", taskId).put("state", user.getId()).put("customId", customId).build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"action", param);
|
|
|
+ return saveConversation(user.getId(), Long.parseLong(result.getResult()),result.getProperties(),"ACTION", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
- public JSONObject action(Long userId, Long taskId, String customId) {
|
|
|
- Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("taskId", taskId).put("state", userId).put("customId", customId).build();
|
|
|
- String body = HttpUtil.post(HOST + "/mj/submit/action", param);
|
|
|
+
|
|
|
+ public SubmitResult submit(Integer mode,String action, Map<String, Object> param) throws Exception {
|
|
|
+ String body = HttpRequest.post(mode == 1 ? FAST_HOST : RELAX_HOST + getActionUrl(action)).form(param).header("Authorization", FAST_TOKEN).execute().body();
|
|
|
log.info("action body:{}", body);
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
- if (jsonObject.getInt("code") != 1) {
|
|
|
- throw BusinessRuntimeException.getInstance("action error message:" + jsonObject.getStr("description"));
|
|
|
+ SubmitResult submitResult = Jsons.parseObject(body, SubmitResult.class);
|
|
|
+ if (submitResult.getCode() != 1) {
|
|
|
+ throw BusinessRuntimeException.getInstance("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
+ }
|
|
|
+ return submitResult;
|
|
|
+ }
|
|
|
+ private String getActionUrl(String action) {
|
|
|
+ switch (action) {
|
|
|
+ case "imagine":
|
|
|
+ return "/mj/submit/imagine";
|
|
|
+ case "action":
|
|
|
+ return "/mj/submit/action";
|
|
|
+ case "describe":
|
|
|
+ return "/mj/submit/describe";
|
|
|
+ case "blend":
|
|
|
+ return"/mj/submit/blend";
|
|
|
+ case "modal":
|
|
|
+ return "/mj/submit/modal";
|
|
|
+ case "shorten":
|
|
|
+ return "/mj/submit/shorten";
|
|
|
+ default: throw new IllegalArgumentException("Unknown action: " + action);
|
|
|
}
|
|
|
- return jsonObject;
|
|
|
}
|
|
|
@Override
|
|
|
- public List<MidjourneyUserConversation> listConversationByIds(List<Long> ids) {
|
|
|
+ public List<MidjourneyUserConversation> listConversationByIds(MidjourneyUser user, List<Long> ids) {
|
|
|
List<MidjourneyUserConversation> list = new ArrayList<>();
|
|
|
- listByIds(ids).forEach(json -> {
|
|
|
+ listByIds(user.getMode(),ids).forEach(json -> {
|
|
|
JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
conversation.setUserId(jsons.getLong("state"));
|
|
|
@@ -231,9 +197,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
JSONObject result = JSONUtil.parseObj(HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap));
|
|
|
return result.getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
}
|
|
|
- public JSONArray listByIds(List<Long> ids) {
|
|
|
+ public JSONArray listByIds(Integer mode,List<Long> ids) {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String, Object>()).put("ids", ids).build();
|
|
|
- String body = HttpUtil.post(HOST + "/mj/task/list-by-ids", param);
|
|
|
+ String body = HttpRequest.post(mode == 1 ? FAST_HOST : RELAX_HOST + "/mj/task/list-by-condition").header("Authorization", FAST_TOKEN).form(param).execute().body();
|
|
|
log.info("listByIds body:{}", body);
|
|
|
return JSONUtil.parseArray(body);
|
|
|
}
|
|
|
@@ -249,12 +215,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
conversation.setStatus("CANCEL");
|
|
|
conversationMapper.updateById(conversation);
|
|
|
- cancel(conversation.getTaskId());
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ cancel(user.getMode(),conversation.getTaskId());
|
|
|
+ });
|
|
|
return conversation;
|
|
|
}
|
|
|
|
|
|
- public void cancel(Long taskId) {
|
|
|
- String body = HttpUtil.post(HOST + "/mj/task/"+taskId+"/cancel",MapUtil.empty());
|
|
|
+ public void cancel(Integer mode,Long taskId) {
|
|
|
+ String body = HttpUtil.post(mode == 1 ? FAST_HOST : RELAX_HOST + "/mj/task/"+taskId+"/cancel",MapUtil.empty());
|
|
|
log.info("cancel body:{}", body);
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
if (jsonObject.getInt("code") != 1) {
|