Tanut commited on
Commit
2073de9
·
1 Parent(s): 56a99b7

fix blend image

Browse files
Files changed (1) hide show
  1. app.py +39 -28
app.py CHANGED
@@ -5,29 +5,36 @@ from PIL import Image
5
  import qrcode
6
  from qrcode.constants import ERROR_CORRECT_H
7
 
8
- # ========= Stable Diffusion (prompt-only) =========
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
 
 
12
  sd_pipe = StableDiffusionPipeline.from_pretrained(
13
  "runwayml/stable-diffusion-v1-5",
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 sd_pipe(prompt, num_inference_steps=int(steps), guidance_scale=float(guidance), generator=gen).images[0]
 
 
 
 
 
 
21
  if device in ("cuda", "mps"):
22
  with torch.autocast(device):
23
  return run()
24
  return run()
25
 
26
  # ========= QR Maker =========
27
- def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int = 2) -> Image.Image:
28
  qr = qrcode.QRCode(
29
  version=None,
30
- error_correction=ERROR_CORRECT_H,
31
  box_size=10,
32
  border=border
33
  )
@@ -36,13 +43,14 @@ def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int
36
  img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
37
  return img.resize((size, size), resample=Image.NEAREST)
38
 
39
- # ========= ControlNet Stylizer (prompt + QR) =========
40
- # lazy-load to speed initial startup
41
- _cn_loaded = {"pipe": None}
42
  def _load_controlnet():
43
- if _cn_loaded["pipe"] is None:
44
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
45
  from controlnet_aux import CannyDetector
 
 
46
  controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=dtype)
47
  pipe = StableDiffusionControlNetPipeline.from_pretrained(
48
  "runwayml/stable-diffusion-v1-5",
@@ -50,16 +58,16 @@ def _load_controlnet():
50
  torch_dtype=dtype,
51
  safety_checker=None
52
  ).to(device)
 
 
53
  pipe.enable_attention_slicing()
54
  pipe.enable_vae_slicing()
55
- _cn_loaded["pipe"] = pipe
56
- _cn_loaded["canny"] = CannyDetector()
57
- return _cn_loaded["pipe"], _cn_loaded["canny"]
58
-
59
- def stylize_qr(prompt: str, steps: int, guidance: float, seed: int, canny_low: int, canny_high: int):
60
- # Always make QR from URL
61
- qr_image = make_qr("http://www.mybirdfire.com", size=512, border=2)
62
 
 
 
63
  pipe, canny = _load_controlnet()
64
  edges = canny(qr_image, low_threshold=int(canny_low), high_threshold=int(canny_high))
65
 
@@ -67,6 +75,7 @@ def stylize_qr(prompt: str, steps: int, guidance: float, seed: int, canny_low: i
67
  def run():
68
  return pipe(
69
  prompt=str(prompt),
 
70
  image=edges,
71
  num_inference_steps=int(steps),
72
  guidance_scale=float(guidance),
@@ -79,32 +88,34 @@ def stylize_qr(prompt: str, steps: int, guidance: float, seed: int, canny_low: i
79
 
80
  # ========= UI =========
81
  with gr.Blocks() as demo:
82
- gr.Markdown("## Stable Diffusion + QR Code + ControlNet (step by step)")
83
 
84
  with gr.Tab("Stable Diffusion (prompt → image)"):
85
  prompt = gr.Textbox(label="Prompt", value="A fantasy castle at sunset")
 
86
  steps = gr.Slider(10, 50, value=30, label="Steps", step=1)
87
- cfg = gr.Slider(1, 12, value=7.5, label="Guidance Scale", step=0.1)
88
  seed = gr.Number(value=0, label="Seed (0 = random)", precision=0)
89
  out_sd = gr.Image(label="Generated Image")
90
- gr.Button("Generate").click(sd_generate, [prompt, steps, cfg, seed], out_sd)
91
 
92
  with gr.Tab("QR Maker (mybirdfire)"):
93
  url = gr.Textbox(label="URL/Text", value="http://www.mybirdfire.com")
94
  size = gr.Slider(256, 1024, value=512, step=64, label="Size (px)")
95
- quiet = gr.Slider(0, 8, value=2, step=1, label="Border (quiet zone)")
96
  out_qr = gr.Image(label="QR Code", type="pil")
97
  gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
98
 
99
- with gr.Tab("QR Stylizer (ControlNet)"):
100
- s_prompt = gr.Textbox(label="Style Prompt", value="floral papercut style, high contrast, preserve sharp squares")
101
- s_steps = gr.Slider(10, 50, value=30, label="Steps", step=1)
102
- s_cfg = gr.Slider(1, 12, value=7.5, label="Guidance Scale", step=0.1)
103
- s_seed = gr.Number(value=0, label="Seed (0 = random)", precision=0)
104
- canny_l = gr.Slider(0, 255, value=100, step=1, label="Canny low")
105
- canny_h = gr.Slider(0, 255, value=200, step=1, label="Canny high")
 
106
  out_styl = gr.Image(label="Stylized QR")
107
- gr.Button("Stylize").click(stylize_qr, [s_prompt, s_steps, s_cfg, s_seed, canny_l, canny_h], out_styl)
108
 
109
  if __name__ == "__main__":
110
  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
11
 
12
+ # ========= SD 1.5 (prompt-only) =========
13
  sd_pipe = StableDiffusionPipeline.from_pretrained(
14
  "runwayml/stable-diffusion-v1-5",
15
  torch_dtype=dtype
16
  ).to(device)
17
 
18
+ def sd_generate(prompt, negative, steps, guidance, seed):
19
  gen = torch.Generator(device=device).manual_seed(int(seed)) if int(seed) != 0 else None
20
  def run():
21
+ return sd_pipe(
22
+ prompt,
23
+ negative_prompt=negative or "",
24
+ num_inference_steps=int(steps),
25
+ guidance_scale=float(guidance),
26
+ generator=gen
27
+ ).images[0]
28
  if device in ("cuda", "mps"):
29
  with torch.autocast(device):
30
  return run()
31
  return run()
32
 
33
  # ========= QR Maker =========
34
+ def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int = 4) -> Image.Image:
35
  qr = qrcode.QRCode(
36
  version=None,
37
+ error_correction=ERROR_CORRECT_H, # highest EC
38
  box_size=10,
39
  border=border
40
  )
 
43
  img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
44
  return img.resize((size, size), resample=Image.NEAREST)
45
 
46
+ # ========= ControlNet Stylizer (SD1.5 + sd15-canny) =========
47
+ _cn = {"pipe": None}
 
48
  def _load_controlnet():
49
+ if _cn["pipe"] is None:
50
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
51
  from controlnet_aux import CannyDetector
52
+ from diffusers.schedulers.scheduling_euler_discrete import EulerDiscreteScheduler
53
+
54
  controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=dtype)
55
  pipe = StableDiffusionControlNetPipeline.from_pretrained(
56
  "runwayml/stable-diffusion-v1-5",
 
58
  torch_dtype=dtype,
59
  safety_checker=None
60
  ).to(device)
61
+ # Use Euler to match your sampler preference
62
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
63
  pipe.enable_attention_slicing()
64
  pipe.enable_vae_slicing()
65
+ _cn["pipe"] = pipe
66
+ _cn["canny"] = CannyDetector()
67
+ return _cn["pipe"], _cn["canny"]
 
 
 
 
68
 
69
+ def stylize_qr(prompt: str, negative: str, steps: int, guidance: float, seed: int, canny_low: int, canny_high: int):
70
+ qr_image = make_qr("http://www.mybirdfire.com", size=512, border=4)
71
  pipe, canny = _load_controlnet()
72
  edges = canny(qr_image, low_threshold=int(canny_low), high_threshold=int(canny_high))
73
 
 
75
  def run():
76
  return pipe(
77
  prompt=str(prompt),
78
+ negative_prompt=negative or "",
79
  image=edges,
80
  num_inference_steps=int(steps),
81
  guidance_scale=float(guidance),
 
88
 
89
  # ========= UI =========
90
  with gr.Blocks() as demo:
91
+ gr.Markdown("## Stable Diffusion + QR Code + ControlNet")
92
 
93
  with gr.Tab("Stable Diffusion (prompt → image)"):
94
  prompt = gr.Textbox(label="Prompt", value="A fantasy castle at sunset")
95
+ negative = gr.Textbox(label="Negative Prompt", value="lowres, bad anatomy, worst quality")
96
  steps = gr.Slider(10, 50, value=30, label="Steps", step=1)
97
+ cfg = gr.Slider(1, 12, value=7.0, label="Guidance Scale", step=0.1)
98
  seed = gr.Number(value=0, label="Seed (0 = random)", precision=0)
99
  out_sd = gr.Image(label="Generated Image")
100
+ gr.Button("Generate").click(sd_generate, [prompt, negative, steps, cfg, seed], out_sd)
101
 
102
  with gr.Tab("QR Maker (mybirdfire)"):
103
  url = gr.Textbox(label="URL/Text", value="http://www.mybirdfire.com")
104
  size = gr.Slider(256, 1024, value=512, step=64, label="Size (px)")
105
+ quiet = gr.Slider(0, 8, value=4, step=1, label="Border (quiet zone)")
106
  out_qr = gr.Image(label="QR Code", type="pil")
107
  gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
108
 
109
+ with gr.Tab("QR Stylizer (ControlNet sd15-canny + Euler)"):
110
+ s_prompt = gr.Textbox(label="Style Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet")
111
+ s_negative = gr.Textbox(label="Negative Prompt", value="lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, worst quality, low quality, very displeasing, (bad)")
112
+ s_steps = gr.Slider(10, 50, value=28, label="Steps", step=1)
113
+ s_cfg = gr.Slider(1, 12, value=7.0, label="CFG", step=0.1)
114
+ s_seed = gr.Number(value=1470713301, label="Seed", precision=0)
115
+ canny_l = gr.Slider(0, 255, value=80, step=1, label="Canny low")
116
+ canny_h = gr.Slider(0, 255, value=160, step=1, label="Canny high")
117
  out_styl = gr.Image(label="Stylized QR")
118
+ gr.Button("Stylize").click(stylize_qr, [s_prompt, s_negative, s_steps, s_cfg, s_seed, canny_l, canny_h], out_styl)
119
 
120
  if __name__ == "__main__":
121
  demo.launch()