File size: 21,689 Bytes
5f8079c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b3ce2
 
5f8079c
 
 
 
 
 
 
 
 
 
 
70b3ce2
 
 
 
 
 
5f8079c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f469697
5f8079c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b3ce2
5f8079c
70b3ce2
 
 
 
5f8079c
 
 
 
 
 
 
 
 
 
 
 
 
 
70b3ce2
 
5f8079c
 
 
70b3ce2
5f8079c
 
 
 
 
 
 
 
 
 
 
 
 
70b3ce2
 
 
5f8079c
 
 
 
 
 
 
70b3ce2
 
 
5f8079c
 
f4341de
 
 
 
 
5f8079c
 
 
 
70b3ce2
 
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
"""Thinking in Boxes β€” Hugging Face Spaces (ZeroGPU) demo.

Self-contained Gradio-SDK app for a FREE ZeroGPU Space. 
  β€’ Generation runs inside @spaces.GPU (the pipeline moves to CUDA per request),
  β€’ The 3D box editor is embedded via <iframe srcdoc=...>,
  β€’ Place and move boxes by hand.

Space config (README front-matter): sdk: gradio Β· app_file: demo.py
Secret: HF_TOKEN (account that accepted the FLUX.1-Kontext-dev license).
Local run: `python demo.py`.
"""
import base64
import inspect
import io
import json
import os
import random
from pathlib import Path
from types import SimpleNamespace

os.environ.setdefault("HF_ENDPOINT", "https://huggingface.co")
os.environ["GRADIO_TEMP_DIR"] = os.environ.get("GRADIO_TEMP_DIR") or "/tmp/gradio_tib"
os.makedirs(os.environ["GRADIO_TEMP_DIR"], exist_ok=True)

# ZeroGPU: import spaces BEFORE torch β€” it patches torch's CUDA at import time,
# and ZeroGPU detects @spaces.GPU functions through that patch.
try:
    import spaces
except Exception:
    def _gpu(*a, **k):
        if a and callable(a[0]):
            return a[0]
        def deco(f):
            return f
        return deco
    spaces = SimpleNamespace(GPU=_gpu)

import gradio as gr
import numpy as np
import torch
from diffusers import DiffusionPipeline
from PIL import Image

HERE = Path(__file__).resolve().parent
EDITOR_HTML = HERE / "box-editor.html"

# ── Defaults (mirror inference.sh) ──
DEFAULT_MODEL    = "black-forest-labs/FLUX.1-Kontext-dev"
DEFAULT_PIPELINE = "pipelines/pipeline_flux_kontext.py"
DEFAULT_DTYPE    = "bfloat16"
DEFAULT_LORA     = "pradhaansbhat/Thinking-In-Boxes"
DEFAULT_WEIGHT   = "pytorch_lora_weights.safetensors"
DEFAULT_ADAPTER  = "100K+20K-LoRA"
DEFAULT_LOCAL    = False
DEFAULT_STEPS    = 28
DEFAULT_GUIDANCE = 3.5
DEFAULT_TRUE_CFG = 1.0
DEFAULT_SCALE    = 1.0
DEFAULT_HEIGHT   = 512
DEFAULT_WIDTH    = 512
DEFAULT_MAX_SEQ  = 512

DTYPE_MAP = {
    "bfloat16": torch.bfloat16, "float16": torch.float16, "half": torch.float16,
    "float32": torch.float32, "float": torch.float32, "float64": torch.float64, "double": torch.float64,
}

STATE = {"pipe": None, "max_sequence_length": DEFAULT_MAX_SEQ, "last_seed": 0}


# ─────────────────────────────────────────────────────── pipeline (CPU load) ──
def load_pipe_cpu():
    """Load the pipeline on CPU (ZeroGPU exposes CUDA only inside @spaces.GPU)."""
    dtype = DTYPE_MAP[DEFAULT_DTYPE]
    token = os.environ.get("HF_TOKEN") or None
    pipe = DiffusionPipeline.from_pretrained(
        DEFAULT_MODEL, custom_pipeline=DEFAULT_PIPELINE or None, trust_remote_code=True,
        local_files_only=DEFAULT_LOCAL, token=token, dtype=dtype)
    if DEFAULT_LORA:
        pipe.transformer.load_lora_adapter(
            DEFAULT_LORA, weight_name=DEFAULT_WEIGHT or None, adapter_name=DEFAULT_ADAPTER or None)
        print(f"[tib-spaces] LoRA {DEFAULT_ADAPTER} loaded from {DEFAULT_LORA}", flush=True)
    pipe.transformer.to(memory_format=torch.channels_last)
    pipe.vae.to(memory_format=torch.channels_last)
    return pipe


def call_pipe(pipe, **kwargs):
    sig = inspect.signature(pipe.__call__)
    valid = {k: v for k, v in kwargs.items() if k in sig.parameters}
    return pipe(**valid).images


def dataurl_to_pil(u):
    if not u:
        return None
    if "," in u:
        u = u.split(",", 1)[1]
    return Image.open(io.BytesIO(base64.b64decode(u))).convert("RGB")


# ──────────────────────────────────────────────── generation (ZeroGPU) ──
@spaces.GPU(duration=120)
def generate(image_u, src_u, tgt_u, steps, guidance, true_cfg, lora_scale,
             randomize, height, width, num_images, progress=gr.Progress(track_tqdm=True)):
    if STATE["pipe"] is None:
        raise gr.Error("Model not loaded β€” check the Space logs (HF_TOKEN / license).")
    pipe = STATE["pipe"].to("cuda")
    src = dataurl_to_pil(src_u); tgt = dataurl_to_pil(tgt_u)
    if src is None or tgt is None:
        raise gr.Error("Grab the source + target boxes from the editor first.")
    image = dataurl_to_pil(image_u)
    seed = random.randint(0, 2**31 - 1) if randomize else STATE.get("last_seed", 0)
    STATE["last_seed"] = seed
    generator = torch.Generator(device="cuda").manual_seed(seed)
    images = call_pipe(
        pipe,
        image=image, source_primitive=src, target_primitive=tgt,
        prompt="", negative_prompt=None,
        guidance_scale=float(guidance), true_cfg_scale=float(true_cfg),
        num_inference_steps=int(steps), num_images_per_prompt=int(num_images),
        height=int(height), width=int(width), max_area=int(height) * int(width),
        max_sequence_length=int(STATE["max_sequence_length"]), generator=generator,
        joint_attention_kwargs={"scale": float(lora_scale)},
    )
    return list(images), f"seed {seed}"


def grab_show(status, src_u, tgt_u):
    msg = (status or "").strip()
    if msg.startswith("⚠") or not (src_u and tgt_u):
        gr.Warning(msg.lstrip("⚠ ").strip() or "Set a source, edit the boxes, then grab.")
    else:
        gr.Info("βœ“ Grabbed image + source/target boxes from the editor.")
    return dataurl_to_pil(src_u), dataurl_to_pil(tgt_u)


# ── mock detector (no WildDet3D on Spaces): plausible metric boxes, camera frame y-down ──
def run_mock(image_np, prompts, points=None, boxes=None):
    H, W = image_np.shape[:2]
    fx = 1.2 * W
    K = [[fx, 0, W / 2], [0, fx, H / 2], [0, 0, 1]]
    prompt_xy = []
    if points:
        prompt_xy = [(p[0], p[1]) for p in points if int(p[2]) == 1]
    elif boxes:
        prompt_xy = [((b[0] + b[2]) / 2, (b[1] + b[3]) / 2) for b in boxes]
    if prompt_xy:
        dets = []
        for i, (u, v) in enumerate(prompt_xy):
            Z = 6.0
            X = (u - W / 2) * Z / fx
            Y = (v - H / 2) * Z / fx
            dets.append({"label": prompts[i] if i < len(prompts) else f"object {i+1}",
                         "class_id": i, "score": 0.88,
                         "box2d": [u - 40, v - 40, u + 40, v + 40],
                         "center": [X, Y, Z], "dims": [0.8, 1.0, 0.8], "quat": [1, 0, 0, 0]})
        return {"image_hw": [H, W], "intrinsics": K, "detections": dets}
    catalog = {
        "car":     {"center": [0.4, 0.9, 8.0],  "dims": [1.8, 1.5, 4.3], "quat": [0.966, 0, 0.259, 0]},
        "person":  {"center": [-2.2, 0.5, 6.0], "dims": [0.6, 1.7, 0.6], "quat": [1, 0, 0, 0]},
        "bicycle": {"center": [2.4, 0.8, 5.0],  "dims": [0.6, 1.1, 1.7], "quat": [0.985, 0, 0.174, 0]},
        "chair":   {"center": [0.0, 0.6, 3.2],  "dims": [0.6, 0.9, 0.6], "quat": [1, 0, 0, 0]},
        "cup":     {"center": [0.1, 0.4, 1.4],  "dims": [0.09, 0.11, 0.09], "quat": [1, 0, 0, 0]},
    }
    dets = []
    for i, p in enumerate(prompts):
        pr = catalog.get(p.strip().lower(), {"center": [0, 0.6, 4 + i], "dims": [0.8, 0.8, 0.8], "quat": [1, 0, 0, 0]})
        dets.append({"label": p.strip(), "class_id": i, "score": 0.9 - 0.05 * i,
                     "box2d": [W * 0.3, H * 0.4, W * 0.7, H * 0.8], **pr})
    return {"image_hw": [H, W], "intrinsics": K, "detections": dets}


def detect_objects(payload_json):
    try:
        req = json.loads(payload_json) if payload_json else {}
    except Exception:
        return json.dumps({"error": "bad request payload"})
    img = dataurl_to_pil(req.get("image"))
    if img is None:
        return json.dumps({"error": "no image β€” load one in the editor first"})
    arr = np.array(img).astype(np.float32)
    prompts = [p.strip() for p in (req.get("prompts") or "").split(",") if p.strip()]
    points = req.get("points") or None
    boxes = req.get("boxes") or None
    try:
        return json.dumps(run_mock(arr, prompts, points, boxes))
    except Exception as e:
        return json.dumps({"error": str(e)})


# ───────────────────────────────────────────── editor bridge (browser JS) ──
GRAB_JS = """
() => {
  const f = document.getElementById('tib-editor');
  if (!f || !f.contentWindow) return ["", "", "", "⚠ Editor not loaded yet."];
  let st;
  try { st = f.contentWindow.getSourceTarget(); }
  catch (e) { return ["", "", "", "⚠ Editor not ready: " + e.message]; }
  if (!st || st.error) return ["", "", "", "⚠ " + ((st && st.error) || "no data")];
  return [st.image || "", st.source, st.target, "βœ“ Grabbed image + source + target from the editor."];
}
"""

DETECT_BRIDGE_JS = """
() => {
  if (window.__tibDetectBridge) return;
  window.__tibDetectBridge = true;
  const setVal = (el, v) => {
    const proto = el.tagName === 'TEXTAREA' ? window.HTMLTextAreaElement.prototype
                                            : window.HTMLInputElement.prototype;
    const d = Object.getOwnPropertyDescriptor(proto, 'value');
    d && d.set ? d.set.call(el, v) : (el.value = v);
    el.dispatchEvent(new Event('input', { bubbles: true }));
  };
  window.addEventListener('message', (e) => {
    const d = e.data; if (!d || d.type !== 'tib-detect') return;
    const box = document.querySelector('#tib-detect-payload');
    const ta = box && (box.querySelector('textarea') || box.querySelector('input'));
    if (!ta) return;
    setVal(ta, JSON.stringify(d));
    setTimeout(() => { const b = document.querySelector('#tib-detect-trigger');
      if (b) (b.querySelector('button') || b).click(); }, 50);
  });
}
"""

DETECT_RESULT_JS = """
(resultJson) => {
  const f = document.getElementById('tib-editor');
  if (!f || !f.contentWindow) return;
  let p; try { p = JSON.parse(resultJson || '{}'); } catch (e) { p = { error: 'bad detector result' }; }
  const msg = (p && p.error) ? { type: 'tib-detect-result', error: p.error }
                             : { type: 'tib-detect-result', detections: p };
  try { f.contentWindow.postMessage(msg, '*'); } catch (e) {}
}
"""


# ──────────────────────────────────────────────────────────────── theme/CSS ──
TERRACOTTA = gr.themes.colors.Color(
    name="terracotta", c50="#faf1ee", c100="#f3d9d0", c200="#e8b5a6", c300="#d98f78",
    c400="#cf7458", c500="#c0604a", c600="#a5503d", c700="#7b4f3a", c800="#5f3b2c",
    c900="#432a20", c950="#2a1a14")

THEME = gr.themes.Soft(
    primary_hue=TERRACOTTA, secondary_hue=TERRACOTTA, neutral_hue=gr.themes.colors.stone,
    font=[gr.themes.GoogleFont("Outfit"), "system-ui", "sans-serif"],
    font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"],
)

_VARS = """
  --body-background-fill:#F6F3EE; --background-fill-primary:#FFFFFF; --background-fill-secondary:#EDEAE3;
  --block-background-fill:#FFFFFF; --block-border-color:#D8D4CB; --border-color-primary:#D8D4CB;
  --border-color-accent-subdued:#e6c3ba; --panel-background-fill:#FFFFFF;
  --body-text-color:#1A1714; --body-text-color-subdued:#6B6560;
  --block-label-text-color:#C0604A; --block-label-background-fill:#F1EBE3; --block-title-text-color:#C0604A;
  --button-primary-background-fill:#C0604A; --button-primary-background-fill-hover:#7B4F3A;
  --button-primary-text-color:#FFFFFF; --button-primary-border-color:#C0604A;
  --button-secondary-background-fill:#FFFFFF; --button-secondary-background-fill-hover:#EDEAE3;
  --button-secondary-border-color:#D8D4CB; --button-secondary-text-color:#1A1714;
  --input-background-fill:#FFFFFF; --input-border-color:#D8D4CB; --neutral-800:#3a352f;
  --color-accent:#C0604A; --color-accent-soft:#f3d9d0; --link-text-color:#C0604A;
  --checkbox-background-color:#FFFFFF; --checkbox-border-color:#C0604A;
  --checkbox-background-color-selected:#C0604A; --checkbox-border-color-selected:#C0604A;
  --checkbox-background-color-focus:#C0604A; --checkbox-border-color-focus:#C0604A;
"""
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,500;0,600;1,500;1,600&display=swap');
:root, .gradio-container, gradio-app, .dark { __VARS__ }
body, .gradio-container, gradio-app, .dark { background:#F6F3EE !important; color:#1A1714 !important; }
.gradio-container{max-width:100%!important;width:100%!important;margin:0!important;padding:0 28px 28px!important}
.gradio-container .main, .main.fillable, .fillable.app, main.contain{max-width:100%!important}
.dark input, .dark textarea{background:#FFFFFF!important;color:#1A1714!important}
.gradio-container .block > label > span:first-child,
.gradio-container .block .head > label > span:first-child,
.gradio-container .block > .container > span:first-child,
.gradio-container .block .wrap > .head span{color:#C0604A!important;background:#F1EBE3!important}
.gradio-container label > span{color:#C0604A}
button.primary, .primary{color:#FFF6F1!important}
.gr-button-primary, button[variant="primary"]{color:#FFF6F1!important}
#tib-header{position:relative;text-align:center;margin:2px 0 14px;padding:22px 0 18px;overflow:hidden}
#tib-header .v{position:relative;z-index:2;display:inline-block;font-family:'Outfit',system-ui,sans-serif;font-weight:600;font-size:.78rem;letter-spacing:.1em;text-transform:uppercase;color:#C0604A;background:#F1EBE3;border:1px solid #e6c3ba;border-radius:999px;padding:4px 15px;margin-bottom:12px}
#tib-header .t{position:relative;z-index:2;font-family:'Cormorant Garamond',Georgia,serif;font-weight:600;font-size:3.1rem;line-height:1;letter-spacing:-.01em;color:#1A1714}
#tib-header .t em{font-style:italic;color:#C0604A}
#tib-header .s{position:relative;z-index:2;font-family:'Outfit',sans-serif;font-weight:400;color:#6B6560;font-size:1.06rem;margin-top:6px;letter-spacing:.01em}
.tib-cube{position:absolute;z-index:1;opacity:.5;animation:tibfloat 9s ease-in-out infinite}
.tib-cube.c1{left:6%;top:24%;width:54px;animation-delay:0s}
.tib-cube.c2{left:16%;top:58%;width:38px;opacity:.35;animation-delay:1.4s}
.tib-cube.c3{right:8%;top:20%;width:60px;animation-delay:.6s}
.tib-cube.c4{right:19%;top:60%;width:40px;opacity:.35;animation-delay:2.1s}
.tib-cube.c5{left:38%;top:8%;width:30px;opacity:.28;animation-delay:1.1s}
.tib-cube.c6{right:36%;top:70%;width:32px;opacity:.3;animation-delay:.3s}
@keyframes tibfloat{0%,100%{transform:translateY(0) rotate(-6deg)}50%{transform:translateY(-12px) rotate(6deg)}}
h2,h3{font-family:'Cormorant Garamond',Georgia,serif!important;color:#1A1714}
.tib-edcol{display:flex!important;flex-direction:column;min-height:0}
.tib-edcol > *, .tib-edcol .block, .tib-edcol .html-container, .tib-edcol .prose{
  display:flex!important;flex-direction:column;flex:1 1 auto!important;min-height:0;height:auto!important}
#tib-editor{flex:1 1 auto;height:100%!important;min-height:620px;display:block}
.tib-ctrl{align-self:stretch;flex-wrap:nowrap!important;overflow-y:auto;padding-right:8px;gap:12px}
.tib-ctrl .prose:empty, .tib-ctrl .md:empty, .tib-ctrl .block.padded:empty{display:none!important;min-height:0!important;padding:0!important;margin:0!important}
.tib-sq .image-container{aspect-ratio:1/1;min-height:190px}
.tib-sq .image-container img{object-fit:contain}
.tib-out .wrap, .tib-out .empty, .tib-out .grid-wrap, .tib-out .fixed-height, .tib-out .preview{
  aspect-ratio:1/1!important;height:auto!important;min-height:0!important;max-height:none!important;
  display:flex!important;align-items:center!important;justify-content:center!important;overflow:hidden}
.tib-out .grid-container{width:100%!important;height:100%!important;display:grid!important;grid-template-columns:1fr!important;place-items:center!important}
.tib-out .thumbnail-item, .tib-out .image-container{width:100%!important;height:100%!important;display:flex!important;align-items:center!important;justify-content:center!important}
.tib-out img{max-width:100%!important;max-height:100%!important;width:auto!important;height:auto!important;object-fit:contain!important;margin:auto!important}
/* accordion titles + section headings readable on cream (were invisible) */
.gradio-container .label-wrap, .gradio-container .label-wrap > span, .gradio-container .label-wrap span{color:#1A1714!important}
.gradio-container h4, .gradio-container .prose h4{color:#1A1714!important}
.tib-step button, button.tib-step{position:relative;padding-left:46px!important;font-weight:600!important}
.tib-step button::before, button.tib-step::before{
  position:absolute;left:12px;top:50%;transform:translateY(-50%);
  width:24px;height:24px;border-radius:50%;display:flex;align-items:center;justify-content:center;
  font-family:'Outfit',system-ui,sans-serif;font-weight:700;font-size:.88rem;line-height:1;border:1.5px solid #C0604A}
.tib-step1 button::before, button.tib-step1::before{content:"1";background:#C0604A;color:#FFF6F1}
.tib-step2 button::before, button.tib-step2::before{content:"2";background:#FFF6F1;color:#C0604A;border-color:#FFF6F1}
.gradio-container input[type=checkbox]{accent-color:#C0604A;width:18px;height:18px;border:1.5px solid #C0604A!important;background:#FFFFFF!important;appearance:auto!important;-webkit-appearance:auto!important;opacity:1!important}
.gradio-container input[type=checkbox]:checked{background:#C0604A!important;border-color:#C0604A!important}
.tib-hidden{display:none!important}
footer{display:none!important}
""".replace("__VARS__", _VARS)


def _editor_srcdoc():
    """Inline box-editor.html into an iframe srcdoc (Gradio SDK serves no custom route)."""
    html = EDITOR_HTML.read_text(encoding="utf-8")
    esc = html.replace("&", "&amp;").replace('"', "&quot;")
    return ('<iframe id="tib-editor" srcdoc="' + esc + '" '
            'style="width:100%;height:100%;min-height:620px;border:1px solid #D8D4CB;'
            'border-radius:12px;background:#F6F3EE"></iframe>')


def build_demo():
    iframe = _editor_srcdoc()
    with gr.Blocks(title="Thinking in Boxes") as demo:
        cube = ('<svg class="tib-cube {c}" viewBox="0 0 100 100" fill="none" stroke="#C0604A" '
            'stroke-width="4" stroke-linejoin="round">'
            '<path d="M20 35 L50 20 L80 35 L50 50 Z"/>'
            '<path d="M20 35 L20 70 L50 85 L50 50 Z"/>'
            '<path d="M80 35 L80 70 L50 85 L50 50 Z"/></svg>')
        cubes = "".join(cube.format(c=f"c{i}") for i in range(1, 7))
        gr.HTML(f'<div id="tib-header">{cubes}'
                '<div class="v">NeurIPS 2026</div>'
                '<div class="t">Thinking in <em>Boxes</em></div>'
                '<div class="s">3D Editing in Real Images Made Easy</div></div>')

        with gr.Row(equal_height=True):
            with gr.Column(scale=3, elem_classes="tib-edcol"):
                gr.HTML(iframe)
            with gr.Column(scale=2, elem_classes="tib-ctrl"):
                grab = gr.Button("Grab image + boxes from editor", elem_classes="tib-step tib-step1")
                with gr.Row(equal_height=True):
                    src_prev = gr.Image(label="Source boxes", elem_classes="tib-sq", show_label=True)
                    tgt_prev = gr.Image(label="Target boxes", elem_classes="tib-sq", show_label=True)
                image_u = gr.Textbox(elem_classes="tib-hidden"); src_u = gr.Textbox(elem_classes="tib-hidden")
                tgt_u = gr.Textbox(elem_classes="tib-hidden"); status_u = gr.Textbox(elem_classes="tib-hidden")
                randomize = gr.Checkbox(value=True, label="🎲 Randomize seed")
                gen = gr.Button("Generate", variant="primary", elem_classes="tib-step tib-step2")
                out = gr.Gallery(label="Generated edit", columns=1, object_fit="contain",
                                    show_label=True, elem_classes="tib-out")
                seed_used = gr.Markdown()

        detect_payload = gr.Textbox(elem_id="tib-detect-payload", elem_classes="tib-hidden")
        detect_result  = gr.Textbox(elem_id="tib-detect-result", elem_classes="tib-hidden")
        detect_trigger = gr.Button("detect", elem_id="tib-detect-trigger", elem_classes="tib-hidden")

        with gr.Accordion("Generation settings", open=False):
            with gr.Row():
                steps = gr.Slider(1, 100, value=DEFAULT_STEPS, step=1, label="Inference steps")
                num_images = gr.Slider(1, 4, value=1, step=1, label="Images per run")
            with gr.Row():
                height = gr.Slider(256, 1536, value=DEFAULT_HEIGHT, step=16, label="Height")
                width = gr.Slider(256, 1536, value=DEFAULT_WIDTH, step=16, label="Width")
        guidance = gr.Number(value=DEFAULT_GUIDANCE, elem_classes="tib-hidden")
        true_cfg = gr.Number(value=DEFAULT_TRUE_CFG, elem_classes="tib-hidden")
        lora_scale = gr.Number(value=DEFAULT_SCALE, elem_classes="tib-hidden")

        grab.click(None, None, [image_u, src_u, tgt_u, status_u], js=GRAB_JS).then(
            grab_show, [status_u, src_u, tgt_u], [src_prev, tgt_prev])
        detect_trigger.click(detect_objects, [detect_payload], [detect_result]).then(
            None, [detect_result], None, js=DETECT_RESULT_JS)
        demo.load(None, None, None, js=DETECT_BRIDGE_JS)
        gen.click(generate,
                    [image_u, src_u, tgt_u, steps, guidance, true_cfg, lora_scale,
                    randomize, height, width, num_images],
                    [out, seed_used])
    return demo

try:
    STATE["pipe"] = load_pipe_cpu()
    print("[tib-spaces] pipeline ready on CPU (moves to GPU per request).", flush=True)
except Exception:
    import traceback; traceback.print_exc()

demo = build_demo()

if __name__ == "__main__":
    demo.launch(theme=THEME, css=CSS, ssr_mode=False,
                favicon_path=str(HERE / "assets" / "favicon.svg"),share=True)