初始化
This commit is contained in:
35
easy-agents-image/easy-agents-image-siliconflow/pom.xml
Normal file
35
easy-agents-image/easy-agents-image-siliconflow/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-image</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-image-siliconflow</name>
|
||||
<artifactId>easy-agents-image-siliconflow</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.easyagents.image.siliconflow;
|
||||
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.easyagents.core.util.Maps;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SiliconImageModel implements ImageModel {
|
||||
private SiliconflowImageModelConfig config;
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public SiliconImageModel(SiliconflowImageModelConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer " + config.getApiKey());
|
||||
|
||||
String payload = Maps.of("prompt", request.getPrompt())
|
||||
.setIfNotEmpty("negative_prompt", request.getNegativePrompt())
|
||||
.setOrDefault("image_size", request.getSize(), config.getImageSize())
|
||||
.setOrDefault("batch_size", request.getN(), 1)
|
||||
.setOrDefault("num_inference_steps", request.getOption("num_inference_steps"), config.getNumInferenceSteps())
|
||||
.setOrDefault("guidance_scale", request.getOption("guidance_scale"), config.getGuidanceScale())
|
||||
.toJSON();
|
||||
|
||||
String url = config.getEndpoint() + SiliconflowImageModels.getPath(config.getModel());
|
||||
String response = httpClient.post(url, headers, payload);
|
||||
if (StringUtil.noText(response)) {
|
||||
return ImageResponse.error("response is no text");
|
||||
}
|
||||
|
||||
if (StringUtil.notJsonObject(response)) {
|
||||
return ImageResponse.error(response);
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(response);
|
||||
JSONArray imagesArray = jsonObject.getJSONArray("images");
|
||||
if (imagesArray == null || imagesArray.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ImageResponse imageResponse = new ImageResponse();
|
||||
for (int i = 0; i < imagesArray.size(); i++) {
|
||||
JSONObject imageObject = imagesArray.getJSONObject(i);
|
||||
imageResponse.addImage(imageObject.getString("url"));
|
||||
}
|
||||
|
||||
return imageResponse;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new IllegalStateException("SiliconImageModel Can not support edit image.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new IllegalStateException("SiliconImageModel Can not support vary image.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.easyagents.image.siliconflow;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SiliconflowImageModelConfig implements Serializable {
|
||||
private String endpoint = "https://api.siliconflow.cn";
|
||||
private String model = SiliconflowImageModels.flux_1_schnell;
|
||||
private String apiKey;
|
||||
private Integer numInferenceSteps = 20;
|
||||
private Integer guidanceScale = 7;
|
||||
private String imageSize = "1024x1024";
|
||||
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public Integer getNumInferenceSteps() {
|
||||
return numInferenceSteps;
|
||||
}
|
||||
|
||||
public void setNumInferenceSteps(Integer numInferenceSteps) {
|
||||
this.numInferenceSteps = numInferenceSteps;
|
||||
}
|
||||
|
||||
public Integer getGuidanceScale() {
|
||||
return guidanceScale;
|
||||
}
|
||||
|
||||
public void setGuidanceScale(Integer guidanceScale) {
|
||||
this.guidanceScale = guidanceScale;
|
||||
}
|
||||
|
||||
public String getImageSize() {
|
||||
return imageSize;
|
||||
}
|
||||
|
||||
public void setImageSize(String imageSize) {
|
||||
this.imageSize = imageSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.easyagents.image.siliconflow;
|
||||
|
||||
import com.easyagents.core.util.Maps;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SiliconflowImageModels {
|
||||
|
||||
|
||||
/**
|
||||
* 由 Black Forest Labs 开发的 120 亿参数文生图模型,采用潜在对抗扩散蒸馏技术,能够在 1 到 4 步内生成高质量图像。该模型性能媲美闭源替代品,并在 Apache-2.0 许可证下发布,适用于个人、科研和商业用途。
|
||||
*/
|
||||
public static final String flux_1_schnell = "FLUX.1-schnell";
|
||||
|
||||
/**
|
||||
* 由 Stability AI 开发并开源的文生图大模型,其创意图像生成能力位居行业前列。具备出色的指令理解能力,能够支持反向 Prompt 定义来精确生成内容。
|
||||
*/
|
||||
public static final String Stable_Diffusion_3 = "Stable Diffusion 3";
|
||||
public static final String Stable_Diffusion_XL = "Stable Diffusion XL";
|
||||
public static final String Stable_Diffusion_2_1 = "Stable Diffusion 2.1";
|
||||
public static final String Stable_Diffusion_Turbo = "Stable Diffusion Turbo";
|
||||
public static final String Stable_Diffusion_XL_Turbo = "Stable Diffusion XL Turbo";
|
||||
public static final String Stable_Diffusion_XL_Lighting = "Stable Diffusion XL Lighting";
|
||||
|
||||
|
||||
private static Map<String, Object> modelsPathMapping = Maps
|
||||
.of(flux_1_schnell, "/v1/black-forest-labs/FLUX.1-schnell/text-to-image")
|
||||
.set(Stable_Diffusion_3, "/v1/stabilityai/stable-diffusion-3-medium/text-to-image")
|
||||
.set(Stable_Diffusion_XL, "/v1/stabilityai/stable-diffusion-xl-base-1.0/text-to-image")
|
||||
.set(Stable_Diffusion_2_1, "/v1/stabilityai/stable-diffusion-2-1/text-to-image")
|
||||
.set(Stable_Diffusion_Turbo, "/v1/stabilityai/sd-turbo/text-to-image")
|
||||
.set(Stable_Diffusion_XL_Turbo, "/v1/stabilityai/sdxl-turbo/text-to-image")
|
||||
.set(Stable_Diffusion_XL_Lighting, "/v1/ByteDance/SDXL-Lightning/text-to-image")
|
||||
;
|
||||
|
||||
public static String getPath(String model) {
|
||||
return (String) modelsPathMapping.get(model);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.easyagents.image.siliconflow.test;
|
||||
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.Image;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.siliconflow.SiliconImageModel;
|
||||
import com.easyagents.image.siliconflow.SiliconflowImageModelConfig;
|
||||
import com.easyagents.image.siliconflow.SiliconflowImageModels;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class SiliconflowImageModelTest {
|
||||
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
SiliconflowImageModelConfig config = new SiliconflowImageModelConfig();
|
||||
config.setModel(SiliconflowImageModels.Stable_Diffusion_XL);
|
||||
config.setApiKey("sk-****");
|
||||
|
||||
SiliconImageModel imageModel = new SiliconImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
request.setPrompt("A cute little tiger standing in the high-speed train");
|
||||
request.setSize(1024,1024);
|
||||
request.setN(4);
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
if (generate != null && generate.getImages() != null){
|
||||
int index = 0;
|
||||
for (Image image : generate.getImages()) {
|
||||
image.writeToFile(new File("/Users/michael/Desktop/test/image"+(index++)+".jpg"));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user