Tanut commited on
Commit
2e2f472
·
1 Parent(s): 4a9e124
Files changed (2) hide show
  1. app.py +10 -18
  2. requirements.txt +1 -1
app.py CHANGED
@@ -5,7 +5,6 @@ from PIL import Image
5
  import qrcode
6
  from qrcode.constants import ERROR_CORRECT_H
7
 
8
-
9
  # ========= device/dtype =========
10
  device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
11
  dtype = torch.float16 if device != "cpu" else torch.float32
@@ -44,21 +43,20 @@ def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int
44
  img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
45
  return img.resize((size, size), resample=Image.NEAREST)
46
 
47
- # ========= ControlNet Stylizer (SD1.5 + sd15-canny) =========
48
- # --- SDXL dual ControlNet stylizer (canny + tile) ---
49
  from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
50
  from diffusers.schedulers.scheduling_euler_discrete import EulerDiscreteScheduler
51
  from controlnet_aux import CannyDetector
52
 
53
- SDXL_MODEL = "stabilityai/stable-diffusion-xl-base-1.0" # swap to your SDXL anime checkpoint if you have one
54
  CN_CANNY = "diffusers/controlnet-canny-sdxl-1.0"
55
- CN_TILE = "diffusers/controlnet-tile-sdxl-1.0"
56
 
57
  _sdxl = {"pipe": None}
58
  def _load_sdxl_dual():
59
  if _sdxl["pipe"] is None:
60
  cn1 = ControlNetModel.from_pretrained(CN_CANNY, torch_dtype=dtype)
61
- cn2 = ControlNetModel.from_pretrained(CN_TILE, torch_dtype=dtype)
62
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
63
  SDXL_MODEL, controlnet=[cn1, cn2], torch_dtype=dtype, safety_checker=None
64
  ).to(device)
@@ -80,7 +78,7 @@ def stylize_qr_sdxl(prompt: str, steps: int=28, guidance: float=7.0, seed: int=1
80
 
81
  gen = torch.Generator(device=device).manual_seed(int(seed)) if int(seed)!=0 else None
82
 
83
- # Control weights + schedule (canny, tile)
84
  cn_scales = [1.1, 0.6]
85
  cn_start = [0.25, 0.00]
86
  cn_end = [0.95, 1.00]
@@ -89,18 +87,14 @@ def stylize_qr_sdxl(prompt: str, steps: int=28, guidance: float=7.0, seed: int=1
89
  img = pipe(
90
  prompt=prompt,
91
  negative_prompt=NEG,
92
- image=[edges, qr], # canny first, tile second
93
- controlnet_conditioning_scale=cn_scales, # strengths
94
- control_guidance_start=cn_start, # when they start
95
- control_guidance_end=cn_end, # when they end
96
  num_inference_steps=int(steps),
97
  guidance_scale=float(guidance),
98
  generator=gen
99
  ).images[0]
100
-
101
- # optional: re‑overlay razor‑sharp finder squares to boost scanning
102
- # (uncomment if scans are borderline)
103
- # img = overlay_finders(img, qr)
104
  return img
105
 
106
  if device in ("cuda", "mps"):
@@ -108,7 +102,6 @@ def stylize_qr_sdxl(prompt: str, steps: int=28, guidance: float=7.0, seed: int=1
108
  return run()
109
  return run()
110
 
111
-
112
  # ========= UI =========
113
  with gr.Blocks() as demo:
114
  gr.Markdown("## Stable Diffusion + QR Code + ControlNet")
@@ -129,7 +122,7 @@ with gr.Blocks() as demo:
129
  out_qr = gr.Image(label="QR Code", type="pil")
130
  gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
131
 
132
- with gr.Tab("QR Stylizer (SDXL dual ControlNet)"):
133
  p = gr.Textbox(label="Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet, Beautiful, Fantasy, Art, Anime")
134
  st = gr.Slider(20, 40, 28, step=1, label="Steps")
135
  cfg = gr.Slider(4.5, 9.0, 7.0, step=0.1, label="CFG")
@@ -139,6 +132,5 @@ with gr.Blocks() as demo:
139
  out = gr.Image(label="Stylized QR (SDXL)")
140
  gr.Button("Stylize").click(stylize_qr_sdxl, [p, st, cfg, sd, cl, ch], out)
141
 
142
-
143
  if __name__ == "__main__":
144
  demo.launch()
 
5
  import qrcode
6
  from qrcode.constants import ERROR_CORRECT_H
7
 
 
8
  # ========= device/dtype =========
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
 
43
  img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
44
  return img.resize((size, size), resample=Image.NEAREST)
45
 
46
+ # ========= SDXL dual ControlNet stylizer (canny + softedge) =========
 
47
  from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
48
  from diffusers.schedulers.scheduling_euler_discrete import EulerDiscreteScheduler
49
  from controlnet_aux import CannyDetector
50
 
51
+ SDXL_MODEL = "stabilityai/stable-diffusion-xl-base-1.0" # swap to your SDXL anime model if desired
52
  CN_CANNY = "diffusers/controlnet-canny-sdxl-1.0"
53
+ CN_SOFT = "diffusers/controlnet-softedge-sdxl-1.0" # <-- replaces non-existent tile SDXL
54
 
55
  _sdxl = {"pipe": None}
56
  def _load_sdxl_dual():
57
  if _sdxl["pipe"] is None:
58
  cn1 = ControlNetModel.from_pretrained(CN_CANNY, torch_dtype=dtype)
59
+ cn2 = ControlNetModel.from_pretrained(CN_SOFT, torch_dtype=dtype)
60
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
61
  SDXL_MODEL, controlnet=[cn1, cn2], torch_dtype=dtype, safety_checker=None
62
  ).to(device)
 
78
 
79
  gen = torch.Generator(device=device).manual_seed(int(seed)) if int(seed)!=0 else None
80
 
81
+ # Control weights + schedule (canny, softedge)
82
  cn_scales = [1.1, 0.6]
83
  cn_start = [0.25, 0.00]
84
  cn_end = [0.95, 1.00]
 
87
  img = pipe(
88
  prompt=prompt,
89
  negative_prompt=NEG,
90
+ image=[edges, qr], # canny first, softedge second
91
+ controlnet_conditioning_scale=cn_scales,
92
+ control_guidance_start=cn_start,
93
+ control_guidance_end=cn_end,
94
  num_inference_steps=int(steps),
95
  guidance_scale=float(guidance),
96
  generator=gen
97
  ).images[0]
 
 
 
 
98
  return img
99
 
100
  if device in ("cuda", "mps"):
 
102
  return run()
103
  return run()
104
 
 
105
  # ========= UI =========
106
  with gr.Blocks() as demo:
107
  gr.Markdown("## Stable Diffusion + QR Code + ControlNet")
 
122
  out_qr = gr.Image(label="QR Code", type="pil")
123
  gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
124
 
125
+ with gr.Tab("QR Stylizer (SDXL canny + softedge)"):
126
  p = gr.Textbox(label="Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet, Beautiful, Fantasy, Art, Anime")
127
  st = gr.Slider(20, 40, 28, step=1, label="Steps")
128
  cfg = gr.Slider(4.5, 9.0, 7.0, step=0.1, label="CFG")
 
132
  out = gr.Image(label="Stylized QR (SDXL)")
133
  gr.Button("Stylize").click(stylize_qr_sdxl, [p, st, cfg, sd, cl, ch], out)
134
 
 
135
  if __name__ == "__main__":
136
  demo.launch()
requirements.txt CHANGED
@@ -5,4 +5,4 @@ accelerate
5
  safetensors
6
  gradio
7
  qrcode[pil]
8
- controlnet-aux
 
5
  safetensors
6
  gradio
7
  qrcode[pil]
8
+ controlnet-aux