File size: 27,616 Bytes
d2c0ac9
65be679
d2c0ac9
 
 
 
 
50e5fdf
 
 
 
 
65be679
 
 
 
 
2630de4
c346860
65be679
c346860
 
 
65be679
 
c346860
 
 
 
65be679
 
 
 
 
 
 
 
 
 
2630de4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c573ff7
65be679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c346860
 
 
 
65be679
c346860
 
 
 
 
d6f508d
c346860
 
 
65be679
c346860
 
 
 
 
 
 
 
 
 
65be679
 
 
 
 
 
c346860
65be679
c346860
 
 
 
 
 
 
 
 
 
 
 
65be679
c346860
 
 
 
 
 
 
d6f508d
c346860
 
 
65be679
c346860
 
 
 
 
 
 
 
 
 
 
 
65be679
 
 
 
 
 
c346860
65be679
c346860
 
 
 
 
 
 
 
 
 
 
 
d2c0ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50e5fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65be679
 
 
 
4e88db1
 
 
 
 
 
 
 
 
d2c0ac9
4e88db1
 
d2c0ac9
4e88db1
 
 
 
 
 
 
 
 
 
 
 
d2c0ac9
4e88db1
 
d2c0ac9
4e88db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65be679
 
 
 
 
 
d2c0ac9
65be679
 
d2c0ac9
 
65be679
 
50e5fdf
c573ff7
50e5fdf
 
 
d2c0ac9
 
 
 
 
50e5fdf
65be679
d2c0ac9
 
 
 
50e5fdf
65be679
c573ff7
 
d2c0ac9
 
 
 
c573ff7
65be679
 
 
 
 
 
 
 
d2c0ac9
 
50e5fdf
 
65be679
 
 
50e5fdf
 
 
 
 
 
65be679
 
 
50e5fdf
65be679
50e5fdf
 
 
 
 
 
 
65be679
 
 
 
 
c346860
 
65be679
d2c0ac9
4e88db1
 
 
d2c0ac9
65be679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f914b10
 
 
 
 
 
 
c573ff7
 
 
f914b10
c0e5343
 
f914b10
c0e5343
 
d2c0ac9
f914b10
 
 
c0e5343
 
 
 
 
 
 
 
d2c0ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
280d066
d2c0ac9
e07dda8
 
 
f914b10
 
50e5fdf
f914b10
 
 
 
 
c573ff7
f914b10
f06b3ef
 
 
 
 
 
 
65be679
 
f06b3ef
 
 
65be679
 
d2c0ac9
 
 
c573ff7
 
f06b3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2c0ac9
 
 
 
 
 
 
c573ff7
 
 
 
d2c0ac9
c573ff7
 
 
 
 
d2c0ac9
c573ff7
 
 
 
65be679
4e88db1
65be679
4e88db1
 
 
 
 
 
 
 
c346860
d2c0ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f914b10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c346860
4e88db1
c346860
4e88db1
 
 
 
65be679
4e88db1
 
c346860
 
 
d2c0ac9
 
 
 
c346860
d2c0ac9
65be679
c346860
 
4e88db1
50e5fdf
4e88db1
 
 
 
d2c0ac9
4e88db1
 
 
d6f508d
c573ff7
50e5fdf
280d066
50e5fdf
280d066
 
 
 
c573ff7
280d066
c573ff7
65be679
50e5fdf
 
 
 
65be679
 
 
d2c0ac9
50e5fdf
c573ff7
65be679
c346860
65be679
 
 
 
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
"""MOSS-VL-Instruct Space — multi-turn media chat demo.

Full-width chat log with a chat/raw toggle; the input bar sits directly under
it: attach button INSIDE the input area (right side), pending media thumbnails
in a row just above the text (click a thumbnail to open the full-resolution
file), round send button beside the bar. Each turn sends whatever is staged:
media + text, media alone (default prompt), or text alone (no images sent).
TRUE MULTI-TURN: the model sees the whole conversation — earlier turns and
their media — via its offline_batch_generate session-state API (the state is
a plain message list held in gr.State and re-encoded on every stateless
ZeroGPU call). The 🗑 button resets the conversation. analyze_image /
analyze_video / analyze_text remain exposed as single-turn API/MCP tools.

MOCK mode (MOSS_DEMO_MOCK=1): no torch / spaces imports; canned responses for
local UI work.
"""

import inspect
import os
import time

os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")

MOCK = os.getenv("MOSS_DEMO_MOCK") == "1"

import gradio as gr

MODEL_ID = "OpenMOSS-Team/MOSS-VL-Instruct-0708"

if not MOCK:
    import spaces
    import torch
    from transformers import AutoModelForCausalLM, AutoProcessor

    processor = AutoProcessor.from_pretrained(
        MODEL_ID,
        trust_remote_code=True,
        frame_extract_num_threads=1,
    )

    # transformers 4.5x fast image processors pass `interpolation=` into
    # _preprocess, but this model's remote-code MossVLImageProcessorFast still
    # names the parameter `resample` (same torchvision InterpolationMode type,
    # only the name is stale) -> TypeError on every image request. Bridge the
    # rename here; this no-ops once the model repo updates the signature.
    _ip_cls = type(processor.image_processor)
    if "resample" in inspect.signature(_ip_cls._preprocess).parameters:
        _orig_preprocess = _ip_cls._preprocess

        def _compat_preprocess(self, images, *args, **kwargs):
            if "interpolation" in kwargs and "resample" not in kwargs:
                kwargs["resample"] = kwargs.pop("interpolation")
            return _orig_preprocess(self, images, *args, **kwargs)

        _ip_cls._preprocess = _compat_preprocess

    model = AutoModelForCausalLM.from_pretrained(
        MODEL_ID,
        trust_remote_code=True,
        torch_dtype=torch.bfloat16,
        attn_implementation="sdpa",
    ).to("cuda")
else:
    class _MockSpaces:
        """Effect-free stand-in for the spaces module in MOCK mode."""

        @staticmethod
        def GPU(*args, **kwargs):
            if args and callable(args[0]):
                return args[0]
            return lambda fn: fn

    spaces = _MockSpaces()


@spaces.GPU(duration=120)
def analyze_image(
    image: str,
    prompt: str,
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    do_sample: bool,
) -> str:
    """Analyze an image with a text prompt.

    Args:
        image: Path of the input image to analyze.
        prompt: Text instruction or question about the image.
        max_new_tokens: Maximum number of tokens to generate.
        temperature: Sampling temperature (1.0 = greedy when do_sample=False).
        top_p: Nucleus sampling probability threshold.
        do_sample: Whether to use sampling instead of greedy decoding.
    """
    if image is None:
        return "Please upload an image."
    if not prompt.strip():
        prompt = "Describe this image in detail."
    if MOCK:
        time.sleep(1.2)
        return (
            f"[MOCK] Scripted image analysis for {os.path.basename(image)} — prompt: {prompt!r}.\n\n"
            + "This is a long mock paragraph to exercise scrolling in the response view. " * 12
        )

    return model.offline_image_generate(
        processor,
        prompt=prompt,
        image=image,
        max_new_tokens=int(max_new_tokens),
        temperature=float(temperature),
        top_p=float(top_p),
        do_sample=do_sample,
    )


@spaces.GPU(duration=180)
def analyze_video(
    video: str,
    prompt: str,
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    video_fps: float,
    max_frames: int,
    do_sample: bool,
) -> str:
    """Analyze a video with a text prompt.

    Args:
        video: Path of the input video file to analyze.
        prompt: Text instruction or question about the video.
        max_new_tokens: Maximum number of tokens to generate.
        temperature: Sampling temperature (1.0 = greedy when do_sample=False).
        top_p: Nucleus sampling probability threshold.
        video_fps: Frames per second to sample from the video.
        max_frames: Maximum number of frames to extract.
        do_sample: Whether to use sampling instead of greedy decoding.
    """
    if video is None:
        return "Please upload a video."
    if not prompt.strip():
        prompt = "Describe this video in detail."
    if MOCK:
        time.sleep(1.5)
        return (
            f"[MOCK] Scripted video analysis for {os.path.basename(video)} — prompt: {prompt!r}.\n\n"
            + "This is a long mock paragraph to exercise scrolling in the response view. " * 12
        )

    return model.offline_video_generate(
        processor,
        prompt=prompt,
        video=video,
        max_new_tokens=int(max_new_tokens),
        temperature=float(temperature),
        top_p=float(top_p),
        video_fps=float(video_fps),
        max_frames=int(max_frames),
        do_sample=do_sample,
    )


@spaces.GPU(duration=90)
def analyze_text(
    prompt: str,
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    do_sample: bool,
) -> str:
    """Answer a text-only prompt (no media attached).

    Args:
        prompt: The text instruction or question.
        max_new_tokens: Maximum number of tokens to generate.
        temperature: Sampling temperature (1.0 = greedy when do_sample=False).
        top_p: Nucleus sampling probability threshold.
        do_sample: Whether to use sampling instead of greedy decoding.
    """
    if not prompt.strip():
        return "Please enter a prompt."
    if MOCK:
        time.sleep(0.8)
        return f"[MOCK] Scripted text answer — prompt: {prompt!r}."

    query = {
        "prompt": prompt,
        "images": [],
        "videos": [],
        "generate_kwargs": {
            "max_new_tokens": int(max_new_tokens),
            "temperature": float(temperature),
            "top_p": float(top_p),
            "repetition_penalty": 1.0,
            "do_sample": bool(do_sample),
        },
    }
    return model._offline_generate_one_with_processor_overrides(processor, query)


@spaces.GPU(duration=180)
def chat_generate(session: list, prompt: str, images: list, videos: list,
                  max_new_tokens: int, temperature: float, top_p: float,
                  do_sample: bool, video_fps: float, max_frames: int) -> dict:
    """One multi-turn chat step: prior session messages + this turn -> reply.

    Built on the model's offline_batch_generate session API. `session` is a
    plain list of message dicts (media referenced by file path), so it
    round-trips through gr.State across stateless ZeroGPU calls; every turn
    re-encodes the full context including earlier media.

    Args:
        session: Message history from the previous call's "session" output ([] to start).
        prompt: This turn's text instruction (may be empty when media is attached).
        images: Image file paths attached to this turn (usually 0 or 1).
        videos: Video file paths attached to this turn (usually 0 or 1).
        max_new_tokens: Maximum number of tokens to generate.
        temperature: Sampling temperature (1.0 = greedy when do_sample=False).
        top_p: Nucleus sampling probability threshold.
        do_sample: Whether to use sampling instead of greedy decoding.
        video_fps: Frames per second to sample from attached videos.
        max_frames: Maximum frames to extract from attached videos.

    Returns:
        {"text": assistant reply, "session": updated message history to pass next turn}.
    """
    session = list(session or [])
    if MOCK:
        time.sleep(1.0)
        n_prev = sum(1 for m in session if isinstance(m, dict) and m.get("role") == "assistant")
        media = (list(images or []) + list(videos or []))
        media_note = f" with {os.path.basename(media[0])}" if media else ""
        text = (f"[MOCK] Turn {n_prev + 1} reply{media_note} — prompt: {prompt!r}. "
                f"History carried: {len(session)} messages.")
        content = [{"type": "image", "image": m} for m in (images or [])]
        content += [{"type": "video", "video": m} for m in (videos or [])]
        content.append({"type": "text", "text": prompt})
        return {"text": text, "session": session + [
            {"role": "user", "content": content},
            {"role": "assistant", "content": text},
        ]}

    query = {
        "prompt": prompt,
        "images": list(images or []),
        "videos": list(videos or []),
        "generate_kwargs": {
            "max_new_tokens": int(max_new_tokens),
            "temperature": float(temperature),
            "top_p": float(top_p),
            "repetition_penalty": 1.0,
            "do_sample": bool(do_sample),
        },
    }
    if videos:
        query["media_kwargs"] = {"video_fps": float(video_fps), "max_frames": int(max_frames)}
    out = model.offline_batch_generate(processor, [query], session_states=[session])
    return {"text": out["results"][0]["text"], "session": out["session_states"][0]}


# --- UI glue ---

IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}

MOCK_BADGE = "\n\n`MOCK mode — scripted model responses`"

# UI language packs, same switcher pattern as the live demo: radio choices are
# (label, value) pairs so switching language never fires change events.
LANGS = {
    "en": {
        "btn": "中文",
        "title": "# MOSS-VL-Instruct-0708\n\n"
                 "An 11B multimodal vision-language model for image and video understanding. "
                 "Attach an image or video (or none) and ask anything, turn by turn.",
        "view_chat": "💬 Chat",
        "view_raw": "{ } Raw",
        "ph": "Ask anything — attach an image/video for this turn, or none…",
        "adv": "Advanced Settings",
        "max_tokens": "Max New Tokens",
        "temperature": "Temperature",
        "top_p": "Top-p",
        "do_sample": "Do Sample",
        "fps": "Video FPS (video only)",
        "max_frames": "Max Frames (video only)",
    },
    "zh": {
        "btn": "English",
        "title": "# MOSS-VL-Instruct-0708\n\n"
                 "110 亿参数的多模态视觉语言模型,支持图片与视频理解。"
                 "每一轮可附带图片/视频(或不带媒体),随意提问。",
        "view_chat": "💬 对话",
        "view_raw": "{ } 原始",
        "ph": "随意提问——本轮可附带图片/视频,也可不带…",
        "adv": "高级设置",
        "max_tokens": "最大生成长度",
        "temperature": "温度",
        "top_p": "Top-p",
        "do_sample": "启用采样",
        "fps": "视频采样帧率(仅视频)",
        "max_frames": "最大帧数(仅视频)",
    },
}


def switch_lang(lang):
    lang = "zh" if lang == "en" else "en"
    L = LANGS[lang]
    return (
        lang,
        gr.update(value=L["btn"]),
        L["title"] + (MOCK_BADGE if MOCK else ""),
        gr.update(choices=[(L["view_chat"], "chat"), (L["view_raw"], "raw")]),
        gr.update(placeholder=L["ph"]),
        gr.update(label=L["adv"]),
        gr.update(label=L["max_tokens"]),
        gr.update(label=L["temperature"]),
        gr.update(label=L["top_p"]),
        gr.update(label=L["do_sample"]),
        gr.update(label=L["fps"]),
        gr.update(label=L["max_frames"]),
    )


def classify_media(path):
    return "image" if os.path.splitext(path)[1].lower() in IMAGE_EXTS else "video"


def _file_path(value):
    """Normalize a file entry (str | dict | FileData) to a path or None."""
    if isinstance(value, dict):
        value = value.get("path") or value.get("name")
    path = getattr(value, "path", value)
    return path or None


def run_analyze(msg, history, events, session,
                max_new_tokens, temperature, top_p, do_sample, video_fps, max_frames):
    """One MULTI-TURN chat turn: send whatever is staged — media + text, media
    alone (default prompt), or text alone (no images sent). The model sees the
    whole session (earlier turns and their media) via the session-state API."""
    msg = msg or {}
    text = (msg.get("text") or "").strip()
    files = [p for p in (_file_path(f) for f in (msg.get("files") or [])) if p]
    if not text and not files:
        gr.Warning("Type a question or attach an image/video · 请输入问题或添加图片/视频")
        yield (gr.skip(),) * 4
        return

    media = files[0] if files else None
    kind = classify_media(media) if media else "text"
    prompt = text or f"Describe this {kind} in detail."
    session = list(session or [])

    history = list(history or [])
    events = list(events or [])
    if media:
        history.append({"role": "user", "content": {"path": media}})
    if text:
        history.append({"role": "user", "content": text})
    history.append({"role": "assistant", "content": "⏳ Analyzing · 分析中…"})
    params = {
        "max_new_tokens": int(max_new_tokens),
        "temperature": float(temperature),
        "top_p": float(top_p),
        "do_sample": bool(do_sample),
    }
    if kind == "video":
        params.update(video_fps=float(video_fps), max_frames=int(max_frames))
    events.append({"event": "request", "kind": kind,
                   "media": os.path.basename(media) if media else None,
                   "prompt": prompt, "session_msgs": len(session), "params": params})
    yield history, events, {"text": "", "files": []}, gr.skip()

    t0 = time.monotonic()
    try:
        out = chat_generate(
            session, prompt,
            [media] if kind == "image" else [],
            [media] if kind == "video" else [],
            max_new_tokens, temperature, top_p, do_sample, video_fps, max_frames,
        )
    except Exception as exc:
        history[-1] = {"role": "assistant", "content": f"⚠️ {type(exc).__name__}: {exc}"}
        events.append({"event": "error", "message": f"{type(exc).__name__}: {exc}"})
        yield history, events, gr.skip(), gr.skip()
        return
    history[-1] = {"role": "assistant", "content": out["text"]}
    events.append({"event": "response", "elapsed_s": round(time.monotonic() - t0, 2), "text": out["text"]})
    yield history, events, gr.skip(), out["session"]


def clear_chat():
    return [], [], {"text": "", "files": []}, []


def toggle_view(choice):
    chat = choice == "chat"
    return gr.update(visible=chat), gr.update(visible=not chat)


CSS = """
#col-container { max-width: 1250px; margin: 0 auto; --console-h: 46px; --stage-h: 640px; }
#links-row { align-items: center; }
#links-md { flex-grow: 1; }
#lang-btn { flex-grow: 0 !important; min-width: 84px; }
/* full-width chat stage with a fixed height — content scrolls inside */
#message-stage {
  flex-grow: 0 !important; height: var(--stage-h); min-height: var(--stage-h); max-height: var(--stage-h);
  display: flex; flex-direction: column;
}
#message-stage > *, #message-stage .styler {
  flex-grow: 1 !important; display: flex; flex-direction: column; min-height: 0;
}
#chatbot, #raw-json {
  flex-grow: 1 !important; min-height: 0 !important; height: auto !important; max-height: none !important;
}
/* segmented chat/raw toggle: two half-width buttons, centered text, no radio
   dot — black = off, accent yellow = on (same as the live demo) */
#view-toggle { flex-grow: 0 !important; flex-shrink: 0 !important; min-height: fit-content; }
#view-toggle .wrap { display: flex; flex-direction: row; gap: 0; width: 100%; }
#view-toggle label {
  flex: 1 1 50%; justify-content: center; text-align: center; margin: 0;
  padding: var(--spacing-sm) 0; cursor: pointer; border-radius: 0;
  background: #141414; color: #9aa0a6; border: 1px solid var(--border-color-primary);
  transition: background 0.15s, color 0.15s;
}
#view-toggle label:first-child { border-radius: var(--radius-md) 0 0 var(--radius-md); }
#view-toggle label:last-child { border-radius: 0 var(--radius-md) var(--radius-md) 0; }
#view-toggle label.selected { background: var(--color-accent); color: #111; font-weight: 600; }
#view-toggle input[type="radio"] { display: none; }
/* input line INSIDE the message block: one divider line separates it from
   the chat — no second box, no gap */
#session-console {
  flex-grow: 0 !important; align-items: center; gap: 2px; margin: 0;
  border-top: 1px solid var(--border-color-primary);
  padding: 4px 8px; min-height: var(--console-h);
}
#session-console .form {
  flex: 1 1 auto; border: none !important; background: transparent !important; box-shadow: none !important;
}
/* the multimodal input renders bare — the surrounding block provides the box */
#prompt-box { background: transparent !important; border: none !important; box-shadow: none !important; }
#prompt-box .full-container { padding: 0 !important; }
/* the floating pending strip sits above the input line — nothing may clip it */
#prompt-box, #prompt-box .full-container, #prompt-box .input-container,
#session-console, #session-console .form { overflow: visible !important; }
#prompt-box .input-wrapper {
  position: relative; min-height: 38px;
  border: none; border-radius: 0; background: transparent;
  padding: 0 var(--spacing-xs);
}
/* pending media live in a floating strip at the BOTTOM OF THE MESSAGE BLOCK
   (just above the input bar), not inside the bar */
#prompt-box .thumbnails {
  position: absolute; bottom: calc(100% + 8px); left: 0; margin: 0;
  padding: 5px 8px; z-index: 30; width: auto; max-width: 100%;
  background: rgba(20, 20, 20, 0.55); backdrop-filter: blur(3px);
  border-radius: var(--radius-md);
}
#prompt-box .input-row { align-items: center; }
#prompt-box .input-row textarea {
  order: 1; padding: 6px 8px; line-height: 20px; resize: none;
  max-height: 34px; overflow-y: auto; scrollbar-width: none;
}
#prompt-box .input-row textarea::-webkit-scrollbar { display: none; width: 0; }
#prompt-box .input-row button { order: 2; }
/* pending thumbnails: whole tile opens the full-res file; the delete control
   shrinks to a corner badge so it doesn't swallow the click */
#prompt-box .thumbnails img { cursor: zoom-in; }
#prompt-box .thumbnail-wrapper .delete-button {
  inset: auto; top: -4px; right: -4px; width: 18px; height: 18px;
  border-radius: 50%; opacity: 1; padding: 0;
}
#prompt-box .thumbnail-wrapper .delete-button svg { width: 10px; height: 10px; }
/* Citrus dark mode leaves example chips near-black on dark background —
   follow the theme's body text color (white on black / black on white) */
.dark .gallery-item { color: var(--body-text-color) !important; }
/* send + clear-chat: flat line-form icon buttons, same look as the attach
   icon inside the input line */
#send-btn, #clear-chat-btn {
  width: 36px; min-width: 36px !important;
  height: 36px !important; min-height: 36px !important;
  border-radius: var(--radius-md); padding: 0; font-size: 1rem; flex-grow: 0 !important;
  background: transparent !important; border: none !important; box-shadow: none !important;
  color: var(--body-text-color) !important;
}
#send-btn:hover, #clear-chat-btn:hover { background: var(--button-secondary-background-fill) !important; }
/* scrollbars: no rail is drawn; the thumb is invisible until the pointer
   enters the rail strip at the scrollable edge (.sb-hot, set by the page
   script) or grabs the thumb itself. scrollbar-color drives modern
   Chrome/Firefox; the ::-webkit rules cover older WebKit. */
* { scrollbar-width: thin; scrollbar-color: transparent transparent; }
.sb-hot { scrollbar-color: rgba(128, 128, 128, 0.55) transparent !important; }
::-webkit-scrollbar { width: 10px; height: 10px; background: transparent !important; }
::-webkit-scrollbar-track, ::-webkit-scrollbar-corner { background: transparent !important; }
::-webkit-scrollbar-button { display: none !important; width: 0 !important; height: 0 !important; }
::-webkit-scrollbar-thumb { background: transparent; border-radius: 5px; }
.sb-hot::-webkit-scrollbar-thumb,
::-webkit-scrollbar-thumb:hover, ::-webkit-scrollbar-thumb:active { background: rgba(128, 128, 128, 0.6) !important; }
"""

# Page JS: (1) keep the raw event view pinned to the bottom while it grows,
# unless the user has scrolled up to read; (2) clicking a pending thumbnail
# opens the full-resolution file (the corner ✕ still deletes).
PAGE_JS = """
() => {
  if (!window.__mossSbHot) {
    window.__mossSbHot = true;
    let hot = null;
    document.addEventListener('mousemove', (e) => {
      let el = e.target instanceof Element ? e.target : null;
      let found = null;
      while (el && el !== document.documentElement) {
        const vs = el.scrollHeight > el.clientHeight + 4;
        const hs = el.scrollWidth > el.clientWidth + 4;
        const cs = vs || hs ? getComputedStyle(el) : null;
        if (cs && /(auto|scroll)/.test(cs.overflowY + cs.overflowX)) {
          const r = el.getBoundingClientRect();
          if ((vs && r.right - e.clientX <= 18) || (hs && r.bottom - e.clientY <= 18)) found = el;
          break;
        }
        el = el.parentElement;
      }
      if (found !== hot) {
        if (hot) hot.classList.remove('sb-hot');
        if (found) found.classList.add('sb-hot');
        hot = found;
      }
    }, {passive: true});
  }
  if (window.__mossInstructInit) return;
  window.__mossInstructInit = true;
  document.addEventListener('click', (e) => {
    if (e.target.closest('button')) return;
    const img = e.target.closest('#prompt-box .thumbnails img');
    if (img && img.src) window.open(img.src, '_blank');
  }, true);
  const timer = setInterval(() => {
    const rawRoot = document.querySelector('#raw-json');
    if (!rawRoot) return;
    clearInterval(timer);
    const obs = new MutationObserver(() => {
      if (rawRoot.offsetParent === null) return;
      const sc = [...rawRoot.querySelectorAll('*')].find(e => e.scrollHeight > e.clientHeight + 8);
      if (!sc) return;
      if (sc.scrollHeight - sc.scrollTop - sc.clientHeight < 240) sc.scrollTop = sc.scrollHeight;
    });
    obs.observe(rawRoot, {childList: true, subtree: true, characterData: true});
  }, 500);
}
"""

with gr.Blocks(title="MOSS-VL-Instruct Demo") as demo:
    EN = LANGS["en"]
    with gr.Column(elem_id="col-container"):
        title_md = gr.Markdown(EN["title"] + (MOCK_BADGE if MOCK else ""))
        with gr.Row(elem_id="links-row"):
            gr.Markdown(
                "[Model Card](https://huggingface.co/OpenMOSS-Team/MOSS-VL-Instruct-0708) | "
                "[GitHub](https://github.com/OpenMOSS/MOSS-VL)",
                elem_id="links-md",
            )
            lang_btn = gr.Button(EN["btn"], size="sm", scale=0, elem_id="lang-btn")

        with gr.Group(elem_id="message-stage"):
            view_toggle = gr.Radio(
                [(EN["view_chat"], "chat"), (EN["view_raw"], "raw")],
                value="chat",
                show_label=False,
                container=False,
                elem_id="view-toggle",
            )
            chatbot = gr.Chatbot(
                height=560,
                show_label=False,
                buttons=["copy"],
                autoscroll=True,
                elem_id="chatbot",
            )
            raw_json = gr.JSON(
                value=[], show_label=False, visible=False, height=560, elem_id="raw-json"
            )
            # input line lives INSIDE the same block, separated from the chat
            # by a single divider line; send/clear are flat line-form icons
            # matching the attach icon
            with gr.Row(elem_id="session-console"):
                prompt_box = gr.MultimodalTextbox(
                    scale=1,
                    show_label=False,
                    container=False,
                    file_types=["image", "video"],
                    file_count="single",
                    submit_btn=False,
                    placeholder=EN["ph"],
                    elem_id="prompt-box",
                )
                send_btn = gr.Button("➤", variant="secondary", scale=0, elem_id="send-btn")
                clear_chat_btn = gr.Button("🗑︎", variant="secondary", scale=0, elem_id="clear-chat-btn")

        with gr.Accordion(EN["adv"], open=False) as adv_acc:
            with gr.Row():
                max_new_tokens = gr.Slider(64, 2048, value=512, step=64, label=EN["max_tokens"])
                temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label=EN["temperature"])
                top_p = gr.Slider(0.1, 1.0, value=1.0, step=0.05, label=EN["top_p"])
                do_sample = gr.Checkbox(label=EN["do_sample"], value=False)
            with gr.Row():
                video_fps = gr.Slider(0.5, 4.0, value=1.0, step=0.5, label=EN["fps"])
                max_frames = gr.Slider(8, 256, value=64, step=8, label=EN["max_frames"])

        gr.Examples(
            examples=[
                [{"text": "Extract the store name, waiter name, bill number, number of people, items purchased with their quantities and amounts, total amount, and print time from this receipt. Output in JSON format.", "files": ["example_bill.png"]}],
                [{"text": "Describe this image in detail.", "files": ["astronaut.jpg"]}],
                [{"text": "What species of bird is this? Describe its appearance and habitat.", "files": ["bird_kingfisher.jpg"]}],
                [{"text": "Describe what happens in this video.", "files": ["example_video.mp4"]}],
            ],
            inputs=[prompt_box],
            label="Examples",
        )

    ui_lang = gr.State("en")
    chat_session = gr.State([])  # model-side multi-turn message history

    lang_btn.click(
        switch_lang,
        [ui_lang],
        [ui_lang, lang_btn, title_md, view_toggle, prompt_box,
         adv_acc, max_new_tokens, temperature, top_p, do_sample, video_fps, max_frames],
        api_visibility="private",
    )
    view_toggle.change(toggle_view, [view_toggle], [chatbot, raw_json], api_visibility="private")

    analyze_inputs = [prompt_box, chatbot, raw_json, chat_session,
                      max_new_tokens, temperature, top_p, do_sample, video_fps, max_frames]
    analyze_outputs = [chatbot, raw_json, prompt_box, chat_session]
    send_btn.click(
        run_analyze, analyze_inputs, analyze_outputs,
        api_visibility="private", show_progress="hidden",
    )
    prompt_box.submit(
        run_analyze, analyze_inputs, analyze_outputs,
        api_visibility="private", show_progress="hidden",
    )
    clear_chat_btn.click(
        clear_chat, None, [chatbot, raw_json, prompt_box, chat_session],
        api_visibility="private",
    )

    gr.api(analyze_image, api_name="analyze_image")
    gr.api(analyze_video, api_name="analyze_video")
    gr.api(analyze_text, api_name="analyze_text")
    gr.api(chat_generate, api_name="chat")
    demo.load(None, None, None, js=PAGE_JS, api_visibility="private")


if __name__ == "__main__":
    # gradio 6.x: theme/css/mcp_server are launch() parameters
    demo.queue(max_size=32)
    demo.launch(theme=gr.themes.Citrus(), css=CSS, mcp_server=True)