File size: 34,956 Bytes
77685d1
 
 
f3a9b67
 
 
77685d1
f3a9b67
77685d1
f3a9b67
77685d1
 
 
 
 
 
 
2b9837a
77685d1
f3a9b67
77685d1
 
2b9837a
77685d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3a9b67
 
 
 
 
 
 
 
 
77685d1
 
2b9837a
77685d1
 
c7311c2
 
 
 
 
 
 
 
 
 
 
77685d1
 
 
 
 
c7311c2
 
77685d1
c7311c2
 
77685d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b9837a
 
 
 
78c89f8
f3a9b67
 
f14833d
 
 
 
d77a1fe
 
 
 
 
 
78c89f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419fbb3
 
 
 
 
 
 
 
 
2766452
 
 
 
78c89f8
 
 
 
 
 
 
2b9837a
78c89f8
 
 
 
 
 
 
 
 
 
 
 
2b9837a
 
f3a9b67
78c89f8
 
 
 
 
 
 
 
 
 
 
2b9837a
f3a9b67
 
78c89f8
 
 
f3a9b67
78c89f8
 
 
f3a9b67
78c89f8
 
 
 
 
 
2b9837a
78c89f8
 
 
 
 
 
 
 
f3a9b67
78c89f8
9f5c361
 
78c89f8
 
 
 
2b9837a
78c89f8
2b9837a
78c89f8
 
 
 
 
 
 
 
 
 
dc543dd
 
 
 
 
 
 
 
2b9837a
 
 
 
c7311c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419fbb3
 
c7311c2
 
 
 
 
 
 
 
 
 
 
 
 
2766452
c7311c2
 
 
 
 
 
 
 
 
2766452
c7311c2
 
 
 
 
 
 
 
419fbb3
 
 
c7311c2
419fbb3
 
 
 
 
 
 
c7311c2
419fbb3
 
 
 
 
c7311c2
419fbb3
 
 
 
c7311c2
419fbb3
 
c7311c2
419fbb3
 
 
 
 
 
c7311c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419fbb3
 
 
2b9837a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78c89f8
2b9837a
78c89f8
2b9837a
78c89f8
2b9837a
78c89f8
f3a9b67
 
78c89f8
f3a9b67
2b9837a
 
 
 
 
 
 
 
 
 
 
 
f3a9b67
 
 
 
 
 
 
 
 
 
 
2b9837a
c7311c2
 
 
 
 
 
 
 
 
 
77685d1
f3a9b67
 
 
 
 
77685d1
2b9837a
 
 
d77a1fe
 
 
2b9837a
 
 
 
6baec08
9f5c361
f3a9b67
 
2b9837a
 
 
 
6baec08
2b9837a
 
 
 
 
 
 
 
6baec08
2b9837a
 
f3a9b67
2b9837a
9f5c361
 
 
 
 
2b9837a
 
 
 
9f5c361
f3a9b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b9837a
f3a9b67
2b9837a
 
6baec08
2b9837a
 
d77a1fe
2b9837a
 
419fbb3
 
2b9837a
 
 
 
 
 
 
 
 
 
77685d1
 
f3a9b67
 
77685d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3a9b67
77685d1
 
f3a9b67
 
77685d1
 
 
9f5c361
77685d1
 
 
d77a1fe
 
77685d1
2b9837a
77685d1
2b9837a
77685d1
 
2b9837a
d77a1fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77685d1
 
 
2b9837a
77685d1
 
 
d77a1fe
 
77685d1
 
d77a1fe
 
 
 
 
77685d1
 
 
 
 
2b9837a
d77a1fe
 
 
 
 
 
77685d1
d77a1fe
77685d1
2b9837a
77685d1
 
 
5d11e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77685d1
9f5c361
 
 
5d11e5b
 
 
77685d1
9f5c361
 
2b9837a
9f5c361
2b9837a
 
 
9f5c361
 
 
2b9837a
 
 
 
 
9f5c361
 
 
 
 
77685d1
 
 
9f5c361
5d11e5b
 
 
77685d1
9f5c361
 
 
77685d1
 
 
 
 
2b9837a
9f5c361
 
2b9837a
77685d1
dc543dd
 
 
 
 
 
 
 
c7311c2
dc543dd
 
c7311c2
 
 
dc543dd
 
c7311c2
 
9f5c361
 
5d11e5b
 
dc543dd
9f5c361
 
dc543dd
 
9f5c361
 
 
 
dc543dd
 
 
9f5c361
 
2b9837a
77685d1
 
 
a42608e
3e79669
a42608e
d268d2c
be5d033
dc543dd
 
 
 
 
 
 
 
 
 
 
 
 
77685d1
 
 
c7311c2
dc543dd
c7311c2
77685d1
 
 
f14833d
 
2b9837a
 
77685d1
 
 
dc543dd
77685d1
a42608e
dc543dd
 
77685d1
 
dc543dd
 
77685d1
 
 
 
 
 
f3a9b67
77685d1
 
 
2b9837a
 
77685d1
dc543dd
 
 
 
2b9837a
 
dc543dd
d77a1fe
 
 
 
 
77685d1
d77a1fe
 
 
 
 
 
 
77685d1
 
d77a1fe
 
dc543dd
77685d1
 
 
5d11e5b
 
77685d1
dc543dd
 
 
 
 
 
 
 
 
77685d1
 
dc543dd
 
 
 
 
 
77685d1
dc543dd
 
 
 
 
 
 
 
 
 
77685d1
c7311c2
77685d1
dc543dd
77685d1
 
c7311c2
77685d1
dc543dd
77685d1
9f5c361
 
 
dc543dd
9f5c361
77685d1
 
 
dc543dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77685d1
 
 
c7311c2
 
77685d1
 
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
"""ViTeX-Bench public leaderboard.

Three tabs:
* Leaderboard β€” full thirteen-metric vector for every approved method,
                rendered as styled HTML with TextScore highlighted as the
                explicit sort key.
* Submit     β€” upload eval.json from a successful `bash scripts/run_benchmark.sh`
                run; saved as a pending entry awaiting maintainer review.
* Admin      β€” passphrase-gated panel listing pending submissions with
                approve / reject actions.

State lives in `submissions.jsonl` inside this Space repo. Each line is one
JSON object; `status` is `pending` / `approved` / `rejected`. Pre-populated
with the paper baselines (status=approved). Owner-only writes via HF_TOKEN
(set as a Space secret).
"""

import html as _html
import json
import math
import os
import time
from typing import Dict, List, Optional, Tuple

import gradio as gr
import pandas as pd
from huggingface_hub import HfApi, hf_hub_download
from huggingface_hub.utils import HfHubHTTPError

REPO_ID = "ViTeX-Bench/ViTeX-Bench-Leaderboard"
SUBMISSIONS_FILE = "submissions.jsonl"

HF_TOKEN = os.environ.get("HF_TOKEN", "")
ADMIN_PASSPHRASE = os.environ.get("ADMIN_PASSPHRASE", "")

api = HfApi(token=HF_TOKEN) if HF_TOKEN else HfApi()


METRIC_KEYS = [
    "SeqAcc", "CharAcc", "TTS",
    "Flicker_full", "Flicker_crop", "Warp_full", "Warp_crop",
    "MUSIQ_full", "MUSIQ_crop",
    "PSNR_loc", "SSIM_loc", "LPIPS_loc", "DreamSim_loc",
]
TEXT_KEYS = ["SeqAcc", "CharAcc", "TTS"]


def _text_score(seq, char, tts):
    if seq is None or char is None or tts is None:
        return None
    if seq <= 0.0 or char <= 0.0 or tts <= 0.0:
        return 0.0
    return math.exp((math.log(seq) + math.log(char) + math.log(tts)) / 3.0)


# ---------- I/O ----------

def fetch_submissions() -> List[Dict]:
    """Read submissions.jsonl from the Space repo (or local fallback).

    Returns an empty list on any error so the leaderboard can still render
    instead of breaking the whole Gradio app.
    """
    try:
        if os.path.exists(SUBMISSIONS_FILE):
            with open(SUBMISSIONS_FILE) as f:
                return [json.loads(line) for line in f if line.strip()]
    except Exception as e:
        print(f"[fetch_submissions] local read failed: {e}", flush=True)
    try:
        path = hf_hub_download(REPO_ID, SUBMISSIONS_FILE, repo_type="space",
                               token=HF_TOKEN or None)
        with open(path) as f:
            return [json.loads(line) for line in f if line.strip()]
    except HfHubHTTPError as e:
        print(f"[fetch_submissions] hub download HTTP error: {e}", flush=True)
        return []
    except Exception as e:
        print(f"[fetch_submissions] hub download failed: {e}", flush=True)
        return []


def write_submissions(items: List[Dict]) -> None:
    """Persist submissions.jsonl to the Space repo + local cache."""
    content = "\n".join(json.dumps(x, ensure_ascii=False) for x in items) + "\n"
    with open(SUBMISSIONS_FILE, "w") as f:
        f.write(content)
    if HF_TOKEN:
        api.upload_file(
            path_or_fileobj=content.encode(),
            path_in_repo=SUBMISSIONS_FILE,
            repo_id=REPO_ID,
            repo_type="space",
            commit_message=f"Update submissions ({len(items)} entries)",
        )


# ---------- HTML leaderboard rendering ----------

LEADERBOARD_CSS = """
<style>
/* Palette: Stripe-inspired (navy #0a2540 / slate text #425466 / pale #f6f9fc).
   TextScore column = deep navy primary; the three text-correctness primitives
   that feed it carry a soft tint; remaining detail metrics are quiet slate. */
/* Borderless wrap. The outer .vbench-wrap is now a transparent
   pass-through with no border, radius, or shadow; the inner
   .vbench-scroll handles the actual scrolling. */
.vbench-wrap   { width: 100%; background: transparent; }
.vbench-scroll {
    width: 100%;
    overflow-x: auto;
    overflow-y: auto;
    max-height: 640px;
}
.vbench-tbl {
    font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
                 "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    border-collapse: separate; border-spacing: 0;
    width: max-content; min-width: 100%;
    font-size: 13px; line-height: 1.4; color: #425466;
}
.vbench-tbl thead th {
    background: #f6f9fc; color: #425466;
    font-weight: 500; font-size: 11px; letter-spacing: 0.04em;
    text-transform: uppercase;
    padding: 11px 12px;
    border-bottom: 1px solid #e3e8ee;
    position: sticky; top: 0; z-index: 5; white-space: nowrap;
}
.vbench-tbl thead th.sortable { cursor: pointer; user-select: none; }
.vbench-tbl thead th.sortable:hover { background: #eef2f8; color: #0a2540; }
.vbench-tbl thead th.sorted-active {
    background: #eef2f8; color: #0a2540;
    box-shadow: inset 0 -2px 0 #0a2540;
}
.vbench-tbl thead th.score.sorted-active {
    background: #1e3a5f; box-shadow: inset 0 -2px 0 #f6f9fc;
}
.vbench-tbl td.cell-good { background: #ebf6ee !important; }
.vbench-tbl td.cell-bad  { background: #fbedef !important; }
.vbench-tbl tbody tr:hover td.cell-good { background: #ddeee2 !important; }
.vbench-tbl tbody tr:hover td.cell-bad  { background: #f4dde1 !important; }
.vbench-tbl td {
    padding: 9px 12px; border-bottom: 1px solid #f1f4f8;
    text-align: right; white-space: nowrap;
    font-variant-numeric: tabular-nums;
}
.vbench-tbl tbody tr:last-child td { border-bottom: none; }
.vbench-tbl tbody tr:hover td { background: #fafbfc; }

.vbench-tbl td.left { text-align: left; }
.vbench-tbl td.rank {
    color: #a3acb9; font-weight: 500; font-size: 12px;
    text-align: center; width: 38px;
}
.vbench-tbl td.method {
    color: #0a2540; font-weight: 600; font-size: 13px;
    min-width: 170px;
}
.vbench-tbl td.org {
    color: #697386; font-size: 12px; max-width: 200px;
    overflow: hidden; text-overflow: ellipsis;
}

/* TextScore column: deep navy, dominant β€” this is the sort key */
.vbench-tbl th.score {
    background: #0a2540; color: #f6f9fc;
    font-weight: 600; font-size: 11px;
    border-bottom: 1px solid #0a2540;
}
.vbench-tbl td.score {
    background: #0a2540; color: #fff;
    font-weight: 700; font-size: 14px;
    letter-spacing: -0.01em;
}
.vbench-tbl tbody tr:hover td.score { background: #1e3a5f; }

/* Text-correctness primitives: the three feeding TextScore β€” soft tint */
.vbench-tbl th.text {
    background: #eef2f8; color: #0a2540;
    font-weight: 600;
}
.vbench-tbl td.text {
    background: #f6f9fc; color: #0a2540;
    font-weight: 600; font-size: 13px;
}
.vbench-tbl tbody tr:hover td.text { background: #eef2f8; }

/* Detail metrics: subtle */
.vbench-tbl td.metric { color: #425466; }
.vbench-tbl .num0 { color: #c1c9d2; }

/* Family pastel badges */
.vbench-tbl .badge {
    display: inline-block; padding: 2px 8px;
    font-size: 10.5px; font-weight: 500;
    border-radius: 999px; letter-spacing: 0.02em;
}
.vbench-tbl .badge.A     { background: #cff5d1; color: #0a3514; }
.vbench-tbl .badge.B     { background: #fff5cc; color: #7e3503; }
.vbench-tbl .badge.C     { background: #ffd1d8; color: #7d122c; }
.vbench-tbl .badge.D     { background: #dadcff; color: #322a87; }
.vbench-tbl .badge.Ref   { background: #e6e0ff; color: #3b1f7a; }
.vbench-tbl .badge.Other { background: #e3e8ee; color: #0a2540; }
.vbench-tbl .badge.Admin { background: #0a2540; color: #f6f9fc; }
.vbench-tbl .badge.User  { background: #635bff; color: #fff; }

.vbench-tbl .links a {
    text-decoration: none; margin-right: 6px; font-size: 13px;
    opacity: 0.65; transition: opacity 0.15s;
}
.vbench-tbl .links a:hover { opacity: 1; }

.vbench-legend {
    font-size: 11.5px; color: #697386;
    padding: 14px 4px 0; line-height: 1.65;
}
.vbench-legend code {
    background: #f6f9fc; color: #425466;
    padding: 1px 6px; border-radius: 4px; font-size: 11px;
    border: 1px solid #e3e8ee;
}
.vbench-legend b { color: #0a2540; font-weight: 600; }
.vbench-foot, .vbench-foot p {
    font-size: 12px; color: #697386; margin-top: 6px;
}
.vbench-foot code {
    background: #f6f9fc; color: #425466;
    padding: 1px 5px; border-radius: 4px; font-size: 11px;
    border: 1px solid #e3e8ee;
}
</style>
"""


# Global JS injected via Blocks(js=...). It hooks every .vbench-tbl table
# in the DOM (immediately + via MutationObserver as Gradio re-renders the
# HTML component) and attaches click-to-sort + above/below-mean shading.
# Inline <script> tags inside gr.HTML are NOT executed by Gradio's React
# renderer, which is why we route through Blocks(js=...).
SORT_JS = r"""
() => {
  function cellNum(cell) {
    if (!cell) return null;
    const txt = (cell.textContent || '').replace(/[†\s]/g, '');
    if (txt === '' || txt === 'β€”') return null;
    const n = Number(txt);
    return isNaN(n) ? null : n;
  }
  function cellTxt(cell) {
    return (cell && cell.textContent || '').replace(/\s+/g, ' ').trim();
  }
  function dirFor(th) {
    const t = th.textContent || '';
    if (t.indexOf('↑') >= 0) return 'up';
    if (t.indexOf('↓') >= 0) return 'down';
    return null;
  }

  function applyMeanShading(table) {
    const tbody = table.querySelector('tbody');
    const headers = Array.from(table.querySelectorAll('thead th'));
    const rows = Array.from(tbody.querySelectorAll('tr'));
    rows.forEach(r => Array.from(r.cells).forEach(c => {
      c.classList.remove('cell-good', 'cell-bad');
    }));
    headers.forEach((th, col) => {
      const dir = dirFor(th);
      if (!dir) return;
      const values = [];
      rows.forEach(r => {
        const cell = r.children[col];
        if (!cell || cell.classList.contains('score')) return;
        const n = cellNum(cell);
        if (n !== null) values.push(n);
      });
      if (values.length < 2) return;
      const mean = values.reduce((a, b) => a + b, 0) / values.length;
      rows.forEach(r => {
        const cell = r.children[col];
        if (!cell || cell.classList.contains('score')) return;
        const n = cellNum(cell);
        if (n === null) return;
        const better = (dir === 'up') ? n > mean : n < mean;
        cell.classList.add(better ? 'cell-good' : 'cell-bad');
      });
    });
  }

  function attachSortAndShade(table) {
    if (table.dataset.vbenchAttached === '1') return;
    table.dataset.vbenchAttached = '1';
    const tbody = table.querySelector('tbody');
    const headers = Array.from(table.querySelectorAll('thead th'));

    function sortBy(idx, dir) {
      const rows = Array.from(tbody.querySelectorAll('tr'));
      rows.sort((ra, rb) => {
        const va = cellNum(ra.children[idx]);
        const vb = cellNum(rb.children[idx]);
        if (va !== null && vb !== null) return (va - vb) * dir;
        if (va !== null) return -1;
        if (vb !== null) return  1;
        return cellTxt(ra.children[idx]).localeCompare(cellTxt(rb.children[idx])) * dir;
      });
      rows.forEach((row, k) => {
        if (row.children[0] && row.children[0].classList.contains('rank')) {
          row.children[0].textContent = String(k + 1);
        }
        tbody.appendChild(row);
      });
      headers.forEach(h => h.classList.remove('sorted-active'));
      headers[idx].classList.add('sorted-active');
    }

    let lastIdx = -1, lastDir = 0;
    headers.forEach((th, i) => {
      if (i === 0) return;
      th.classList.add('sortable');
      th.addEventListener('click', () => {
        const txt = th.textContent || '';
        const dir = (i === lastIdx) ? -lastDir : (txt.indexOf('↓') >= 0 ? +1 : -1);
        lastIdx = i; lastDir = dir;
        sortBy(i, dir);
      });
    });
    applyMeanShading(table);
  }

  function rescan() {
    document.querySelectorAll('table.vbench-tbl').forEach(t => {
      // Re-shade on every rescan so newly injected rows pick up colours
      // even when the table-level click handlers were already attached.
      attachSortAndShade(t);
      applyMeanShading(t);
    });
  }

  rescan();
  // Gradio re-renders the gr.HTML component on every refresh; watch the
  // body for subtree mutations and re-scan whenever a vbench-tbl appears
  // (or is replaced) anywhere in the page.
  const obs = new MutationObserver(() => rescan());
  obs.observe(document.body, { childList: true, subtree: true });
}
"""


def _fmt(v, fmt=".4f", dim_zero=True) -> str:
    if v is None:
        return '<span style="color:#9ca3af">β€”</span>'
    try:
        f = float(v)
    except Exception:
        return _html.escape(str(v))
    s = format(f, fmt)
    if dim_zero and f == 0.0:
        return f'<span class="num0">{s}</span>'
    return s


def _family_badge(fam: str) -> str:
    raw = (fam or "β€”").strip()
    if raw.startswith("A"):
        cls, label = "A", "A"
    elif raw.startswith("B"):
        cls, label = "B", "B"
    elif raw.startswith("C"):
        cls, label = "C", "C"
    elif raw.startswith("D"):
        cls, label = "D", "D"
    elif raw.lower().startswith("ref"):
        cls, label = "Ref", "Ref"
    else:
        cls, label = "Other", raw[:5] or "Other"
    return f'<span class="badge {cls}" title="{_html.escape(raw)}">{_html.escape(label)}</span>'


def _links(r: Dict) -> str:
    out = []
    if r.get("paper_url"):
        out.append(f'<a href="{_html.escape(r["paper_url"])}" target="_blank" title="Paper">πŸ“„</a>')
    if r.get("code_url"):
        out.append(f'<a href="{_html.escape(r["code_url"])}" target="_blank" title="Code">πŸ”—</a>')
    return "".join(out)


def _record_text_score(r: Dict) -> Optional[float]:
    """Prefer the stored TextScore; fall back to recomputing it from the
    three primitives so older submissions render correctly."""
    if r.get("TextScore") is not None:
        try:
            return float(r["TextScore"])
        except Exception:
            return None
    return _text_score(r.get("SeqAcc"), r.get("CharAcc"), r.get("TTS"))


def render_leaderboard_html() -> str:
    try:
        return _render_leaderboard_html_inner()
    except Exception as e:
        print(f"[render_leaderboard_html] failed: {e}", flush=True)
        return (LEADERBOARD_CSS +
                f"<p style='padding:16px;color:#b91c1c'>Could not render the "
                f"leaderboard: {_html.escape(str(e))}</p>")


def _render_leaderboard_html_inner() -> str:
    rows = [r for r in fetch_submissions() if r.get("status") == "approved"]
    rows.sort(key=lambda r: (
        -(_record_text_score(r) if _record_text_score(r) is not None else -1.0),
        -(r.get("CharAcc") or 0.0),
        -(r.get("TTS") or 0.0),
    ))

    if not rows:
        return LEADERBOARD_CSS + "<p style='padding:16px;color:#6b7280'>No approved submissions yet.</p>"

    lines = [LEADERBOARD_CSS,
             '<div class="vbench-wrap"><div class="vbench-scroll">'
             '<table class="vbench-tbl">']
    lines.append(
        "<thead><tr>"
        '<th class="rank">#</th>'
        '<th class="left">Method</th>'
        '<th class="left">Authors / Org</th>'
        '<th class="left">Src</th>'
        '<th class="score">TextScore↑</th>'
        '<th class="text">SeqAcc↑</th><th class="text">CharAcc↑</th><th class="text">TTS↑</th>'
        "<th>Flk_f↓</th><th>Flk_c↓</th><th>Wp_f↓</th><th>Wp_c↓</th>"
        "<th>MUSIQ_f↑</th><th>MUSIQ_c↑</th>"
        "<th>PSNR↑</th><th>SSIM↑</th><th>LPIPS↓</th><th>DSim↓</th>"
        '<th class="left">Links</th>'
        '<th class="left">Fam</th>'
        "</tr></thead><tbody>"
    )
    for i, r in enumerate(rows, 1):
        method = _html.escape(r.get("method", "β€”"))
        fam = _family_badge(r.get("family", "β€”"))
        org = _html.escape(r.get("organization", "β€”") or "β€”")
        note = r.get("note", "")
        method_with_note = (
            f'{method} <span title="{_html.escape(note)}" style="color:#635bff;font-weight:600;cursor:help">†</span>'
            if note else method
        )
        ts = _record_text_score(r)

        submitter = (r.get("submitter") or "user").strip().lower()
        src_label = "Admin" if submitter == "admin" else "User"
        src_class = "Admin" if submitter == "admin" else "User"
        src_badge = f'<span class="badge {src_class}">{src_label}</span>'

        lines.append("<tr>")
        lines.append(f'<td class="rank">{i}</td>')
        lines.append(f'<td class="left method">{method_with_note}</td>')
        lines.append(f'<td class="left org" title="{org}">{org}</td>')
        lines.append(f'<td class="left">{src_badge}</td>')
        lines.append(f'<td class="score">{_fmt(ts, ".4f")}</td>')

        for k, fmt, dim, cls in [
            ("SeqAcc", ".3f", True, "text"),
            ("CharAcc", ".3f", True, "text"),
            ("TTS", ".3f", True, "text"),
            ("Flicker_full", ".2f", False, "metric"),
            ("Flicker_crop", ".2f", False, "metric"),
            ("Warp_full", ".2f", False, "metric"),
            ("Warp_crop", ".2f", False, "metric"),
            ("MUSIQ_full", ".2f", False, "metric"),
            ("MUSIQ_crop", ".2f", False, "metric"),
            ("PSNR_loc", ".2f", False, "metric"),
            ("SSIM_loc", ".3f", False, "metric"),
            ("LPIPS_loc", ".3f", False, "metric"),
            ("DreamSim_loc", ".4f", False, "metric"),
        ]:
            lines.append(f'<td class="{cls}">{_fmt(r.get(k), fmt, dim)}</td>')

        lines.append(f'<td class="left links">{_links(r)}</td>')
        lines.append(f'<td class="left family">{fam}</td>')
        lines.append("</tr>")

    lines.append("</tbody></table></div></div>")
    lines.append(
        '<div class="vbench-legend">'
        "Ranked by <b>TextScore</b>, the geometric mean of the three text-correctness primitives. "
        "Click any column header to re-sort by that metric. "
        "↑ higher-better, ↓ lower-better. "
        "<code>Flk</code> = Flicker, <code>Wp</code> = Warp, <code>DSim</code> = DreamSim; "
        "subscripts <code>f</code> / <code>c</code> = full-frame / text-crop scope. "
        "A † next to a method name marks a published caveat (hover for details)."
        "</div>"
    )
    return "\n".join(lines)


# ---------- Submit / admin handlers ----------

def parse_eval_json(file_obj) -> Tuple[Optional[Dict], Optional[str]]:
    """Extract the 13 metric means + TextScore from an eval.json produced
    by `benchmark/evaluate.py`."""
    if file_obj is None:
        return None, "No file uploaded."
    try:
        path = file_obj.name if hasattr(file_obj, "name") else file_obj
        with open(path) as f:
            data = json.load(f)
    except Exception as e:
        return None, f"Could not parse JSON: {e}"
    agg = data.get("aggregate")
    if not isinstance(agg, dict):
        return None, "eval.json is missing the top-level `aggregate` field."

    metrics = {}
    for k in METRIC_KEYS:
        v = agg.get(k)
        m = v.get("mean") if isinstance(v, dict) else v
        if m is None:
            return None, f"aggregate.{k}.mean is missing or null."
        metrics[k] = float(m)

    score_obj = agg.get("TextScore") or {}
    s = score_obj.get("mean") if isinstance(score_obj, dict) else score_obj
    if s is None:
        s = _text_score(*[metrics[k] for k in TEXT_KEYS])
    metrics["TextScore"] = float(s) if s is not None else 0.0
    metrics["n_clips"] = (
        len(data["per_clip"]) if isinstance(data.get("per_clip"), dict) else None
    )
    metrics["submitter"] = "user"
    return metrics, None


def submit_handler(method, organization, family, paper_url, code_url,
                   contact_email, file_obj, admin_passphrase):
    if not method or not method.strip():
        return "❌ Method name is required.", render_leaderboard_html()
    if not organization or not organization.strip():
        return "❌ Organization / author(s) is required.", render_leaderboard_html()
    metrics, err = parse_eval_json(file_obj)
    if err:
        return f"❌ {err}", render_leaderboard_html()

    # If a non-empty admin passphrase is provided, validate it: matching
    # the secret promotes the submission to submitter='admin' + status=
    # 'approved' (no review queue). Any mismatch is rejected outright,
    # rather than silently downgrading to a user submission, so a typo
    # cannot accidentally enter the public table as Admin.
    is_admin = False
    pass_attempt = (admin_passphrase or "").strip()
    if pass_attempt:
        if not (ADMIN_PASSPHRASE or "").strip():
            return ("❌ ADMIN_PASSPHRASE is not configured on this Space. "
                    "Leave the admin passphrase field blank to submit as a user.",
                    render_leaderboard_html())
        if pass_attempt != (ADMIN_PASSPHRASE or "").strip():
            return ("❌ Admin passphrase does not match. "
                    "Leave the field blank to submit as a user.",
                    render_leaderboard_html())
        is_admin = True

    now = time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
    entry = {
        "method": method.strip(),
        "organization": organization.strip(),
        "family": (family or "").strip() or "Other",
        "paper_url": (paper_url or "").strip(),
        "code_url": (code_url or "").strip(),
        "contact_email": (contact_email or "").strip(),
        "submitted_at": now,
        "status": "approved" if is_admin else "pending",
        **metrics,
    }
    if is_admin:
        entry["submitter"] = "admin"
        entry["approved_at"] = now
    # parse_eval_json defaults submitter='user' when not overridden above.

    items = fetch_submissions()
    items.append(entry)
    try:
        write_submissions(items)
    except Exception as e:
        return f"❌ Could not save submission: {e}", render_leaderboard_html()
    if is_admin:
        return (
            f"βœ… Admin submission published. {entry['method']} added with "
            f"TextScore = {entry['TextScore']:.4f} (n_clips = {entry.get('n_clips')}).",
            render_leaderboard_html(),
        )
    return (
        f"βœ… Submitted for review. TextScore = {entry['TextScore']:.4f} "
        f"(n_clips = {entry.get('n_clips')}). The maintainers will review it shortly.",
        render_leaderboard_html(),
    )


def _check_passphrase(passphrase: str):
    """Return (ok: bool, message: str). Distinguishes between an unset
    Space secret and a mistyped value so the admin knows which one to
    fix. Both sides are stripped of surrounding whitespace because copy-
    pasting from a password manager often picks up a stray newline."""
    if not (ADMIN_PASSPHRASE or "").strip():
        return False, ("❌ ADMIN_PASSPHRASE is not configured on this Space. "
                       "Set it in **Space β†’ Settings β†’ Variables and secrets** "
                       "and restart the Space.")
    if (passphrase or "").strip() != (ADMIN_PASSPHRASE or "").strip():
        return False, "❌ Wrong passphrase."
    return True, ""


def submission_diagnostics():
    """Status summary readable WITHOUT a passphrase: counts per status and
    whether the Space secrets are configured. Lets the maintainer confirm
    their submission landed before unlocking the admin actions."""
    items = fetch_submissions()
    n_total = len(items)
    n_pending = sum(1 for r in items if r.get("status") == "pending")
    n_approved = sum(1 for r in items if r.get("status") == "approved")
    n_rejected = sum(1 for r in items if r.get("status") == "rejected")
    pass_set = bool((ADMIN_PASSPHRASE or "").strip())
    token_set = bool((HF_TOKEN or "").strip())
    return (
        f"**Submissions** β€” {n_total} total Β· "
        f"{n_pending} pending Β· {n_approved} approved Β· {n_rejected} rejected.\n\n"
        f"**Space secrets** β€” `ADMIN_PASSPHRASE`: "
        f"{'βœ… set' if pass_set else '❌ not set'} &nbsp;Β·&nbsp; "
        f"`HF_TOKEN`: {'βœ… set' if token_set else '❌ not set (submissions stay in-memory only and disappear on Space restart)'}."
    )


def admin_view(passphrase: str):
    """Show every entry (pending / approved / rejected) with its absolute
    index so the admin operates on stable row numbers regardless of
    status."""
    ok, msg = _check_passphrase(passphrase)
    if not ok:
        return pd.DataFrame(), msg
    items = fetch_submissions()
    if not items:
        return pd.DataFrame(), "πŸ“‹ No submissions yet."
    rows = []
    for i, r in enumerate(items):
        rows.append({
            "#": i,
            "Method": r.get("method", "β€”"),
            "Status": r.get("status", "β€”"),
            "Source": (r.get("submitter") or "β€”"),
            "TextScore": _record_text_score(r),
            "Family": r.get("family", "β€”"),
            "Organization": r.get("organization", "β€”"),
            "Email": r.get("contact_email", ""),
            "Submitted": r.get("submitted_at", ""),
        })
    n_pending = sum(1 for r in items if r.get("status") == "pending")
    return pd.DataFrame(rows), (
        f"πŸ“‹ {len(items)} entry(ies) β€” {n_pending} pending. "
        "Use the absolute index in the # column for every action below."
    )


def admin_act(passphrase: str, idx: int, action: str):
    """Approve or reject the entry at absolute index `idx`."""
    ok, msg = _check_passphrase(passphrase)
    if not ok:
        return msg, render_leaderboard_html()
    items = fetch_submissions()
    if idx is None or int(idx) < 0 or int(idx) >= len(items):
        return f"❌ Index {idx} out of range ({len(items)} entries).", render_leaderboard_html()
    target = int(idx)
    items[target]["status"] = action
    items[target][f"{action}_at"] = time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
    try:
        write_submissions(items)
    except Exception as e:
        return f"❌ Could not save: {e}", render_leaderboard_html()
    method = items[target].get("method", "β€”")
    return f"βœ… Entry #{target} ({method}) β†’ {action}.", render_leaderboard_html()


def _admin_action_response(passphrase: str, msg: str):
    """Return the (msg, leaderboard_html, df, diag_md) tuple every admin
    action emits β€” keeps the dataframe and the public diagnostics block
    in sync with the leaderboard after every approve / reject / delete."""
    df, _ = admin_view(passphrase)
    return msg, render_leaderboard_html(), df, submission_diagnostics()


def admin_approve(passphrase: str, idx: int):
    msg, _ = admin_act(passphrase, idx, "approved")
    return _admin_action_response(passphrase, msg)


def admin_reject(passphrase: str, idx: int):
    msg, _ = admin_act(passphrase, idx, "rejected")
    return _admin_action_response(passphrase, msg)


def admin_delete(passphrase: str, idx: int):
    """Permanently remove the entry at absolute index `idx`."""
    ok, msg = _check_passphrase(passphrase)
    if not ok:
        return _admin_action_response(passphrase, msg)
    items = fetch_submissions()
    if idx is None or int(idx) < 0 or int(idx) >= len(items):
        return _admin_action_response(
            passphrase, f"❌ Index {idx} out of range ({len(items)} entries).")
    removed = items.pop(int(idx))
    try:
        write_submissions(items)
    except Exception as e:
        return _admin_action_response(passphrase, f"❌ Could not save: {e}")
    return _admin_action_response(
        passphrase, f"πŸ—‘οΈ Deleted #{int(idx)} ({removed.get('method', 'β€”')}).")


# ---------- UI ----------

HEADER_MD = (
    "# πŸ† ViTeX-Bench Leaderboard\n\n"
    "🌐 [Project page](https://vitex-bench.github.io/) &nbsp;·&nbsp; "
    "πŸ“Š [Dataset](https://huggingface.co/datasets/ViTeX-Bench/ViTeX-Dataset) &nbsp;Β·&nbsp; "
    "πŸ§ͺ [Benchmark code](https://huggingface.co/ViTeX-Bench/ViTeX-Bench) &nbsp;Β·&nbsp; "
    "πŸ€– [Model & Inference code](https://huggingface.co/ViTeX-Bench/ViTeX-Edit-14B) &nbsp;Β·&nbsp; "
    "πŸ† **Leaderboard**\n\n"
    "Public ranking for **video scene text editing** under the 13-metric ViTeX-Bench "
    "protocol. Methods are ranked by **TextScore** = βˆ›(SeqAcc Β· CharAcc Β· TTS), "
    "the geometric mean of the three text-correctness primitives; the full thirteen-"
    "metric vector is shown alongside it."
)


THEME = gr.themes.Soft(
    font=[gr.themes.GoogleFont("Inter"),
          "ui-sans-serif", "system-ui", "-apple-system",
          "Segoe UI", "Roboto", "Helvetica Neue", "Arial", "sans-serif"],
    font_mono=[gr.themes.GoogleFont("JetBrains Mono"),
               "ui-monospace", "SFMono-Regular", "Menlo", "Consolas", "monospace"],
)


with gr.Blocks(title="ViTeX-Bench Leaderboard",
               theme=THEME,
               js=SORT_JS) as demo:
    gr.Markdown(HEADER_MD)

    with gr.Tab("πŸ“Š Leaderboard"):
        leaderboard_html = gr.HTML(value=render_leaderboard_html(),
                                   container=False)
        refresh_btn = gr.Button("πŸ”„ Refresh", variant="primary", size="sm")
        refresh_btn.click(fn=render_leaderboard_html, outputs=leaderboard_html)

    with gr.Tab("πŸš€ Submit"):
        gr.Markdown(
            "Upload the `eval.json` produced by "
            "`bash scripts/run_benchmark.sh <method>` in the "
            "[Benchmark code repo](https://huggingface.co/ViTeX-Bench/ViTeX-Bench). "
            "All 13 metrics and TextScore are read directly from the JSON; "
            "fill the method metadata below."
        )
        with gr.Row():
            method_in = gr.Textbox(label="Method name *",
                                   placeholder="e.g., MyVideoTextEditor v2")
            family_in = gr.Dropdown(
                label="Family",
                choices=["A β€” per-frame image editor",
                         "B β€” first-frame + I2V propagation",
                         "C β€” mask-conditioned video inpainting",
                         "D β€” instruction-guided V2V",
                         "Reference",
                         "Other"],
                value="Other",
            )
        org_in = gr.Textbox(label="Organization / author(s) *",
                            placeholder="e.g., Acme Research / J. Doe et al.")
        with gr.Row():
            paper_in = gr.Textbox(label="Paper URL (optional)",
                                  placeholder="https://arxiv.org/abs/...")
            code_in = gr.Textbox(label="Code URL (optional)",
                                 placeholder="https://github.com/...")
        email_in = gr.Textbox(label="Contact email (optional, kept private)",
                              placeholder="you@example.com")
        file_in = gr.File(label="eval.json", file_types=[".json"])
        admin_pass_submit = gr.Textbox(
            label="Admin passphrase (optional)", type="password",
            placeholder="Leave blank to submit as a user (entry will be reviewed before appearing).",
        )
        submit_btn = gr.Button("Submit", variant="primary")
        result_out = gr.Markdown()
        gr.Markdown(
            "If the **admin passphrase** field is filled with the correct value, "
            "the entry is published immediately with the **Admin** source badge "
            "and skips the review queue. Otherwise the submission is queued for "
            "the maintainers to approve.",
            elem_classes=["vbench-foot"],
        )
        submit_btn.click(
            submit_handler,
            inputs=[method_in, org_in, family_in, paper_in, code_in,
                    email_in, file_in, admin_pass_submit],
            outputs=[result_out, leaderboard_html],
        )

    with gr.Tab("πŸ” Admin"):
        diag_md = gr.Markdown(value=submission_diagnostics())

        with gr.Row():
            admin_pass = gr.Textbox(label="Passphrase", type="password",
                                    scale=3, container=True)
            view_btn = gr.Button("πŸ“‹ View entries", variant="primary",
                                 scale=1, min_width=160)

        all_df = gr.Dataframe(interactive=False, wrap=True,
                              headers=["#", "Method", "Status", "Source",
                                       "TextScore", "Family", "Organization",
                                       "Email", "Submitted"])

        with gr.Row():
            idx_in = gr.Number(label="Row index (#)", value=0, precision=0,
                               scale=1, min_width=120)
            approve_btn = gr.Button("βœ… Approve", variant="primary", scale=1)
            reject_btn = gr.Button("❌ Reject", scale=1)
            delete_btn = gr.Button("πŸ—‘οΈ Delete", variant="stop", scale=1)

        action_msg = gr.Markdown()

        gr.Markdown(
            "Configure `ADMIN_PASSPHRASE` (auth) and `HF_TOKEN` "
            "(persistence) in **Space β†’ Settings β†’ Variables and secrets**.",
            elem_classes=["vbench-foot"],
        )

        view_btn.click(admin_view,
                       inputs=admin_pass, outputs=[all_df, action_msg])

        approve_btn.click(
            admin_approve,
            inputs=[admin_pass, idx_in],
            outputs=[action_msg, leaderboard_html, all_df, diag_md],
        )
        reject_btn.click(
            admin_reject,
            inputs=[admin_pass, idx_in],
            outputs=[action_msg, leaderboard_html, all_df, diag_md],
        )
        delete_btn.click(
            admin_delete,
            inputs=[admin_pass, idx_in],
            outputs=[action_msg, leaderboard_html, all_df, diag_md],
        )

    with gr.Tab("ℹ️ About"):
        gr.Markdown(
            "**ViTeX-Bench** evaluates video scene text editing on a frozen "
            "157-clip test split sourced from Panda-70M and InternVid, "
            "across three orthogonal axes:\n\n"
            "- **Text correctness** β€” SeqAcc, CharAcc, TTS via PP-OCRv5 "
            "with per-clip language routing and substring edit distance.\n"
            "- **Visual quality** β€” Flicker, Warp (RAFT-flow), MUSIQ; "
            "each at full-frame and text-crop scope.\n"
            "- **Edit locality** β€” PSNR, SSIM, LPIPS on a locality-only "
            "prediction, plus DreamSim for VAE-noise-robust similarity.\n\n"
            "Ranking key **TextScore** = βˆ›(SeqAcc Β· CharAcc Β· TTS). "
            "Each primitive is natively in [0, 1]; SeqAcc = 0 collapses "
            "TextScore to zero β€” the intended semantics for methods that "
            "never produce the requested target string. The full thirteen-"
            "metric vector remains the unit of report.\n\n"
            "Full protocol, normalization rules, and per-axis weights live "
            "in [`docs/PROTOCOL.md`](https://huggingface.co/ViTeX-Bench/"
            "ViTeX-Bench/blob/main/docs/PROTOCOL.md) on the Benchmark code "
            "repo.\n\n"
            "Anonymous release under double-blind review at NeurIPS 2026 "
            "Datasets and Benchmarks Track. Author list and DOI updated "
            "after deanonymization."
        )


demo.queue()

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