|
|
@@ -1,6 +1,7 @@
|
|
|
package com.cyksj.service.midjourney.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
@@ -12,14 +13,13 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.EnvCommonService;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
-import com.cyksj.common.util.Codec;
|
|
|
-import com.cyksj.common.util.J11HttpC;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.MidjourneyUserConversationMapper;
|
|
|
import com.cyksj.mapper.MidjourneyUserMapper;
|
|
|
import com.cyksj.model.dto.BlendDimensions;
|
|
|
import com.cyksj.model.dto.MessageButton;
|
|
|
+import com.cyksj.model.dto.SubmitModalDTO;
|
|
|
import com.cyksj.model.entity.MidjourneyAccount;
|
|
|
import com.cyksj.model.entity.MidjourneyUser;
|
|
|
import com.cyksj.model.entity.MidjourneyUserConversation;
|
|
|
@@ -33,18 +33,10 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
-import java.net.URI;
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.net.http.HttpResponse;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
-import java.security.MessageDigest;
|
|
|
-import java.time.ZoneOffset;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -73,6 +65,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
@Value("${midjourney.url}")
|
|
|
private String midjourneyHost;
|
|
|
+ @Value("${midjourney.cdn}")
|
|
|
+ private String midjourneyCdnHost;
|
|
|
|
|
|
private final EnvCommonService envCommonService;
|
|
|
|
|
|
@@ -91,6 +85,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
imagineParam.put("base64Array", base64Array);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"imagine", null,imagineParam);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"IMAGINE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -106,6 +103,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
conversation.setChannelId(properties.get("discordChannelId") == null ? null : Long.parseLong(properties.get("discordChannelId").toString()))
|
|
|
.setInstanceId(properties.get("discordInstanceId") == null ? null : Long.parseLong(properties.get("discordInstanceId").toString()));
|
|
|
}
|
|
|
+ if (conversation.getInstanceId() == null) {
|
|
|
+ conversation.setInstanceId(result.getInstanceId());
|
|
|
+ }
|
|
|
conversationMapper.insert(conversation);
|
|
|
return conversation;
|
|
|
}
|
|
|
@@ -118,6 +118,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("base64", base64)
|
|
|
.build();
|
|
|
SubmitResult result = submit(user.getMode(),"describe", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"DESCRIBE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -131,6 +134,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"blend", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"BLEND", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -147,54 +153,61 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"modal", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitShorten(MidjourneyUser user, String prompt, String botType) throws Exception {
|
|
|
+ public MidjourneyUserConversation submitShorten(MidjourneyUser user, String botType, String prompt) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
.put("prompt", prompt)
|
|
|
.put("state", user.getId())
|
|
|
.build();
|
|
|
SubmitResult result = submit(user.getMode(),"shorten", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(), result,"SHORTEN", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 恢复次数
|
|
|
*/
|
|
|
- public Long recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
+ public void recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
if (mode == 1){
|
|
|
- num = redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
+ redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrFastNum(id, 1);
|
|
|
}
|
|
|
if (mode == 2){
|
|
|
- if (num != null) {
|
|
|
- num = redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + id, 1L);
|
|
|
+ if (num != null){
|
|
|
+ redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrRelaxNum(id, 1);
|
|
|
}
|
|
|
}
|
|
|
- return num;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 同步数据库
|
|
|
- */
|
|
|
- public void syncUser(Long id,Integer mode,Long num){
|
|
|
- log.info("同步次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
- if (num == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
- .eq(MidjourneyUser::getId, id);
|
|
|
- if (mode == 1){
|
|
|
- wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
- }
|
|
|
- if (mode == 2){
|
|
|
- wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
- }
|
|
|
- midjourneyUserMapper.update(null, wrapper);
|
|
|
}
|
|
|
+// /**
|
|
|
+// * 同步数据库
|
|
|
+// */
|
|
|
+// public void syncUser(Long id,Integer mode,Long num){
|
|
|
+// log.info("同步次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
+// if (num == null){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
+// .eq(MidjourneyUser::getId, id);
|
|
|
+// if (mode == 1){
|
|
|
+// wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
+// }
|
|
|
+// if (mode == 2){
|
|
|
+// wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
+// }
|
|
|
+// midjourneyUserMapper.update(null, wrapper);
|
|
|
+// }
|
|
|
|
|
|
@Override
|
|
|
- public SubmitResult submitAction(MidjourneyUser user, Long taskId, String customId, Long num, String botType) throws Exception {
|
|
|
+ public Object submitAction(MidjourneyUser user, Long taskId, String customId, Long num, String botType) throws Exception {
|
|
|
MidjourneyUserConversation conversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, taskId).last("limit 1"));
|
|
|
if (conversation == null) {
|
|
|
throw BusinessRuntimeException.getInstance("关联任务不存在或已失效");
|
|
|
@@ -202,12 +215,16 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (!user.getMode().equals(conversation.getMode())) {
|
|
|
throw BusinessRuntimeException.getInstance("当前出图模式与关联任务出图模式不符");
|
|
|
}
|
|
|
+ AtomicBoolean modalFlag = new AtomicBoolean(false);
|
|
|
if (StringUtils.isNotBlank(conversation.getButtons())){
|
|
|
List<MessageButton> messageButtons = Jsons.parseList(conversation.getButtons(), MessageButton.class);
|
|
|
messageButtons.forEach(button -> {
|
|
|
if (button.getCustomId().equals(customId)) {
|
|
|
log.info("action customId:{}", customId);
|
|
|
button.setStyle(3);
|
|
|
+ if (!"Vary (Region)".equals(button.getLabel()) && !"Custom Zoom".equals(button.getLabel())) {
|
|
|
+ modalFlag.set(true);
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
conversation.setButtons(Jsons.toJson(messageButtons));
|
|
|
@@ -224,10 +241,19 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.build();
|
|
|
SubmitResult result = submit(user.getMode(),"action", conversation.getInstanceId(),param);
|
|
|
if (result.getCode() == 21) {
|
|
|
- // 以上操作有弹窗确认,恢复次数
|
|
|
- recoverUserLimit(user.getId(), user.getMode(),num);
|
|
|
+ if (user.getMode() == 2 && modalFlag.get()){
|
|
|
+ MidjourneyUserConversation modal = submitModal(user, Long.valueOf(result.getResult()), conversation.getPrompt() + " --v 6", null);
|
|
|
+ log.info("action-modal:{}", modal);
|
|
|
+ return modal;
|
|
|
+ }else {
|
|
|
+ // 以上操作有弹窗确认,恢复次数
|
|
|
+ recoverUserLimit(user.getId(), user.getMode(),num);
|
|
|
+ }
|
|
|
}else {
|
|
|
- syncUser(user.getId(), user.getMode(),num);
|
|
|
+ //syncUser(user.getId(), user.getMode(),num);
|
|
|
+ if (!customId.contains("upsample") && user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
}
|
|
|
saveConversation(user.getId(), user.getMode(), result,"ACTION", StringUtils.EMPTY, botType);
|
|
|
return result;
|
|
|
@@ -257,6 +283,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (code == 3) {
|
|
|
if(mode == 2 && StringUtils.isNotBlank(accountWithMinUsage)){
|
|
|
redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),accountWithMinUsage);
|
|
|
+ String finalAccountWithMinUsage = accountWithMinUsage;
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, finalAccountWithMinUsage).last("limit 1"));
|
|
|
+ midjourneyAccountService.updateStatus(account.getId());
|
|
|
+ });
|
|
|
}
|
|
|
throw BusinessRuntimeException.getInstance("账号不存在");
|
|
|
}
|
|
|
@@ -264,11 +295,15 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
throw BusinessRuntimeException.getInstance(submitResult.getDescription());
|
|
|
}
|
|
|
if (code == 24) {
|
|
|
- throw BusinessRuntimeException.getInstance("prompt包含敏感词");
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(submitResult.getResult());
|
|
|
+ throw BusinessRuntimeException.getInstance("prompt包含敏感词:"+jsonObject.getStr("bannedWord"));
|
|
|
}
|
|
|
log.error("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
throw BusinessRuntimeException.getInstance("队列已满,请稍后尝试");
|
|
|
}
|
|
|
+ if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
+ submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
+ }
|
|
|
return submitResult;
|
|
|
}
|
|
|
|
|
|
@@ -279,9 +314,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
Map<Object, Object> accountsUsage = redisService.hmget(key);
|
|
|
|
|
|
if (accountsUsage == null || accountsUsage.isEmpty()) {
|
|
|
- Map<Object, Object> map = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
+ accountsUsage = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
// 保存所有账号ID和使用次数
|
|
|
- redisService.hmset(key, map);
|
|
|
+ redisService.hmset(key, accountsUsage);
|
|
|
}
|
|
|
|
|
|
// 找到使用次数最少的账号ID
|
|
|
@@ -298,10 +333,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- // 增加使用次数
|
|
|
- if (minAccountId != null) {
|
|
|
- redisService.hincr(key, minAccountId, 1.0);
|
|
|
- }
|
|
|
return minAccountId;
|
|
|
} catch (Exception e) {
|
|
|
throw BusinessRuntimeException.getInstance("获取账号失败");
|
|
|
@@ -367,14 +398,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
MidjourneyUserConversation dbConversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, conversation.getTaskId())
|
|
|
.eq(MidjourneyUserConversation::getUserId, userId).orderByDesc(MidjourneyUserConversation::getId).last("limit 1"));
|
|
|
if (dbConversation != null) {
|
|
|
- if ("SUCCESS".equals(dbConversation.getStatus())) {
|
|
|
+ if ("SUCCESS".equals(dbConversation.getStatus()) || "FAILURE".equals(dbConversation.getStatus()) || "MODAL".equals(dbConversation.getStatus())) {
|
|
|
return;
|
|
|
}
|
|
|
conversation.setId(dbConversation.getId());
|
|
|
conversationMapper.updateById(conversation);
|
|
|
- if (dbConversation.getMode() == 2){
|
|
|
- redisService.hdecr(RedisService.key.MIDJOURNEY_ACCOUNT.getName() , dbConversation.getInstanceId().toString(), 1.0);
|
|
|
- }
|
|
|
TASK_EXECUTOR.execute(() -> {
|
|
|
try {
|
|
|
String imageUrl = conversation.getImageUrl();
|
|
|
@@ -409,17 +437,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private static String uploadPic(String url, String prefix) throws IOException {
|
|
|
+ private String uploadPic(String url, String prefix) throws IOException {
|
|
|
byte[] body = HttpUtil.downloadBytes(url);
|
|
|
- Path tempFile = Files.createTempFile(prefix, ".png");
|
|
|
- try (FileImageOutputStream imageOutput = new FileImageOutputStream(tempFile.toFile())) {
|
|
|
+ Path tempFile = Files.createTempFile(Path.of("/mnt/pirture"),prefix, ".png");
|
|
|
+ File file = tempFile.toFile();
|
|
|
+ try (FileImageOutputStream imageOutput = new FileImageOutputStream(file)) {
|
|
|
imageOutput.write(body, 0, body.length);
|
|
|
}
|
|
|
- Map<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("file", tempFile.toFile());
|
|
|
- String json = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
- log.info("上传图片结果:url:{},json:{}",url, json);
|
|
|
- return new JSONObject(json).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
+ return midjourneyCdnHost + "/picture/" + file.getName();
|
|
|
}
|
|
|
|
|
|
private static List<String> uploadBase64Pic(List<String> base64Array) throws IOException {
|