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詳情頁面進行了解。