使用 python调用FLUX文生图模型:阿里DashScope API接入指南
本文最后更新于 231 天前,其中的信息可能已经过时.

一、获取 API-KEY

最近看到阿里 DashScope 模型服务中有 flux-schnell 和 flux-dev 两个文生图模型,FLUX 文生图模型在阿里 DashScope 模型服务灵积首次申请 api 后可以免费生成 1000 张图片。

image-20240822091838281

第一步首先要访问 DashScope 管理控制台:模型服务灵积 - 总览 (aliyun.com)

p716185

然后开通 DashScope

img

然后访问 DashScope 管理控制台 API-KEY管理页面:单击创建新的API-KEY

img

二、python 调用 api

  1. 安装 DashScope SDK

    pip install dashscope
  2. python 代码

    from http import HTTPStatus
    from urllib.parse import urlparse, unquote
    from pathlib import PurePosixPath
    import requests
    from dashscope import ImageSynthesis
    import dashscope
    dashscope.api_key = 'sk-0000000000000' # 替换为你的API密钥
    model='flux-dev'
    # model = "flux-schnell"
    prompt = "A beautiful Chinese girl"
    prompt_cn = "一个漂亮的中国女孩"
    def sample_block_call(input_prompt):
    rsp = ImageSynthesis.call(model=model,
    prompt=input_prompt,
    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))
    def sample_async_call(input_prompt):
    rsp = ImageSynthesis.async_call(model=model,
    prompt=input_prompt,
    size='1024*1024')
    if rsp.status_code == HTTPStatus.OK:
    print(rsp.output)
    print(rsp.usage)
    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.output)
    else:
    print('Failed, status_code: %s, code: %s, message: %s' %
    (rsp.status_code, rsp.code, rsp.message))
    if __name__ == '__main__':
    sample_block_call(prompt)
    sample_async_call(prompt_cn)
  3. 参数说明

参数 类型 默认值 说明
model string - 指定用于对话的 FLUX 文生图模型名,目前支持传入 "flux-schnell" 和 "flux-dev"
prompt string - 用户当前输入的期望模型生成的文本信息。用户当前输入的期望模型生成的文本信息。支持中英文,中文不超过 500 个字符,英文不超过 500 个单词,超过部分会自动截断。
size(可选) string 1024*1024 生成图像的分辨率,目前支持 "512 1024, 768 512, 768 1024, 1024 576, 576 1024, 1024 1024" 六种分辨率,默认为 1024 * 1024 像素。
seed(可选) int - 图片生成时候的种子值,如果不提供,则算法自动用一个随机生成的数字作为种子。
steps(可选) int - 图片生成的推理步数,如果不提供,则默认为 30。 flux-schnell 模型官方默认 steps 为 4,flux-dev 模型官方默认 steps 为 50。
  1. 模型效果

    下面是上面代码运行后生成的图片,感觉还是可以的。

    206885e2-6a69-49e6-a22a-2e53aeaf075e-1

bc9b5a76-e468-4f46-ab96-594f6aeb0a66-1

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇