Skip to content
jianguo.wjg_通用海报专项_results_cache_dashscope_2025-04-14_2025-04-14-15-26-29_9f6e37a6-f757-4e32-9759-0bfd8c70caf5_0.render
python
def create_task():
    Authorization = "Bearer "+os.getenv("DASHSCOPE_API_KEY")
    headers = {
        "content-type":"application/json",
        "X-DashScope-Async":"enable",
        "Authorization":Authorization,
        }
    data = {
        "model":"wanx-poster-generation-v1",
        "input": {
            "title":"春节快乐",
            "sub_title":"家庭团聚,共享天伦之乐",
            "body_text":"春节是中国最重要的传统节日之一,它象征着新的开始和希望",
            "prompt_text_zh":"灯笼,小猫,梅花",
            "wh_ratios":"竖版",
            "lora_name":"童话油画",
            "lora_weight":0.8,
            "ctrl_ratio":0.7,
            "ctrl_step":0.7,
            "generate_mode":"generate",
            "generate_num":1
        },
        "parameters":{}

    }
    res = requests.post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis',json.dumps(data),headers=headers)
    data = res.json()
    return data

4c11cb0b475c3dfff57b8af0173a1e2e4203703a41c8edfbd55e39d1

python
def create_task():
    Authorization = "Bearer "+os.getenv("DASHSCOPE_API_KEY")
    headers = {
        "content-type":"application/json",
        "X-DashScope-Async":"enable",
        "Authorization":Authorization,
        }
    data = {
        "model":"wanx-poster-generation-v1",
        "input": {
            "title":"五一劳动节快乐",
            "sub_title":"致敬劳动者,共享奋斗成果",
            "body_text":"五一国际劳动节是全球劳动者共同的节日,彰显劳动创造价值、奋斗成就梦想的时代精神",
            "prompt_text_zh":"工人雕塑,齿轮,麦穗,奖章,蓝天白云",
            "wh_ratios":"竖版",
            "lora_name":"童话油画",
            "lora_weight":0.75,
            "ctrl_ratio":0.65,
            "ctrl_step":0.7,
            "generate_mode":"generate",
            "generate_num":1
        },
        "parameters":{}

    }
    res = requests.post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis',json.dumps(data),headers=headers)
    data = res.json()
    return data

完整代码

python
import streamlit as st
import requests
import json
import os

# https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=https%3A%2F%2Fhelp.aliyun.com%2Fdocument_detail%2F2807172.html

# cb74d61e-038d-42c0-aea8-7beb3f13d81f
def create_task():
    Authorization = "Bearer "+os.getenv("DASHSCOPE_API_KEY")
    headers = {
        "content-type":"application/json",
        "X-DashScope-Async":"enable",
        "Authorization":Authorization,
        }
    data = {
        "model":"wanx-poster-generation-v1",
        "input": {
            "title":"五一劳动节快乐",
            "sub_title":"致敬劳动者,共享奋斗成果",
            "body_text":"五一国际劳动节是全球劳动者共同的节日,彰显劳动创造价值、奋斗成就梦想的时代精神",
            "prompt_text_zh":"工人雕塑,齿轮,麦穗,奖章,蓝天白云",
            "wh_ratios":"竖版",
            "lora_name":"童话油画",
            "lora_weight":0.75,
            "ctrl_ratio":0.65,
            "ctrl_step":0.7,
            "generate_mode":"generate",
            "generate_num":1
        },
        "parameters":{}

    }
    res = requests.post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis',json.dumps(data),headers=headers)
    data = res.json()
    return data

def query_result(task_id):
    Authorization = "Bearer "+os.getenv("DASHSCOPE_API_KEY")
    headers = {
            "Authorization":Authorization,
            }
    res = requests.get(f'https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}',headers=headers)
    return res.json()

if st.button("生成"):
    res = create_task()
    st.write(res)

task_id = st.text_input("请输入任务id")
if st.button("查询结果"):
    with st.spinner("查询中..."):
        res = query_result(task_id)
        st.write(res)