main
HuangHai 2 months ago
parent 1fc1588b12
commit b2af724e01

@ -2,6 +2,7 @@ package com.dsideal.aiSupport.Util.Liblib.Kit;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@ -9,24 +10,26 @@ import javax.crypto.spec.SecretKeySpec;
import com.dsideal.aiSupport.Plugin.YamlProp;
import com.jfinal.kit.Prop;
import lombok.Getter;
import okhttp3.OkHttpClient;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.RandomStringUtils;
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
public class SignUtil {
public class LibLibUtil {
// API基础URL
protected static final String API_BASE_URL = "https://openapi.liblibai.cloud";
// API访问凭证
protected static final String ak; // Access Key
protected static final String sk; // Secret Key
protected static final String accessKey; // Access Key
protected static final String secretKey; // Secret Key
public static Prop PropKit; // 配置文件工具
static {
//加载配置文件
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
PropKit = new YamlProp(configFile);
ak = PropKit.get("LIBLIB.accessKey"); // 从配置文件获取Access Key
sk = PropKit.get("LIBLIB.secretKey"); // 从配置文件获取Secret Key
accessKey = PropKit.get("LIBLIB.accessKey"); // 从配置文件获取Access Key
secretKey = PropKit.get("LIBLIB.secretKey"); // 从配置文件获取Secret Key
}
/**
@ -45,13 +48,23 @@ public class SignUtil {
}
}
/**
* OkHttpClient
* @return OkHttpClient
*/
protected static OkHttpClient createHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
}
/**
*
* @param uri APIuri
* @return
*/
public static SignatureInfo makeSignInfo(String uri) {
public static SignatureInfo sign(String uri) {
// 当前毫秒时间戳
long timestamp = System.currentTimeMillis();
// 随机字符串
@ -60,7 +73,7 @@ public class SignUtil {
String content = uri + "&" + timestamp + "&" + signatureNonce;
try {
// 生成签名
SecretKeySpec secret = new SecretKeySpec(sk.getBytes(), "HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secret);
String signature = Base64.encodeBase64URLSafeString(mac.doFinal(content.getBytes()));

@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dsideal.aiSupport.Plugin.YamlProp;
import com.dsideal.aiSupport.Util.Liblib.Kit.SignUtil;
import com.dsideal.aiSupport.Util.Liblib.Kit.LibLibUtil;
import com.jfinal.kit.Prop;
import okhttp3.*;
import org.slf4j.Logger;
@ -15,79 +15,39 @@ import java.util.concurrent.TimeUnit;
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
/**
* LibLib API
* 使ControlNet
*/
public class ImageToImageUltra {
private static final Logger log = LoggerFactory.getLogger(ImageToImageUltra.class);
// API基础URL
private static final String API_BASE_URL = "https://openapi.liblibai.cloud";
public class UltraImg2Img extends LibLibUtil {
//日志
private static final Logger log = LoggerFactory.getLogger(UltraImg2Img.class);
// 图生图API路径
private static final String IMG_TO_IMG_PATH = "/api/generate/webui/img2img/ultra";
// API访问凭证
protected static String accessKey;
protected static String secretKey;
public static Prop PropKit;
static {
//加载配置文件
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
PropKit = new YamlProp(configFile);
accessKey = PropKit.get("LIBLIB.accessKey");
secretKey = PropKit.get("LIBLIB.secretKey");
}
/**
* OkHttpClient
* @return OkHttpClient
*/
private static OkHttpClient createHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
}
/**
*
* @param templateUuid UUID
* @param prompt
* @param sourceImageUrl URL
* @param width
* @param height
* @param steps
* @param cfgScale
* @param seed -1
*
* @param templateUuid UUID
* @param prompt
* @param sourceImageUrl URL
* @param width
* @param height
* @param steps
* @param cfgScale
* @param seed -1
* @param controlNetModel ControlNetID
* @param controlWeight
* @param controlWeight
* @return UUID
* @throws IOException
*/
public static String submitImageToImageTask(
String templateUuid,
String prompt,
String sourceImageUrl,
int width,
int height,
int steps,
double cfgScale,
long seed,
String controlNetModel,
String templateUuid, String prompt, String sourceImageUrl, int width,
int height, int steps, double cfgScale, long seed, String controlNetModel,
double controlWeight) throws IOException {
// 创建OkHttpClient
OkHttpClient client = createHttpClient();
// 构建请求体
JSONObject requestBody = new JSONObject();
// 添加模板UUID
requestBody.put("templateUuid", templateUuid);
// 构建生成参数
JSONObject generateParams = new JSONObject();
generateParams.put("prompt", prompt);
@ -102,59 +62,18 @@ public class ImageToImageUltra {
generateParams.put("steps", steps);
generateParams.put("restoreFaces", 0);
generateParams.put("sourceImage", sourceImageUrl);
// 构建ControlNet参数
JSONArray controlNetArray = new JSONArray();
JSONObject controlNet = new JSONObject();
controlNet.put("unitOrder", 0);
/*
1. controlType
1. line线稿
2. depth
3. pose姿
4. IPAdapter
2. controlImage访URL
*/
controlNet.put("controlImage", sourceImageUrl);
controlNet.put("width", width);
controlNet.put("height", height);
controlNet.put("preprocessor", 68);
// 构建注释参数
JSONObject annotationParameters = new JSONObject();
annotationParameters.put("entityControl", new JSONObject());
controlNet.put("annotationParameters", annotationParameters);
// 设置ControlNet模型和权重
controlNet.put("model", controlNetModel);
controlNet.put("controlWeight", controlWeight);
controlNet.put("startingControlStep", 0);
controlNet.put("endingControlStep", 1);
controlNet.put("pixelPerfect", 1);
controlNet.put("controlMode", 0);
controlNet.put("resizeMode", 1);
// 添加ControlNet到数组
// 暂不加载ControlNet
//controlNetArray.add(controlNet);
//generateParams.put("controlNet", controlNetArray);
// 将生成参数添加到请求体
requestBody.put("generateParams", generateParams);
// 获取API路径
String uri = IMG_TO_IMG_PATH;
// 生成签名信息
SignUtil.SignatureInfo signInfo = SignUtil.makeSignInfo(uri);
LibLibUtil.SignatureInfo signInfo = LibLibUtil.sign(uri);
// 构建带签名的URL
HttpUrl.Builder urlBuilder = HttpUrl.parse(API_BASE_URL + uri).newBuilder()
.addQueryParameter("AccessKey", accessKey)
.addQueryParameter("Signature", signInfo.getSignature())
.addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp()))
.addQueryParameter("SignatureNonce", signInfo.getSignatureNonce());
// 创建请求
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString());
@ -163,36 +82,34 @@ public class ImageToImageUltra {
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
// 执行请求
log.info("提交图生图任务: {}", requestBody.toJSONString());
log.info("请求URL: {}", urlBuilder.build());
Response response = client.newCall(request).execute();
// 处理响应
if (!response.isSuccessful()) {
String errorMsg = "图生图任务提交失败,状态码: " + response.code();
log.error(errorMsg);
throw new IOException(errorMsg);
}
// 解析响应
String responseBody = response.body().string();
log.info("图生图任务提交响应: {}", responseBody);
JSONObject responseJson = JSON.parseObject(responseBody);
int code = responseJson.getIntValue("code");
if (code != 0) {
String errorMsg = "图生图任务提交失败,错误码: " + code + ", 错误信息: " + responseJson.getString("msg");
log.error(errorMsg);
throw new IOException(errorMsg);
}
// 获取生成任务UUID
String generateUuid = responseJson.getJSONObject("data").getString("generateUuid");
log.info("图生图任务已提交任务UUID: {}", generateUuid);
return generateUuid;
}
@ -220,7 +137,7 @@ public class ImageToImageUltra {
String controlNetModel = "6f1767b5f9eb47289525d06ae882a0e5";
// 控制权重
double controlWeight = 0.9;
// 提交图生图任务
String generateUuid = submitImageToImageTask(
templateUuid,
@ -234,11 +151,11 @@ public class ImageToImageUltra {
controlNetModel,
controlWeight
);
// 输出生成任务UUID
log.info("图生图任务已提交任务UUID: {}", generateUuid);
log.info("请使用此UUID查询任务进度和结果");
} catch (Exception e) {
log.error("图生图任务执行失败", e);
}
Loading…
Cancel
Save