要通过API调用Stable Diffusion文生图模型,遵循以下步骤:
1.准备条件:
2.调用API:
stable-diffusion-xl
或stable-diffusion-v1.5
版本。 https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis
,设置Header中的Content-Type
为application/json
,并在Authorization
中使用Bearer {your-api-key}
格式携带API-Key。 prompt
: 文本描述,指示生成图片的主题。 negative_prompt
: 负向提示,指明不想在图片中出现的内容。 size
: 图片尺寸。 n
: 期望生成的图片数量。 3.处理响应:
注意:
此过程概括了使用API调用Stable Diffusion文生图模型的基本步骤,确保按官方文档指引操作。
参考链接:https://help.aliyun.com/zh/dashscope/developer-reference/stable-diffusion-apis----
demo如下:
python如下
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os
model = "stable-diffusion-xl"
prompt = "Eagle flying freely in the blue sky and white clouds"
# 同步调用
def sample_block_call():
rsp = ImageSynthesis.call(model=model,
api_key=os.getenv("DASHSCOPE_API_KEY"),
prompt=prompt,
negative_prompt="garfield",
n=1,
size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
print(rsp)
# 保存图片到当前文件夹
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
# 异步调用
def sample_async_call():
rsp = ImageSynthesis.async_call(model=model,
api_key=os.getenv("DASHSCOPE_API_KEY"),
prompt=prompt,
negative_prompt="garfield",
n=1,
size='512*512')
if rsp.status_code == HTTPStatus.OK:
print(rsp)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
status = ImageSynthesis.fetch(rsp)
if status.status_code == HTTPStatus.OK:
print(status.output.task_status)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(status.status_code, status.code, status.message))
rsp = ImageSynthesis.wait(rsp)
if rsp.status_code == HTTPStatus.OK:
print(rsp)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
if __name__ == '__main__':
sample_block_call()
sample_async_call()
// Copyright (c) Alibaba, Inc. and its affiliates.
// 需要安装okhttp依赖
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Main {
private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String MODEL = "stable-diffusion-xl";
private static final String PROMPT = "Eagle flying freely in the blue sky and white clouds";
private static final String SIZE = "1024*1024";
public static void basicCall() throws ApiException, NoApiKeyException, IOException {
ImageSynthesis is = new ImageSynthesis();
ImageSynthesisParam param =
ImageSynthesisParam.builder()
.model(Main.MODEL)
.n(1)
.size(Main.SIZE)
.prompt(Main.PROMPT)
.negativePrompt("garfield")
.build();
ImageSynthesisResult result = is.call(param);
System.out.println(JsonUtils.toJson(result));
// save image to local files.
for(Map<String, String> item :result.getOutput().getResults()){
String paths = new URL(item.get("url")).getPath();
String[] parts = paths.split("/");
String fileName = parts[parts.length-1];
Request request = new Request.Builder()
.url(item.get("url"))
.build();
try (Response response = CLIENT.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
Path file = Paths.get(fileName);
Files.write(file, response.body().bytes());
}
}
}
public void fetchTask() throws ApiException, NoApiKeyException {
String taskId = "your task id";
ImageSynthesis is = new ImageSynthesis();
// If set DASHSCOPE_API_KEY environment variable, apiKey can null.
ImageSynthesisResult result = is.fetch(taskId, null);
System.out.println(result.getOutput());
System.out.println(result.getUsage());
}
public static void main(String[] args){
try{
basicCall();
}catch(ApiException|NoApiKeyException | IOException e){
System.out.println(e.getMessage());
}
System.exit(0);
}
}
参考链接
https://help.aliyun.com/zh/model-studio/developer-reference/stable-diffusion-apis
可以参考 StableDiffusion文生图模型
https://help.aliyun.com/zh/model-studio/developer-reference/stable-diffusion-apis
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。