sbicy commited on
Commit
669fd03
·
verified ·
1 Parent(s): afbcd32

Updated for ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -1,28 +1,38 @@
1
- from diffusers import StableDiffusionPipeline
2
  import gradio as gr
3
- import torch
 
4
 
5
- # Load the Stable Diffusion model
6
- pipe = StableDiffusionPipeline.from_pretrained(
7
- "runwayml/stable-diffusion-v1-5"
8
- )
 
 
 
 
9
 
10
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
 
11
 
12
- # Define the image generation function
13
- def generate_image(prompt):
14
- image = pipe(prompt).images[0]
15
- return image
 
 
16
 
17
- # Build the Gradio interface
18
  interface = gr.Interface(
19
  fn=generate_image,
20
- inputs=gr.Textbox(lines=2, placeholder="Enter your creative prompt here..."),
21
- outputs="image",
 
 
 
22
  title="Text-to-Image Generator",
23
- description="Enter a creative prompt and see it turned into an image!"
24
  )
25
 
26
  # Launch the app
27
- interface.launch()
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)