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6fd1a4
8850e22
 
4dcec2d
e9f4bbc
2332d1d
 
e9f4bbc
8850e22
2332d1d
 
 
 
e9f4bbc
2332d1d
 
 
 
 
e9f4bbc
 
2332d1d
 
e9f4bbc
 
 
2332d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6fd1a4
2332d1d
a6fd1a4
 
 
2332d1d
1e0df68
2332d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9f4bbc
 
2332d1d
 
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
SD 1.5 Lab β€” pick two conditioning slots (ControlNet or one of several
IP-Adapter variants), supply images, and see how different conditioners
interact during generation.

File layout:
  1. CONFIG       β€” registry of available conditioners (ControlNets and IP-Adapter variants)
  2. STATE        β€” global cache of loaded models (lazy loading)
  3. PREPROCESS   β€” detectors that turn an input image into a condition map
  4. PIPELINE     β€” diffusers pipeline assembly based on selected slots
  5. GENERATE     β€” main function called by the UI
  6. UI           β€” Gradio interface
"""

import gc
import torch
import numpy as np
from PIL import Image
import gradio as gr

from diffusers import (
    StableDiffusionControlNetPipeline,
    StableDiffusionPipeline,
    ControlNetModel,
    UniPCMultistepScheduler,
)

# Diagnostic: print versions of key packages on startup so we can
# debug any compatibility issues from the logs.
import diffusers, transformers, huggingface_hub
print(
    f"[versions] torch={torch.__version__} "
    f"diffusers={diffusers.__version__} "
    f"transformers={transformers.__version__} "
    f"hf_hub={huggingface_hub.__version__}"
)
try:
    import peft
    print(f"[versions] peft={peft.__version__}")
except ImportError:
    print("[versions] peft=not installed")

# Detectors are imported lazily inside functions to avoid loading
# everything into memory at startup.

# ============================================================
# 1. CONFIG
# ============================================================

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32

BASE_MODEL = "runwayml/stable-diffusion-v1-5"

CONTROLNETS = {
    "depth": {
        "repo": "lllyasviel/sd-controlnet-depth",
        "detector_kind": "midas",
    },
    "normals": {
        "repo": "lllyasviel/sd-controlnet-normal",
        "detector_kind": "midas_normal",
    },
    "pose": {
        "repo": "lllyasviel/sd-controlnet-openpose",
        "detector_kind": "openpose",
    },
    "lineart": {
        "repo": "lllyasviel/control_v11p_sd15_lineart",
        "detector_kind": "lineart",
    },
    "canny": {
        "repo": "lllyasviel/sd-controlnet-canny",
        "detector_kind": "canny",
    },
    "scribble": {
        "repo": "lllyasviel/sd-controlnet-scribble",
        "detector_kind": "scribble",
    },
}

# IP-Adapter variants. All share the same repo, differ in weight_name.
# - base:       general composition / style transfer
# - plus:       sharper detail preservation (uses patch tokens)
# - plus_face:  face-focused, better identity from face crops
# - full_face:  strongest identity, full-face variant
IP_ADAPTERS = {
    "ip_adapter": {
        "repo": "h94/IP-Adapter",
        "subfolder": "models",
        "weight_name": "ip-adapter_sd15.bin",
    },
    "ip_adapter_plus": {
        "repo": "h94/IP-Adapter",
        "subfolder": "models",
        "weight_name": "ip-adapter-plus_sd15.bin",
    },
    "ip_adapter_plus_face": {
        "repo": "h94/IP-Adapter",
        "subfolder": "models",
        "weight_name": "ip-adapter-plus-face_sd15.bin",
    },
    "ip_adapter_full_face": {
        "repo": "h94/IP-Adapter",
        "subfolder": "models",
        "weight_name": "ip-adapter-full-face_sd15.bin",
    },
}

SLOT_CHOICES = list(CONTROLNETS.keys()) + list(IP_ADAPTERS.keys()) + ["none"]


# ============================================================
# 2. STATE β€” lazy model cache
# ============================================================

_controlnet_cache = {}
_detector_cache = {}


def get_controlnet(name):
    """Return a ControlNetModel, downloading on first access."""
    if name not in _controlnet_cache:
        repo = CONTROLNETS[name]["repo"]
        print(f"[load] ControlNet: {name} ({repo})")
        _controlnet_cache[name] = ControlNetModel.from_pretrained(
            repo, torch_dtype=DTYPE
        )
    return _controlnet_cache[name]


def get_detector(kind):
    """Return a preprocessor by kind. Loaded lazily."""
    if kind in _detector_cache:
        return _detector_cache[kind]

    print(f"[load] detector: {kind}")
    if kind == "midas":
        from controlnet_aux import MidasDetector
        det = MidasDetector.from_pretrained("lllyasviel/Annotators")
    elif kind == "midas_normal":
        from controlnet_aux import MidasDetector
        det = MidasDetector.from_pretrained("lllyasviel/Annotators")
    elif kind == "openpose":
        from controlnet_aux import OpenposeDetector
        det = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
    elif kind == "lineart":
        from controlnet_aux import LineartDetector
        det = LineartDetector.from_pretrained("lllyasviel/Annotators")
    elif kind == "scribble":
        from controlnet_aux import HEDdetector
        det = HEDdetector.from_pretrained("lllyasviel/Annotators")
    elif kind == "canny":
        det = "canny"
    else:
        raise ValueError(f"Unknown detector: {kind}")

    _detector_cache[kind] = det
    return det


# ============================================================
# 3. PREPROCESS
# ============================================================

def preprocess_for_controlnet(image, cn_name):
    """Run the appropriate detector on the input image. Returns a PIL.Image."""
    kind = CONTROLNETS[cn_name]["detector_kind"]
    detector = get_detector(kind)

    if kind == "canny":
        import cv2
        arr = np.array(image)
        edges = cv2.Canny(arr, 100, 200)
        edges = np.stack([edges] * 3, axis=-1)
        return Image.fromarray(edges)

    if kind == "midas_normal":
        result = detector(image, depth_and_normal=True)
        return result[1] if isinstance(result, tuple) else result

    return detector(image)


# ============================================================
# 4. PIPELINE β€” assembled per slot configuration
# ============================================================

def build_pipeline(slot1, slot2):
    """
    Assemble a pipeline matching the selected slot pair.
    Logic:
      - Count selected ControlNets (0, 1, or 2)
      - If any ControlNet is selected, use StableDiffusionControlNetPipeline
      - Otherwise fall back to StableDiffusionPipeline
      - Detect which IP-Adapter variant (if any) is selected
      - IP-Adapter is loaded BEFORE moving the pipe to device, so its
        image_encoder ends up on the correct device.
    """
    cn_slots = [s for s in (slot1, slot2) if s in CONTROLNETS]
    ip_slot = next((s for s in (slot1, slot2) if s in IP_ADAPTERS), None)

    if cn_slots:
        controlnet_arg = (
            get_controlnet(cn_slots[0]) if len(cn_slots) == 1
            else [get_controlnet(n) for n in cn_slots]
        )
        pipe = StableDiffusionControlNetPipeline.from_pretrained(
            BASE_MODEL,
            controlnet=controlnet_arg,
            torch_dtype=DTYPE,
            safety_checker=None,
        )
    else:
        pipe = StableDiffusionPipeline.from_pretrained(
            BASE_MODEL,
            torch_dtype=DTYPE,
            safety_checker=None,
        )

    pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

    if ip_slot is not None:
        cfg = IP_ADAPTERS[ip_slot]
        print(f"[load] IP-Adapter: {ip_slot} ({cfg['weight_name']})")
        pipe.load_ip_adapter(
            cfg["repo"],
            subfolder=cfg["subfolder"],
            weight_name=cfg["weight_name"],
        )

    pipe = pipe.to(DEVICE)

    # NOTE: attention_slicing is intentionally disabled here.
    # In some diffusers versions it conflicts with IP-Adapter and produces
    # the "tuple object has no attribute shape" error. We pay with higher
    # peak RAM but get correctness. Re-enable cautiously after verifying
    # that IP-Adapter still works in your installed diffusers version.
    # if DEVICE == "cpu":
    #     pipe.enable_attention_slicing()
    pipe.enable_vae_slicing()
    pipe.enable_vae_tiling()

    return pipe, cn_slots, ip_slot


# ============================================================
# 5. GENERATE
# ============================================================

def generate(
    prompt,
    negative_prompt,
    slot1, slot2,
    image1, image2,
    is_preprocessed1, is_preprocessed2,
    weight1, weight2,
    steps, guidance, seed,
):
    """
    image1/image2 β€” input image for each slot.
    is_preprocessed1/2 β€” checkbox: skip detector if the image is already a
                         condition map.
    weight1/2 β€” controlnet_conditioning_scale or ip_adapter_scale.
    """
    if slot1 == "none" and slot2 == "none":
        return None, "Both slots are empty β€” pick at least one conditioner."

    # 1. Prepare conditioning inputs
    cn_images = []
    cn_weights = []
    ip_image = None
    ip_weight = 1.0

    for slot, img, is_pre, w in [
        (slot1, image1, is_preprocessed1, weight1),
        (slot2, image2, is_preprocessed2, weight2),
    ]:
        if slot == "none":
            continue
        if img is None:
            return None, f"Slot '{slot}' is selected but no image was provided."

        img = img.convert("RGB").resize((512, 512))

        if slot in CONTROLNETS:
            cond = img if is_pre else preprocess_for_controlnet(img, slot)
            cond = cond.resize((512, 512))
            cn_images.append(cond)
            cn_weights.append(float(w))
        elif slot in IP_ADAPTERS:
            ip_image = img
            ip_weight = float(w)

    # 2. Build pipeline
    pipe, cn_slots, ip_slot = build_pipeline(slot1, slot2)

    if ip_slot is not None:
        pipe.set_ip_adapter_scale(ip_weight)

    # 3. Call arguments
    generator = torch.Generator(device=DEVICE).manual_seed(int(seed))

    call_kwargs = dict(
        prompt=prompt,
        negative_prompt=negative_prompt or None,
        num_inference_steps=int(steps),
        guidance_scale=float(guidance),
        generator=generator,
        height=512,
        width=512,
    )

    if cn_images:
        call_kwargs["image"] = cn_images[0] if len(cn_images) == 1 else cn_images
        call_kwargs["controlnet_conditioning_scale"] = (
            cn_weights[0] if len(cn_weights) == 1 else cn_weights
        )

    if ip_slot is not None:
        # Pass raw PIL image; diffusers handles CLIP encoding internally.
        # This is the documented API path on huggingface.co/docs/diffusers.
        call_kwargs["ip_adapter_image"] = ip_image

    # 4. Run generation
    result = pipe(**call_kwargs).images[0]

    # 5. Cleanup
    del pipe
    gc.collect()
    if DEVICE == "cuda":
        torch.cuda.empty_cache()

    info = (
        f"Slots: {slot1} ({weight1}) + {slot2} ({weight2}) | "
        f"steps={steps}, cfg={guidance}, seed={seed}, device={DEVICE}"
    )
    return result, info


# ============================================================
# 6. UI
# ============================================================

def make_slot_ui(slot_idx):
    """One slot: type dropdown, image input, preprocessed checkbox, weight slider."""
    with gr.Group():
        gr.Markdown(f"### Slot {slot_idx}")
        slot_type = gr.Dropdown(
            choices=SLOT_CHOICES,
            value="none",
            label="Conditioner type",
        )
        image = gr.Image(type="pil", label="Image")
        is_preprocessed = gr.Checkbox(
            label="Already a condition map (skip detector)",
            value=False,
        )
        weight = gr.Slider(
            minimum=0.0, maximum=2.0, step=0.05, value=1.0,
            label="Weight (conditioning scale)",
        )
    return slot_type, image, is_preprocessed, weight


with gr.Blocks(title="SD 1.5 Lab β€” dual conditioner playground") as demo:
    gr.Markdown(
        "# SD 1.5 Lab\n"
        "Pick two conditioners (ControlNet or IP-Adapter variant), supply images, "
        "and see how they combine.\n\n"
        "**IP-Adapter variants:** `ip_adapter` (general), `ip_adapter_plus` "
        "(sharper detail), `ip_adapter_plus_face` (face-focused), "
        "`ip_adapter_full_face` (strong identity).\n\n"
        f"Current device: **{DEVICE}**. On CPU, generating a 512Γ—512 image "
        "takes roughly 15–30 minutes β€” that's expected."
    )

    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Prompt", value="a photo of a cat in a garden")
            negative_prompt = gr.Textbox(label="Negative prompt", value="")
            with gr.Row():
                steps = gr.Slider(5, 50, value=20, step=1, label="Steps")
                guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="CFG")
                seed = gr.Number(value=42, label="Seed", precision=0)

    with gr.Row():
        s1_type, s1_img, s1_pre, s1_w = make_slot_ui(1)
        s2_type, s2_img, s2_pre, s2_w = make_slot_ui(2)

    run_btn = gr.Button("Generate", variant="primary")
    output_img = gr.Image(label="Result")
    info = gr.Textbox(label="Info", interactive=False)

    run_btn.click(
        fn=generate,
        inputs=[
            prompt, negative_prompt,
            s1_type, s2_type,
            s1_img, s2_img,
            s1_pre, s2_pre,
            s1_w, s2_w,
            steps, guidance, seed,
        ],
        outputs=[output_img, info],
    )


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