Tanut commited on
Commit
0eaf945
·
1 Parent(s): 2e2f472

Change Model

Browse files
Files changed (2) hide show
  1. app.py +49 -48
  2. requirements.txt +2 -0
app.py CHANGED
@@ -43,59 +43,54 @@ def make_qr(url: str = "http://www.mybirdfire.com", size: int = 512, border: int
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)
63
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
 
64
  pipe.enable_vae_slicing()
65
- _sdxl["pipe"] = pipe
66
- _sdxl["canny"] = CannyDetector()
67
- return _sdxl["pipe"], _sdxl["canny"]
68
 
69
- NEG = "lowres, low contrast, blurry, jpeg artifacts, worst quality, extra digits, bad anatomy"
70
 
71
- def stylize_qr_sdxl(prompt: str, steps: int=28, guidance: float=7.0, seed: int=1470713301,
72
- canny_low: int=80, canny_high: int=160):
73
- # 1) make a strong QR @1024
74
- qr = make_qr("http://www.mybirdfire.com", size=1024, border=6)
75
- # 2) edges for canny CN
76
- pipe, canny = _load_sdxl_dual()
77
- edges = canny(qr, low_threshold=int(canny_low), high_threshold=int(canny_high))
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]
85
 
86
  def run():
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"):
101
  with torch.autocast(device):
@@ -104,7 +99,7 @@ def stylize_qr_sdxl(prompt: str, steps: int=28, guidance: float=7.0, seed: int=1
104
 
105
  # ========= UI =========
106
  with gr.Blocks() as demo:
107
- gr.Markdown("## Stable Diffusion + QR Code + ControlNet")
108
 
109
  with gr.Tab("Stable Diffusion (prompt → image)"):
110
  prompt = gr.Textbox(label="Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet, Beautiful, Fantasy, Art, Anime")
@@ -121,16 +116,22 @@ with gr.Blocks() as demo:
121
  quiet = gr.Slider(0, 8, value=4, step=1, label="Border (quiet zone)")
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")
129
- sd = gr.Number(value=1470713301, label="Seed", precision=0)
130
- cl = gr.Slider(0, 255, 80, step=1, label="Canny low")
131
- ch = gr.Slider(0, 255, 160, step=1, label="Canny high")
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()
 
43
  img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
44
  return img.resize((size, size), resample=Image.NEAREST)
45
 
46
+ # ========= SD1.5 ControlNet stylizer (canny + tile) =========
47
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
48
  from diffusers.schedulers.scheduling_euler_discrete import EulerDiscreteScheduler
49
  from controlnet_aux import CannyDetector
50
 
51
+ BASE_15 = "runwayml/stable-diffusion-v1-5"
52
+ CN_CANNY_15 = "lllyasviel/sd-controlnet-canny"
53
+ CN_TILE_15 = "lllyasviel/control_v11f1e_sd15_tile"
54
+
55
+ _cn = {"pipe": None}
56
+ def _load_sd15_dual():
57
+ if _cn["pipe"] is None:
58
+ canny = ControlNetModel.from_pretrained(CN_CANNY_15, torch_dtype=dtype)
59
+ tile = ControlNetModel.from_pretrained(CN_TILE_15, torch_dtype=dtype)
60
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
61
+ BASE_15, controlnet=[canny, tile], torch_dtype=dtype, safety_checker=None
62
  ).to(device)
63
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config) # Sampler: Euler
64
+ pipe.enable_attention_slicing()
65
  pipe.enable_vae_slicing()
66
+ _cn["pipe"] = pipe
67
+ _cn["canny_aux"] = CannyDetector()
68
+ return _cn["pipe"], _cn["canny_aux"]
69
 
70
+ NEG_DEFAULT = "lowres, low contrast, blurry, jpeg artifacts, worst quality, bad anatomy, extra digits"
71
 
72
+ def stylize_qr_sd15(prompt: str, negative: str, steps: int, guidance: float, seed: int,
73
+ canny_low: int, canny_high: int, border: int):
74
+ # Make fresh QR each time
75
+ qr_img = make_qr("http://www.mybirdfire.com", size=512, border=int(border))
76
+ pipe, canny = _load_sd15_dual()
77
+ edges = canny(qr_img, low_threshold=int(canny_low), high_threshold=int(canny_high))
 
78
 
79
+ gen = torch.Generator(device=device).manual_seed(int(seed)) if int(seed) != 0 else None
80
 
81
+ # Control weights (canny, tile). Tune if too “lego” or too artsy.
82
+ cn_scales = [1.2, 0.6]
 
 
83
 
84
  def run():
85
+ return pipe(
86
+ prompt=str(prompt),
87
+ negative_prompt=negative or NEG_DEFAULT,
88
+ image=[edges, qr_img], # canny first, tile second
89
  controlnet_conditioning_scale=cn_scales,
 
 
90
  num_inference_steps=int(steps),
91
  guidance_scale=float(guidance),
92
  generator=gen
93
  ).images[0]
 
94
 
95
  if device in ("cuda", "mps"):
96
  with torch.autocast(device):
 
99
 
100
  # ========= UI =========
101
  with gr.Blocks() as demo:
102
+ gr.Markdown("## Stable Diffusion + QR Code + ControlNet (SD1.5)")
103
 
104
  with gr.Tab("Stable Diffusion (prompt → image)"):
105
  prompt = gr.Textbox(label="Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet, Beautiful, Fantasy, Art, Anime")
 
116
  quiet = gr.Slider(0, 8, value=4, step=1, label="Border (quiet zone)")
117
  out_qr = gr.Image(label="QR Code", type="pil")
118
  gr.Button("Generate QR").click(make_qr, [url, size, quiet], out_qr)
119
+
120
+ with gr.Tab("QR Stylizer (SD1.5 canny + tile, Euler)"):
121
+ s_prompt = gr.Textbox(label="Style Prompt", value="Sky, Moon, Bird, Blue, In the dark, Goddess, Sweet, Beautiful, Fantasy, Art, Anime")
122
+ s_negative = gr.Textbox(label="Negative Prompt", value=NEG_DEFAULT)
123
+ s_steps = gr.Slider(10, 50, value=28, label="Steps", step=1)
124
+ s_cfg = gr.Slider(1, 12, value=7.0, label="CFG", step=0.1)
125
+ s_seed = gr.Number(value=1470713301, label="Seed", precision=0)
126
+ canny_l = gr.Slider(0, 255, value=80, step=1, label="Canny low")
127
+ canny_h = gr.Slider(0, 255, value=160, step=1, label="Canny high")
128
+ s_border = gr.Slider(2, 10, value=6, step=1, label="QR border")
129
+ out_styl = gr.Image(label="Stylized QR")
130
+ gr.Button("Stylize").click(
131
+ stylize_qr_sd15,
132
+ [s_prompt, s_negative, s_steps, s_cfg, s_seed, canny_l, canny_h, s_border],
133
+ out_styl
134
+ )
135
 
136
  if __name__ == "__main__":
137
  demo.launch()
requirements.txt CHANGED
@@ -6,3 +6,5 @@ safetensors
6
  gradio
7
  qrcode[pil]
8
  controlnet-aux
 
 
 
6
  gradio
7
  qrcode[pil]
8
  controlnet-aux
9
+ mediapipe
10
+ timm