File size: 2,017 Bytes
16b902a
 
 
 
 
 
8646235
16b902a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import gradio as gr

def generate_image(prompt, aspect_ratio='1:1', model='sd3', seed=0, output_format='png'):
    api_url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
    api_key = os.environ.get("STABILITY_AI_API_KEY")  # Retrieve the API key from the environment variable

    m = MultipartEncoder(
        fields={
            'prompt': prompt,
            'aspect_ratio': aspect_ratio,
            'model': model,
            'seed': str(seed),
            'output_format': output_format,
            'mode': 'text-to-image'  # Default mode
        }
    )

    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': m.content_type,
        'Accept': 'image/*'  # To receive the image directly
    }

    response = requests.post(api_url, data=m, headers=headers)
    print(response.status_code, response.content)
    
    if response.status_code == 200:
        # Assuming the response content is the image in binary format
        output_path = 'generated_image.png'
        with open(output_path, 'wb') as f:
            f.write(response.content)
        return output_path  # Return the path for Gradio to display the image
    else:
        return f"Error: {response.text}"

def wrap_generate_image(prompt, model, aspect_ratio):
    return generate_image(prompt, aspect_ratio, model)

iface = gr.Interface(
    fn=wrap_generate_image,
    inputs=[
        gr.Textbox(lines=2, label="Prompt", placeholder="Enter a description for the image..."),
        gr.Radio(choices=['sd3', 'sd3-turbo'], label="Model", value='sd3'),
        gr.Dropdown(choices=['1:1', '16:9', '21:9', '2:3', '3:2', '4:5', '5:4', '9:16', '9:21'], label="Aspect Ratio", value='1:1')
    ],
    outputs=gr.Image(),
    title="Stable Diffusion 3 Text-to-Image Generator",
    description="Select a model, aspect ratio, and enter a prompt to generate an image using Stable Diffusion 3."
)

iface.launch(share=True)