File size: 1,090 Bytes
9175f45
669fd03
 
9175f45
669fd03
 
 
 
 
 
 
 
3aaaf76
669fd03
 
0a24053
669fd03
 
 
 
 
 
0a24053
669fd03
0a24053
 
669fd03
 
 
 
 
0a24053
669fd03
0a24053
 
 
669fd03
 
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
import gradio as gr
from transformers import pipeline
import spaces

# Load the pipeline (lazy-load the model to save resources)
@spaces.GPU
def load_model():
    return pipeline(
        "text-to-image",
        model="stabilityai/stable-diffusion-2-1",
        torch_dtype="float16"  # Ensure compatibility with ZeroGPU
    )

# Initialize the pipeline
model = load_model()

# Function to generate images
@spaces.GPU
def generate_image(prompt, guidance_scale=7.5):
    print(f"Generating image for prompt: {prompt}")
    images = model(prompt, guidance_scale=guidance_scale)
    return images[0]

# Gradio interface
interface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Describe your image..."),
        gr.Slider(1, 20, value=7.5, label="Guidance Scale")
    ],
    outputs=gr.Image(label="Generated Image"),
    title="Text-to-Image Generator",
    description="Generate images from text prompts using Stable Diffusion."
)

# Launch the app
if __name__ == "__main__":
    interface.launch(server_name="0.0.0.0", server_port=7860)