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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
import gradio as gr
from gradio_leaderboard import Leaderboard, SelectColumns, SearchColumns
import config
from pathlib import Path
import pandas as pd
import json

import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, Literal
import pandas as pd
from pandas.io.formats.style import Styler

import semantic_version
from dataclasses import dataclass, field

from gradio.components import Component
from gradio.data_classes import GradioModel
from gradio.events import Events

@dataclass
class SelectColumns:
    default_selection: Optional[list[str]] = field(default_factory=list)
    cant_deselect: Optional[list[str]] = field(default_factory=list)
    allow: bool = True
    label: Optional[str] = None
    show_label: bool = True
    info: Optional[str] = None

@dataclass
class ColumnFilter:
    column: str
    type: Literal["slider", "dropdown", "checkboxgroup", "boolean"] = None
    default: Optional[Union[int, float, List[Tuple[str, str]]]] = None
    choices: Optional[Union[int, float, List[Tuple[str, str]]]] = None
    label: Optional[str] = None
    info: Optional[str] = None
    show_label: bool = True
    min: Optional[Union[int, float]] = None
    max: Optional[Union[int, float]] = None
    
class DataframeData(GradioModel):
    headers: List[str]
    data: Union[List[List[Any]], List[Tuple[Any, ...]]]
    metadata: Optional[Dict[str, Optional[List[Any]]]] = None


abs_path = Path(__file__).parent

# Load the leaderboard data for 
zero_shot_df = pd.read_json("leaderboards/Zero-Shot_leaderboard_data.json", precise_float=True)
five_shot_df = pd.read_json("leaderboards/Few-Shot_leaderboard_data.json", precise_float=True)
cot_df = pd.read_json("leaderboards/CoT_leaderboard_data.json", precise_float=True)

# Original Average Performances
original_zero_shot_avg_perf = zero_shot_df["Average Performance"]
original_five_shot_avg_perf = five_shot_df["Average Performance"]
original_cot_avg_perf = cot_df["Average Performance"]

# Load the task information json data
with open("task_information.json", 'r') as file:
    task_information_json = json.load(file)

cot_currently_selected_filters = {
    "Language": [],
    "Task Type": [],
    "Clinical Context": [],
    "Data Access": [],
    "Applications": [],
    "Clinical Stage": []
}

five_shot_currently_selected_filters = {
    "Language": [],
    "Task Type": [],
    "Clinical Context": [],
    "Data Access": [],
    "Applications": [],
    "Clinical Stage": []
}

zero_shot_currently_selected_filters = {
    "Language": [],
    "Task Type": [],
    "Clinical Context": [],
    "Data Access": [],
    "Applications": [],
    "Clinical Stage": []
}

# with open("/Users/kevinxie/Desktop/Clinical NLP/Clinical-Text-Leaderboard/leaderboard_data.json", 'r') as file:
with open("leaderboards/Few-Shot_leaderboard_data.json", 'r') as file:
    five_shot_leaderboard_json = json.load(file)

with open("leaderboards/CoT_leaderboard_data.json", 'r') as file:
    CoT_leaderboard_json = json.load(file)

with open("leaderboards/Zero-Shot_leaderboard_data.json", 'r') as file:
    zero_shot_leaderboard_json = json.load(file)

valid_tasks = {'NUBES', 'NorSynthClinical-NER', 'MEDIQA 2023-sum-A', 'Medication extraction', 
               'IMCS-V2-DAC', 'Cantemist-Coding', 'IFMIR-NER', 'EHRQA-QA', 'Ex4CDS', 'MedDG', 
               'MTS-Temporal', 'CHIP-MDCFNPC', 'n2c2 2014-Diabetes', 'MIMIC-III Outcome.LoS', 
               'n2c2 2014-Hypertension', 'RuCCoN', 'CARES-ICD10 Chapter', 'RuDReC-NER', 'MIMIC-IV DiReCT.Dis', 
               'n2c2 2014-Medication', 'iCorpus', 'Brateca-Hospitalization', 'n2c2 2010-Assertion', 
               'NorSynthClinical-PHI', 'IFMIR - NER&factuality', 'JP-STS', 'NorSynthClinical-RE', 
               'n2c2 2010-Concept', 'BARR2', 'IMCS-V2-NER', 'IMCS-V2-MRG', 'cMedQA', 'MedSTS', 
               'BRONCO150-NER&Status', 'n2c2 2018-ADE&medication', 'CLISTER', 'ClinicalNotes-UPMC', 
               'PPTS', 'CLIP', 'IMCS-V2-SR', 'EHRQA-Sub department', 'BrainMRI-AIS', 'Brateca-Mortality', 
               'meddocan', 'CHIP-CDEE', 'CAS-evidence', 'MEDIQA 2019-RQE', 'Cantemis-Norm', 'MEDIQA 2023-sum-B', 
               'CHIP-CTC', 'C-EMRS', 'CARES ICD10 Block', 'Cantemis-NER', 'CLINpt-NER', 'MEDIQA 2023-chat-A', 
               'n2c2 2014-De-identification', 'n2c2 2014-Hyperlipidemia', 'EHRQA-Primary department', 
               'ADE-Drug dosage', 'IFMIR-Incident type', 'MIMIC-III Outcome.Mortality', 'n2c2 2006-De-identification', 
               'CAS-label', 'MIMIC-IV CDM', 'CodiEsp-ICD-10-CM', 'n2c2 2010-Relation', 'CARES-ICD10 Subblock', 
               'MIE', 'HealthCareMagic-100k', 'ADE-Identification', 'MIMIC-IV DiReCT.PDD', 'ADE-Extraction', 
               'DialMed', 'GOUT-CC-Consensus', 'GraSSCo PHI', 'RuMedNLI', 'RuMedDaNet', 'CBLUE-CDN', 'icliniq-10k', 
               'CARDIO-DE', 'CARES-Area', 'DiSMed-NER', 'CodiEsp-ICD-10-PCS', 'MedNLI', 'MTS', 'MIMIC-IV BHC', 
               'n2c2 2014-CAD'}

n_models = int(list(zero_shot_leaderboard_json["Model"].keys())[-1]) + 1

def get_filtered_columns(filter_selections):
    """
    Given an array of selected filters, this function will return a list of all
    the columns that match the criteria.

    Input:
        filter_selections: dictionary of all task type filter selections

    Output:
        Returns a list of all valid tasks to display (by task name)
    """
    # Need to add a flag to this filter so that it only displays those that match all attributes
    valid_columns = []
    for task in task_information_json:
        task_info = task_information_json[task]
        
        # Flag to keep track of whether this task is valid
        isValid = True

        # Iterate through each attribute of the task
        for attribute in task_info:
            # If the filter is empty
            if not filter_selections[attribute]:
                continue

            value = task_info[attribute]

            # print(filter_selections[attribute])

            # Handle edge case for multiple categories
            if "," in value:
                all_categories = value.split(", ")

                flag = False
                for category in all_categories:
                    if category in filter_selections[attribute]:
                        flag = True
                        break

                if flag:  # one category matches
                    isValid = True

                else: # none of the categories matched
                    isValid  = False

            # Handle Brazilian Edge Case
            elif (value == 'Portuguese\n(Brazilian)') and ('Portuguese' in filter_selections[attribute]):
                isValid = True
                break
        
            elif value not in filter_selections[attribute]:
            # if filter_selections[attribute] not in task_info[attribute]:
                isValid = False
                # break

        if task in valid_tasks and isValid:
            valid_columns.append(task)

    return valid_columns

def isEmpty(currently_selected_filters):
    """
    Checks if there are no selected filters
    """
    flag = True
    for key, value in currently_selected_filters.items():
        if not value:
            continue
        else:
            return False
        
    return True


####################################################################################################
####### CoT Filters
####################################################################################################


def cot_filter_language(language_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Language"] = language_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)
    
    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Size (B)', 'Average Performance'] + filtered_cols]
  
def cot_filter_task_type(task_type_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Task Type"] = task_type_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)

    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]
    
def cot_filter_clinical_context(clinical_context_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Clinical Context"] = clinical_context_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)

    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def cot_filter_applications(applications_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Applications"] = applications_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)

    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def cot_filter_stage_options(stage_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Clinical Stage"] = stage_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)

    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def cot_filter_data_access(data_access_choice):
    # Update the Global store for the currently selected filters
    cot_currently_selected_filters["Data Access"] = data_access_choice

    if isEmpty(cot_currently_selected_filters):
        cot_df["Average Performance"] = original_cot_avg_perf
        return cot_df

    filtered_cols = get_filtered_columns(cot_currently_selected_filters)

    updated_performance = cot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    cot_df["Average Performance"] = cot_df.index.map(updated_performance_int)

    return cot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def cot_update_average_performance(selected_columns):
    """
    When a user clicks filters to filter certain tasks, the average performance
    of the model should update. This function takes uses the updated filtered columns
    and calculates the average performances of only those columns. It then updates
    the leaderboard accordingly.
    """
    updated_average_performance = {}
    
    for i in range(n_models):
        performance = 0

        num_tasks = 0
        for task in selected_columns:
            num_tasks += 1
            performance += float(CoT_leaderboard_json[task][str(i)])

        if num_tasks == 0:
            num_tasks = 1
        
        updated_average_performance[f"{i}"] = float(round(performance / num_tasks, 2))

    return updated_average_performance


####################################################################################################
####### Few Shot Filters
####################################################################################################

def five_shot_filter_language(language_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Language"] = language_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)
    
    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]
  
def five_shot_filter_task_type(task_type_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Task Type"] = task_type_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)

    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def five_shot_filter_clinical_context(clinical_context_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Clinical Context"] = clinical_context_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)

    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def five_shot_filter_applications(applications_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Applications"] = applications_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)

    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def five_shot_filter_stage_options(stage_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Clinical Stage"] = stage_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)

    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def five_shot_filter_data_access(data_access_choice):
    # Update the Global store for the currently selected filters
    five_shot_currently_selected_filters["Data Access"] = data_access_choice

    if isEmpty(five_shot_currently_selected_filters):
        five_shot_df["Average Performance"] = original_five_shot_avg_perf
        return five_shot_df

    filtered_cols = get_filtered_columns(five_shot_currently_selected_filters)

    updated_performance = five_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    five_shot_df["Average Performance"] = five_shot_df.index.map(updated_performance_int)

    return five_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]


def five_shot_update_average_performance(selected_columns):
    """
    When a user clicks filters to filter certain tasks, the average performance
    of the model should update. This function takes uses the updated filtered columns
    and calculates the average performances of only those columns. It then updates
    the leaderboard accordingly.
    """
    updated_average_performance = {}
    
    for i in range(n_models):
        performance = 0

        num_tasks = 0
        for task in selected_columns:
            num_tasks += 1
            performance += float(five_shot_leaderboard_json[task][str(i)])

        if num_tasks == 0:
            num_tasks = 1
        
        updated_average_performance[f"{i}"] = float(round(performance / num_tasks, 2))

    return updated_average_performance


####################################################################################################
###### Zero Shot Filters
####################################################################################################


def zero_shot_filter_language(language_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Language"] = language_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)
    
    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]
  
def zero_shot_filter_task_type(task_type_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Task Type"] = task_type_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)

    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]
    
def zero_shot_filter_clinical_context(clinical_context_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Clinical Context"] = clinical_context_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)

    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def zero_shot_filter_applications(applications_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Applications"] = applications_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)

    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def zero_shot_filter_stage_options(stage_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Clinical Stage"] = stage_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)

    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def zero_shot_filter_data_access(data_access_choice):
    # Update the Global store for the currently selected filters
    zero_shot_currently_selected_filters["Data Access"] = data_access_choice

    if isEmpty(zero_shot_currently_selected_filters):
        zero_shot_df["Average Performance"] = original_zero_shot_avg_perf
        return zero_shot_df

    filtered_cols = get_filtered_columns(zero_shot_currently_selected_filters)

    updated_performance = zero_shot_update_average_performance(filtered_cols)

    # Convert dictionary keys to integers to match the DataFrame index
    updated_performance_int = {int(k): v for k, v in updated_performance.items()}

    # Map the values to the 'Average Performance' column based on index
    zero_shot_df["Average Performance"] = zero_shot_df.index.map(updated_performance_int)

    return zero_shot_df[['T', 'Model', 'Model: Domain', 'Model: Accessibility', 'Model: Size Range', 'Size (B)', 'Average Performance'] + filtered_cols]

def zero_shot_update_average_performance(selected_columns):
    """
    When a user clicks filters to filter certain tasks, the average performance
    of the model should update. This function takes uses the updated filtered columns
    and calculates the average performances of only those columns. It then updates
    the leaderboard accordingly.
    """
    updated_average_performance = {}
    
    for i in range(n_models):
        performance = 0

        num_tasks = 0
        for task in selected_columns:
            num_tasks += 1
            performance += float(zero_shot_leaderboard_json[task][str(i)])

        if num_tasks == 0:
            num_tasks = 1
        
        updated_average_performance[f"{i}"] = float(round(performance / num_tasks, 2))

    return updated_average_performance


def postprocess(self, value: pd.DataFrame) -> DataframeData:
        # Ensure that the "Average Performance" column exists
        if "Average Performance" in value.columns:
            # Sort the DataFrame by the "average performance" column in descending order
            value = value.sort_values(by="Average Performance", ascending=False)
        
            return DataframeData(
                headers=list(value.columns),  # type: ignore
                data=value.to_dict(orient="split")["data"],  # type: ignore
            )

        if value is None:
            return self.postprocess(pd.DataFrame({"column 1": []}))
        if isinstance(value, (str, pd.DataFrame)):
            if isinstance(value, str):
                value = pd.read_csv(value)  # type: ignore
            if len(value) == 0:
                return DataframeData(
                    headers=list(value.columns),  # type: ignore
                    data=[[]],  # type: ignore
                )
            return DataframeData(
                headers=list(value.columns),  # type: ignore
                data=value.to_dict(orient="split")["data"],  # type: ignore
            )
        elif isinstance(value, Styler):
            if semantic_version.Version(pd.__version__) < semantic_version.Version(
                "1.5.0"
            ):
                raise ValueError(
                    "Styler objects are only supported in pandas version 1.5.0 or higher. Please try: `pip install --upgrade pandas` to use this feature."
                )
            if self.interactive:
                warnings.warn(
                    "Cannot display Styler object in interactive mode. Will display as a regular pandas dataframe instead."
                )
            df: pd.DataFrame = value.data  # type: ignore
            if len(df) == 0:
                return DataframeData(
                    headers=list(df.columns),
                    data=[[]],
                    metadata=self.__extract_metadata(value),  # type: ignore
                )
            return DataframeData(
                headers=list(df.columns),
                data=df.to_dict(orient="split")["data"],  # type: ignore
                metadata=self.__extract_metadata(value),  # type: ignore
            )

# Models are sorted in order of decreasing average performance (best performance at the top!)
Leaderboard.postprocess = postprocess


####################################################################################################
###### Leaderboard
####################################################################################################

with gr.Blocks() as app:
    gr.Markdown("# BRIDGE (Benchmarking Large Language Models for Understanding Real-world Clinical Practice Text)")

    with gr.Tabs():
        with gr.Tab("README"):
            # gr.Markdown((Path(__file__).parent / "docs.md").read_text())
            html_content = (Path(__file__).parent / "docs.md").read_text()
            gr.HTML(html_content)

        with gr.Tab("Zero-Shot"):
            leaderboard = Leaderboard(
                value=zero_shot_df,
                select_columns = None,
                search_columns=SearchColumns(primary_column = "Model", secondary_columns = "",
                                     placeholder="Search by Model Name",
                                     label="Model Search"),
                hide_columns=["Model: Size Range", "Model: Accessibility"],
                filter_columns=["Model: Domain", "Model: Size Range", "Model: Accessibility"],
                datatype=config.TYPES,
            )

            # Language Filter
            all_languages = ['English', 'Spanish', 
                             'Chinese', 'Norwegian', 
                             'Russian', 'Portuguese', 
                             'German', 'Japanese', 'French']
            
            language_options = gr.CheckboxGroup(all_languages, label="Filter Task: Language")

            # Task Type Filter
            all_task_types = ['Question Answering', 'Text Classification', 'Named Entity Recognition', 
                              'Normalization and Coding', 'Natural Language Inference', 'Summarization', 
                              'Event Extraction', 'Semantic Similarity']


            task_type_options = gr.CheckboxGroup(all_task_types, label="Filter Task: Task Type")
            
            all_clinical_contexts = ['Neurology',  'Oncology',  'Radiology',  'Pulmonology',  
                                     'Cardiology',  'Dermatology',  'Critical Care',  'Nephrology',  
                                     'General',  'Endocrinology',  'Pediatrics',  'Pharmacology',  
                                     'Gastroenterology',  'Psychology']
            
            cc_options = gr.CheckboxGroup(all_clinical_contexts, label="Filter Task: Clinical Context")

            # Applications Filter
            all_applications = ['Procudure information', 'Concept standarization', 
                                'Specialist recommendation', 'Negation identification', 
                                'Clinical trial matching', 'Consultation summarization', 
                                'Semantic relation', 'Post-discharge patient management', 
                                'De-identification', 'Billing & Coding', 'Phenotyping', 
                                'Data organization', 'Temporal & Causality relation', 
                                'Summarization', 'Screen & Consultation', 'Diagnosis', 
                                'ADE & Incidents', 'Risk factor extraction', 'Prognosis', 
                                'Medication information']


            application_options = gr.CheckboxGroup(all_applications, label="Filter Task: Clinical Application")

            # Clinical Stage Filter
            all_stages = ['Treatment and Intervention', 'Triage and Referral', 
                          'Initial Assessment', 'Discharge and Administration', 
                          'Research', 'Diagnosis and Prognosis']
            
            stage_options = gr.CheckboxGroup(all_stages, label="Filter Task: Clinical Stage")

            # Data Access Filter
            all_data_access = ['Open Access', 'Regulated']
            
            da_options = gr.CheckboxGroup(all_data_access, label="Filter Task: Data Access")


            language_options.change(fn=zero_shot_filter_language, inputs=language_options, outputs=leaderboard)
            task_type_options.change(fn=zero_shot_filter_task_type, inputs=task_type_options, outputs=leaderboard)
            cc_options.change(fn=zero_shot_filter_clinical_context, inputs=cc_options, outputs=leaderboard)
            application_options.change(fn=zero_shot_filter_applications, inputs=application_options, outputs=leaderboard)
            da_options.change(fn=zero_shot_filter_data_access, inputs=da_options, outputs=leaderboard)
            stage_options.change(fn=zero_shot_filter_stage_options, inputs=stage_options, outputs=leaderboard)


        with gr.Tab("Few-Shot"):
            leaderboard = Leaderboard(
                value=five_shot_df,
                select_columns = None,
                search_columns=SearchColumns(primary_column = "Model", secondary_columns = "",
                                     placeholder="Search by Model Name",
                                     label="Model Search"),
                hide_columns=["Model: Size Range", "Model: Accessibility"],
                filter_columns=["Model: Domain", "Model: Size Range", "Model: Accessibility"],
                datatype=config.TYPES,
            )

            # Language Filter
            all_languages = ['English', 'Spanish', 
                             'Chinese', 'Norwegian', 
                             'Russian', 'Portuguese', 
                             'German', 'Japanese', 'French']
            
            language_options = gr.CheckboxGroup(all_languages, label="Filter Task: Language")

            # Task Type Filter
            all_task_types = ['Question Answering', 'Text Classification', 'Named Entity Recognition', 
                              'Normalization and Coding', 'Natural Language Inference', 'Summarization', 
                              'Event Extraction', 'Semantic Similarity']

            task_type_options = gr.CheckboxGroup(all_task_types, label="Filter Task: Task Type")


            # Clinical Context Filter
            all_clinical_contexts = ['Neurology',  'Oncology',  'Radiology',  'Pulmonology',  
                                     'Cardiology',  'Dermatology',  'Critical Care',  'Nephrology',  
                                     'General',  'Endocrinology',  'Pediatrics',  'Pharmacology',  
                                     'Gastroenterology',  'Psychology']
            
            cc_options = gr.CheckboxGroup(all_clinical_contexts, label="Filter Task: Clinical Context")

            # Applications Filter
            all_applications = ['Procudure information', 'Concept standarization', 
                                'Specialist recommendation', 'Negation identification', 
                                'Clinical trial matching', 'Consultation summarization', 
                                'Semantic relation', 'Post-discharge patient management', 
                                'De-identification', 'Billing & Coding', 'Phenotyping', 
                                'Data organization', 'Temporal & Causality relation', 
                                'Summarization', 'Screen & Consultation', 'Diagnosis', 
                                'ADE & Incidents', 'Risk factor extraction', 'Prognosis', 
                                'Medication information']

            application_options = gr.CheckboxGroup(all_applications, label="Filter Task: Clinical Application")

            # Clinical Stage Filter
            all_stages = ['Treatment and Intervention', 'Triage and Referral', 
                          'Initial Assessment', 'Discharge and Administration', 
                          'Research', 'Diagnosis and Prognosis']
            
            stage_options = gr.CheckboxGroup(all_stages, label="Filter Task: Clinical Stage")

            # Data Access Filter
            all_data_access = ['Open Access', 'Regulated']
            
            da_options = gr.CheckboxGroup(all_data_access, label="Filter Task: Data Access")

            language_options.change(fn=five_shot_filter_language, inputs=language_options, outputs=leaderboard)
            task_type_options.change(fn=five_shot_filter_task_type, inputs=task_type_options, outputs=leaderboard)
            cc_options.change(fn=five_shot_filter_clinical_context, inputs=cc_options, outputs=leaderboard)
            application_options.change(fn=five_shot_filter_applications, inputs=application_options, outputs=leaderboard)
            da_options.change(fn=five_shot_filter_data_access, inputs=da_options, outputs=leaderboard)
            stage_options.change(fn=five_shot_filter_stage_options, inputs=stage_options, outputs=leaderboard)


        with gr.Tab("CoT"):
            leaderboard = Leaderboard(
                value=cot_df,
                select_columns = None,
                search_columns=SearchColumns(primary_column = "Model", secondary_columns = "",
                                     placeholder="Search by Model Name",
                                     label="Model Search"),
                hide_columns=["Model: Size Range", "Model: Accessibility"],
                filter_columns=["Model: Domain", "Model: Size Range", "Model: Accessibility"],
                datatype=config.TYPES,
            )

            # Language Filter
            all_languages = ['English', 'Spanish', 
                             'Chinese', 'Norwegian', 
                             'Russian', 'Portuguese', 
                             'German', 'Japanese', 'French']
            
            language_options = gr.CheckboxGroup(all_languages, label="Filter Task: Language")

            # Task Type Filter
            all_task_types = ['Question Answering', 'Text Classification', 'Named Entity Recognition', 
                              'Normalization and Coding', 'Natural Language Inference', 'Summarization', 
                              'Event Extraction', 'Semantic Similarity']

            task_type_options = gr.CheckboxGroup(all_task_types, label="Filter Task: Task Type")

            # Clinical Context Filter
            all_clinical_contexts = ['Neurology',  'Oncology',  'Radiology',  'Pulmonology',  
                                     'Cardiology',  'Dermatology',  'Critical Care',  'Nephrology',  
                                     'General',  'Endocrinology',  'Pediatrics',  'Pharmacology',  
                                     'Gastroenterology',  'Psychology']
            
            cc_options = gr.CheckboxGroup(all_clinical_contexts, label="Filter Task: Clinical Context")

            # Applications Filter
            all_applications = ['Procudure information', 'Concept standarization', 
                                'Specialist recommendation', 'Negation identification', 
                                'Clinical trial matching', 'Consultation summarization', 
                                'Semantic relation', 'Post-discharge patient management', 
                                'De-identification', 'Billing & Coding', 'Phenotyping', 
                                'Data organization', 'Temporal & Causality relation', 
                                'Summarization', 'Screen & Consultation', 'Diagnosis', 
                                'ADE & Incidents', 'Risk factor extraction', 'Prognosis', 
                                'Medication information']

            application_options = gr.CheckboxGroup(all_applications, label="Filter Task: Clinical Application")

            # Clinical Stage Filter
            all_stages = ['Treatment and Intervention', 'Triage and Referral', 
                          'Initial Assessment', 'Discharge and Administration', 
                          'Research', 'Diagnosis and Prognosis']
            
            stage_options = gr.CheckboxGroup(all_stages, label="Filter Task: Clinical Stage")
            

            # Data Access Filter
            all_data_access = ['Open Access', 'Regulated']
            
            da_options = gr.CheckboxGroup(all_data_access, label="Filter Task: Data Access")


            language_options.change(fn=cot_filter_language, inputs=language_options, outputs=leaderboard)
            task_type_options.change(fn=cot_filter_task_type, inputs=task_type_options, outputs=leaderboard)
            cc_options.change(fn=cot_filter_clinical_context, inputs=cc_options, outputs=leaderboard)
            application_options.change(fn=cot_filter_applications, inputs=application_options, outputs=leaderboard)
            da_options.change(fn=cot_filter_data_access, inputs=da_options, outputs=leaderboard)

            stage_options.change(fn=cot_filter_stage_options, inputs=stage_options, outputs=leaderboard)

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