Updated for ZeroGPU
Browse files
app.py
CHANGED
@@ -1,28 +1,38 @@
|
|
1 |
-
from diffusers import StableDiffusionPipeline
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
4 |
|
5 |
-
# Load the
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
interface = gr.Interface(
|
19 |
fn=generate_image,
|
20 |
-
inputs=
|
21 |
-
|
|
|
|
|
|
|
22 |
title="Text-to-Image Generator",
|
23 |
-
description="
|
24 |
)
|
25 |
|
26 |
# Launch the app
|
27 |
-
|
28 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import spaces
|
4 |
|
5 |
+
# Load the pipeline (lazy-load the model to save resources)
|
6 |
+
@spaces.GPU
|
7 |
+
def load_model():
|
8 |
+
return pipeline(
|
9 |
+
"text-to-image",
|
10 |
+
model="stabilityai/stable-diffusion-2-1",
|
11 |
+
torch_dtype="float16" # Ensure compatibility with ZeroGPU
|
12 |
+
)
|
13 |
|
14 |
+
# Initialize the pipeline
|
15 |
+
model = load_model()
|
16 |
|
17 |
+
# Function to generate images
|
18 |
+
@spaces.GPU
|
19 |
+
def generate_image(prompt, guidance_scale=7.5):
|
20 |
+
print(f"Generating image for prompt: {prompt}")
|
21 |
+
images = model(prompt, guidance_scale=guidance_scale)
|
22 |
+
return images[0]
|
23 |
|
24 |
+
# Gradio interface
|
25 |
interface = gr.Interface(
|
26 |
fn=generate_image,
|
27 |
+
inputs=[
|
28 |
+
gr.Textbox(label="Prompt", placeholder="Describe your image..."),
|
29 |
+
gr.Slider(1, 20, value=7.5, label="Guidance Scale")
|
30 |
+
],
|
31 |
+
outputs=gr.Image(label="Generated Image"),
|
32 |
title="Text-to-Image Generator",
|
33 |
+
description="Generate images from text prompts using Stable Diffusion."
|
34 |
)
|
35 |
|
36 |
# Launch the app
|
37 |
+
if __name__ == "__main__":
|
38 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|