File size: 24,346 Bytes
f9a6349
 
 
 
 
 
 
 
 
f71f431
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
888f9e4
 
f9a6349
 
 
 
 
 
 
 
 
17f21ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
f9a6349
 
 
 
 
 
 
 
17f21ca
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
 
 
 
 
 
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
f9a6349
 
17f21ca
 
 
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17f21ca
f9a6349
17f21ca
 
 
f9a6349
17f21ca
 
 
f9a6349
17f21ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
17f21ca
f9a6349
17f21ca
f9a6349
 
 
 
 
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
from pathlib import Path
from collections.abc import Mapping, Sequence
from functools import lru_cache
import inspect
import shutil
import tempfile
import os
import sys

# Add src/ to sys.path so LASER, video-sam2, GroundingDINO are importable
current_dir = Path(__file__).resolve().parent
src_dir = current_dir / "src"
if src_dir.is_dir() and str(src_dir) not in sys.path:
    sys.path.insert(0, str(src_dir))

import spaces  # <-- ZeroGPU integration
import gradio as gr
import torch
from transformers import pipeline  # not strictly necessary, but fine


# -----------------------------
# Environment / diagnostics
# -----------------------------
os.environ["GRADIO_TEMP_DIR"] = str(Path(__file__).parent / "gradio_temp")
os.environ["OPENAI_API_KEY"] = "test"
os.environ["OMP_NUM_THREADS"] = "4"

print("All imports finished")
print(f"Python version: {sys.version}")
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
print(f"cuDNN version: {torch.backends.cudnn.version()}")
print(f"Number of GPUs: {torch.cuda.device_count()}")

if torch.cuda.is_available():
    for i in range(torch.cuda.device_count()):
        print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
        print(
            f"  Memory: {torch.cuda.get_device_properties(i).total_memory / 1e9:.2f} GB"
        )

torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
os.environ["TORCH_DTYPE"] = "float32"
torch.set_default_dtype(torch.float32)

current_dir = Path(__file__).resolve().parent
# For Spaces, assume checkpoints live alongside app.py or in a "checkpoints" subdir.
# If you keep them next to app.py locally, this still works.
# NOTE: SAM2 config uses Hydra, so we use just the filename (it searches in sam2/configs/)
sam_config_path = "sam2_hiera_t.yaml"  # Hydra will find this in sam2/configs/
sam_checkpoint_path = str(current_dir / "sam2_hiera_tiny.pt")
gd_config_path = str(current_dir / "GroundingDINO_SwinT_OGC.py")
gd_checkpoint_path = str(current_dir / "groundingdino_swint_ogc.pth")
visualization_dir = str(current_dir / "outputs")
print(
    f"Setting up paths: {sam_config_path}, {sam_checkpoint_path}, {gd_config_path}, {gd_checkpoint_path}"
)


def format_summary(summary, binary_confidence_threshold=0.8):
    """
    Format the summary dictionary into a readable markdown string.
    Filters binary relations by confidence threshold.
    """
    if not summary or not isinstance(summary, dict):
        return "# Detection Summary\n\nNo events detected or processing in progress..."

    output_lines = ["# Detection Summary\n"]
    has_content = False

    # Categorical keywords
    if "categorical_keywords" in summary and summary["categorical_keywords"]:
        output_lines.append("## Categorical Keywords\n")
        cate = summary["categorical_keywords"]
        if isinstance(cate, dict) and cate:
            has_content = True
            for kw, info in cate.items():
                output_lines.append(f"**{kw}**")
                if isinstance(info, dict):
                    for key, val in info.items():
                        output_lines.append(f"  - {key}: {val}")
                else:
                    output_lines.append(f"  - {info}")
                output_lines.append("")
        elif isinstance(cate, list) and cate:
            has_content = True
            for item in cate:
                output_lines.append(f"- {item}")
            output_lines.append("")

    # Unary keywords
    if "unary_keywords" in summary and summary["unary_keywords"]:
        output_lines.append("## Unary Keywords\n")
        unary = summary["unary_keywords"]
        if isinstance(unary, dict) and unary:
            has_content = True
            for kw, info in unary.items():
                output_lines.append(f"**{kw}**")
                if isinstance(info, dict):
                    for key, val in info.items():
                        output_lines.append(f"  - {key}: {val}")
                else:
                    output_lines.append(f"  - {info}")
                output_lines.append("")
        elif isinstance(unary, list) and unary:
            has_content = True
            for item in unary:
                output_lines.append(f"- {item}")
            output_lines.append("")

    # Binary keywords - show ALL binary relations for debugging
    print(f"DEBUG: Checking binary_keywords...")
    print(f"  'binary_keywords' in summary: {'binary_keywords' in summary}")
    if 'binary_keywords' in summary:
        print(f"  summary['binary_keywords'] truthy: {bool(summary['binary_keywords'])}")
        print(f"  summary['binary_keywords'] type: {type(summary['binary_keywords'])}")
        print(f"  summary['binary_keywords'] value: {summary['binary_keywords']}")

    if "binary_keywords" in summary and summary["binary_keywords"]:
        output_lines.append(f"## Binary Keywords\n")
        binary = summary["binary_keywords"]
        print(f"DEBUG: Processing binary keywords, type: {type(binary)}, length: {len(binary) if isinstance(binary, (dict, list)) else 'N/A'}")
        if isinstance(binary, dict) and binary:
            has_content = True
            # Show all binary relations, sorted by confidence
            binary_items = []
            for kw, info in binary.items():
                if isinstance(info, dict):
                    confidence = info.get("confidence", info.get("score", 0))
                    binary_items.append((kw, info, confidence))
                else:
                    binary_items.append((kw, info, 0))

            # Sort by confidence descending
            binary_items.sort(key=lambda x: x[2], reverse=True)

            high_conf_count = 0
            low_conf_count = 0

            # Show high confidence items first
            output_lines.append(f"### High Confidence (≥ {binary_confidence_threshold})\n")
            for kw, info, confidence in binary_items:
                if confidence >= binary_confidence_threshold:
                    high_conf_count += 1
                    if isinstance(info, dict):
                        output_lines.append(f"**{kw}** (confidence: {confidence:.2f})")
                        for key, val in info.items():
                            if key not in ["confidence", "score"]:
                                output_lines.append(f"  - {key}: {val}")
                    else:
                        output_lines.append(f"**{kw}**: {info}")
                    output_lines.append("")

            if high_conf_count == 0:
                output_lines.append(f"*No binary relations found with confidence ≥ {binary_confidence_threshold}*\n")

            # Show lower confidence items for debugging
            output_lines.append(f"### Lower Confidence (< {binary_confidence_threshold})\n")
            for kw, info, confidence in binary_items:
                if confidence < binary_confidence_threshold:
                    low_conf_count += 1
                    if isinstance(info, dict):
                        output_lines.append(f"**{kw}** (confidence: {confidence:.2f})")
                        for key, val in info.items():
                            if key not in ["confidence", "score"]:
                                output_lines.append(f"  - {key}: {val}")
                    else:
                        output_lines.append(f"**{kw}**: {info}")
                    output_lines.append("")

            if low_conf_count == 0:
                output_lines.append(f"*No binary relations found with confidence < {binary_confidence_threshold}*\n")

            output_lines.append(f"**Total binary relations detected: {len(binary_items)}**\n")
        elif isinstance(binary, list) and binary:
            has_content = True
            for item in binary:
                output_lines.append(f"- {item}")
            output_lines.append("")

    # Object pairs - show ALL object pair interactions for debugging
    print(f"DEBUG: Checking object_pairs...")
    print(f"  'object_pairs' in summary: {'object_pairs' in summary}")
    if 'object_pairs' in summary:
        print(f"  summary['object_pairs'] truthy: {bool(summary['object_pairs'])}")
        print(f"  summary['object_pairs'] type: {type(summary['object_pairs'])}")
        print(f"  summary['object_pairs'] value: {summary['object_pairs']}")

    if "object_pairs" in summary and summary["object_pairs"]:
        output_lines.append(f"## Object Pair Interactions\n")
        pairs = summary["object_pairs"]
        print(f"DEBUG: Processing object pairs, type: {type(pairs)}, length: {len(pairs) if isinstance(pairs, (dict, list)) else 'N/A'}")
        if isinstance(pairs, dict) and pairs:
            has_content = True
            # Show all object pairs, sorted by confidence
            pair_items = []
            for pair, info in pairs.items():
                if isinstance(info, dict):
                    confidence = info.get("confidence", info.get("score", 0))
                    pair_items.append((pair, info, confidence))
                else:
                    pair_items.append((pair, info, 0))

            # Sort by confidence descending
            pair_items.sort(key=lambda x: x[2], reverse=True)

            high_conf_count = 0
            low_conf_count = 0

            # Show high confidence items first
            output_lines.append(f"### High Confidence (≥ {binary_confidence_threshold})\n")
            for pair, info, confidence in pair_items:
                if confidence >= binary_confidence_threshold:
                    high_conf_count += 1
                    if isinstance(info, dict):
                        output_lines.append(f"**{pair}** (confidence: {confidence:.2f})")
                        for key, val in info.items():
                            if key not in ["confidence", "score"]:
                                output_lines.append(f"  - {key}: {val}")
                    else:
                        output_lines.append(f"**{pair}**: {info}")
                    output_lines.append("")

            if high_conf_count == 0:
                output_lines.append(f"*No object pairs found with confidence ≥ {binary_confidence_threshold}*\n")

            # Show lower confidence items for debugging
            output_lines.append(f"### Lower Confidence (< {binary_confidence_threshold})\n")
            for pair, info, confidence in pair_items:
                if confidence < binary_confidence_threshold:
                    low_conf_count += 1
                    if isinstance(info, dict):
                        output_lines.append(f"**{pair}** (confidence: {confidence:.2f})")
                        for key, val in info.items():
                            if key not in ["confidence", "score"]:
                                output_lines.append(f"  - {key}: {val}")
                    else:
                        output_lines.append(f"**{pair}**: {info}")
                    output_lines.append("")

            if low_conf_count == 0:
                output_lines.append(f"*No object pairs found with confidence < {binary_confidence_threshold}*\n")

            output_lines.append(f"**Total object pairs detected: {len(pair_items)}**\n")
        elif isinstance(pairs, list) and pairs:
            has_content = True
            for item in pairs:
                output_lines.append(f"- {item}")
            output_lines.append("")

    # If no content was added, show the raw summary for debugging
    if not has_content:
        output_lines.append("## Raw Summary Data\n")
        output_lines.append("```json")
        import json
        output_lines.append(json.dumps(summary, indent=2, default=str))
        output_lines.append("```")

    return "\n".join(output_lines)


@lru_cache(maxsize=1)
def _load_vine_pipeline():
    """
    Lazy-load and cache the Vine pipeline so we don't re-download/rebuild it on every request.
    """
    from vine_hf import VineConfig, VineModel, VinePipeline

    config = VineConfig(
        segmentation_method="grounding_dino_sam2",
        model_name="openai/clip-vit-base-patch32",
        use_hf_repo=True,
        model_repo="KevinX-Penn28/testing",
        box_threshold=0.35,
        text_threshold=0.25,
        target_fps=1,  # default 1 FPS
        topk_cate=5,
        white_alpha=0.3,
        visualization_dir=visualization_dir,
        visualize=True,
        debug_visualizations=False,
        device="cuda",
        categorical_pool="max",
    )
    model = VineModel(config)
    return VinePipeline(
        model=model,
        tokenizer=None,
        sam_config_path=sam_config_path,
        sam_checkpoint_path=sam_checkpoint_path,
        gd_config_path=gd_config_path,
        gd_checkpoint_path=gd_checkpoint_path,
        device="cuda",
        trust_remote_code=True,
    )


@spaces.GPU(duration=120)  # Up to ~5 minutes of H200 ZeroGPU time per call
def process_video(
    video_file,
    categorical_keywords,
    unary_keywords,
    binary_keywords,
    output_fps,
    box_threshold,
    text_threshold,
    binary_confidence_threshold,
):
    vine_pipe = _load_vine_pipeline()

    # Normalize incoming video input to a file path
    if isinstance(video_file, dict):
        video_file = (
            video_file.get("name")
            or video_file.get("filepath")
            or video_file.get("data")
        )
    if not isinstance(video_file, (str, Path)):
        raise ValueError(f"Unsupported video input type: {type(video_file)}")

    categorical_keywords = (
        [kw.strip() for kw in categorical_keywords.split(",")]
        if categorical_keywords
        else []
    )
    unary_keywords = (
        [kw.strip() for kw in unary_keywords.split(",")] if unary_keywords else []
    )
    binary_keywords = (
        [kw.strip() for kw in binary_keywords.split(",")] if binary_keywords else []
    )

    # Debug: Print what we're sending to the pipeline
    print("\n" + "=" * 80)
    print("INPUT TO VINE PIPELINE:")
    print(f"  categorical_keywords: {categorical_keywords}")
    print(f"  unary_keywords: {unary_keywords}")
    print(f"  binary_keywords: {binary_keywords}")
    print("=" * 80 + "\n")

    # Object pairs is now optional - empty list will auto-generate all pairs in vine_model.py
    object_pairs = []

    results = vine_pipe(
        inputs=video_file,
        categorical_keywords=categorical_keywords,
        unary_keywords=unary_keywords,
        binary_keywords=binary_keywords,
        object_pairs=object_pairs,
        segmentation_method="grounding_dino_sam2",
        return_top_k=5,
        include_visualizations=True,
        debug_visualizations=False,
        device="cuda",
        box_threshold=box_threshold,
        text_threshold=text_threshold,
        target_fps=output_fps,
        binary_confidence_threshold=binary_confidence_threshold,
    )

    # Debug: Print what the pipeline returned
    print("\n" + "=" * 80)
    print("PIPELINE RESULTS DEBUG:")
    print(f"  results type: {type(results)}")
    if isinstance(results, dict):
        print(f"  results keys: {list(results.keys())}")
    print("=" * 80 + "\n")

    vine_pipe.box_threshold = box_threshold
    vine_pipe.text_threshold = text_threshold
    vine_pipe.target_fps = output_fps

    if isinstance(results, Mapping):
        results_dict = results
    elif isinstance(results, Sequence) and results and isinstance(results[0], Mapping):
        results_dict = results[0]
    else:
        results_dict = {}

    visualizations = results_dict.get("visualizations") or {}
    vine = visualizations.get("vine") or {}
    all_vis = vine.get("all") or {}
    result_video_path = all_vis.get("video_path")
    if not result_video_path:
        candidates = sorted(
            Path(visualization_dir).rglob("*.mp4"),
            key=lambda p: p.stat().st_mtime,
            reverse=True,
        )
        result_video_path = str(candidates[0]) if candidates else None
    summary = results_dict.get("summary") or {}

    if result_video_path and os.path.exists(result_video_path):
        gradio_tmp = Path(
            os.environ.get("GRADIO_TEMP_DIR", tempfile.gettempdir())
        ) / "vine_outputs"
        gradio_tmp.mkdir(parents=True, exist_ok=True)
        dest_path = gradio_tmp / Path(result_video_path).name
        try:
            shutil.copyfile(result_video_path, dest_path)
            video_path_for_ui = str(dest_path)
        except Exception as e:
            print(f"Warning: failed to copy video to Gradio temp dir: {e}")
            video_path_for_ui = str(result_video_path)
    else:
        video_path_for_ui = None
        print(
            "Warning: annotated video not found or empty; check visualization settings."
        )

    # Debug: Print summary structure
    import json
    print("=" * 80)
    print("SUMMARY DEBUG OUTPUT:")
    print(f"Summary type: {type(summary)}")
    print(f"Summary keys: {summary.keys() if isinstance(summary, dict) else 'N/A'}")
    if isinstance(summary, dict):
        print("\nFULL SUMMARY JSON:")
        print(json.dumps(summary, indent=2, default=str))
        print("\n" + "=" * 80)

        # Check for any keys that might contain binary relation data
        print("\nLOOKING FOR BINARY RELATION DATA:")
        possible_keys = ['binary', 'binary_keywords', 'binary_relations', 'object_pairs',
                        'pairs', 'relations', 'interactions', 'pairwise']
        for pkey in possible_keys:
            if pkey in summary:
                print(f"  FOUND: '{pkey}' -> {summary[pkey]}")

        print("\nALL KEYS IN SUMMARY:")
        for key in summary.keys():
            print(f"\n{key}:")
            print(f"  Type: {type(summary[key])}")
            if isinstance(summary[key], dict):
                print(f"  Length: {len(summary[key])}")
                print(f"  Keys (first 10): {list(summary[key].keys())[:10]}")
                # Print all items for anything that might be binary relations
                if any(term in key.lower() for term in ['binary', 'pair', 'relation', 'interaction']):
                    print(f"  ALL ITEMS:")
                    for k, v in list(summary[key].items())[:20]:  # First 20 items
                        print(f"    {k}: {v}")
                else:
                    print(f"  Sample: {dict(list(summary[key].items())[:2])}")
            elif isinstance(summary[key], list):
                print(f"  Length: {len(summary[key])}")
                print(f"  Sample: {summary[key][:2]}")
    print("=" * 80)

    # Format summary as readable markdown text, filtering by confidence threshold
    formatted_summary = format_summary(summary, binary_confidence_threshold)
    return video_path_for_ui, formatted_summary


def _video_component(label: str, *, is_output: bool = False):
    """
    Build a Gradio Video component that is compatible with older Gradio versions
    (no `type`/`sources`/`format` kwargs) and newer ones when available.
    """
    kwargs = {"label": label}
    sig = inspect.signature(gr.Video.__init__)

    # Only set format for OUTPUT components
    if is_output and "format" in sig.parameters:
        kwargs["format"] = "mp4"

    if not is_output:
        if "type" in sig.parameters:
            kwargs["type"] = "filepath"
        if "sources" in sig.parameters:
            kwargs["sources"] = ["upload"]
        # Restrict to MP4 files only
        if "file_types" in sig.parameters:
            kwargs["file_types"] = [".mp4"]

    if is_output and "autoplay" in sig.parameters:
        kwargs["autoplay"] = True

    return gr.Video(**kwargs)


def _create_blocks():
    """
    Build a Blocks context that works across Gradio versions.
    """
    blocks_kwargs = {"title": "VINE Demo"}
    soft_theme = None

    if hasattr(gr, "themes") and hasattr(gr.themes, "Soft"):
        try:
            soft_theme = gr.themes.Soft()
        except Exception:
            soft_theme = None

    if "theme" in inspect.signature(gr.Blocks).parameters and soft_theme is not None:
        blocks_kwargs["theme"] = soft_theme

    return gr.Blocks(**blocks_kwargs)


# Create Gradio interface with two-column layout
with _create_blocks() as demo:
    gr.Markdown(
        """
        # 🎬 VINE: Video-based Interaction and Event Detection

        Upload an MP4 video and specify keywords to detect objects, actions, and interactions in your video.
        """
    )

    with gr.Row():
        # Left column: Inputs
        with gr.Column(scale=1):
            gr.Markdown("### Input Configuration")

            video_input = _video_component("Upload Video (MP4 only)", is_output=False)
            gr.Markdown("*Note: Only MP4 format is currently supported*")

            gr.Markdown("#### Detection Keywords")
            categorical_input = gr.Textbox(
                label="Categorical Keywords",
                placeholder="e.g., person, car, dog",
                value="person, car, dog",
                info="Objects to detect in the video (comma-separated)"
            )
            unary_input = gr.Textbox(
                label="Unary Keywords",
                placeholder="e.g., walking, running, standing",
                value="walking, running, standing",
                info="Single-object actions to detect (comma-separated)"
            )
            binary_input = gr.Textbox(
                label="Binary Keywords",
                placeholder="e.g., chasing, carrying",
                info="Object-to-object interactions to detect (comma-separated)"
            )

            gr.Markdown("#### Processing Settings")
            fps_input = gr.Number(
                label="Output FPS",
                value=1,
                info="Frames per second for processing (lower = faster)"
            )

            with gr.Accordion("Advanced Settings", open=False):
                box_threshold_input = gr.Slider(
                    label="Box Threshold",
                    minimum=0.1,
                    maximum=0.9,
                    value=0.35,
                    step=0.05,
                    info="Confidence threshold for object detection"
                )
                text_threshold_input = gr.Slider(
                    label="Text Threshold",
                    minimum=0.1,
                    maximum=0.9,
                    value=0.25,
                    step=0.05,
                    info="Confidence threshold for text-based detection"
                )
                binary_confidence_input = gr.Slider(
                    label="Binary Relation Confidence Threshold",
                    minimum=0.0,
                    maximum=1.0,
                    value=0.8,
                    step=0.05,
                    info="Minimum confidence to show binary relations and object pairs"
                )

            submit_btn = gr.Button("🚀 Process Video", variant="primary", size="lg")

        # Right column: Outputs
        with gr.Column(scale=1):
            gr.Markdown("### Results")

            video_output = _video_component("Annotated Video Output", is_output=True)

            gr.Markdown("### Detection Summary")
            summary_output = gr.Markdown(
                value="Results will appear here after processing...",
                elem_classes=["summary-output"]
            )

    gr.Markdown(
        """
        ---
        ### How to Use
        1. Upload an MP4 video file
        2. Specify the objects, actions, and interactions you want to detect
        3. Adjust processing settings if needed (including binary relation confidence threshold)
        4. Click "Process Video" to analyze

        The system will automatically detect all binary relations between detected objects
        and show only those with confidence above the threshold (default: 0.8).
        """
    )

    submit_btn.click(
        fn=process_video,
        inputs=[
            video_input,
            categorical_input,
            unary_input,
            binary_input,
            fps_input,
            box_threshold_input,
            text_threshold_input,
            binary_confidence_input,
        ],
        outputs=[video_output, summary_output],
    )

if __name__ == "__main__":
    print("Got to main")
    demo.launch(share=True, debug=True)