Spaces:
Sleeping
Sleeping
Tanut
commited on
Commit
·
d25b42d
1
Parent(s):
184daa2
QRCode generate
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionPipeline
|
|
|
|
|
|
|
4 |
|
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 |
|
@@ -11,22 +14,48 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
11 |
torch_dtype=dtype
|
12 |
).to(device)
|
13 |
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
if __name__ == "__main__":
|
32 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import StableDiffusionPipeline
|
4 |
+
from PIL import Image
|
5 |
+
import qrcode
|
6 |
+
from qrcode.constants import ERROR_CORRECT_H
|
7 |
|
8 |
+
# -------- Stable Diffusion setup --------
|
9 |
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
10 |
dtype = torch.float16 if device != "cpu" else torch.float32
|
11 |
|
|
|
14 |
torch_dtype=dtype
|
15 |
).to(device)
|
16 |
|
17 |
+
def sd_generate(prompt, steps, guidance, seed):
|
18 |
+
gen = torch.Generator(device=device).manual_seed(int(seed)) if int(seed) != 0 else None
|
19 |
+
def run():
|
20 |
+
return pipe(prompt, num_inference_steps=int(steps), guidance_scale=float(guidance), generator=gen).images[0]
|
21 |
+
# autocast only where supported
|
22 |
+
if device in ("cuda", "mps"):
|
23 |
+
with torch.autocast(device):
|
24 |
+
return run()
|
25 |
+
else:
|
26 |
+
return run()
|
27 |
+
|
28 |
+
# -------- QR code helper --------
|
29 |
+
def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int = 2) -> Image.Image:
|
30 |
+
qr = qrcode.QRCode(
|
31 |
+
version=None,
|
32 |
+
error_correction=ERROR_CORRECT_H, # high EC to survive stylization later
|
33 |
+
box_size=10,
|
34 |
+
border=border
|
35 |
+
)
|
36 |
+
qr.add_data(url.strip())
|
37 |
+
qr.make(fit=True)
|
38 |
+
img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
|
39 |
+
return img.resize((size, size), resample=Image.NEAREST)
|
40 |
+
|
41 |
+
# -------- UI --------
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
gr.Markdown("## Stable Diffusion + QR (step by step)")
|
44 |
+
|
45 |
+
with gr.Tab("Stable Diffusion (prompt → image)"):
|
46 |
+
prompt = gr.Textbox(label="Prompt", value="A fantasy castle at sunset")
|
47 |
+
steps = gr.Slider(10, 50, value=30, label="Steps", step=1)
|
48 |
+
cfg = gr.Slider(1, 12, value=7.5, label="Guidance Scale", step=0.1)
|
49 |
+
seed = gr.Number(value=0, label="Seed (0 = random)", precision=0)
|
50 |
+
out_sd = gr.Image(label="Generated Image")
|
51 |
+
gr.Button("Generate").click(sd_generate, [prompt, steps, cfg, seed], out_sd)
|
52 |
+
|
53 |
+
with gr.Tab("QR Maker (mybirdfire)"):
|
54 |
+
url = gr.Textbox(label="URL/Text", value="http://www.mybirdfire.com")
|
55 |
+
size = gr.Slider(256, 1024, value=512, step=64, label="Size (px)")
|
56 |
+
quiet = gr.Slider(0, 8, value=2, step=1, label="Border (quiet zone)")
|
57 |
+
out_qr = gr.Image(label="QR Code", type="pil")
|
58 |
+
gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
demo.launch()
|