|
|
|
@ -9,6 +9,7 @@ import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.dsideal.aiSupport.Util.KeLing.Kit.KeLingJwtUtil;
|
|
|
|
|
import com.dsideal.aiSupport.Util.KeLing.Kit.KlCommon;
|
|
|
|
|
import com.dsideal.aiSupport.Util.KeLing.Kit.KlErrorCode;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
@ -46,6 +47,7 @@ public class KlText2Image extends KlCommon {
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
|
|
log.info("生成图片请求体:{}", JSONUtil.toJsonStr(requestBody));
|
|
|
|
|
log.info("生成图片响应体:{}", response.body());
|
|
|
|
|
// 检查响应状态码
|
|
|
|
|
if (response.getStatus() != 200) {
|
|
|
|
|
throw new Exception("请求失败,状态码:" + response.getStatus());
|
|
|
|
@ -60,7 +62,16 @@ public class KlText2Image extends KlCommon {
|
|
|
|
|
int code = responseJson.getInt("code");
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String message = responseJson.getStr("message");
|
|
|
|
|
throw new Exception("生成图片失败:" + message);
|
|
|
|
|
String solution = KlErrorCode.getSolutionByCode(code);
|
|
|
|
|
String errorMsg = String.format("生成图片失败:[%d] %s - %s", code, message, solution);
|
|
|
|
|
|
|
|
|
|
// 特殊处理资源包耗尽的情况
|
|
|
|
|
if (code == KlErrorCode.RESOURCE_EXHAUSTED.getCode()) {
|
|
|
|
|
log.error("可灵AI资源包已耗尽,请充值后再试");
|
|
|
|
|
throw new Exception("可灵AI资源包已耗尽,请充值后再试");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取任务ID
|
|
|
|
@ -101,7 +112,9 @@ public class KlText2Image extends KlCommon {
|
|
|
|
|
int code = responseJson.getInt("code");
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String message = responseJson.getStr("message");
|
|
|
|
|
throw new Exception("查询任务状态失败:" + message);
|
|
|
|
|
String solution = KlErrorCode.getSolutionByCode(code);
|
|
|
|
|
String errorMsg = String.format("查询任务状态失败:[%d] %s - %s", code, message, solution);
|
|
|
|
|
throw new Exception(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseJson;
|
|
|
|
@ -132,66 +145,92 @@ public class KlText2Image extends KlCommon {
|
|
|
|
|
|
|
|
|
|
// 添加重试逻辑
|
|
|
|
|
int generateRetryCount = 0;
|
|
|
|
|
int maxGenerateRetries = 1000; // 最大重试次数
|
|
|
|
|
int maxGenerateRetries = 5; // 最大重试次数
|
|
|
|
|
int generateRetryInterval = 5000; // 重试间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
String taskId;
|
|
|
|
|
while (true) {
|
|
|
|
|
String taskId = null;
|
|
|
|
|
boolean accountIssue = false;
|
|
|
|
|
|
|
|
|
|
while (generateRetryCount < maxGenerateRetries && !accountIssue) {
|
|
|
|
|
try {
|
|
|
|
|
taskId = generateImage(prompt, modelName);
|
|
|
|
|
break;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成图片异常: {}", e.getMessage(), e);
|
|
|
|
|
generateRetryCount++;
|
|
|
|
|
if (generateRetryCount < maxGenerateRetries) {
|
|
|
|
|
log.warn("等待{}毫秒后重试...", generateRetryInterval);
|
|
|
|
|
Thread.sleep(generateRetryInterval);
|
|
|
|
|
|
|
|
|
|
// 检查是否是账户问题
|
|
|
|
|
if (e.getMessage().contains("资源包已耗尽") ||
|
|
|
|
|
e.getMessage().contains("账户欠费") ||
|
|
|
|
|
e.getMessage().contains("无权限")) {
|
|
|
|
|
log.error("账户问题,停止重试");
|
|
|
|
|
accountIssue = true;
|
|
|
|
|
} else {
|
|
|
|
|
throw e; // 达到最大重试次数,抛出异常
|
|
|
|
|
generateRetryCount++;
|
|
|
|
|
if (generateRetryCount < maxGenerateRetries) {
|
|
|
|
|
log.warn("等待{}毫秒后重试...", generateRetryInterval);
|
|
|
|
|
Thread.sleep(generateRetryInterval);
|
|
|
|
|
} else {
|
|
|
|
|
throw e; // 达到最大重试次数,抛出异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (taskId == null) {
|
|
|
|
|
log.error("生成图片失败,已达到最大重试次数: {}", maxGenerateRetries);
|
|
|
|
|
if (accountIssue) {
|
|
|
|
|
log.error("账户问题,请检查账户状态或充值后再试");
|
|
|
|
|
} else {
|
|
|
|
|
log.error("生成图片失败,已达到最大重试次数: {}", maxGenerateRetries);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询任务状态
|
|
|
|
|
int queryRetryCount = 0;
|
|
|
|
|
int maxQueryRetries = 1000; // 最大查询次数
|
|
|
|
|
int maxQueryRetries = 30; // 最大查询次数
|
|
|
|
|
int queryRetryInterval = 3000; // 查询间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
while (queryRetryCount < maxQueryRetries) {
|
|
|
|
|
JSONObject result = queryTaskStatus(taskId);
|
|
|
|
|
JSONObject data = result.getJSONObject("data");
|
|
|
|
|
String taskStatus = data.getStr("task_status");
|
|
|
|
|
|
|
|
|
|
if ("failed".equals(taskStatus)) {
|
|
|
|
|
String taskStatusMsg = data.getStr("task_status_msg");
|
|
|
|
|
log.error("任务失败: {}", taskStatusMsg);
|
|
|
|
|
break;
|
|
|
|
|
} else if ("succeed".equals(taskStatus)) {
|
|
|
|
|
// 获取图片URL
|
|
|
|
|
JSONObject taskResult = data.getJSONObject("task_result");
|
|
|
|
|
JSONArray images = taskResult.getJSONArray("images");
|
|
|
|
|
try {
|
|
|
|
|
JSONObject result = queryTaskStatus(taskId);
|
|
|
|
|
JSONObject data = result.getJSONObject("data");
|
|
|
|
|
String taskStatus = data.getStr("task_status");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
|
|
JSONObject image = images.getJSONObject(i);
|
|
|
|
|
int index = image.getInt("index");
|
|
|
|
|
String imageUrl = image.getStr("url");
|
|
|
|
|
if ("failed".equals(taskStatus)) {
|
|
|
|
|
String taskStatusMsg = data.getStr("task_status_msg");
|
|
|
|
|
log.error("任务失败: {}", taskStatusMsg);
|
|
|
|
|
break;
|
|
|
|
|
} else if ("succeed".equals(taskStatus)) {
|
|
|
|
|
// 获取图片URL
|
|
|
|
|
JSONObject taskResult = data.getJSONObject("task_result");
|
|
|
|
|
JSONArray images = taskResult.getJSONArray("images");
|
|
|
|
|
|
|
|
|
|
// 下载图片
|
|
|
|
|
String saveImagePath = basePath + "image_" + index + ".png";
|
|
|
|
|
log.info("开始下载图片...");
|
|
|
|
|
downloadFile(imageUrl, saveImagePath);
|
|
|
|
|
log.info("图片已下载到: {}", saveImagePath);
|
|
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
|
|
JSONObject image = images.getJSONObject(i);
|
|
|
|
|
int index = image.getInt("index");
|
|
|
|
|
String imageUrl = image.getStr("url");
|
|
|
|
|
|
|
|
|
|
// 下载图片
|
|
|
|
|
String saveImagePath = basePath + "image_" + index + ".png";
|
|
|
|
|
log.info("开始下载图片...");
|
|
|
|
|
downloadFile(imageUrl, saveImagePath);
|
|
|
|
|
log.info("图片已下载到: {}", saveImagePath);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
log.info("任务状态: {}, 等待{}毫秒后重试...", taskStatus, queryRetryInterval);
|
|
|
|
|
Thread.sleep(queryRetryInterval);
|
|
|
|
|
queryRetryCount++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
log.info("任务状态: {}, 等待{}毫秒后重试...", taskStatus, queryRetryInterval);
|
|
|
|
|
Thread.sleep(queryRetryInterval);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("查询任务状态异常: {}", e.getMessage(), e);
|
|
|
|
|
queryRetryCount++;
|
|
|
|
|
if (queryRetryCount < maxQueryRetries) {
|
|
|
|
|
log.warn("等待{}毫秒后重试...", queryRetryInterval);
|
|
|
|
|
Thread.sleep(queryRetryInterval);
|
|
|
|
|
} else {
|
|
|
|
|
throw e; // 达到最大重试次数,抛出异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|