Spaces:
Sleeping
Sleeping
Stephen Ebert
commited on
Commit
·
7dbc444
1
Parent(s):
b9f3039
Replace placeholder demo with working SD v1.5 Gradio app
Browse files
app.py
CHANGED
|
@@ -1,62 +1,30 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from diffusers import StableDiffusionPipeline
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
device = (
|
| 7 |
-
"mps" if torch.backends.mps.is_available()
|
| 8 |
-
else "cuda" if torch.cuda.is_available()
|
| 9 |
-
else "cpu"
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
# Load the model (you can remove safety_checker=None for public deploys)
|
| 13 |
-
model_id = "runwayml/stable-diffusion-v1-5"
|
| 14 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 15 |
-
|
| 16 |
-
torch_dtype=torch.float16,
|
| 17 |
safety_checker=None
|
| 18 |
).to(device)
|
| 19 |
|
| 20 |
-
def generate(prompt
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
torch.Generator(device=device).manual_seed(int(seed))
|
| 27 |
-
if seed and seed > 0
|
| 28 |
-
else None
|
| 29 |
-
)
|
| 30 |
-
output = pipe(
|
| 31 |
-
prompt,
|
| 32 |
-
num_inference_steps=steps,
|
| 33 |
-
guidance_scale=guidance,
|
| 34 |
-
generator=generator
|
| 35 |
-
)
|
| 36 |
-
# returns a list of PIL images
|
| 37 |
-
return output.images
|
| 38 |
-
|
| 39 |
-
# Build the Gradio UI
|
| 40 |
-
demo = gr.Blocks()
|
| 41 |
|
| 42 |
-
with demo:
|
| 43 |
gr.Markdown("# Stable Diffusion Text→Image Generation Demo")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
btn = gr.Button("Generate")
|
| 51 |
-
with gr.Column():
|
| 52 |
-
gallery = gr.Gallery(label="Generated Images", columns=2, height="auto")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
btn.click(
|
| 56 |
-
fn=generate,
|
| 57 |
-
inputs=[prompt, steps, guidance, seed],
|
| 58 |
-
outputs=gallery,
|
| 59 |
-
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch, os
|
| 4 |
|
| 5 |
+
device = "mps" if torch.backends.mps.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 7 |
+
"runwayml/stable-diffusion-v1-5",
|
|
|
|
| 8 |
safety_checker=None
|
| 9 |
).to(device)
|
| 10 |
|
| 11 |
+
def generate(prompt, steps, guidance, seed):
|
| 12 |
+
generator = torch.Generator(device).manual_seed(int(seed)) if seed else None
|
| 13 |
+
img = pipe(prompt, num_inference_steps=int(steps),
|
| 14 |
+
guidance_scale=float(guidance),
|
| 15 |
+
generator=generator).images[0]
|
| 16 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
with gr.Blocks() as demo:
|
| 19 |
gr.Markdown("# Stable Diffusion Text→Image Generation Demo")
|
| 20 |
+
prompt = gr.Textbox(label="Prompt", value="A serene forest at dawn")
|
| 21 |
+
steps = gr.Slider(1, 100, value=30, label="Inference Steps")
|
| 22 |
+
cfg = gr.Slider(1, 15, value=7.5, label="Guidance Scale")
|
| 23 |
+
seed = gr.Number(value=0, label="Random Seed (0 = random)")
|
| 24 |
+
btn = gr.Button("Generate")
|
| 25 |
+
gallery = gr.Gallery(label="Generated Images", columns=1, height="auto")
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
btn.click(generate, [prompt, steps, cfg, seed], gallery)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
| 30 |
demo.launch()
|