main
HuangHai 2 months ago
parent 76b3b12a87
commit 275c1a5116

@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* AI
@ -28,11 +27,10 @@ public class KlText2Video extends KlCommon {
*
* @param prompt
* @param modelName kling-v1, kling-v1-6
* @param externalTaskId ID
* @return ID
* @throws Exception
*/
public static String generateVideo(String prompt, String modelName, String externalTaskId) throws Exception {
public static String generateVideo(String prompt, String modelName) throws Exception {
// 获取JWT令牌
String jwt = getJwt();
@ -40,12 +38,7 @@ public class KlText2Video extends KlCommon {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model_name", modelName);
requestBody.put("prompt", prompt);
// 如果提供了外部任务ID则添加到请求体中
if (externalTaskId != null && !externalTaskId.isEmpty()) {
requestBody.put("external_task_id", externalTaskId);
}
// 使用Hutool发送POST请求
HttpResponse response = HttpRequest.post(BASE_URL + GENERATION_PATH)
.header("Content-Type", "application/json")
@ -54,13 +47,10 @@ public class KlText2Video extends KlCommon {
.execute();
log.info("生成视频请求体:{}", JSONUtil.toJsonStr(requestBody));
log.info("生成视频响应体:{}", response.body());
// 检查响应状态码
if (response.getStatus() != 200) {
throw new Exception("请求失败,状态码:" + response.getStatus());
}
// 解析响应
String responseBody = response.body();
JSONObject responseJson = JSONUtil.parseObj(responseBody);
@ -128,16 +118,12 @@ public class KlText2Video extends KlCommon {
return responseJson;
}
// 下载函数已移至KlCommon类
public static void main(String[] args) throws Exception {
// 提示词和模型名称
String prompt = "一只可爱的小猫咪在草地上奔跑,阳光明媚";
String modelName = "kling-v1"; // 可选kling-v1, kling-v1-6
// 生成外部任务ID可选
String externalTaskId = "video_" + UUID.randomUUID().toString().replace("-", "");
// 添加重试逻辑
int generateRetryCount = 0;
int maxGenerateRetries = 5; // 最大重试次数
@ -148,7 +134,7 @@ public class KlText2Video extends KlCommon {
while (!accountIssue) {
try {
taskId = generateVideo(prompt, modelName, externalTaskId);
taskId = generateVideo(prompt, modelName);
break;
} catch (Exception e) {
log.error("生成视频异常: {}", e.getMessage(), e);

Loading…
Cancel
Save