File size: 1,740 Bytes
7a58110
 
 
31f5d1f
6b3696a
31f5d1f
6b3696a
31f5d1f
6b3696a
7a58110
 
 
 
 
6b3696a
7a58110
 
 
6b3696a
 
 
 
 
 
7a58110
31f5d1f
 
6b3696a
7a58110
 
 
6b3696a
d4ee5ca
6b3696a
 
 
 
7a58110
 
 
6b3696a
7a58110
31f5d1f
6b3696a
 
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
import requests
import io
from PIL import Image
import gradio as gr
import os

# Assuming you've set up API_TOKEN and other constants as before

def generate_image(prompt, negative_prompt=None, guidance_scale=7.5, width=1024, height=768, num_inference_steps=30):
    payload = {
        "inputs": prompt,
        "parameters": {
            "width": width,
            "height": height,
            "guidance_scale": guidance_scale,
            "num_inference_steps": num_inference_steps,
        },
    }
    # Include negative prompt in the payload if provided
    if negative_prompt:
        payload["parameters"]["negative_prompt"] = negative_prompt

    response = requests.post(API_URL, headers=headers, json=payload)
    image_bytes = response.content
    image = Image.open(io.BytesIO(image_bytes))
    return image

# Define Gradio interface components
iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
        gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)..."),
        gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, default=7.5),
        gr.Slider(label="Width", minimum=768, maximum=1024, step=1, default=1024),
        gr.Slider(label="Height", minimum=768, maximum=1024, step=1, default=768),
        gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, default=30)
    ],
    outputs=gr.Image(type="pil"),
    title="Stable Diffusion XL Image Generator",
    description="Generate images with Stable Diffusion XL. Provide a prompt, optionally specify a negative prompt, and adjust other parameters as desired."
)

# Launch the Gradio app
iface.launch()