parent
627344d0b2
commit
1955fa69a7
@ -0,0 +1,73 @@
|
||||
package com.dsideal.aiSupport.Util.Liblib;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import cloud.liblibai.client.LibLib;
|
||||
import cloud.liblibai.openapi.client.ApiException;
|
||||
import cloud.liblibai.openapi.client.model.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ComfyTask {
|
||||
|
||||
public static void main(String[] args) throws ApiException, JsonProcessingException, InterruptedException {
|
||||
LibLib api = new LibLib();
|
||||
ComfyRequest request = new ComfyRequest();
|
||||
|
||||
request.templateUuid("");
|
||||
|
||||
ComfyNodeParams inputNode = new ComfyNodeParams();
|
||||
inputNode.classType("LoadImage")
|
||||
.putInputsItem("image", "https://liblibai-tmp-image.liblib.cloud/img/baf2e419ce1cb06812314957efd2e067/af0c523d3d2b4092ab45c64c72e4deb76babb12e9b8a178eb524143c3b71bf85.png");
|
||||
|
||||
ComfyNodeParams scaleNode = new ComfyNodeParams();
|
||||
scaleNode.classType("ScaleImage")
|
||||
.putInputsItem("width", 768);
|
||||
|
||||
ComfyNodeParams repeatLatentNode = new ComfyNodeParams();
|
||||
repeatLatentNode.classType("RepeatLatentImage")
|
||||
.putInputsItem("amount", 4);
|
||||
|
||||
ComfyRequestGenerateParams generateParams = new ComfyRequestGenerateParams();
|
||||
generateParams.workflowUuid("2f22ab7ce4c044afb6d5eee2e61547f3");
|
||||
generateParams.putAdditionalProperty("12", inputNode);
|
||||
generateParams.putAdditionalProperty("112", scaleNode);
|
||||
generateParams.putAdditionalProperty("136", repeatLatentNode);
|
||||
request.generateParams(generateParams);
|
||||
|
||||
// 异步 SDK API 调用
|
||||
SubmitComfyResponse submitComfyResponse = api.submitComfyTask(request);
|
||||
String uuid = submitComfyResponse.getData().getGenerateUuid();
|
||||
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
ComfyStatusResponse comfyStatus = api.getComfyStatus(new ComfyStatusRequest().generateUuid(uuid));
|
||||
GenerateStatus status = comfyStatus.getData().getGenerateStatus();
|
||||
System.out.println(comfyStatus);
|
||||
switch (status) {
|
||||
case RUNNING:
|
||||
case PENDING:
|
||||
case APPROVING:
|
||||
case GENERATED:
|
||||
break;
|
||||
case SUCCEED:
|
||||
finished = true;
|
||||
System.out.println(status);
|
||||
break;
|
||||
case TIMEOUT:
|
||||
case FAILED:
|
||||
finished = true;
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Unknown comfy status: " + status);
|
||||
}
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
//同步 SDK API 调用 ComfyTask
|
||||
ComfyStatusResponseData comfyStatusResponseData = api.runComfy(request);
|
||||
if (comfyStatusResponseData.getGenerateStatus() == GenerateStatus.SUCCEED) {
|
||||
System.out.println("generated images: " + comfyStatusResponseData.getImages());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.dsideal.aiSupport.Util.Liblib;
|
||||
|
||||
import cloud.liblibai.client.LibLib;
|
||||
import cloud.liblibai.openapi.client.ApiException;
|
||||
import cloud.liblibai.openapi.client.model.*;
|
||||
|
||||
public class ControlNetExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
LibLib api = new LibLib();
|
||||
TextToImageRequest request = new TextToImageRequest();
|
||||
TextToImageRequestGenerateParams params = new TextToImageRequestGenerateParams();
|
||||
// ControlNet controlnet = new ControlNet();
|
||||
// controlnet.unitOrder(1);
|
||||
// controlnet.setModel()
|
||||
params.prompt("1 girl").imgCount(1);
|
||||
request.generateParams(params);
|
||||
request.templateUuid("6f7c4652458d4802969f8d089cf5b91f");
|
||||
|
||||
try {
|
||||
StatusResponseData statusResponseData = api.textToImage(request);
|
||||
if (statusResponseData.getGenerateStatus() == GenerateStatus.SUCCEED ) {
|
||||
System.out.println("Generated output examples" + statusResponseData.getImages());
|
||||
}
|
||||
|
||||
} catch (ApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
package com.dsideal.aiSupport.Util.Liblib;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dsideal.aiSupport.Plugin.YamlProp;
|
||||
import com.jfinal.kit.Prop;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
|
||||
|
||||
/**
|
||||
* Liblib模型查询工具类
|
||||
*/
|
||||
public class QueryModel {
|
||||
|
||||
private static final String API_BASE_URL = "https://openapi.liblibai.cloud";
|
||||
private static final String MODEL_VERSION_URL = API_BASE_URL + "/api/model/version/get";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(QueryModel.class);
|
||||
|
||||
// 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 versionUuid 模型版本UUID
|
||||
* @return 模型版本信息
|
||||
* @throws IOException 异常信息
|
||||
*/
|
||||
public static JSONObject queryModelVersion(String versionUuid) throws IOException {
|
||||
// 创建OkHttpClient
|
||||
OkHttpClient client = createHttpClient();
|
||||
|
||||
// 构建请求体
|
||||
JSONObject requestBody = new JSONObject();
|
||||
requestBody.put("versionUuid", versionUuid);
|
||||
|
||||
// 获取API路径
|
||||
String uri = "/api/model/version/get";
|
||||
|
||||
// 生成签名信息
|
||||
SignUtil.SignatureInfo signInfo = SignUtil.makeSign(uri, secretKey);
|
||||
|
||||
// 构建带签名的URL
|
||||
HttpUrl.Builder urlBuilder = HttpUrl.parse(MODEL_VERSION_URL).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());
|
||||
Request request = new Request.Builder()
|
||||
.url(urlBuilder.build())
|
||||
.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);
|
||||
}
|
||||
|
||||
return responseJson.getJSONObject("data");
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用示例
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 查询模型版本信息
|
||||
//String versionUuid = "86961315d0b34f229d58fadb7f284972";
|
||||
String versionUuid = "31360f2f031b4ff6b589412a52713fcf";
|
||||
JSONObject modelVersionInfo = queryModelVersion(versionUuid);
|
||||
|
||||
// 打印模型版本信息
|
||||
log.info("模型版本信息: {}", modelVersionInfo);
|
||||
|
||||
// 提取关键信息
|
||||
String modelName = modelVersionInfo.getString("modelName");
|
||||
String versionName = modelVersionInfo.getString("versionName");
|
||||
String description = modelVersionInfo.getString("description");
|
||||
|
||||
log.info("模型名称: {}", modelName);
|
||||
log.info("版本名称: {}", versionName);
|
||||
log.info("描述: {}", description);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("查询模型版本信息失败", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.dsideal.aiSupport.Util.Liblib;
|
||||
|
||||
import cloud.liblibai.client.LibLib;
|
||||
import cloud.liblibai.openapi.client.ApiException;
|
||||
import cloud.liblibai.openapi.client.model.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TextToImage {
|
||||
public static void main(String[] args) throws ApiException, InterruptedException {
|
||||
LibLib api = new LibLib();
|
||||
GetModelVersionRequest getModelVersionRequest = new GetModelVersionRequest();
|
||||
getModelVersionRequest.setVersionUuid("a57911b5dfe64c6aa78821be99367276");
|
||||
VersionResponse modelVersion = api.getModelVersion(getModelVersionRequest);
|
||||
if (modelVersion.getData().getCommercialUse() == CommercialUseEnum.CommercialUse) {
|
||||
System.out.println(modelVersion);
|
||||
}
|
||||
TextToImageRequest request = new TextToImageRequest();
|
||||
TextToImageRequestGenerateParams params = new TextToImageRequestGenerateParams();
|
||||
params.checkPointId("a57911b5dfe64c6aa78821be99367276");
|
||||
params.prompt("1 girl").imgCount(2);
|
||||
params.cfgScale(7.5);
|
||||
ControlNet controlNet = new ControlNet();
|
||||
controlNet.width(1024).height(768);
|
||||
Map<String, Object> annoParams = new HashMap<>();
|
||||
Map<String, Object> cannyParams = new HashMap<>();
|
||||
cannyParams.put("preprocessorResolution", 512);
|
||||
annoParams.put("canny", cannyParams);
|
||||
controlNet.unitOrder(1);
|
||||
controlNet.model("efb97e9d8c237573298c3a5a7869b89c").preprocessor(1)
|
||||
.sourceImage("https://liblibai-online.liblib.cloud/img/081e9f07d9bd4c2ba090efde163518f9/7c1cc38e-522c-43fe-aca9-07d5420d743e.png")
|
||||
.annotationParameters(annoParams);
|
||||
params.addControlNetItem(controlNet);
|
||||
params.vaeId("2c1a337416e029dd65ab58784e8a4763");
|
||||
request.generateParams(params);
|
||||
request.templateUuid("6f7c4652458d4802969f8d089cf5b91f");
|
||||
|
||||
|
||||
//NOTE(gz): 异步 SDK 调用方法
|
||||
SubmitResponse submitResponse = api.submitTextToImage(request);
|
||||
while(true) {
|
||||
if (submitResponse.getData() == null) {
|
||||
System.out.println("Error: " + submitResponse.getMsg());
|
||||
break;
|
||||
}
|
||||
StatusResponse status = api.getStatus(new StatusRequest().generateUuid(submitResponse.getData().getGenerateUuid()));
|
||||
System.out.println(status);
|
||||
if (status.getData().getGenerateStatus() == GenerateStatus.SUCCEED) {
|
||||
System.out.println(status.getData().getImages());
|
||||
break;
|
||||
}
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
//NOTE(gz): 同步 SDK 调用方法
|
||||
// StatusResponseData statusResponseData = api.textToImage(request);
|
||||
// if (statusResponseData.getGenerateStatus() == GenerateStatus.SUCCEED) {
|
||||
// System.out.println(statusResponseData.getImages());
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.dsideal.aiSupport.Util.Liblib;
|
||||
|
||||
import cloud.liblibai.client.LibLib;
|
||||
import cloud.liblibai.openapi.client.ApiException;
|
||||
import cloud.liblibai.openapi.client.model.*;
|
||||
|
||||
public class TextToImageUltra {
|
||||
public static void main(String[] args) throws ApiException, InterruptedException {
|
||||
LibLib api = new LibLib();
|
||||
TextToImageUltraRequest request = new TextToImageUltraRequest();
|
||||
request.templateUuid("5d7e67009b344550bc1aa6ccbfa1d7f4");
|
||||
TextToImageUltraRequestGenerateParams params = new TextToImageUltraRequestGenerateParams();
|
||||
request.generateParams(params);
|
||||
params.prompt("1 girl").aspectRatio(TextToImageUltraRequestGenerateParams.AspectRatioEnum.PORTRAIT)
|
||||
.steps(30).imgCount(4);
|
||||
SubmitResponse submitResponse = api.submitTextToImageUltra(request);
|
||||
|
||||
while(true) {
|
||||
StatusResponse status = api.getStatus(new StatusRequest().generateUuid(submitResponse.getData().getGenerateUuid()));
|
||||
System.out.println(status.toJson());
|
||||
if (status.getData().getGenerateStatus() == GenerateStatus.SUCCEED) {
|
||||
System.out.println(status.getData().getImages());
|
||||
break;
|
||||
}
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue