初始化
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.volcengine;
|
||||
|
||||
import com.easyagents.core.model.image.*;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.volcengine.service.visual.IVisualService;
|
||||
import com.volcengine.service.visual.impl.VisualServiceImpl;
|
||||
|
||||
public class VolcengineImageModel implements ImageModel {
|
||||
|
||||
private VolcengineImageModelConfig config;
|
||||
private IVisualService visualService = VisualServiceImpl.getInstance();
|
||||
|
||||
public VolcengineImageModel(VolcengineImageModelConfig config) {
|
||||
this.config = config;
|
||||
visualService.setAccessKey(config.getAccessKey());
|
||||
visualService.setSecretKey(config.getSecretKey());
|
||||
}
|
||||
|
||||
private ImageResponse processImageRequest(GenerateImageRequest request) {
|
||||
JSONObject req = new JSONObject(request.getOptions());
|
||||
ImageResponse responseimage = new ImageResponse();
|
||||
try {
|
||||
Object response = visualService.cvProcess(req);
|
||||
if (response instanceof JSONObject) {
|
||||
JSONObject jsonResponse = (JSONObject) response;
|
||||
JSONObject dataObject = jsonResponse.getJSONObject("data");
|
||||
JSONArray imageUrlsArray = dataObject.getJSONArray("image_urls");
|
||||
for (int i = 0; i < imageUrlsArray.size(); i++) {
|
||||
responseimage.addImage(imageUrlsArray.getString(i));
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("Unexpected response type: " + response.getClass().getName());
|
||||
}
|
||||
return responseimage;
|
||||
} catch (Exception e) {
|
||||
ImageResponse.error(e.getMessage());
|
||||
e.printStackTrace(); // 记录堆栈跟踪方便调试
|
||||
return responseimage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse generate(GenerateImageRequest request) {
|
||||
return processImageRequest(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse img2imggenerate(GenerateImageRequest request) {
|
||||
return processImageRequest(request);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ImageResponse edit(EditImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support edit image");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse vary(VaryImageRequest request) {
|
||||
throw new UnsupportedOperationException("not support vary image");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.volcengine;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class VolcengineImageModelConfig implements Serializable {
|
||||
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import com.easyagents.core.model.image.GenerateImageRequest;
|
||||
import com.easyagents.core.model.image.ImageResponse;
|
||||
import com.easyagents.image.volcengine.VolcengineImageModel;
|
||||
import com.easyagents.image.volcengine.VolcengineImageModelConfig;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class VolcengineImageTest {
|
||||
@Test
|
||||
public void testGenImage(){
|
||||
VolcengineImageModelConfig config = new VolcengineImageModelConfig();
|
||||
config.setAccessKey("*********************");
|
||||
config.setSecretKey("*********************");
|
||||
|
||||
VolcengineImageModel imageModel = new VolcengineImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
//请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)参考通用2.1-文生图
|
||||
req.put("req_key","high_aes_general_v21_L");
|
||||
req.put("prompt","千军万马");
|
||||
req.put("model_version","general_v2.1_L");
|
||||
req.put("req_schedule_conf","general_v20_9B_pe");
|
||||
req.put("llm_seed",-1);
|
||||
req.put("seed",-1);
|
||||
req.put("scale",3.5);
|
||||
req.put("ddim_steps",25);
|
||||
req.put("width",512);
|
||||
req.put("height",512);
|
||||
req.put("use_pre_llm",true);
|
||||
req.put("use_sr",true);
|
||||
req.put("sr_seed",-1);
|
||||
req.put("sr_strength",0.4);
|
||||
req.put("sr_scale",3.5);
|
||||
req.put("sr_steps",20);
|
||||
req.put("is_only_sr",false);
|
||||
req.put("return_url",true);
|
||||
// 创建子级JSONObject
|
||||
JSONObject subData = new JSONObject();
|
||||
subData.put("add_logo", true);
|
||||
subData.put("position", 2);
|
||||
subData.put("language", 0);
|
||||
subData.put("opacity", 0.3);
|
||||
subData.put("logo_text_content", "wangyangyang");
|
||||
req.put("logo_info",subData);
|
||||
|
||||
request.setOptions(req);
|
||||
|
||||
ImageResponse generate = imageModel.generate(request);
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test( )
|
||||
public void testImg2ImgXLSft() throws IOException {
|
||||
VolcengineImageModelConfig config = new VolcengineImageModelConfig();
|
||||
config.setAccessKey("*********************");
|
||||
config.setSecretKey("*********************");
|
||||
|
||||
VolcengineImageModel imageModel = new VolcengineImageModel(config);
|
||||
|
||||
GenerateImageRequest request = new GenerateImageRequest();
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
req.put("req_key","i2i_xl_sft");
|
||||
List<String> images=new ArrayList<>();
|
||||
|
||||
File file = new File(System.getProperty("user.dir"), "../../testresource/ark_demo_img_1.png");
|
||||
// 将图片读取为字节数组
|
||||
byte[] imageBytes = Files.readAllBytes(Paths.get(file.toURI()));
|
||||
|
||||
// 将字节数组编码为Base64
|
||||
String base64String = Base64.getEncoder().encodeToString(imageBytes);
|
||||
|
||||
images.add(base64String);
|
||||
// images.add("https://ark-project.tos-cn-beijing.volces.com/doc_image/ark_demo_img_1.png");
|
||||
req.put("binary_data_base64",images);
|
||||
req.put("prompt","根据图片内容生成风格、服装及发型一样的亚洲美女图片");
|
||||
req.put("return_url",true);
|
||||
request.setOptions(req);
|
||||
|
||||
ImageResponse generate = imageModel.img2imggenerate(request);
|
||||
System.out.println(generate);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.volcengine.service.visual.IVisualService;
|
||||
import com.volcengine.service.visual.impl.VisualServiceImpl;
|
||||
|
||||
public class VolcengineTest {
|
||||
//以下是同步调用直接返回结果的方法,按照实际需求可调用sdk同步及异步相关方法;
|
||||
public static void main(String[] args) {
|
||||
IVisualService visualService = VisualServiceImpl.getInstance();
|
||||
// call below method if you dont set ak and sk in ~/.vcloud/config
|
||||
visualService.setAccessKey("AKLTNmU0M2RkNWZkMmZmNDQwYWI2NTZiMjA1ODYxY2M3MjE");
|
||||
visualService.setSecretKey("TkdObFpXSTFZMlJtWldReE5EVTRPRGt4TkRsaE1EVTRaalpsTnpnMllURQ==");
|
||||
|
||||
|
||||
JSONObject req=new JSONObject();
|
||||
//请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)
|
||||
req.put("req_key","high_aes_general_v21_L");
|
||||
req.put("prompt","千军万马");
|
||||
req.put("model_version","general_v2.1_L");
|
||||
req.put("req_schedule_conf","general_v20_9B_pe");
|
||||
req.put("llm_seed",-1);
|
||||
req.put("seed",-1);
|
||||
req.put("scale",3.5);
|
||||
req.put("ddim_steps",25);
|
||||
req.put("width",512);
|
||||
req.put("height",512);
|
||||
req.put("use_pre_llm",true);
|
||||
req.put("use_sr",true);
|
||||
req.put("sr_seed",-1);
|
||||
req.put("sr_strength",0.4);
|
||||
req.put("sr_scale",3.5);
|
||||
req.put("sr_steps",20);
|
||||
req.put("is_only_sr",false);
|
||||
req.put("return_url",true);
|
||||
//创建子级JSONObject
|
||||
JSONObject subData = new JSONObject();
|
||||
subData.put("add_logo", true);
|
||||
subData.put("position", 2);
|
||||
subData.put("language", 0);
|
||||
subData.put("opacity", 0.3);
|
||||
subData.put("logo_text_content", "wangyangyang");
|
||||
req.put("logo_info",subData);
|
||||
|
||||
|
||||
try {
|
||||
Object response = visualService.cvProcess(req);
|
||||
System.out.println(JSON.toJSONString(response));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user