快速开始

通义万相

说明

支持的领域 / 任务:aigc

通义万相是基于自研的Composer组合生成框架的AI绘画创作大模型,提供了一系列的图像生成能力。支持根据用户输入的文字内容,生成符合语义描述的不同风格的图像,或者根据用户输入的图像,生成不同用途的图像结果。通过知识重组与可变维度扩散模型,加速收敛并提升最终生成图片的效果。图像结果贴合语义,构图自然、细节丰富。支持中英文双语输入。

通义万相大模型系列目前支持了文字生成图像、人像风格重绘、图像背景生成等多个模型。

快速开始

前提条件

示例代码

说明

以下以Dashscope SDK调用通义万相-文本生成图像模型为例,其他更多模型的调用与示例代码,请查看其对应的API详情文档。

设置API-KEY

 export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import dashscope


def simple_call():
    prompt = 'Mouse rides elephant'
    rsp = dashscope.ImageSynthesis.call(model=dashscope.ImageSynthesis.Models.wanx_v1,
                              prompt=prompt,
                              n=4,
                              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 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;

public class Main {
  public static void basicCall() throws ApiException, NoApiKeyException {
    ImageSynthesis is = new ImageSynthesis();
    ImageSynthesisParam param =
        ImageSynthesisParam.builder()
            .model(ImageSynthesis.Models.WANX_V1)
            .n(4)
            .size("1024*1024")
            .prompt("雄鹰自由自在的在蓝天白云下飞翔")
            .build();

    ImageSynthesisResult result = is.call(param);
    System.out.println(result);
  }

  public static void main(String[] args){
    try{
      basicCall();
    }catch(ApiException|NoApiKeyException e){
      System.out.println(e.getMessage());
    }
    System.exit(0);
  }
}

调用成功后,将会返回如下示例结果。

{
  "task_id": "dcc42498-3bc3-4931-9199-41394fe58e1c",
  "task_status": "SUCCEEDED",
  "results": [
    {
      "url": "RESULT_URL1"
    },
    {
      "url": "RESULT_URL2"
    },
    {
      "url": "RESULT_URL3"
    },
    {
      "url": "RESULT_URL4"
    }
  ],
  "submit_time": "2024-07-19 14:25:33.529",
  "scheduled_time": "2024-07-19 14:25:33.576",
  "end_time": "2024-07-19 14:26:02.911",
  "task_metrics": {
    "TOTAL": 4,
    "SUCCEEDED": 4,
    "FAILED": 0
  }
}

了解更多

有关通义万相系列模型API的详细调用文档可前往文本生成图像API详情人像风格重绘API详情图像背景生成API详情页面进行了解。