Paulina commited on
Commit
fe01e72
·
1 Parent(s): 2c95b30
Files changed (3) hide show
  1. app.py +106 -0
  2. pipeline.py +0 -0
  3. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ from PIL import Image
5
+ import numpy as np
6
+ import spaces
7
+
8
+ MODEL_ID = "runwayml/stable-diffusion-v1-5"
9
+
10
+ # Global pipeline variable
11
+ pipe = None
12
+
13
+
14
+ def initialize_pipeline():
15
+ """Initialize the pipeline if not already loaded."""
16
+ global pipe
17
+ if pipe is None:
18
+ device = "cuda" if torch.cuda.is_available() else "cpu"
19
+ print(f"Initializing pipeline on device: {device}")
20
+
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
+ MODEL_ID,
23
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
24
+ )
25
+ pipe = pipe.to(device)
26
+ return pipe
27
+
28
+
29
+ @spaces.GPU
30
+ def generate_image(prompt, seed, num_inference_steps):
31
+ """
32
+ Generate an image using Stable Diffusion.
33
+ This function runs on Zero GPU when deployed on Hugging Face Spaces.
34
+
35
+ Args:
36
+ prompt: Text description of the image to generate
37
+ seed: Random seed for reproducibility
38
+ num_inference_steps: Number of denoising steps
39
+
40
+ Returns:
41
+ PIL Image
42
+ """
43
+ # Initialize pipeline
44
+ pipeline = initialize_pipeline()
45
+ device = pipeline.device
46
+
47
+ # Set the random seed for reproducibility
48
+ generator = torch.Generator(device=device).manual_seed(int(seed))
49
+
50
+ # Generate the image
51
+ with torch.no_grad():
52
+ result = pipeline(
53
+ prompt=prompt,
54
+ num_inference_steps=int(num_inference_steps),
55
+ generator=generator,
56
+ )
57
+
58
+ return result.images[0]
59
+
60
+
61
+ def create_interface():
62
+ """Create and configure the Gradio interface."""
63
+ # Create the Gradio interface
64
+ interface = gr.Interface(
65
+ fn=generate_image,
66
+ inputs=[
67
+ gr.Textbox(
68
+ label="Prompt",
69
+ placeholder="Enter a text description of the image you want to generate...",
70
+ lines=3,
71
+ ),
72
+ gr.Slider(
73
+ minimum=0,
74
+ maximum=2147483647,
75
+ value=42,
76
+ step=1,
77
+ label="Seed",
78
+ info="Random seed for reproducibility",
79
+ ),
80
+ gr.Slider(
81
+ minimum=1,
82
+ maximum=150,
83
+ value=50,
84
+ step=1,
85
+ label="Diffusion Steps",
86
+ info="Number of denoising steps (more steps = higher quality but slower)",
87
+ ),
88
+ ],
89
+ outputs=gr.Image(label="Generated Image", type="pil"),
90
+ title="Stable Diffusion Image Generator",
91
+ description="Generate images from text using Stable Diffusion. Enter a prompt, set the seed for reproducibility, and adjust the number of diffusion steps.",
92
+ examples=[
93
+ ["A beautiful sunset over mountains", 42, 50],
94
+ ["A cat wearing a space suit, digital art", 123, 50],
95
+ ["Cyberpunk city at night, neon lights", 456, 75],
96
+ ],
97
+ cache_examples=False,
98
+ )
99
+
100
+ return interface
101
+
102
+
103
+ if __name__ == "__main__":
104
+ # Create and launch the interface
105
+ demo = create_interface()
106
+ demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
pipeline.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ torchvision>=0.15.0
3
+ numpy>=1.24.0
4
+ Pillow>=9.5.0
5
+ tqdm>=4.65.0
6
+ transformers>=4.30.0
7
+ diffusers>=0.27.0
8
+ accelerate>=0.20.0
9
+ safetensors>=0.3.1
10
+ gradio>=4.0.0
11
+ spaces>=0.19.0