文档

快速开始

更新时间:
一键部署

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配置到环境变量中以降低API-KEY的泄漏风险,详情可参考通过环境变量配置API-KEY。您也可以在代码中配置API-KEY,但是泄漏风险会增加。

  • 请登录到百炼控制台,在模型广场处找到StableDiffusion文生图模型并申请,申请通过后即可进行模型使用。

示例代码

以下示例展示了调用 stable-diffusion-xl 模型API进行文生图的示例代码,如果需要调用 Stable Diffusion v1.5版本只需要用 stable-diffusion-v1.5 替换 stable-diffusion-xl 即可。

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)
        # 下载文件到当前文件夹
        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.
// 请添加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));
        // 保存图片到本地
        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 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": "08327623-c671-92ff-bdd7-1a2a87f40589",
    "code": null,
    "message": "",
    "output": {
        "task_id": "80e3e478-b276-4b68-a328-628982abd40d",
        "task_status": "SUCCEEDED",
        "results": [
            {
                "url": "example_url"
            }
        ],
        "submit_time": "2024-06-14 11:46:18.604",
        "scheduled_time": "2024-06-14 11:46:18.621",
        "end_time": "2024-06-14 11:46:36.043",
        "task_metrics": {
            "TOTAL": 1,
            "SUCCEEDED": 1,
            "FAILED": 0
        }
    },
    "usage": {
        "image_count": 1
    }
}

您可以从url中获取结果图片信息。

了解更多

有关StableDiffusion文生图模型API的详细调用文档可前往API详情页面进行了解。