Spaces:
Running
on
Zero
Running
on
Zero
Tanut
commited on
Commit
·
184daa2
1
Parent(s):
51d9f34
Try stable diffusion
Browse files- app.py +28 -51
- requirements.txt +2 -4
app.py
CHANGED
@@ -1,55 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from diffusers import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
)
|
24 |
-
pipe.to(device)
|
25 |
-
|
26 |
-
def generate(prompt, control_image, guidance_scale=7.5, steps=30, seed=0):
|
27 |
-
print("API called:", type(control_image))
|
28 |
-
generator = torch.Generator(device=device).manual_seed(int(seed)) if seed else None
|
29 |
-
img = pipe(
|
30 |
-
prompt=prompt,
|
31 |
-
image=control_image,
|
32 |
-
num_inference_steps=int(steps),
|
33 |
-
guidance_scale=float(guidance_scale),
|
34 |
-
generator=generator
|
35 |
-
).images[0]
|
36 |
-
return img
|
37 |
-
|
38 |
-
with gr.Blocks() as demo:
|
39 |
-
gr.Markdown("# ControlNet Image Generator")
|
40 |
-
with gr.Row():
|
41 |
-
prompt = gr.Textbox(label="Prompt", value="A futuristic poster, high detail")
|
42 |
-
seed = gr.Number(label="Seed (0=random)", value=0)
|
43 |
-
with gr.Row():
|
44 |
-
control = gr.Image(type="pil", label="Control image (e.g., QR or edge map)")
|
45 |
-
steps = gr.Slider(10, 50, 30, step=1, label="Steps")
|
46 |
-
guidance = gr.Slider(1.0, 12.0, 7.5, step=0.1, label="Guidance scale")
|
47 |
-
out = gr.Image(label="Result")
|
48 |
-
|
49 |
-
btn = gr.Button("Generate")
|
50 |
-
btn.click(generate, [prompt, control, guidance, steps, seed], out)
|
51 |
-
|
52 |
-
# Enable simple API use
|
53 |
-
gr.Examples([], inputs=[prompt, control, guidance, steps, seed], outputs=out)
|
54 |
|
55 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
|
5 |
+
# Load Stable Diffusion (v1.5)
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
7 |
+
dtype = torch.float16 if device != "cpu" else torch.float32
|
8 |
+
|
9 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
10 |
+
"runwayml/stable-diffusion-v1-5",
|
11 |
+
torch_dtype=dtype
|
12 |
+
).to(device)
|
13 |
+
|
14 |
+
def generate(prompt, steps, guidance, seed):
|
15 |
+
generator = torch.Generator(device=device).manual_seed(int(seed)) if seed != 0 else None
|
16 |
+
with torch.autocast(device):
|
17 |
+
image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance, generator=generator).images[0]
|
18 |
+
return image
|
19 |
+
|
20 |
+
demo = gr.Interface(
|
21 |
+
fn=generate,
|
22 |
+
inputs=[
|
23 |
+
gr.Textbox(label="Prompt", value="A fantasy castle at sunset"),
|
24 |
+
gr.Slider(10, 50, value=30, label="Steps"),
|
25 |
+
gr.Slider(1, 12, value=7.5, label="Guidance Scale"),
|
26 |
+
gr.Number(value=0, label="Seed (0 = random)")
|
27 |
+
],
|
28 |
+
outputs=gr.Image()
|
29 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
if __name__ == "__main__":
|
32 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
torch
|
2 |
-
diffusers
|
3 |
transformers
|
4 |
accelerate
|
5 |
safetensors
|
6 |
-
|
7 |
-
gradio>=4.0.0
|
8 |
-
Pillow
|
|
|
1 |
torch
|
2 |
+
diffusers
|
3 |
transformers
|
4 |
accelerate
|
5 |
safetensors
|
6 |
+
gradio
|
|
|
|