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 
 
 
d16cee2
5233c22
a8c346c
 
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
import os
import json
import numpy as np
import pandas as pd
import gradio as gr
from datetime import datetime, timedelta
from huggingface_hub.hf_api import ModelInfo
from transformers import AutoConfig, AutoTokenizer
from huggingface_hub import HfApi, hf_hub_download, ModelCard
from apscheduler.schedulers.background import BackgroundScheduler

################################################################################
# GLOBALS & CONSTANTS
################################################################################

OWNER = "OALL"
REPO_ID = f"{OWNER}/Open-Arabic-LLM-Leaderboard-v2-exp"
RESULTS_REPO_ID = f"{OWNER}/v2_results"
REQUESTS_REPO_ID = f"{OWNER}/requests_v2"

# Global HF API instance (set once)
hf_api_token = os.environ.get('HF_API_TOKEN', None)
API = HfApi(token=hf_api_token)

TASKS = [
    ("community|alghafa:_average|0",        "acc_norm",     "AlGhafa"),
    ("community|arabic_mmlu:_average|0",    "acc_norm",     "ArabicMMLU"),
    ("community|arabic_exams|0",            "acc_norm",     "EXAMS"),
    ("community|madinah_qa:_average|0",     "acc_norm",     "MadinahQA"),
    ("community|aratrust:_average|0",       "acc_norm",     "AraTrust"),
    ("community|alrage_qa|0",               "llm_as_judge", "ALRAGE"),
    ("community|arabic_mmlu_ht:_average|0", "acc_norm",     "ArbMMLU-HT"),
]

MODEL_TYPE_TO_EMOJI = {
    "🟢 : pretrained": "🟢",
    "🟩 : continuously pretrained": "🟩",
    "💬 : chat models (RLHF, DPO, IFT, ...)": "💬",
    "🔶 : fine-tuned on domain-specific datasets": "🔶",
    "🤝 : base merges and merges": "🤝",
    "Missing": "?",
}

HEADER = """
<img src="https://raw.githubusercontent.com/alielfilali01/OALL-assets/main/TITLE.png" style="width:30%;display:block;margin-left:auto;margin-right:auto;border-radius:15px;">
"""

BOTTOM_LOGO = """<img src="https://raw.githubusercontent.com/alielfilali01/OALL-assets/main/BOTTOM.png" style="width:50%;display:block;margin-left:auto;margin-right:auto;border-radius:15px;">"""

SUBMISSION_TEXT = """
# Submit Your Model for Evaluation 🌴 

**The Open Arabic LLM Leaderboard** aims to help you evaluate and compare the performance of Arabic Large Language Models.

When you submit a model on this page, it is automatically evaluated on a set of arabic native benchmarks ([find here](https://github.com/huggingface/lighteval/blob/main/examples/tasks/OALL_v2_tasks.txt)) with one additional human-translated version of [MMLU](https://arxiv.org/abs/2009.03300).

The GPU used for evaluation is operated with the support of  __[Technology Innovation Institute (TII)](https://www.tii.ae/)__.

More details about the benchmarks and the evaluation process is provided on the “About” section below.

Find the first version of the leaderboard hosted as Legacy in this [Space](https://huggingface.co/spaces/OALL/Open-Arabic-LLM-Leaderboard-v1).
"""

ABOUT_SECTION = """
## About

While outstanding LLM models are being released competitively, most of them are centered on English and are familiar with the English cultural sphere. We operate the Open Arabic LLM Leaderboard (OALL), to evaluate models that reflect the characteristics of the Arabic language, culture and heritage. Through this, we hope that users can conveniently use the leaderboard, participate, and contribute to the advancement of research in the Arab region 🔥.

### Icons & Model types
🟢 : `pretrained`

🟩 : `continuously pretrained`

💬 : `chat models (RLHF, DPO, IFT, ...)`

🔶 : `fine-tuned on domain-specific datasets`

🤝 : `base merges and moerges`

### Notes:
- We reserve the right to correct any incorrect tags or icons after manual verification to ensure the accuracy and reliability of the leaderboard. This helps maintain the integrity and trustworthiness of the platform.
- Some models may be flagged as “Subjects of Caution” by the community. These models might have used the evaluation set for training, attempted to manipulate rankings, or raised ethical concerns. Models deemed as such may face restricted visibility or removal from the leaderboard. Users are advised to exercise discretion when interpreting rankings.
- The leaderboard automatically hides models that were submitted, evaluated, and subsequently made private or gated post-evaluation. This platform is designed for **“open”** models that benefit the wider community. If you intend to restrict your model’s accessibility after using the leaderboard’s resources or exploit the platform solely for personal gains, please refrain from submitting. Violators may face bans on their usernames and/or organization IDs from future submissions.
- The leaderboard no longer accepts models in **float32** precision except under special circumstances. If you are the developer of a float32 model and believe it deserves inclusion, please reach out to us.
- To ensure fair and equitable access to leaderboard resources, all usernames and organization IDs are limited to **5 submissions per week**. This policy minimizes spamming, encourages thoughtful participation, and allows everyone in the community to benefit from the platform.

By adhering to these guidelines, we aim to foster a fair, collaborative, and transparent environment for evaluating and advancing open models for the arabic/arabic-interested communities.

### How it works

📈 We evaluate models using [LightEval](https://github.com/huggingface/lighteval), a unified and straightforward framework from the HuggingFace Eval Team to test and assess causal language models on a large number of different evaluation tasks.

To ensure a fair and unbiased assessment of the models' true capabilities, all evaluations are conducted in zero-shot settings `0-shots`. This approach eliminates any potential advantage from task-specific fine-tuning, providing a clear indication of how well the models can generalize to new tasks.

Also, given the nature of the tasks, which include multiple-choice questions, the leaderboard primarily uses normalized log likelihood accuracy `loglikelihood_acc_norm` for all tasks.

Please, consider reaching out to us through the discussions tab if you are working on benchmarks for Arabic LLMs and willing to see them on this leaderboard as well. Your benchmark might change the whole game for Arabic models !

### Details and Logs
- Detailed numerical results in the `results` OALL dataset: https://huggingface.co/datasets/OALL/v2_results
- Community queries and running status in the `requests` OALL dataset: https://huggingface.co/datasets/OALL/requests_v2

### More resources
For evaluations of chat models using 3C3H on generative tasks benchmarks, please refer to the [AraGen-Leaderboard](https://huggingface.co/spaces/inceptionai/AraGen-Leaderboard).

If you still have questions, you can check our FAQ [here](https://huggingface.co/spaces/OALL/Open-Arabic-LLM-Leaderboard/discussions/15)!

"""

CITATION_BUTTON_LABEL = """
Copy the following snippet to cite these results
"""

CITATION_BUTTON_TEXT = """
@misc{OALL-2,
  author = {El Filali, Ali and ALOUI, Manel and Husaain, Tarique and Alzubaidi, Ahmed and Boussaha, Basma El Amel and Cojocaru, Ruxandra and Fourrier, Clémentine and Habib, Nathan and Hacid, Hakim},
  title = {Open Arabic LLM Leaderboard 2},
  year = {2025},
  publisher = {OALL},
  howpublished = {https://huggingface.co/spaces/OALL/Open-Arabic-LLM-Leaderboard}
}
"""

################################################################################
# UTILITY & HELPER FUNCTIONS
################################################################################

def model_hyperlink(model_name):
    link = f"https://huggingface.co/{model_name}"
    # return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'
    # return f'[{model_name}]({link})' # WHYYYYYYYY It is not working !!!???
    return f"{model_name}"

def restart_space():
    """Restart the Gradio space periodically."""
    API.restart_space(repo_id=REPO_ID)

def unify_precision(raw_precision: str) -> str:
    """
    Map raw precision strings (e.g. 'torch.float16', 'fp16', 'float16')
    to canonical forms: 'float16', 'float32', 'bfloat16', '8bit', '4bit', 'Missing'.
    """
    if not raw_precision or raw_precision.lower() in ["missing", "unk", "none"]:
        return "Missing"
    p = raw_precision.lower()
    if p in ["torch.float16", "float16", "fp16"]:
        return "float16"
    if p in ["torch.float32", "float32", "fp32"]:
        return "float32"
    if p in ["torch.bfloat16", "bfloat16", "bf16"]:
        return "bfloat16"
    if p == "8bit":
        return "8bit"
    if p == "4bit":
        return "4bit"
    return "Missing"

def load_requests(status_folder: str) -> pd.DataFrame:
    """
    Load all .json requests from REQUESTS_REPO_ID, filtering by 'status' == status_folder.
    """
    df_out = []
    try:
        files_info = API.list_repo_files(
            repo_id=REQUESTS_REPO_ID,
            repo_type="dataset",
            token=hf_api_token
        )
        json_files = [f for f in files_info if f.endswith(".json")]
    except Exception as e:
        print(f"Error listing files in {REQUESTS_REPO_ID}: {e}")
        return pd.DataFrame()

    for path in json_files:
        try:
            local_path = hf_hub_download(
                repo_id=REQUESTS_REPO_ID,
                filename=path,
                repo_type="dataset",
                token=hf_api_token
            )
            with open(local_path, "r", encoding="utf-8") as f:
                req = json.load(f)
        except Exception as e:
            print(f"Error loading {path}: {e}")
            continue

        if str(req.get("status", "")).strip().lower() == status_folder.lower():
            df_out.append(req)

    return pd.DataFrame(df_out)

def load_all_requests() -> pd.DataFrame:
    """
    Load *all* requests from the dataset (pending, finished, failed, etc.).
    Returns a single DataFrame with columns from all requests.
    """
    df_out = []
    try:
        files_info = API.list_repo_files(
            repo_id=REQUESTS_REPO_ID,
            repo_type="dataset",
            token=hf_api_token
        )
        json_files = [f for f in files_info if f.endswith(".json")]
    except Exception as e:
        print(f"Error listing files in {REQUESTS_REPO_ID}: {e}")
        return pd.DataFrame()

    for path in json_files:
        try:
            local_path = hf_hub_download(
                repo_id=REQUESTS_REPO_ID,
                filename=path,
                repo_type="dataset",
                token=hf_api_token
            )
            with open(local_path, "r", encoding="utf-8") as f:
                req = json.load(f)
            df_out.append(req)
        except Exception as e:
            print(f"Error loading {path}: {e}")
            continue

    return pd.DataFrame(df_out)

def already_in_queue(df_pending: pd.DataFrame, model_name: str, revision: str, precision: str) -> bool:
    """
    Check if (model, revision, precision) is already in the 'pending' queue.
    """
    if df_pending.empty:
        return False
    matched = df_pending[
        (df_pending["model"] == model_name)
        & (df_pending["revision"] == revision)
        & (df_pending["precision"] == unify_precision(precision))
    ]
    return not matched.empty

def get_model_size(model_info: ModelInfo, precision: str) -> float:
    """
    Return approximate model parameter size in billions, if safetensors info is available.
    Return 0 if unknown.
    For GPTQ, we do a small multiplier to reflect the extra bits, etc.
    """
    try:
        param_bytes = model_info.safetensors.get("total", 0)
        model_size = round(param_bytes / 1e9, 3)
    except (AttributeError, TypeError):
        return 0.0

    if "gptq" in model_info.modelId.lower():
        return model_size * 8
    return model_size

def parse_datetime(dt_str: str) -> datetime:
    """
    Safely parse an ISO datetime string into a Python datetime object.
    """
    try:
        return datetime.fromisoformat(dt_str.replace("Z", ""))
    except Exception:
        return datetime.min

################################################################################
# SCOREBOARD LOADING & DISPLAY
################################################################################

def load_scoreboard() -> pd.DataFrame:
    """
    1) Reads JSON "results_*.json" from RESULTS_REPO_ID to collect scores.
    2) Combines with "finished" requests data for license, revision, etc.
    3) Removes any model that is no longer public or accessible.
    4) Returns a DataFrame ready for display & filtering.
    
    Only models with finished evaluations are kept.
    """
    # Step A: Get scoreboard files from the results dataset
    try:
        files_info = API.list_repo_files(
            repo_id=RESULTS_REPO_ID,
            repo_type="dataset",
            token=hf_api_token
        )
    except Exception as e:
        print(f"Error listing scoreboard files in {RESULTS_REPO_ID}: {e}")
        return pd.DataFrame()

    candidate_json_paths = [
        path for path in files_info
        if path.endswith(".json") and len(path.split("/")) == 3 and path.split("/")[2].startswith("results_")
    ]

    rows = []
    # Step B: Read each scoreboard file
    for file_path in candidate_json_paths:
        try:
            local_file = hf_hub_download(
                repo_id=RESULTS_REPO_ID,
                filename=file_path,
                repo_type="dataset",
                token=hf_api_token
            )
            with open(local_file, "r", encoding="utf-8") as f:
                data = json.load(f)
        except Exception as e:
            print(f"Error loading scoreboard file {file_path}: {e}")
            continue

        config_general = data.get("config_general", {})
        results_block = data.get("results", {})

        model_name  = config_general.get("model_name", "UNK")
        scoreboard_precision = unify_precision(config_general.get("model_dtype", "Missing"))
        # To be consistent with submission logic:
        if scoreboard_precision == "Missing":
            scoreboard_precision = "UNK"
        scoreboard_model_type  = config_general.get("model_type", "Missing")

        row_dict = {
            "Model Name":   model_name,
            "Revision":     "Missing",  # We'll fill from requests
            "License":      "Missing",  # We'll fill from requests
            "Precision":    scoreboard_precision,
            "Full Type":    scoreboard_model_type,
            "Model Size":   0.0,        # We'll fill from requests
            "Hub ❤️":       0,          # We'll fill from requests
        }

        # Fill tasks
        for (task_key, metric_field, display_name) in TASKS:
            val = np.nan
            if task_key in results_block:
                subd = results_block[task_key]
                if isinstance(subd, dict) and metric_field in subd:
                    val = subd[metric_field]
            row_dict[display_name] = val

        rows.append(row_dict)

    df = pd.DataFrame(rows)
    if df.empty:
        base_cols = [
            "Model Name","Revision","License",
            "Precision","Full Type","Model Size","Hub ❤️"
        ]
        task_names = [t[2] for t in TASKS]
        return pd.DataFrame(columns=base_cols + task_names)

    # Step C: Convert tasks to numeric & multiply by 100
    task_cols = [t[2] for t in TASKS if t[2] in df.columns]
    df[task_cols] = df[task_cols].apply(pd.to_numeric, errors="coerce")
    for c in task_cols:
        df[c] = (df[c] * 100).round(2)

    # Step D: Compute average
    if task_cols:
        df["Average ⬆️"] = df[task_cols].mean(axis=1).round(2)
    else:
        df["Average ⬆️"] = np.nan

    # Step E: Overwrite scoreboard data with "finished" requests (except for Precision)
    df_finished = load_requests("finished")
    if not df_finished.empty:
        df_finished["precision"] = df_finished["precision"].apply(unify_precision)
        df_finished["license"]   = df_finished["license"].apply(
            lambda x: ", ".join(x) if isinstance(x, list) else str(x)
        )
        df_finished["model_type"] = df_finished["model_type"].apply(
            lambda x: ", ".join(x) if isinstance(x, list) else str(x)
        )

        # Group by model name and precision to correctly distinguish multiple submissions
        dff_grouped = df_finished.groupby(["model", "precision"], as_index=False).last()

        request_map = {}
        for _, row_ in dff_grouped.iterrows():
            key = f"{row_['model']}__{row_['precision']}"
            request_map[key] = {
                "license":   row_["license"],
                "revision":  row_["revision"],
                "precision": row_["precision"],
                "model_type":row_["model_type"],
                "params":    row_["params"],
                "likes":     row_["likes"]
            }

        def apply_request_info(row_):
            key = f"{row_['Model Name']}__{row_['Precision']}"
            if key in request_map:
                row_["License"]    = request_map[key]["license"]
                row_["Revision"]   = request_map[key]["revision"]
                # Do NOT update "Precision": keep the value from the results file.
                row_["Full Type"]  = request_map[key]["model_type"]
                row_["Model Size"] = request_map[key]["params"]
                row_["Hub ❤️"]     = request_map[key]["likes"]
            return row_

        df = df.apply(apply_request_info, axis=1)

    # Step E2: Remove rows that do not have finished request info (i.e. Revision is still "Missing")
    df = df[df["Revision"] != "Missing"]

    # Step F: Remove any model not public
    remove_idx = []
    for idx, row in df.iterrows():
        model_name = row["Model Name"]
        if model_name == "UNK":
            remove_idx.append(idx)
            continue
        try:
            API.model_info(model_name)
        except Exception:
            remove_idx.append(idx)
    df.drop(remove_idx, inplace=True)
    df.reset_index(drop=True, inplace=True)

    # Step G: Sort scoreboard
    df = df.sort_values(by="Average ⬆️", ascending=False).reset_index(drop=True)

    # Step H: Insert ranking & create a "Model Size Filter" for slider usage
    df.insert(0, "Rank", range(1, len(df) + 1))
    df["Model Size Filter"] = df["Model Size"]

    # Step I: Short label for the model type
    def map_type_to_emoji(full_str):
        if not isinstance(full_str, str):
            return "Missing"
        return MODEL_TYPE_TO_EMOJI.get(full_str.strip(), full_str.strip())
    df["T"] = df["Full Type"].apply(map_type_to_emoji)

    # At this point, convert "Model Name" to a clickable link
    df["Model Name"] = df["Model Name"].apply(model_hyperlink)

    # Reorder columns
    final_cols = ["Rank", "T", "Model Name", "Average ⬆️"] + task_cols
    remainder = ["Model Size", "Hub ❤️", "License", "Precision", "Revision", "Model Size Filter", "Full Type"]
    for rc in remainder:
        if rc not in final_cols and rc in df.columns:
            final_cols.append(rc)

    return df[final_cols]

################################################################################
# SUBMISSION LOGIC
################################################################################

def check_model_card(repo_id: str) -> (bool, str):
    """Check if model card is present, has a license, and is not too short."""
    try:
        card = ModelCard.load(repo_id)
    except Exception:
        return (False, "No model card found. Please add a README.md describing your model and license.")
    if card.data.license is None and not ("license_name" in card.data and "license_link" in card.data):
        return (False, "No license metadata found in the model card.")
    if len(card.text) < 200:
        return (False, "Model card is too short (<200 chars). Please add more details.")
    return (True, "")

def is_model_on_hub(model_name, revision, token=None, trust_remote_code=False, test_tokenizer=True):
    """Check if the model & tokenizer can be loaded from the Hub."""
    try:
        config = AutoConfig.from_pretrained(
            model_name,
            revision=revision,
            trust_remote_code=trust_remote_code,
            token=token
        )
    except ValueError:
        return (False, "requires `trust_remote_code=True`. Not automatically allowed.", None)
    except Exception as e:
        return (False, f"not loadable from hub: {str(e)}", None)

    if test_tokenizer:
        try:
            _ = AutoTokenizer.from_pretrained(
                model_name,
                revision=revision,
                trust_remote_code=trust_remote_code,
                token=token
            )
        except Exception as e:
            return (False, f"tokenizer not loadable: {str(e)}", None)

    return (True, "", config)

def check_org_threshold(org_name: str) -> (bool, str):
    """
    Each org can only submit 5 models in the last 7 days.
    Return (True, "") if allowed. Otherwise, (False, "error message").
    """
    df_all = load_all_requests()
    if df_all.empty:
        return (True, "")

    def get_org(m):
        try:
            return m.split("/")[0]
        except:
            return m

    df_all["org_name"] = df_all["model"].apply(get_org)
    df_org = df_all[df_all["org_name"] == org_name].copy()
    if df_org.empty:
        return (True, "")

    df_org["datetime"] = df_org["submitted_time"].apply(parse_datetime)
    df_org.dropna(subset=["datetime"], inplace=True)

    now = datetime.utcnow()
    week_ago = now - timedelta(days=7)
    df_recent = df_org[df_org["datetime"] >= week_ago]

    if len(df_recent) >= 5:
        df_recent_sorted = df_recent.sort_values(by="datetime")
        earliest = df_recent_sorted.iloc[0]["datetime"]
        next_ok = earliest + timedelta(days=7)
        msg_next = next_ok.isoformat(timespec="seconds") + "Z"
        return (
            False,
            f"Your org '{org_name}' has reached the 5-submissions-per-week limit. You can submit again after {msg_next}."
        )
    return (True, "")

def submit_model(
    model_name: str,
    base_model: str,
    revision: str,
    precision: str,
    weight_type: str,
    model_type: str,
    chat_template: str
):
    # -------------------------------------------------------------------------
    # 0) Strip inputs to avoid trailing or leading spaces
    # -------------------------------------------------------------------------
    model_name  = model_name.strip()
    base_model  = base_model.strip()
    revision    = revision.strip()
    precision   = precision.strip()
    
    if not model_name:
        return "**Error**: Model name cannot be empty (use 'org/model')."
    if not revision:
        revision = "main"

    # 1) Check model card
    card_ok, card_msg = check_model_card(model_name)
    if not card_ok:
        return f"**Error**: {card_msg}"

    # 2) If adapter/delta, check base_model
    if weight_type.lower() in ["adapter", "delta"]:
        if not base_model:
            return "**Error**: For adapter/delta, you must provide a valid `base_model`."
        ok_base, base_err, _ = is_model_on_hub(base_model, revision, hf_api_token, trust_remote_code=True, test_tokenizer=True)
        if not ok_base:
            return f"**Error**: Base model '{base_model}' {base_err}"
    else:
        ok_model, model_err, _ = is_model_on_hub(model_name, revision, hf_api_token, trust_remote_code=True, test_tokenizer=True)
        if not ok_model:
            return f"**Error**: Model '{model_name}' {model_err}"

    # 3) Retrieve ModelInfo
    try:
        info = API.model_info(model_name, revision=revision, token=hf_api_token)
    except Exception as e:
        return f"**Error**: Could not fetch model info. {str(e)}"

    model_license = info.card_data.license
    model_likes   = info.likes or 0
    model_private = bool(getattr(info, "private", False))

    # 4) Check duplicates
    df_pending = load_requests("pending")
    df_finished = load_requests("finished")
    
    if already_in_queue(df_finished, model_name, revision, precision):
        return f"**Warning**: '{model_name}' with (rev='{revision}', prec='{precision}') has already been evaluated (status FINISHED)."
    elif already_in_queue(df_pending, model_name, revision, precision):
        return f"**Warning**: '{model_name}' with (rev='{revision}', prec='{precision}') is already in PENDING."

    # 5) Check threshold
    try:
        org = model_name.split("/")[0]
    except:
        org = model_name
    under_threshold, message = check_org_threshold(org)
    if not under_threshold:
        return f"**Error**: {message}"

    precision_final = unify_precision(precision)
    if precision_final == "Missing":
        precision_final = "UNK"

    model_params = get_model_size(model_info=info, precision=precision)
    current_time = datetime.utcnow().isoformat() + "Z"
    # Convert chat_template input to boolean: True if "Yes", False if "No"
    chat_template_bool = True if chat_template.strip().lower() == "yes" else False

    submission = {
        "model":        model_name,
        "base_model":   base_model.strip(),
        "revision":     revision,
        "precision":    precision_final,
        "weight_type":  weight_type,
        "status":       "PENDING",
        "submitted_time": current_time,
        "model_type":   model_type,
        "likes":        model_likes,
        "params":       model_params,
        "license":      model_license,
        "private":      model_private,
        "job_id":       None,
        "job_start_time": None,
        "chat_template": chat_template_bool
    }

    # Must be "org/repo"
    try:
        org_, repo_id = model_name.split("/")
    except ValueError:
        return "**Error**: Please specify model as 'org/model'. Note that `org` can be `username` as well."

    private_str = "True" if model_private else "False"
    file_path_in_repo = f"{org_}/{repo_id}_eval_request_{private_str}_{precision_final}_{weight_type}.json"

    # 6) Upload submission
    try:
        API.upload_file(
            path_or_fileobj=json.dumps(submission, indent=2).encode("utf-8"),
            path_in_repo=file_path_in_repo,
            repo_id=REQUESTS_REPO_ID,
            repo_type="dataset",
            token=hf_api_token,
            commit_message=f"Add {model_name} to eval queue"
        )
    except Exception as e:
        return f"**Error**: Could not upload to '{REQUESTS_REPO_ID}': {str(e)}"

    return f"**Success**: Model '{model_name}' submitted for evaluation!"

################################################################################
# MAIN GRADIO APP
################################################################################

def main():
    # Periodically restart the Space (e.g., every 30 minutes)
    scheduler = BackgroundScheduler()
    scheduler.add_job(restart_space, "interval", hours=1)
    scheduler.start()

    df_tasks = load_scoreboard()

    # Prepare filter choices from 'finished' requests
    df_finished = load_requests("finished")
    if not df_finished.empty:
        df_finished["precision"]   = df_finished["precision"].apply(unify_precision)
        df_finished["license"]     = df_finished["license"].apply(
            lambda x: ", ".join(x) if isinstance(x, list) else str(x)
        )
        df_finished["model_type"]  = df_finished["model_type"].apply(
            lambda x: ", ".join(x) if isinstance(x, list) else str(x)
        )

        precision_options = sorted(df_finished["precision"].dropna().unique().tolist())
        license_options   = sorted(df_finished["license"].dropna().unique().tolist())
        model_type_opts   = sorted(df_finished["model_type"].dropna().unique().tolist())

        for lst in [precision_options, license_options, model_type_opts]:
            if "Missing" not in lst:
                lst.append("Missing")
    else:
        precision_options = ["float16", "bfloat16", "8bit", "4bit", "Missing"]
        license_options   = ["Missing"]
        model_type_opts   = ["Missing"]

    if not df_tasks.empty:
        min_model_size = int(df_tasks["Model Size Filter"].min())
        max_model_size = int(df_tasks["Model Size Filter"].max())
    else:
        min_model_size, max_model_size = 0, 1000

    all_columns = df_tasks.columns.tolist() if not df_tasks.empty else []
    # We don't want to show "Model Size Filter" or "Full Type" directly
    hidden_cols = {"Model Size Filter", "Full Type"}
    for h in hidden_cols:
        if h in all_columns:
            all_columns.remove(h)

    task_cols   = [t[2] for t in TASKS if t[2] in df_tasks.columns]
    default_cols = ["Rank", "T", "Model Name", "Average ⬆️"] + task_cols
    default_cols = [c for c in default_cols if c in all_columns]

    with gr.Blocks() as demo:
        gr.HTML(HEADER)
        with gr.Tabs():
            ####################################################################
            # TAB 1: LLM Leaderboard
            ####################################################################
            with gr.Tab("🏅 LLM Leaderboard"):
                with gr.Row():
                    search_box = gr.Textbox(
                        label="Search",
                        placeholder="Search for models...",
                        interactive=True
                    )
                with gr.Row():
                    col_selector = gr.CheckboxGroup(
                        choices=all_columns,
                        value=default_cols,
                        label="Select columns to display"
                    )
                    t_filter = gr.CheckboxGroup(
                        choices=model_type_opts,
                        value=model_type_opts.copy(),
                        label="Filter by Model Type"
                    )
                with gr.Row():
                    license_filter = gr.CheckboxGroup(
                        choices=license_options,
                        value=license_options.copy(),
                        label="Filter by License"
                    )
                    precision_filter = gr.CheckboxGroup(
                        choices=precision_options,
                        value=precision_options.copy(),
                        label="Filter by Precision"
                    )
                with gr.Row():
                    size_min_slider = gr.Slider(
                        minimum=min_model_size,
                        maximum=max_model_size,
                        value=min_model_size,
                        step=1,
                        label="Minimum Model Size (params)",
                        interactive=True
                    )
                    size_max_slider = gr.Slider(
                        minimum=min_model_size,
                        maximum=max_model_size,
                        value=max_model_size,
                        step=1,
                        label="Maximum Model Size (params)",
                        interactive=True
                    )

                leaderboard = gr.Dataframe(
                    value=df_tasks[default_cols] if not df_tasks.empty else pd.DataFrame(columns=default_cols),
                    interactive=False
                )

                def filter_by_full_type(dff, selected_full_types):
                    incl_missing = "Missing" in selected_full_types
                    if incl_missing:
                        return dff[
                            (dff["Full Type"].isin(selected_full_types))
                            | (dff["Full Type"].isna())
                            | (dff["Full Type"] == "")
                            | (dff["Full Type"] == "Missing")
                        ]
                    else:
                        return dff[dff["Full Type"].isin(selected_full_types)]

                def filter_leaderboard(
                    search_query,
                    selected_cols,
                    t_filter_values,
                    lic_filter_values,
                    prec_filter_values,
                    min_sz,
                    max_sz
                ):
                    dff = df_tasks.copy()
                    # 1) Filter by size
                    if min_sz > max_sz:
                        min_sz, max_sz = max_sz, min_sz
                    dff = dff[(dff["Model Size Filter"] >= min_sz+1) & (dff["Model Size Filter"] <= max_sz+1)]
                    # 2) Search by name
                    dff["plain_name"] = dff["Model Name"].str.replace(r'<.*?>', '', regex=True)
                    if search_query:
                        dff = dff[dff["plain_name"].str.contains(search_query, case=False, na=False)]
                    # 3) Filter by model type
                    if t_filter_values:
                        dff = filter_by_full_type(dff, t_filter_values)
                    # 4) Filter by license
                    if lic_filter_values:
                        incl_missing = "Missing" in lic_filter_values
                        chosen = [l for l in lic_filter_values if l != "Missing"]
                        if incl_missing:
                            dff = dff[
                                dff["License"].isin(chosen)
                                | (dff["License"] == "Missing")
                                | (dff["License"].isna())
                            ]
                        else:
                            dff = dff[dff["License"].isin(chosen)]
                    # 5) Filter by precision
                    if prec_filter_values:
                        incl_missing = "Missing" in prec_filter_values
                        chosen = [p for p in prec_filter_values if p != "Missing"]
                        if incl_missing:
                            dff = dff[
                                dff["Precision"].isin(chosen)
                                | (dff["Precision"].isna())
                                | (dff["Precision"] == "Missing")
                                | (dff["Precision"] == "UNK")
                            ]
                        else:
                            dff = dff[dff["Precision"].isin(chosen)]

                    dff.drop(columns=["plain_name"], inplace=True, errors="ignore")
                    final_cols = [col for col in dff.columns if col in selected_cols]
                    return dff[final_cols]

                filter_inputs = [
                    search_box, col_selector, t_filter,
                    license_filter, precision_filter,
                    size_min_slider, size_max_slider
                ]
                search_box.submit(filter_leaderboard, inputs=filter_inputs, outputs=leaderboard)
                for comp in filter_inputs:
                    comp.change(filter_leaderboard, inputs=filter_inputs, outputs=leaderboard)

            ####################################################################
            # TAB 2: Submit here
            ####################################################################
            with gr.Tab("🚀 Submit here!"):
                gr.Markdown(SUBMISSION_TEXT)

                with gr.Row():
                    model_name_box = gr.Textbox(
                        label="Model Name",
                        placeholder="myorg/mymodel",
                        interactive=True
                    )
                    revision_box = gr.Textbox(
                        label="Revision Commit",
                        placeholder="main",
                        value="main",
                        interactive=True
                    )
                with gr.Row():
                    model_type_box = gr.Dropdown(
                        label="Model Type",
                        choices=list(MODEL_TYPE_TO_EMOJI.keys()),
                        value="💬 : chat models (RLHF, DPO, IFT, ...)",
                        interactive=True
                    )
                    weight_type_box = gr.Dropdown(
                        label="Weight Type",
                        choices=["Original", "Adapter", "Delta"],
                        value="Original",
                        interactive=True
                    )
                with gr.Row():
                    precision_box = gr.Dropdown(
                        label="Precision",
                        choices=["float16", "bfloat16", "8bit", "4bit"],
                        value="bfloat16",
                        interactive=True
                    )
                    base_model_box = gr.Textbox(
                        label="Base Model (if adapter or delta weights)",
                        placeholder="(Optional) e.g. myorg/base-model",
                        interactive=True
                    )
                with gr.Row():
                    chat_template_box = gr.Radio(
                        label="Evaluate using chat-template?",
                        choices=["Yes", "No"],
                        value="No",
                        interactive=True
                    )

                submit_btn = gr.Button("Submit Model")
                submit_out = gr.Markdown()

                submit_btn.click(
                    fn=submit_model,
                    inputs=[model_name_box, base_model_box, revision_box, precision_box, weight_type_box, model_type_box, chat_template_box],
                    outputs=submit_out
                )

                df_pending  = load_requests("pending")
                df_running  = load_requests("running")
                df_finished2= load_requests("finished")
                df_failed   = load_requests("failed")

                gr.Markdown("## Evaluation Status")
                with gr.Accordion(f"Pending Evaluations ({len(df_pending)})", open=False):
                    if not df_pending.empty:
                        gr.Dataframe(df_pending)
                    else:
                        gr.Markdown("No pending evaluations.")
                with gr.Accordion(f"Running Evaluations ({len(df_running)})", open=False):
                    if not df_running.empty:
                        gr.Dataframe(df_running)
                    else:
                        gr.Markdown("No running evaluations.")
                with gr.Accordion(f"Finished Evaluations ({len(df_finished2)})", open=False):
                    if not df_finished2.empty:
                        gr.Dataframe(df_finished2)
                    else:
                        gr.Markdown("No finished evaluations.")
                with gr.Accordion(f"Failed Evaluations ({len(df_failed)})", open=False):
                    if not df_failed.empty:
                        gr.Dataframe(df_failed)
                    else:
                        gr.Markdown("No failed evaluations.")

                gr.Markdown(ABOUT_SECTION)

        with gr.Row():
            with gr.Accordion("📙 Citation", open=False):
                citation_box = gr.Textbox(
                    value=CITATION_BUTTON_TEXT,
                    label=CITATION_BUTTON_LABEL,
                    lines=9,
                    elem_id="citation-button",
                    show_copy_button=True
                )
                
        gr.HTML(BOTTOM_LOGO)
        
    demo.queue(default_concurrency_limit=40).launch()

if __name__ == "__main__":
    main()