|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package com.dsideal.aiSupport.Util.Midjourney;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.aiSupport.Util.Midjourney.Kit.MjCommon;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
@ -14,10 +15,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
* Midjourney API 工具类
|
|
|
|
|
* 用于调用 Midjourney 的 imagine 接口生成图片
|
|
|
|
|
*/
|
|
|
|
|
public class Midjourney {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(Midjourney.class);
|
|
|
|
|
private static final String BASE_URL = "https://goapi.gptnb.ai";
|
|
|
|
|
private static final String API_KEY = "sk-amQHwiEzPIZIB2KuF5A10dC23a0e4b02B48a7a2b6aFa0662";
|
|
|
|
|
public class Txt2Img extends MjCommon {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(Txt2Img.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交 imagine 请求
|
|
|
|
@ -73,7 +72,7 @@ public class Midjourney {
|
|
|
|
|
.url(BASE_URL + "/mj/submit/imagine")
|
|
|
|
|
.method("POST", body)
|
|
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
|
|
.addHeader("Authorization", "Bearer " + API_KEY)
|
|
|
|
|
.addHeader("Authorization", "Bearer " + ak)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 发送请求并获取响应
|
|
|
|
@ -109,46 +108,6 @@ public class Midjourney {
|
|
|
|
|
return taskId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询任务状态
|
|
|
|
|
*
|
|
|
|
|
* @param taskId 任务ID
|
|
|
|
|
* @return 任务结果
|
|
|
|
|
* @throws Exception 异常信息
|
|
|
|
|
*/
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
public static JSONObject queryTaskStatus(String taskId) {
|
|
|
|
|
// 创建OkHttpClient
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder()
|
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 创建请求
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(BASE_URL + "/mj/task/" + taskId + "/fetch")
|
|
|
|
|
.method("GET", null)
|
|
|
|
|
.addHeader("Authorization", "Bearer " + API_KEY)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 发送请求并获取响应
|
|
|
|
|
log.info("查询Midjourney任务状态: {}", taskId);
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
// 检查响应状态
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
String errorMsg = "Midjourney API请求失败,状态码: " + response.code();
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new Exception(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body().string();
|
|
|
|
|
log.info("查询Midjourney任务状态响应: {}", responseBody);
|
|
|
|
|
|
|
|
|
|
return JSON.parseObject(responseBody);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
// 提示词
|
|
|
|
@ -161,6 +120,7 @@ public class Midjourney {
|
|
|
|
|
int maxRetries = 1000;
|
|
|
|
|
int retryCount = 0;
|
|
|
|
|
int retryInterval = 5000; // 5秒
|
|
|
|
|
String imageUrl = null;
|
|
|
|
|
|
|
|
|
|
while (retryCount < maxRetries) {
|
|
|
|
|
JSONObject result = queryTaskStatus(taskId);
|
|
|
|
@ -177,7 +137,7 @@ public class Midjourney {
|
|
|
|
|
if (status != null && !status.isEmpty()) {
|
|
|
|
|
if ("SUCCESS".equals(status)) {
|
|
|
|
|
// 任务成功,获取图片URL
|
|
|
|
|
String imageUrl = result.getString("imageUrl");
|
|
|
|
|
imageUrl = result.getString("imageUrl");
|
|
|
|
|
log.info("生成的图片URL: {}", imageUrl);
|
|
|
|
|
break;
|
|
|
|
|
} else if ("FAILED".equals(status)) {
|
|
|
|
@ -190,7 +150,7 @@ public class Midjourney {
|
|
|
|
|
String description = result.getString("description");
|
|
|
|
|
if (description != null && description.contains("成功") && !"0%".equals(progress)) {
|
|
|
|
|
// 如果描述包含"成功"且进度不为0%,可能任务已完成
|
|
|
|
|
String imageUrl = result.getString("imageUrl");
|
|
|
|
|
imageUrl = result.getString("imageUrl");
|
|
|
|
|
if (imageUrl != null && !imageUrl.isEmpty()) {
|
|
|
|
|
log.info("生成的图片URL: {}", imageUrl);
|
|
|
|
|
break;
|
|
|
|
@ -207,5 +167,15 @@ public class Midjourney {
|
|
|
|
|
if (retryCount >= maxRetries) {
|
|
|
|
|
log.error("查询任务状态超时,已达到最大重试次数: {}", maxRetries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果获取到了图片URL,则下载保存
|
|
|
|
|
if (imageUrl != null && !imageUrl.isEmpty()) {
|
|
|
|
|
// 生成文件名(使用时间戳和任务ID)
|
|
|
|
|
String fileName = "mj_" + System.currentTimeMillis() + "_" + taskId + ".png";
|
|
|
|
|
// 完整保存路径
|
|
|
|
|
String savePath = basePath + "/" + fileName;
|
|
|
|
|
// 下载图片
|
|
|
|
|
downloadFile(imageUrl, savePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|