|
|
|
@ -30,12 +30,12 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
public static String generateImage(String prompt, String modelName) throws Exception {
|
|
|
|
|
// 获取JWT令牌
|
|
|
|
|
String jwt = getJwt();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建请求体
|
|
|
|
|
Map<String, Object> requestBody = new HashMap<>();
|
|
|
|
|
requestBody.put("model_name", modelName);
|
|
|
|
|
requestBody.put("prompt", prompt);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用Hutool发送POST请求
|
|
|
|
|
HttpResponse response = HttpRequest.post(BASE_URL + GENERATION_PATH)
|
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
@ -48,35 +48,35 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
if (response.getStatus() != 200) {
|
|
|
|
|
throw new Exception("请求失败,状态码:" + response.getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body();
|
|
|
|
|
JSONObject responseJson = JSONObject.parseObject(responseBody);
|
|
|
|
|
log.info("生成图片响应:{}", responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查响应状态
|
|
|
|
|
int code = responseJson.getInteger("code");
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String message = responseJson.getString("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
|
|
|
|
|
String taskId = responseJson.getJSONObject("data").getString("task_id");
|
|
|
|
|
log.info("生成图片任务ID:{}", taskId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return taskId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询任务状态
|
|
|
|
|
*
|
|
|
|
@ -87,31 +87,32 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
public static JSONObject queryTaskStatus(String taskId) throws Exception {
|
|
|
|
|
return KlCommon.queryTaskStatus(taskId, QUERY_PATH, "文生图");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
// 提示词和模型名称
|
|
|
|
|
String prompt = "一只可爱的小猫咪在草地上玩耍,阳光明媚";
|
|
|
|
|
String modelName = "kling-v1"; // 可选:kling-v1, kling-v1-5, kling-v2
|
|
|
|
|
|
|
|
|
|
String saveImagePath = basePath + "KeLing_Txt_2_Image.png";
|
|
|
|
|
|
|
|
|
|
// 添加重试逻辑
|
|
|
|
|
int generateRetryCount = 0;
|
|
|
|
|
int maxGenerateRetries = 5; // 最大重试次数
|
|
|
|
|
int generateRetryInterval = 5000; // 重试间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String taskId = null;
|
|
|
|
|
boolean accountIssue = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (!accountIssue) {
|
|
|
|
|
try {
|
|
|
|
|
taskId = generateImage(prompt, modelName);
|
|
|
|
|
break;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成图片异常: {}", e.getMessage(), e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否是账户问题
|
|
|
|
|
if (e.getMessage().contains("资源包已耗尽") ||
|
|
|
|
|
e.getMessage().contains("账户欠费") ||
|
|
|
|
|
e.getMessage().contains("无权限")) {
|
|
|
|
|
if (e.getMessage().contains("资源包已耗尽") ||
|
|
|
|
|
e.getMessage().contains("账户欠费") ||
|
|
|
|
|
e.getMessage().contains("无权限")) {
|
|
|
|
|
log.error("账户问题,停止重试");
|
|
|
|
|
accountIssue = true;
|
|
|
|
|
} else {
|
|
|
|
@ -125,7 +126,7 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (taskId == null) {
|
|
|
|
|
if (accountIssue) {
|
|
|
|
|
log.error("账户问题,请检查账户状态或充值后再试");
|
|
|
|
@ -134,18 +135,18 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询任务状态
|
|
|
|
|
int queryRetryCount = 0;
|
|
|
|
|
int maxQueryRetries = 1000; // 最大查询次数
|
|
|
|
|
int queryRetryInterval = 3000; // 查询间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (queryRetryCount < maxQueryRetries) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject result = queryTaskStatus(taskId);
|
|
|
|
|
JSONObject data = result.getJSONObject("data");
|
|
|
|
|
String taskStatus = data.getString("task_status");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ("failed".equals(taskStatus)) {
|
|
|
|
|
String taskStatusMsg = data.getString("task_status_msg");
|
|
|
|
|
log.error("任务失败: {}", taskStatusMsg);
|
|
|
|
@ -154,14 +155,14 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
// 获取图片URL
|
|
|
|
|
JSONObject taskResult = data.getJSONObject("task_result");
|
|
|
|
|
JSONArray images = taskResult.getJSONArray("images");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
|
|
JSONObject image = images.getJSONObject(i);
|
|
|
|
|
int index = image.getInteger("index");
|
|
|
|
|
String imageUrl = image.getString("url");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 下载图片
|
|
|
|
|
String saveImagePath = basePath + "image_" + index + ".png";
|
|
|
|
|
|
|
|
|
|
log.info("开始下载图片...");
|
|
|
|
|
downloadFile(imageUrl, saveImagePath);
|
|
|
|
|
log.info("图片已下载到: {}", saveImagePath);
|
|
|
|
@ -183,7 +184,7 @@ public class KlTxt2Img extends KlCommon {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (queryRetryCount >= maxQueryRetries) {
|
|
|
|
|
log.error("任务查询超时,已达到最大查询次数: {}", maxQueryRetries);
|
|
|
|
|
}
|
|
|
|
|