sbicy commited on
Commit
4f72200
·
verified ·
1 Parent(s): 6f0d206

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)