File size: 12,346 Bytes
6ae079b
 
 
 
 
1fc8d06
6ae079b
d25b42d
 
184daa2
e20d060
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20d060
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56a99b7
6ae079b
 
 
 
 
 
 
 
 
e20d060
 
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
e20d060
6ae079b
e20d060
6ae079b
 
 
e20d060
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20d060
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
e20d060
d25b42d
6ae079b
 
 
 
 
 
 
56a99b7
6ae079b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3315876
56a99b7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os, re, gc, random
import numpy as np
from contextlib import nullcontext
from typing import Tuple

import gradio as gr
from PIL import Image, ImageFilter
import qrcode
from qrcode.constants import ERROR_CORRECT_H

import torch
from diffusers import (
    StableDiffusionPipeline,
    StableDiffusionControlNetImg2ImgPipeline,
    ControlNetModel,
    DPMSolverMultistepScheduler,
)
import spaces  # ZeroGPU decorator

# =========================================================
# Auth (optional for private models)
# =========================================================
hf_token = os.getenv("HF_TOKEN")
AUTH_KW = {"token": hf_token} if hf_token else {}

# =========================================================
# Helpers (untouched logic)
# =========================================================
def normalize_color(c):
    if c is None: return "white"
    if isinstance(c, (tuple, list)):
        r, g, b = (int(max(0, min(255, round(float(x))))) for x in c[:3]); return (r, g, b)
    if isinstance(c, str):
        s = c.strip()
        if s.startswith("#"): return s
        m = re.match(r"rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)", s, re.IGNORECASE)
        if m:
            r = int(max(0, min(255, round(float(m.group(1))))))
            g = int(max(0, min(255, round(float(m.group(2))))))
            b = int(max(0, min(255, round(float(m.group(3))))))
            return (r, g, b)
        return s
    return "white"

def strengthen_qr_prompts(pos: str, neg: str) -> Tuple[str, str]:
    # DON’T say “QR code” here – let ControlNet impose it
    pos = (pos or "").strip()
    neg = (neg or "").strip()
    pos2 = f"{pos}, high contrast lighting, clean details, cohesive composition".strip(", ")
    add_neg = "frame, border, ornate frame, watermark, text, numbers, checkerboard, mosaic, halftone, repeated pattern, glitch"
    neg2 = (neg + (", " if neg else "") + add_neg).strip(", ").strip()
    return pos2, neg2

def enforce_qr_contrast(stylized: Image.Image, qr_img: Image.Image, strength: float = 0.6, feather: float = 1.0) -> Image.Image:
    if strength <= 0: return stylized
    q = qr_img.convert("L")
    black_mask = q.point(lambda p: 255 if p < 128 else 0).filter(ImageFilter.GaussianBlur(radius=float(feather)))
    black = np.asarray(black_mask, dtype=np.float32) / 255.0
    white = 1.0 - black
    s = np.asarray(stylized.convert("RGB"), dtype=np.float32) / 255.0
    s = s * (1.0 - float(strength) * black[..., None])
    s = s + (1.0 - s) * (float(strength) * 0.85 * white[..., None])
    s = np.clip(s, 0.0, 1.0)
    return Image.fromarray((s * 255.0).astype(np.uint8), mode="RGB")

# =========================================================
# Models & loading (ZeroGPU-friendly lazy load)
# =========================================================
BASE_15        = "runwayml/stable-diffusion-v1-5"
QR_MONSTER_15  = "monster-labs/control_v1p_sd15_qrcode_monster"  # v2 subfolder is handled by authors; base path is fine
BRIGHTNESS_15  = "latentcat/control_v1p_sd15_brightness"          # optional helper

_sd = {"pipe": None}
_cn = {"pipe": None}

def _setup_scheduler(pipe):
    pipe.scheduler = DPMSolverMultistepScheduler.from_config(
        pipe.scheduler.config,
        use_karras_sigmas=True,
        algorithm_type="dpmsolver++"
    )

def _enable_memory_savers(pipe):
    # Good defaults for Spaces/ZeroGPU
    pipe.enable_attention_slicing()
    pipe.enable_vae_slicing()
    pipe.enable_vae_tiling()
    pipe.enable_model_cpu_offload()

def _load_sd_txt2img():
    if _sd["pipe"] is None:
        pipe = StableDiffusionPipeline.from_pretrained(
            BASE_15,
            torch_dtype=torch.float16,
            safety_checker=None,
            use_safetensors=True,
            low_cpu_mem_usage=True,
            **AUTH_KW
        )
        _setup_scheduler(pipe)
        _enable_memory_savers(pipe)
        _sd["pipe"] = pipe
    return _sd["pipe"]

def _load_cn_img2img():
    if _cn["pipe"] is None:
        qrnet  = ControlNetModel.from_pretrained(
            QR_MONSTER_15, torch_dtype=torch.float16, use_safetensors=True, **AUTH_KW
        )
        bright = ControlNetModel.from_pretrained(
            BRIGHTNESS_15, torch_dtype=torch.float16, use_safetensors=True, **AUTH_KW
        )
        pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
            BASE_15,
            controlnet=[qrnet, bright],
            torch_dtype=torch.float16,
            safety_checker=None,
            use_safetensors=True,
            low_cpu_mem_usage=True,
            **AUTH_KW
        )
        _setup_scheduler(pipe)
        _enable_memory_savers(pipe)
        _cn["pipe"] = pipe
    return _cn["pipe"]

# =========================================================
# Generation utilities (use inside @spaces.GPU)
# =========================================================
def sd_generate(prompt, negative, steps, guidance, seed, size=512):
    pipe = _load_sd_txt2img()
    # Reproducible generator — on GPU if available
    gen = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu")
    if int(seed) != 0:
        gen = gen.manual_seed(int(seed))
    else:
        gen = gen.manual_seed(random.randint(0, 2**31 - 1))

    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    gc.collect()

    out = pipe(
        prompt=prompt,
        negative_prompt=negative or "",
        num_inference_steps=int(steps),
        guidance_scale=float(guidance),
        width=int(size), height=int(size),
        generator=gen
    )
    return out.images[0]

def make_qr(url="http://www.mybirdfire.com", size=512, border=10, back_color="#808080", blur_radius=0.0):
    qr = qrcode.QRCode(version=None, error_correction=ERROR_CORRECT_H, box_size=10, border=int(border))
    qr.add_data(url.strip()); qr.make(fit=True)
    bg  = normalize_color(back_color)
    img = qr.make_image(fill_color="black", back_color=bg).convert("RGB").resize((size, size), Image.NEAREST)
    if blur_radius and blur_radius > 0:
        img = img.filter(ImageFilter.GaussianBlur(radius=float(blur_radius)))
    return img

NEG_DEFAULT = "lowres, low contrast, blurry, jpeg artifacts, worst quality, bad anatomy, extra digits"

# =========================================================
# Main two-stage generator (ZeroGPU-guarded)
# =========================================================
@spaces.GPU(duration=120)  # allocate GPU only while generating
def qr_art_two_stage(
    prompt, negative,
    base_steps, base_cfg, base_seed,
    stylize_steps, stylize_cfg, stylize_seed,
    size, url, border, back_color,
    denoise, qr_weight, bright_weight,
    qr_start, qr_end, bright_start, bright_end,
    control_blur, repair_strength, feather_px
):
    size = max(384, int(size) // 8 * 8)

    # Stage A: base art (txt2img)
    p_pos, p_neg = strengthen_qr_prompts(prompt, negative)
    base_img = sd_generate(p_pos, p_neg, base_steps, base_cfg, base_seed, size=size)

    # Stage B: img2img + ControlNet
    qr_img = make_qr(url=url, size=size, border=border, back_color=back_color, blur_radius=control_blur)
    pipe   = _load_cn_img2img()

    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    gc.collect()

    gen = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu")
    if int(stylize_seed) != 0:
        gen = gen.manual_seed(int(stylize_seed))
    else:
        gen = gen.manual_seed(random.randint(0, 2**31 - 1))

    kwargs = dict(
        prompt=p_pos,
        negative_prompt=p_neg or NEG_DEFAULT,
        image=base_img,                              # init image for img2img
        control_image=[qr_img, qr_img],              # Monster + Brightness
        strength=float(denoise),                     # how much we allow change
        num_inference_steps=int(stylize_steps),
        guidance_scale=float(stylize_cfg),
        generator=gen,
        controlnet_conditioning_scale=[float(qr_weight), float(bright_weight)],
        width=size, height=size,  # (diffusers uses init image size; harmless here)
    )

    try:
        out = pipe(
            **kwargs,
            control_guidance_start=[float(qr_start), float(bright_start)],
            control_guidance_end=[float(qr_end), float(bright_end)],
        )
    except TypeError:
        out = pipe(
            **kwargs,
            controlnet_start=[float(qr_start), float(bright_start)],
            controlnet_end=[float(qr_end), float(bright_end)],
        )

    img = out.images[0]

    # Optional post repair to push blacks/whites where modules demand
    img = enforce_qr_contrast(img, qr_img, strength=float(repair_strength), feather=float(feather_px))
    return img, base_img, qr_img

# =========================================================
# UI (Gradio Space)
# =========================================================
with gr.Blocks() as demo:
    gr.Markdown("## 🧩 QR-Code Monster — Two-Stage (txt2img → img2img + ControlNet) — ZeroGPU")

    with gr.Tab("Two-Stage QR Art"):
        with gr.Row():
            with gr.Column():
                url      = gr.Textbox(label="URL/Text", value="http://www.mybirdfire.com")
                prompt   = gr.Textbox(
                    label="Style prompt (no 'QR code' here)",
                    value="baroque palace interior with intricate roots, cinematic, dramatic lighting, ultra detailed"
                )
                negative = gr.Textbox(label="Negative", value="")
                size     = gr.Slider(512, 1024, value=768, step=64, label="Canvas (px)")

                gr.Markdown("**Stage A — Base art (txt2img)**")
                base_steps = gr.Slider(10, 60, value=26, step=1, label="Base steps")
                base_cfg   = gr.Slider(1.0, 12.0, value=6.0, step=0.1, label="Base CFG")
                base_seed  = gr.Number(value=0, precision=0, label="Base seed (0=random)")

                gr.Markdown("**Stage B — ControlNet img2img**")
                stylize_steps = gr.Slider(10, 60, value=28, step=1, label="Stylize steps")
                stylize_cfg   = gr.Slider(1.0, 12.0, value=6.0, step=0.1, label="Stylize CFG")
                stylize_seed  = gr.Number(value=0, precision=0, label="Stylize seed (0=random)")
                denoise       = gr.Slider(0.1, 0.8, value=0.48, step=0.01, label="Denoising strength (keep composition lower)")

                qr_weight     = gr.Slider(0.5, 1.7, value=1.2, step=0.05, label="QR Monster weight")
                bright_weight = gr.Slider(0.0, 1.0, value=0.20, step=0.05, label="Brightness weight")

                qr_start      = gr.Slider(0.0, 1.0, value=0.05, step=0.01, label="QR start")
                qr_end        = gr.Slider(0.0, 1.0, value=0.95, step=0.01, label="QR end")
                bright_start  = gr.Slider(0.0, 1.0, value=0.40, step=0.01, label="Brightness start")
                bright_end    = gr.Slider(0.0, 1.0, value=0.85, step=0.01, label="Brightness end")

                border        = gr.Slider(4, 20, value=12, step=1, label="QR border (quiet zone)")
                back_color    = gr.ColorPicker(value="#808080", label="QR background (mid-gray blends better)")
                control_blur  = gr.Slider(0.0, 3.0, value=1.2, step=0.1, label="Soften control (Gaussian blur radius)")
                repair_strength = gr.Slider(0.0, 1.0, value=0.65, step=0.05, label="Post repair strength")
                feather_px      = gr.Slider(0.0, 3.0, value=1.0, step=0.1, label="Repair feather (px)")

                go = gr.Button("Generate QR Art", variant="primary")

            with gr.Column():
                final_img = gr.Image(label="Final stylized QR")
                base_img  = gr.Image(label="Base art (Stage A)")
                ctrl_img  = gr.Image(label="Control image (QR used)")

        go.click(
            qr_art_two_stage,
            inputs=[prompt, negative,
                    base_steps, base_cfg, base_seed,
                    stylize_steps, stylize_cfg, stylize_seed,
                    size, url, border, back_color,
                    denoise, qr_weight, bright_weight,
                    qr_start, qr_end, bright_start, bright_end,
                    control_blur, repair_strength, feather_px],
            outputs=[final_img, base_img, ctrl_img]
        )

if __name__ == "__main__":
    demo.launch()