File size: 25,213 Bytes
76c0e18
 
 
 
 
 
 
a90bfd0
 
1421041
a90bfd0
 
 
1421041
a90bfd0
 
 
1421041
a90bfd0
 
 
 
 
 
 
beea86e
76c0e18
 
 
 
 
 
 
 
 
 
 
f78fc0d
 
 
76c0e18
 
 
 
 
f78fc0d
 
 
1421041
76c0e18
 
 
 
 
 
 
 
 
 
a90bfd0
1421041
a90bfd0
 
 
 
 
1421041
a90bfd0
beea86e
76c0e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32424e6
 
76c0e18
 
 
 
 
f78fc0d
76c0e18
 
1421041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76c0e18
1421041
76c0e18
1421041
 
76c0e18
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
1421041
76c0e18
 
 
1421041
76c0e18
1421041
76c0e18
1421041
 
76c0e18
 
1421041
76c0e18
1421041
 
76c0e18
 
1421041
76c0e18
 
1421041
 
 
76c0e18
 
 
1421041
76c0e18
1421041
76c0e18
 
406a0ba
 
f78fc0d
76c0e18
406a0ba
 
 
 
76c0e18
406a0ba
 
 
32424e6
 
406a0ba
76c0e18
406a0ba
 
76c0e18
1421041
 
76c0e18
1421041
76c0e18
 
1421041
 
 
76c0e18
 
 
1421041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76c0e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
 
 
 
 
 
f78fc0d
76c0e18
 
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
1421041
76c0e18
 
 
 
 
 
 
 
 
1421041
 
76c0e18
 
1421041
 
76c0e18
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
a90bfd0
 
beea86e
76c0e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1421041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76c0e18
f78fc0d
76c0e18
 
1421041
 
 
 
 
 
 
 
 
76c0e18
 
1421041
76c0e18
1421041
 
76c0e18
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
1421041
76c0e18
1421041
76c0e18
1421041
76c0e18
 
 
 
 
1421041
76c0e18
1421041
76c0e18
 
 
1421041
76c0e18
 
 
1421041
 
76c0e18
 
 
1421041
 
 
76c0e18
 
 
1421041
76c0e18
 
 
 
 
1421041
 
 
76c0e18
1421041
 
 
 
 
 
76c0e18
1421041
 
76c0e18
1421041
76c0e18
 
1421041
76c0e18
 
1421041
 
 
76c0e18
 
 
1421041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76c0e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
 
 
 
 
 
f78fc0d
76c0e18
 
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
1421041
76c0e18
 
 
 
 
 
 
 
 
1421041
 
76c0e18
 
1421041
 
76c0e18
 
 
 
 
 
 
 
 
 
1421041
 
 
a90bfd0
 
76c0e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1421041
76c0e18
 
 
 
f78fc0d
1421041
76c0e18
 
f78fc0d
1421041
 
76c0e18
f78fc0d
1421041
76c0e18
 
 
 
 
1421041
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
import gradio as gr
import pandas as pd


# Your leaderboard name
TITLE = """<h1 align="center" id="space-title">Physical AI Bench Leaderboard</h1>"""

# CSS to make the leaderboard full height
CSS = """
#predict_leaderboard, #transfer_leaderboard, #reason_leaderboard {
    height: auto !important;
    max-height: none !important;
}
#predict_leaderboard .wrap, #transfer_leaderboard .wrap, #reason_leaderboard .wrap {
    max-height: none !important;
    height: auto !important;
}
#predict_leaderboard .tbody, #transfer_leaderboard .tbody, #reason_leaderboard .tbody {
    max-height: none !important;
    height: auto !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
}
"""


# What does your leaderboard evaluate?
INTRODUCTION_TEXT = """
**Physical AI Bench (PAI-Bench)** is a comprehensive benchmark suite for evaluating physical AI generation and understanding across diverse scenarios including autonomous vehicles, robotics, industrial spaces, and everyday ego-centric environments.
"""

# Which evaluations are you running? how can people reproduce what you have?
LLM_BENCHMARKS_TEXT = """
## How it works

This leaderboard tracks model performance across three core dimensions:

- **🎨 Generation**: Evaluates world foundation models' ability to predict future states across 1,044 diverse physical scenarios
- **πŸ”„ Conditional Generation**: Focuses on world model generation with complex control signals, featuring 600 videos across robotic arm operations, autonomous driving, and ego-centric scenes
- **🧠 Understanding**: Evaluates understanding and reasoning about physical scenes, with 1,214 embodied reasoning scenarios focused on autonomous vehicle actions

PAI-Bench covers multiple physical AI domains including autonomous driving, robotics, industrial spaces, physics simulations, human interactions, and common sense reasoning.

### Resources
- 🌐 [GitHub Repository](https://github.com/SHI-Labs/physical-ai-bench)
- πŸ“Š [Generation Dataset](https://huggingface.co/datasets/shi-labs/physical-ai-bench-predict)
- πŸ“Š [Conditional Generation Dataset](https://huggingface.co/datasets/shi-labs/physical-ai-bench-transfer)
- πŸ“Š [Understanding Dataset](https://huggingface.co/datasets/shi-labs/physical-ai-bench-reason)
- πŸ“¦ [Artifacts](https://huggingface.co/datasets/Leymore/physical-ai-bench-artifacts)

## Reproducibility

To evaluate your models on PAI-Bench, visit our [GitHub repository](https://github.com/SHI-Labs/physical-ai-bench) for evaluation scripts and detailed instructions.

## Citation

If you use Physical AI Bench in your research, please cite:

```bibtex
@misc{zhou2025paibenchcomprehensivebenchmarkphysical,
      title={PAI-Bench: A Comprehensive Benchmark For Physical AI},
      author={Fengzhe Zhou and Jiannan Huang and Jialuo Li and Deva Ramanan and Humphrey Shi},
      year={2025},
      eprint={2512.01989},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2512.01989},
}

```
"""


# ============================================================================
# Model Links Utility
# ============================================================================

def create_model_link(model_name):
    """
    Convert a model name to a markdown link to Hugging Face.

    Args:
        model_name: Model name in format "org/model-name" or just a plain name

    Returns:
        Markdown formatted link or original name if format doesn't match
    """
    if not isinstance(model_name, str):
        return model_name

    if '/' in model_name:
        hf_url = f"https://huggingface.co/{model_name}"
        display_name = model_name.split('/')[-1]
        return f"[{display_name}]({hf_url})"

    return model_name


# ============================================================================
# Generation Tab Configuration and Utilities
# ============================================================================

# Column name to abbreviation mapping for display
PREDICT_COLUMN_ABBREV = {
    'Common Sense': 'CS',
    'AV': 'AV',
    'Robot': 'RO',
    'Industry': 'IN',
    'Human': 'HU',
    'Physics': 'PH',
    'Subject Consistency': 'SC',
    'Background Consistency': 'BC',
    'Motion Smoothness': 'MS',
    'Aesthetic Quality': 'AQ',
    'Imaging Quality': 'IQ',
    'Overall Consistency': 'OC',
    'I2V Subject': 'IS',
    'I2V Background': 'IB',
}

# Expected column order (full names from JSON)
PREDICT_COLUMN_ORDER = [
    'Model',
    'Overall',
    'Domain',
    'Quality',
    'Common Sense',
    'AV',
    'Robot',
    'Industry',
    'Human',
    'Physics',
    'Subject Consistency',
    'Background Consistency',
    'Motion Smoothness',
    'Aesthetic Quality',
    'Imaging Quality',
    'Overall Consistency',
    'I2V Subject',
    'I2V Background'
]

# Columns to hide by default (but still available for filtering/selection)
PREDICT_HIDDEN_COLUMNS = []

# Semantic/Domain dimensions (for selection button) - use abbreviations matching dataframe
PREDICT_DOMAIN_SCORE_DIMENSIONS = [
    'Domain',
    'CS', 'AV', 'RO', 'IN', 'HU', 'PH',
]

# Quality dimensions (for selection button) - use abbreviations matching dataframe
PREDICT_QUALITY_SCORE_DIMENSIONS = [
    'Quality',
    'SC', 'BC', 'MS', 'AQ', 'IQ', 'OC', 'IS', 'IB'
]

PREDICT_DESELECTED_COLUMNS = ['Domain', 'Quality']

PREDICT_ALL_SELECTED_COLUMNS = [
    'Domain', 'Quality',
    'CS', 'AV', 'RO', 'IN', 'HU', 'PH',
    'SC', 'BC', 'MS', 'AQ', 'IQ', 'OC', 'IS', 'IB'
]

# Columns that can never be deselected
PREDICT_NEVER_HIDDEN_COLUMNS = ['Model', 'Overall']

# Columns displayed by default
PREDICT_DEFAULT_DISPLAYED_COLUMNS = PREDICT_NEVER_HIDDEN_COLUMNS + PREDICT_ALL_SELECTED_COLUMNS

def load_predict_json(json_path):
    """
    Load generation leaderboard JSON.

    The JSON should already be pre-processed by generate_predict_leaderboard.py
    with correct column names, ordering, sorting, and separate model/url fields.
    """
    df = pd.read_json(json_path, orient='records')

    if 'model' in df.columns and 'url' in df.columns:
        def create_link(row):
            if pd.notna(row['url']):
                display_name = row['model'].split('/')[-1] if '/' in row['model'] else row['model']
                return f"[{display_name}]({row['url']})"
            return row['model']

        df['model'] = df.apply(create_link, axis=1)
        df = df.drop(columns=['url'])

    df = df.rename(columns={'model': 'Model'})

    for col in df.columns:
        if col != 'Model' and pd.api.types.is_numeric_dtype(df[col]):
            df[col] = df[col].apply(lambda x: f"{x:.1f}" if pd.notna(x) else x)

    # Rename columns to abbreviations for display
    df = df.rename(columns=PREDICT_COLUMN_ABBREV)

    return df


def get_predict_checkbox_choices(dataframe):
    """Get checkbox choices with full name (abbrev) format"""
    # Create reverse mapping from abbreviation to full name
    abbrev_to_full = {v: k for k, v in PREDICT_COLUMN_ABBREV.items()}

    choices = []
    for col in dataframe.columns:
        if col in ['Model', 'Overall']:
            continue
        if col in abbrev_to_full:
            full_name = abbrev_to_full[col]
            choices.append((f"{full_name} ({col})", col))
        else:
            choices.append((col, col))

    return choices


def select_predict_domain_score():
    """Return domain score for checkbox selection"""
    return gr.update(value=PREDICT_DOMAIN_SCORE_DIMENSIONS)

def select_predict_quality_score():
    """Return quality score for checkbox selection"""
    return gr.update(value=PREDICT_QUALITY_SCORE_DIMENSIONS)

def deselect_predict_all():
    """Deselect all dimensions"""
    return gr.update(value=PREDICT_DESELECTED_COLUMNS)

def select_predict_all():
    """Select all dimensions"""
    return gr.update(value=PREDICT_ALL_SELECTED_COLUMNS)

def on_predict_dimension_selection_change(selected_columns, full_df):
    """Handle dimension selection changes and update the dataframe"""
    present_columns = ['Model', 'Overall']

    for col in selected_columns:
        if col not in present_columns and col in full_df.columns:
            present_columns.append(col)

    updated_data = full_df[present_columns]

    datatypes = []
    for col in present_columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    return gr.update(value=updated_data, datatype=datatypes, headers=present_columns)


def init_predict_leaderboard(dataframe):
    """Initialize the Generation leaderboard with given dataframe"""
    if dataframe is None or dataframe.empty:
        raise ValueError("Leaderboard DataFrame is empty or None.")

    # Get columns that exist in the dataframe
    available_default_cols = [col for col in PREDICT_DEFAULT_DISPLAYED_COLUMNS if col in dataframe.columns]

    # Filter dataframe to show only default columns initially
    display_df = dataframe[available_default_cols]

    # Determine datatypes dynamically
    datatypes = []
    for col in display_df.columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    with gr.Row():
        with gr.Column(scale=1):
            domain_score_btn = gr.Button("Domain Score", size="md")
            quality_score_btn = gr.Button("Quality Score", size="md")
            select_all_btn = gr.Button("Select All", size="md")
            deselect_btn = gr.Button("Deselect All", size="md")

        with gr.Column(scale=4):
            # Get checkbox choices with "Full Name (Abbrev)" format
            checkbox_choices = get_predict_checkbox_choices(dataframe)

            checkbox_group = gr.CheckboxGroup(
                choices=checkbox_choices,
                value=[col for col in PREDICT_ALL_SELECTED_COLUMNS if col in dataframe.columns],
                label="Evaluation Dimensions",
                interactive=True,
            )

    data_component = gr.Dataframe(
        value=display_df,
        headers=list(display_df.columns),
        datatype=datatypes,
        interactive=False,
        visible=True,
        wrap=False,
        column_widths=["320px"] + ["80px"] * (len(display_df.columns) - 1),
        pinned_columns=1,
        elem_id="predict_leaderboard",
        max_height=10000,

    )

    # Setup event handlers
    domain_score_btn.click(
        select_predict_domain_score,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_predict_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    quality_score_btn.click(
        select_predict_quality_score,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_predict_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    deselect_btn.click(
        deselect_predict_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_predict_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    select_all_btn.click(
        select_predict_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_predict_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    checkbox_group.change(
        fn=on_predict_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    return data_component


# ============================================================================
# Conditional Generation Tab Configuration and Utilities
# ============================================================================

TRANSFER_COLUMN_ORDER = [
    'Model',
    'Condition',
    'Blur SSIM ↑',
    'Edge F1 ↑',
    'Depth si-RMSE ↓',
    'Mask mIoU ↑',
    'Quality Score ↑',
    'Diversity ↑'
]

TRANSFER_HIDDEN_COLUMNS = []

TRANSFER_QUALITY_DIMENSIONS = [
    'Blur SSIM ↑',
    'Edge F1 ↑',
    'Depth si-RMSE ↓',
    'Mask mIoU ↑',
    'Quality Score ↑',
    'Diversity ↑',
]

TRANSFER_ALL_SELECTED_COLUMNS = TRANSFER_QUALITY_DIMENSIONS

TRANSFER_NEVER_HIDDEN_COLUMNS = ['Model', 'Condition']

TRANSFER_DEFAULT_DISPLAYED_COLUMNS = TRANSFER_NEVER_HIDDEN_COLUMNS + TRANSFER_ALL_SELECTED_COLUMNS


def load_transfer_json(json_path):
    """Load conditional generation leaderboard JSON"""
    df = pd.read_json(json_path, orient='records')

    if 'model' in df.columns and 'url' in df.columns:
        def create_link(row):
            if pd.notna(row['url']):
                display_name = row['model'].split('/')[-1] if '/' in row['model'] else row['model']
                return f"[{display_name}]({row['url']})"
            return row['model']

        df['model'] = df.apply(create_link, axis=1)
        df = df.drop(columns=['url'])

    df = df.rename(columns={'model': 'Model'})

    for col in df.columns:
        if col not in ['Model', 'Condition'] and pd.api.types.is_numeric_dtype(df[col]):
            df[col] = df[col].apply(lambda x: f"{x:.3f}" if pd.notna(x) else x)

    return df


def select_transfer_all():
    """Select all dimensions"""
    return gr.update(value=TRANSFER_ALL_SELECTED_COLUMNS)


def deselect_transfer_all():
    """Deselect all dimensions"""
    return gr.update(value=[])


def on_transfer_dimension_selection_change(selected_columns, full_df):
    """Handle dimension selection changes and update the dataframe"""
    present_columns = ['Model', 'Condition']

    for col in selected_columns:
        if col not in present_columns and col in full_df.columns:
            present_columns.append(col)

    updated_data = full_df[present_columns]

    datatypes = []
    for col in present_columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    return gr.update(value=updated_data, datatype=datatypes, headers=present_columns)


def init_transfer_leaderboard(dataframe):
    """Initialize the Conditional Generation leaderboard with given dataframe"""
    if dataframe is None or dataframe.empty:
        raise ValueError("Leaderboard DataFrame is empty or None.")

    available_default_cols = [col for col in TRANSFER_DEFAULT_DISPLAYED_COLUMNS if col in dataframe.columns]

    display_df = dataframe[available_default_cols]

    datatypes = []
    for col in display_df.columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    with gr.Row():
        with gr.Column(scale=1):
            select_all_btn = gr.Button("Select All", size="md")
            deselect_btn = gr.Button("Deselect All", size="md")

        with gr.Column(scale=4):
            dimension_choices = [col for col in dataframe.columns
                                if col not in TRANSFER_NEVER_HIDDEN_COLUMNS]

            checkbox_group = gr.CheckboxGroup(
                choices=dimension_choices,
                value=[col for col in TRANSFER_DEFAULT_DISPLAYED_COLUMNS if col in dimension_choices],
                label="Evaluation Dimensions",
                interactive=True,
            )

    data_component = gr.Dataframe(
        value=display_df,
        headers=list(display_df.columns),
        datatype=datatypes,
        interactive=False,
        visible=True,
        wrap=False,
        column_widths=["280px", "120px"] + ["150px"] * (len(display_df.columns) - 2),
        pinned_columns=2,
        elem_id="transfer_leaderboard",
        max_height=10000,
    )

    deselect_btn.click(
        deselect_transfer_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_transfer_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    select_all_btn.click(
        select_transfer_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_transfer_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    checkbox_group.change(
        fn=on_transfer_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    return data_component


# ============================================================================
# Understanding Tab Configuration and Utilities
# ============================================================================

# Column name to abbreviation mapping for display
REASON_COLUMN_ABBREV = {
    'Common Sense': 'CS',
    'Embodied Reasoning': 'ER',
    'BridgeData V2': 'BD',
    'RoboVQA': 'RV',
    'RoboFail': 'RF',
    'Agibot': 'AB',
    'HoloAssist': 'HA',
}

# Desired column order (full names from JSON)
REASON_COLUMN_ORDER = [
    'Model',
    'Thinking',
    'Overall',
    'Common Sense',
    'Embodied Reasoning',
    'Space',
    'Time',
    'Physics',
    'BridgeData V2',
    'RoboVQA',
    'RoboFail',
    'Agibot',
    'HoloAssist',
    'AV'
]

# Columns to hide by default (but still available for filtering/selection)
REASON_HIDDEN_COLUMNS = []

# Reasoning dimensions (for selection button) - use abbreviations matching dataframe
REASON_COMMON_SENSE_DIMENSIONS = [
    'CS',
    'Space',
    'Time',
    'Physics',
]

# Domain dimensions (for selection button) - use abbreviations matching dataframe
REASON_EMBODIED_REASONING_DIMENSIONS = [
    'ER',
    'Space',
    'Time',
    'Physics',
    'BD', 'RV', 'RF', 'AB', 'HA', 'AV',
]

REASON_DESELECTED_COLUMNS = [
    'CS',
    'ER',
]

REASON_ALL_SELECTED_COLUMNS = [
    'CS', 'ER',
    'Space', 'Time', 'Physics',
    'BD', 'RV', 'RF', 'AB', 'HA', 'AV',
]

# Columns that can never be deselected
REASON_NEVER_HIDDEN_COLUMNS = ['Model', 'Thinking', 'Overall']

# Columns displayed by default (using renamed column names)
REASON_DEFAULT_DISPLAYED_COLUMNS = REASON_NEVER_HIDDEN_COLUMNS + REASON_ALL_SELECTED_COLUMNS


def load_reason_json(json_path):
    """Load understanding leaderboard JSON"""
    df = pd.read_json(json_path, orient='records')

    if 'model' in df.columns and 'url' in df.columns:
        def create_link(row):
            if pd.notna(row['url']):
                display_name = row['model'].split('/')[-1] if '/' in row['model'] else row['model']
                return f"[{display_name}]({row['url']})"
            return row['model']

        df['model'] = df.apply(create_link, axis=1)
        df = df.drop(columns=['url'])

    df = df.rename(columns={'model': 'Model'})

    for col in df.columns:
        if col != 'Model' and pd.api.types.is_numeric_dtype(df[col]):
            df[col] = df[col].apply(lambda x: f"{x:.1f}" if pd.notna(x) else x)

    # Rename columns to abbreviations for display
    df = df.rename(columns=REASON_COLUMN_ABBREV)

    return df


def get_reason_checkbox_choices(dataframe):
    """Get checkbox choices with full name (abbrev) format"""
    # Create reverse mapping from abbreviation to full name
    abbrev_to_full = {v: k for k, v in REASON_COLUMN_ABBREV.items()}

    choices = []
    for col in dataframe.columns:
        if col in ['Model', 'Thinking', 'Overall']:
            continue
        if col in abbrev_to_full:
            full_name = abbrev_to_full[col]
            choices.append((f"{full_name} ({col})", col))
        else:
            choices.append((col, col))

    return choices


def select_reason_common_sense_dimensions():
    """Return reasoning dimensions for checkbox selection"""
    return gr.update(value=REASON_COMMON_SENSE_DIMENSIONS)


def select_reason_embodied_reasoning_dimensions():
    """Return domain dimensions for checkbox selection"""
    return gr.update(value=REASON_EMBODIED_REASONING_DIMENSIONS)


def deselect_reason_all():
    """Deselect all dimensions"""
    return gr.update(value=REASON_DESELECTED_COLUMNS)


def select_reason_all():
    """Select all dimensions"""
    return gr.update(value=REASON_ALL_SELECTED_COLUMNS)


def on_reason_dimension_selection_change(selected_columns, full_df):
    """Handle dimension selection changes and update the dataframe"""
    present_columns = ['Model', 'Thinking', 'Overall']

    for col in selected_columns:
        if col not in present_columns and col in full_df.columns:
            present_columns.append(col)

    updated_data = full_df[present_columns]

    datatypes = []
    for col in present_columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    return gr.update(value=updated_data, datatype=datatypes, headers=present_columns)


def init_reason_leaderboard(dataframe):
    """Initialize the Understanding leaderboard with given dataframe"""
    if dataframe is None or dataframe.empty:
        raise ValueError("Leaderboard DataFrame is empty or None.")

    # Get columns that exist in the dataframe
    available_default_cols = [col for col in REASON_DEFAULT_DISPLAYED_COLUMNS if col in dataframe.columns]

    # Filter dataframe to show only default columns initially
    display_df = dataframe[available_default_cols]

    # Determine datatypes dynamically
    datatypes = []
    for col in display_df.columns:
        if col == 'Model':
            datatypes.append('markdown')
        else:
            datatypes.append('str')

    with gr.Row():
        with gr.Column(scale=1):
            common_sense_btn = gr.Button("Common Sense", size="md")
            embodied_reasoning_btn = gr.Button("Embodied Reasoning", size="md")
            select_all_btn = gr.Button("Select All", size="md")
            deselect_btn = gr.Button("Deselect All", size="md")

        with gr.Column(scale=4):
            # Get checkbox choices with "Full Name (Abbrev)" format
            checkbox_choices = get_reason_checkbox_choices(dataframe)

            checkbox_group = gr.CheckboxGroup(
                choices=checkbox_choices,
                value=[col for col in REASON_ALL_SELECTED_COLUMNS if col in dataframe.columns],
                label="Evaluation Dimensions",
                interactive=True,
            )

    data_component = gr.Dataframe(
        value=display_df,
        headers=list(display_df.columns),
        datatype=datatypes,
        interactive=False,
        visible=True,
        wrap=False,
        column_widths=["320px", "100px"] + ["100px"] * (len(display_df.columns) - 2),
        pinned_columns=2,
        elem_id="reason_leaderboard",
        max_height=10000,
    )

    # Setup event handlers
    common_sense_btn.click(
        select_reason_common_sense_dimensions,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_reason_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    embodied_reasoning_btn.click(
        select_reason_embodied_reasoning_dimensions,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_reason_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    deselect_btn.click(
        deselect_reason_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_reason_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    select_all_btn.click(
        select_reason_all,
        inputs=None,
        outputs=[checkbox_group]
    ).then(
        fn=on_reason_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    checkbox_group.change(
        fn=on_reason_dimension_selection_change,
        inputs=[checkbox_group, gr.State(dataframe)],
        outputs=data_component
    )

    return data_component


# ============================================================================
# Main Application
# ============================================================================

demo = gr.Blocks()
with demo:
    gr.HTML(f"<style>{CSS}</style>")
    gr.HTML(TITLE)
    gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")

    with gr.Tabs(elem_classes="tab-buttons") as tabs:
        with gr.TabItem("🎨 Generation", elem_id="predict-tab", id=0):
            predict_df = load_predict_json("data/generation-leaderboard.json")
            predict_leaderboard = init_predict_leaderboard(predict_df)

        with gr.TabItem("πŸ”„ Conditional Generation", elem_id="transfer-tab", id=1):
            transfer_df = load_transfer_json("data/conditional_generation-leaderboard.json")
            transfer_leaderboard = init_transfer_leaderboard(transfer_df)

        with gr.TabItem("🧠 Understanding", elem_id="reason-tab", id=2):
            reason_df = load_reason_json("data/understanding-leaderboard.json")
            reason_leaderboard = init_reason_leaderboard(reason_df)

        with gr.TabItem("ℹ️ About", elem_id="about-tab", id=3):
            gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")

demo.launch()