StableDiffusion文生图模型API目前处于"申请体验"阶段,请点击此处申请。
在您的账号未申请体验或申请未通过时,调用StableDiffusion文生图模型API将返回错误状态码。
StableDiffusion文生图模型
支持的领域 / 任务:aigc
StableDiffusion文生图模型目前针对开源社区的stable-diffusion-v1.5版本和stable-diffusion-xl版本进行了服务化支持。
其中,stable-diffusion-v1.5模型通过clip模型能够将文本的embedding和图片embedding映射到相同空间,从而通过输入文本并结合unet的稳定扩散预测噪声的能力,生成图片。是一款基础的文生图模型,得到了业界广泛使用。而stable-diffusion-xl相比于v1.5做了重大的改进,被认为是当前开源文生图模型的SOTA水准,具体改进之处包括: 更大的unet backbone,是之前的3倍; 增加了refinement模块用于改善生成图片的质量;更高效的训练技巧等。
快速开始
前提条件
已开通服务并获得api-key:开通DashScope并创建API-KEY。
已安装SDK:安装DashScope SDK。
示例代码
以下示例展示了调用 stable-diffusion-xl 模型API进行文生图的示例代码,如果需要调用 Stable Diffusion v1.5版本只需要用 stable-diffusion-v1.5 替换 stable-diffusion-xl 即可。
需要使用您的api-key替换示例中的 your-dashscope-api-key ,代码才能正常运行。
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import dashscope
model = "stable-diffusion-xl"
prompt = "Eagle flying freely in the blue sky and white clouds"
def simple_call():
rsp = dashscope.ImageSynthesis.call(model=model,
prompt=prompt,
negative_prompt="garfield",
n=1,
size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
print(rsp.output)
print(rsp.usage)
# save file to current directory
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))
if __name__ == '__main__':
simple_call()
// Copyright (c) Alibaba, Inc. and its affiliates.
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 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(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);
}
}
调用成功后,将会返回如下示例结果。
{
"status_code": 200,
"request_id": "ea8bfe77-2f35-9df3-ba47-7e05e917b3df",
"code": null,
"message": "",
"output": {
"task_id": "dea97660-9651-4e6b-a9c3-8afb325b28d0",
"task_status": "SUCCEEDED",
"results": [
{
"url": "url1"
}
],
"task_metrics": {
"TOTAL": 1,
"SUCCEEDED": 1,
"FAILED": 0
}
},
"usage": {
"image_count": 1
}
}
了解更多
有关StableDiffusion文生图模型API的详细调用文档可前往API详情页面进行了解。