File size: 1,821 Bytes
1d17a15
8370647
 
1d17a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8370647
1d17a15
8370647
 
 
1d17a15
 
 
 
 
 
 
8370647
 
1d17a15
 
 
 
8370647
1d17a15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
import gradio as gr

# 修改 greet 函数以处理图像生成
def generate_image(api_key, prompt, aspect_ratio, model, negative_prompt, seed):
    response = requests.post(
        "https://api.stability.ai/v2beta/stable-image/generate/sd3",
        headers={
            "authorization": f"Bearer {api_key}",
            "accept": "image/*"
        },
        files={"none": ''},
        data={
            "prompt": prompt,
            "aspect_ratio": aspect_ratio,
            "model": model,
            "negative_prompt": negative_prompt,
            "seed": seed,
            "output_format": "jpeg",
        },
    )
    
    if response.status_code == 200:
        return response.content
    else:
        raise Exception(str(response.json()))

# 构建 Gradio 界面
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            api_key = gr.Textbox(label="API Key")
            prompt = gr.Textbox(label="Prompt", placeholder="What you wish to see in the output image.")
            aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "21:9", "2:3", "3:2", "4:5", "5:4", "9:16", "9:21"], value="1:1")
            model = gr.Dropdown(label="Model", choices=["sd3", "sd3-turbo"], value="sd3")
            negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Describe what you do not wish to see.")
            seed = gr.Number(label="Seed", value=0, minimum=0, maximum=4294967294, step=1)
            generate_btn = gr.Button("Generate Image")
        with gr.Column(scale=1):
            output = gr.Image()
            
    generate_btn.click(fn=generate_image, 
                        inputs=[api_key, prompt, aspect_ratio, model, negative_prompt, seed], 
                        outputs=output)

demo.launch(server_port=7588)