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d3e1e
 
 
 
 
6301ef2
 
a82a162
fcd3e1e
47a7ca5
 
 
 
fcd3e1e
 
 
 
47a7ca5
fcd3e1e
6301ef2
 
fcd3e1e
 
 
 
47a7ca5
 
 
fcd3e1e
47a7ca5
 
fcd3e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a7ca5
 
 
 
 
 
 
 
 
fcd3e1e
 
 
 
47a7ca5
fcd3e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47a7ca5
fcd3e1e
47a7ca5
fcd3e1e
 
 
6301ef2
fcd3e1e
6301ef2
fcd3e1e
47a7ca5
fcd3e1e
 
 
 
6301ef2
 
 
 
fcd3e1e
 
 
 
 
 
6301ef2
fcd3e1e
 
 
 
a113cef
a8af1a7
 
 
 
a113cef
f318f5f
 
 
a8af1a7
 
 
 
a113cef
 
a8af1a7
 
 
 
 
 
 
a113cef
a8af1a7
 
a113cef
a8af1a7
a113cef
a8af1a7
 
 
fcd3e1e
 
a113cef
fcd3e1e
 
 
 
 
a8af1a7
fcd3e1e
 
 
 
 
 
a8af1a7
 
fcd3e1e
a8af1a7
 
 
 
 
 
 
a113cef
a8af1a7
 
a113cef
a8af1a7
 
 
 
 
 
 
 
 
a113cef
a8af1a7
a113cef
 
 
a8af1a7
 
a113cef
a8af1a7
a113cef
a8af1a7
 
a113cef
 
fcd3e1e
 
 
 
 
a113cef
a8af1a7
 
 
 
a113cef
e7f0792
 
 
 
 
 
 
 
 
 
 
 
fcd3e1e
 
e7f0792
fcd3e1e
e7f0792
 
fcd3e1e
e7f0792
 
fcd3e1e
 
e7f0792
 
 
 
 
 
 
 
 
a113cef
fcd3e1e
 
 
a113cef
fcd3e1e
 
 
 
a113cef
fcd3e1e
 
 
 
47a7ca5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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a113cef
a8af1a7
 
 
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
import gradio as gr
import pandas as pd
import plotly.express as px
from dataclasses import dataclass, field
from typing import List, Dict, Tuple, Union
import json
import os
from collections import OrderedDict
import re

@dataclass
class ScorecardCategory:
    name: str
    questions: List[Dict[str, Union[str, List[str]]]]
    scores: Dict[str, int] = field(default_factory=dict)


def extract_category_number(category_name: str) -> int:
    """Extract the category number from the category name."""
    match = re.match(r'^(\d+)\.?\s*.*$', category_name)
    return int(match.group(1)) if match else float('inf')

def sort_categories(categories):
    """Sort categories by their numeric prefix."""
    return sorted(categories, key=extract_category_number)


# def load_scorecard_templates(directory):
#     templates = []
#     for filename in os.listdir(directory):
#         if filename.endswith('.json'):
#             with open(os.path.join(directory, filename), 'r') as file:
#                 data = json.load(file)
#                 templates.append(ScorecardCategory(
#                     name=data['name'],
#                     questions=data['questions']
#                 ))
#     return templates

def create_category_summary(category_data):
    """Create a summary section for a category"""
    # Calculate statistics
    total_sections = len(category_data)
    completed_sections = sum(1 for section in category_data.values() if section['status'] == 'Yes')
    na_sections = sum(1 for section in category_data.values() if section['status'] == 'N/A')
    
    # Calculate completion rates
    total_questions = 0
    completed_questions = 0
    evaluation_types = set()
    has_human_eval = False
    has_quantitative = False
    has_documentation = False
    
    for section in category_data.values():
        if section['status'] != 'N/A':
            questions = section.get('questions', {})
            total_questions += len(questions)
            completed_questions += sum(1 for q in questions.values() if q)
            
            # Check for evaluation types
            for question in questions.keys():
                if 'human' in question.lower():
                    has_human_eval = True
                if any(term in question.lower() for term in ['quantitative', 'metric', 'benchmark']):
                    has_quantitative = True
                if 'documentation' in question.lower():
                    has_documentation = True
    
    completion_rate = (completed_questions / total_questions * 100) if total_questions > 0 else 0
    
    # Create summary HTML
    html = "<div class='summary-card'>"
    html += "<div class='summary-title'>πŸ“Š Section Summary</div>"
    
    # Completion metrics
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>πŸ“ˆ Completion Metrics</div>"
    html += f"<div class='metric-row'><span class='metric-label'>Overall Completion Rate:</span> <span class='metric-value'>{completion_rate:.1f}%</span></div>"
    html += f"<div class='metric-row'><span class='metric-label'>Sections Completed:</span> <span class='metric-value'>{completed_sections}/{total_sections}</span></div>"
    html += "</div>"
    
    # Evaluation Coverage
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>🎯 Evaluation Coverage</div>"
    html += "<div class='coverage-grid'>"
    html += f"<div class='coverage-item {get_coverage_class(has_human_eval)}'>πŸ‘₯ Human Evaluation</div>"
    html += f"<div class='coverage-item {get_coverage_class(has_quantitative)}'>πŸ“Š Quantitative Analysis</div>"
    html += f"<div class='coverage-item {get_coverage_class(has_documentation)}'>πŸ“ Documentation</div>"
    html += "</div>"
    html += "</div>"
    
    # Status Breakdown
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>πŸ“‹ Status Breakdown</div>"
    html += create_status_pills(category_data)
    html += "</div>"
    
    html += "</div>"
    return html

def create_overall_summary(model_data, selected_categories):
    """Create a comprehensive summary of all categories"""
    scores = model_data['scores']
    
    # Initialize counters
    total_sections = 0
    completed_sections = 0
    na_sections = 0
    total_questions = 0
    completed_questions = 0
    
    # Track evaluation types across all categories
    evaluation_types = {
        'human': 0,
        'quantitative': 0,
        'documentation': 0,
        'monitoring': 0,
        'transparency': 0
    }
    
    # Calculate completion rates for categories
    category_completion = {}
    
    # Process all categories
    for category, category_data in scores.items():
        if category not in selected_categories:
            continue  # Skip unselected categories
            
        category_questions = 0
        category_completed = 0
        category_na = 0
        total_sections_in_category = len(category_data)
        na_sections_in_category = sum(1 for section in category_data.values() if section['status'] == 'N/A')
        
        for section in category_data.values():
            total_sections += 1
            if section['status'] == 'Yes':
                completed_sections += 1
            elif section['status'] == 'N/A':
                na_sections += 1
                category_na += 1
                
            if section['status'] != 'N/A':
                questions = section.get('questions', {})
                section_total = len(questions)
                section_completed = sum(1 for q in questions.values() if q)
                
                total_questions += section_total
                completed_questions += section_completed
                category_questions += section_total
                category_completed += section_completed
                
                # Check for evaluation types
                for question in questions.keys():
                    if 'human' in question.lower():
                        evaluation_types['human'] += 1
                    if any(term in question.lower() for term in ['quantitative', 'metric', 'benchmark']):
                        evaluation_types['quantitative'] += 1
                    if 'documentation' in question.lower():
                        evaluation_types['documentation'] += 1
                    if 'monitoring' in question.lower():
                        evaluation_types['monitoring'] += 1
                    if 'transparency' in question.lower():
                        evaluation_types['transparency'] += 1
        
        # Store category information
        is_na = na_sections_in_category == total_sections_in_category
        completion_rate = (category_completed / category_questions * 100) if category_questions > 0 and not is_na else 0
        
        category_completion[category] = {
            'completion_rate': completion_rate,
            'is_na': is_na
        }
    
    # Create summary HTML
    html = "<div class='card overall-summary-card'>"
    html += "<div class='card-title'>πŸ“Š Overall Model Evaluation Summary</div>"
    
    # Key metrics section
    html += "<div class='summary-grid'>"
    
    # Overall completion metrics
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>πŸ“ˆ Overall Completion</div>"
    completion_rate = (completed_questions / total_questions * 100) if total_questions > 0 else 0
    html += f"<div class='metric-row'><span class='metric-label'>Overall Completion Rate:</span> <span class='metric-value'>{completion_rate:.1f}%</span></div>"
    html += f"<div class='metric-row'><span class='metric-label'>Sections Completed:</span> <span class='metric-value'>{completed_sections}/{total_sections}</span></div>"
    html += f"<div class='metric-row'><span class='metric-label'>Questions Completed:</span> <span class='metric-value'>{completed_questions}/{total_questions}</span></div>"
    html += "</div>"
    
    # Evaluation coverage
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>🎯 Evaluation Types Coverage</div>"
    html += "<div class='coverage-grid'>"
    for eval_type, count in evaluation_types.items():
        icon = {
            'human': 'πŸ‘₯',
            'quantitative': 'πŸ“Š',
            'documentation': 'πŸ“',
            'monitoring': 'πŸ“‘',
            'transparency': 'πŸ”'
        }.get(eval_type, '❓')
        has_coverage = count > 0
        html += f"<div class='coverage-item {get_coverage_class(has_coverage)}'>{icon} {eval_type.title()}</div>"
    html += "</div>"
    html += "</div>"
    
    html += "</div>"  # End summary-grid
    
    # Category breakdown
    html += "<div class='summary-section'>"
    html += "<div class='summary-subtitle'>πŸ“‹ Category Completion Breakdown</div>"
    html += "<div class='category-completion-grid'>"
    
    # Sort and filter categories
    sorted_categories = [cat for cat in sort_categories(scores.keys()) if cat in selected_categories]
    
    for category in sorted_categories:
        info = category_completion[category]
        category_name = category.split('. ', 1)[1] if '. ' in category else category
        # remove last word from category_name
        category_name = ' '.join(category_name.split(' ')[:-1])
        
        # Determine display text and style
        if info['is_na']:
            completion_text = "N/A"
            bar_width = "0"
            style_class = "na"
        else:
            completion_text = f"{info['completion_rate']:.1f}%"
            bar_width = f"{info['completion_rate']}"
            style_class = "active"
        
        html += f"""
        <div class='category-completion-item'>
            <div class='category-name'>{category_name}</div>
            <div class='completion-bar-container {style_class}'>
                <div class='completion-bar' style='width: {bar_width}%;'></div>
                <span class='completion-text'>{completion_text}</span>
            </div>
        </div>
        """
    
    html += "</div></div>"
    html += "</div>"  # End overall-summary-card
    return html

def get_coverage_class(has_feature):
    """Return CSS class based on feature presence"""
    return 'covered' if has_feature else 'not-covered'

def create_status_pills(category_data):
    """Create status pill indicators"""
    status_counts = {'Yes': 0, 'No': 0, 'N/A': 0}
    for section in category_data.values():
        status_counts[section['status']] += 1
    
    html = "<div class='status-pills'>"
    for status, count in status_counts.items():
        html += f"<div class='status-pill {status.lower()}'>{status}: {count}</div>"
    html += "</div>"
    return html

def get_modality_icon(modality):
    """Return an emoji icon for each modality type."""
    icons = {
        "Text-to-Text": "πŸ“",  # Memo icon for text-to-text
        "Text-to-Image": "🎨",  # Artist palette for text-to-image
        "Image-to-Text": "πŸ”",  # Magnifying glass for image-to-text
        "Image-to-Image": "πŸ–ΌοΈ",  # Frame for image-to-image
        "Audio": "🎡",  # Musical note for audio
        "Video": "🎬",  # Clapper board for video
        "Multimodal": "πŸ”„"  # Cycle arrows for multimodal
    }
    return icons.get(modality, "πŸ’«")  # Default icon if modality not found

def create_metadata_card(metadata):
    """Create a formatted HTML card for metadata."""
    html = "<div class='card metadata-card'>"
    html += "<div class='card-title'>Model Information</div>"
    html += "<div class='metadata-content'>"
    
    # Handle special formatting for modalities
    modalities = metadata.get("Modalities", [])
    formatted_modalities = ""
    if modalities:
        formatted_modalities = " ".join(
            f"<span class='modality-badge'>{get_modality_icon(m)} {m}</span>"
            for m in modalities
        )
    
    # Order of metadata display (customize as needed)
    display_order = ["Name", "Provider", "Type", "URL"]
    
    # Display ordered metadata first
    for key in display_order:
        if key in metadata:
            value = metadata[key]
            if key == "URL":
                html += f"<div class='metadata-row'><span class='metadata-label'>{key}:</span> "
                html += f"<a href='{value}' target='_blank' class='metadata-link'>{value}</a></div>"
            else:
                html += f"<div class='metadata-row'><span class='metadata-label'>{key}:</span> <span class='metadata-value'>{value}</span></div>"
    
    # Add modalities if present
    if formatted_modalities:
        html += f"<div class='metadata-row'><span class='metadata-label'>Modalities:</span> <div class='modality-container'>{formatted_modalities}</div></div>"
    
    # Add any remaining metadata not in display_order
    for key, value in metadata.items():
        if key not in display_order and key != "Modalities":
            html += f"<div class='metadata-row'><span class='metadata-label'>{key}:</span> <span class='metadata-value'>{value}</span></div>"
    
    html += "</div></div>"
    return html


def load_models_from_json(directory):
    models = {}
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            with open(os.path.join(directory, filename), 'r') as file:
                model_data = json.load(file)
                model_name = model_data['metadata']['Name']
                models[model_name] = model_data
    
    return OrderedDict(sorted(models.items(), key=lambda x: x[0].lower()))

# Load templates and models
# scorecard_template = load_scorecard_templates('scorecard_templates')
models = load_models_from_json('model_data')

def create_source_html(sources):
    if not sources:
        return ""
    
    html = "<div class='sources-list'>"
    for source in sources:
        icon = source.get("type", "")
        detail = source.get("detail", "")
        name = source.get("name", detail)
        
        html += f"<div class='source-item'>{icon} "
        if detail.startswith("http"):
            html += f"<a href='{detail}' target='_blank'>{name}</a>"
        else:
            html += name
        html += "</div>"
    html += "</div>"
    return html

def create_leaderboard():
    scores = []
    for model, data in models.items():
        total_score = 0
        total_questions = 0
        
        for category in data['scores'].values():
            for section in category.values():
                if section['status'] != 'N/A':
                    questions = section.get('questions', {})
                    total_score += sum(1 for q in questions.values() if q)
                    total_questions += len(questions)
        
        score_percentage = (total_score / total_questions * 100) if total_questions > 0 else 0
        scores.append((model, score_percentage))
    
    df = pd.DataFrame(scores, columns=['Model', 'Score Percentage'])
    df = df.sort_values('Score Percentage', ascending=False).reset_index(drop=True)
    
    html = "<div class='card leaderboard-card'>"
    html += "<div class='card-title'>AI Model Social Impact Leaderboard</div>"
    html += "<table class='leaderboard-table'>"
    html += "<tr><th>Rank</th><th>Model</th><th>Score Percentage</th></tr>"
    for i, (_, row) in enumerate(df.iterrows(), 1):
        html += f"<tr><td>{i}</td><td>{row['Model']}</td><td>{row['Score Percentage']:.2f}%</td></tr>"
    html += "</table></div>"
    
    return html

def create_category_chart(selected_models, selected_categories):
    if not selected_models:
        return px.bar(title='Please select at least one model for comparison')
    
    # Sort categories before processing
    selected_categories = sort_categories(selected_categories)
    
    data = []
    for model in selected_models:
        for category in selected_categories:
            if category in models[model]['scores']:
                total_score = 0
                total_questions = 0
                
                for section in models[model]['scores'][category].values():
                    if section['status'] != 'N/A':
                        questions = section.get('questions', {})
                        total_score += sum(1 for q in questions.values() if q)
                        total_questions += len(questions)
                
                score_percentage = (total_score / total_questions * 100) if total_questions > 0 else 0
                data.append({
                    'Model': model,
                    'Category': category,
                    'Score Percentage': score_percentage
                })
    
    df = pd.DataFrame(data)
    if df.empty:
        return px.bar(title='No data available for the selected models and categories')
    
    fig = px.bar(df, x='Model', y='Score Percentage', color='Category',
                 title='AI Model Scores by Category',
                 labels={'Score Percentage': 'Score Percentage'},
                 category_orders={"Category": selected_categories})
    return fig

def update_detailed_scorecard(model, selected_categories):
    if not model:
            return [
                gr.update(value="Please select a model to view details.", visible=True),
                gr.update(visible=False),
                gr.update(visible=False)
            ]

    selected_categories = sort_categories(selected_categories)
    metadata_html = create_metadata_card(models[model]['metadata'])
    overall_summary_html = create_overall_summary(models[model], selected_categories)

    # Combine metadata and overall summary
    combined_header = metadata_html + overall_summary_html

    total_yes = 0
    total_no = 0
    total_na = 0
    has_non_na = False

    # Create category cards
    all_cards_content = "<div class='container'>"
    for category_name in selected_categories:
        if category_name in models[model]['scores']:
            category_data = models[model]['scores'][category_name]
            card_content = f"<div class='card'><div class='card-title'>{category_name}</div>"
            
            # Add category-specific summary at the top of each card
            card_content += create_category_summary(category_data)
            
            # Sort sections within each category
            sorted_sections = sorted(category_data.items(), 
                                  key=lambda x: float(re.match(r'^(\d+\.?\d*)', x[0]).group(1)))
            
            category_yes = 0
            category_no = 0
            category_na = 0
            
            for section, details in sorted_sections:
                status = details['status']
                if status != 'N/A':
                    has_non_na = True
                sources = details.get('sources', [])
                questions = details.get('questions', {})
                
                section_class = "section-na" if status == "N/A" else "section-active"
                status_class = status.lower()
                status_icon = "●" if status == "Yes" else "β—‹" if status == "N/A" else "Γ—"
                
                card_content += f"<div class='section {section_class}'>"
                card_content += f"<div class='section-header'><h3>{section}</h3>"
                card_content += f"<span class='status-badge {status_class}'>{status_icon} {status}</span></div>"
                
                if sources:
                    card_content += "<div class='sources-list'>"
                    for source in sources:
                        icon = source.get("type", "")
                        detail = source.get("detail", "")
                        name = source.get("name", detail)
                        
                        card_content += f"<div class='source-item'>{icon} "
                        if detail.startswith("http"):
                            card_content += f"<a href='{detail}' target='_blank'>{name}</a>"
                        else:
                            card_content += name
                        card_content += "</div>"
                    card_content += "</div>"
                
                if questions:
                    yes_count = sum(1 for v in questions.values() if v)
                    total_count = len(questions)
                    
                    card_content += "<details class='question-accordion'>"
                    if status == "N/A":
                        card_content += f"<summary>View {total_count} N/A items</summary>"
                    else:
                        card_content += f"<summary>View details ({yes_count}/{total_count} completed)</summary>"
                    
                    card_content += "<div class='questions'>"
                    for question, is_checked in questions.items():
                        if status == "N/A":
                            style_class = "na"
                            icon = "β—‹"
                            category_na += 1
                            total_na += 1
                        else:
                            if is_checked:
                                style_class = "checked"
                                icon = "βœ“"
                                category_yes += 1
                                total_yes += 1
                            else:
                                style_class = "unchecked"
                                icon = "βœ—"
                                category_no += 1
                                total_no += 1
                        
                        card_content += f"<div class='question-item {style_class}'>{icon} {question}</div>"
                    card_content += "</div></details>"
                
                card_content += "</div>"
            
            if category_yes + category_no > 0:
                category_score = category_yes / (category_yes + category_no) * 100
                card_content += f"<div class='category-score'>Completion Score Breakdown: {category_score:.2f}% Yes: {category_yes}, No: {category_no}, N/A: {category_na}</div>"
            elif category_na > 0:
                card_content += f"<div class='category-score'>Completion Score Breakdown: N/A (All {category_na} items not applicable)</div>"
            
            card_content += "</div>"
            all_cards_content += card_content

    all_cards_content += "</div>"
    
    # Create total score
    if not has_non_na:
        total_score_md = "<div class='total-score'>No applicable scores (all items N/A)</div>"
    elif total_yes + total_no > 0:
        total_score = total_yes / (total_yes + total_no) * 100
        total_score_md = f"<div class='total-score'>Total Score: {total_score:.2f}% (Yes: {total_yes}, No: {total_no}, N/A: {total_na})</div>"
    else:
        total_score_md = "<div class='total-score'>No applicable scores (all items N/A)</div>"
    
    return [
        gr.update(value=combined_header, visible=True),
        gr.update(value=all_cards_content, visible=True),
        gr.update(value=total_score_md, visible=True)
    ]

css = """
.container {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.container.svelte-1hfxrpf.svelte-1hfxrpf {
    height: 0%;
}
.card {
    width: calc(50% - 20px);
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    background-color: #ffffff;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
}
.card:hover {
    box-shadow: 0 6px 8px rgba(0,0,0,0.15);
    transform: translateY(-5px);
}
.card-title {
    font-size: 1.4em;
    font-weight: bold;
    margin-bottom: 15px;
    color: #333;
    border-bottom: 2px solid #e0e0e0;
    padding-bottom: 10px;
}
.sources-list {
    margin: 10px 0;
}
.source-item {
    margin: 5px 0;
    padding: 5px;
    background-color: #f8f9fa;
    border-radius: 4px;
}
.question-item {
    margin: 5px 0;
    padding: 8px;
    border-radius: 4px;
}
.question-item.checked {
    background-color: #e6ffe6;
}
.question-item.unchecked {
    background-color: #ffe6e6;
}
.category-score, .total-score {
    background-color: #f0f8ff;
    border: 1px solid #b0d4ff;
    border-radius: 5px;
    padding: 10px;
    margin-top: 15px;
    font-weight: bold;
    text-align: center;
}
.total-score {
    font-size: 1.2em;
    background-color: #e6f3ff;
    border-color: #80bdff;
}
.leaderboard-card {
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
}
.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
}
.leaderboard-table th, .leaderboard-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #e0e0e0;
}
.leaderboard-table th {
    background-color: #f2f2f2;
    font-weight: bold;
}
.section {
    margin-bottom: 20px;
    padding: 15px;
    border-radius: 5px;
    background-color: #f8f9fa;
}
@media (max-width: 768px) {
    .card {
        width: 100%;
    }
}
.dark {
    background-color: #1a1a1a;
    color: #e0e0e0;

    .card {
        background-color: #2a2a2a;
        border-color: #444;
    }
    .card-title {
        color: #fff;
        border-bottom-color: #444;
    }
    .source-item {
        background-color: #2a2a2a;
    }
    .question-item.checked {
        background-color: #1a3a1a;
    }
    .question-item.unchecked {
        background-color: #3a1a1a;
    }
    .section {
        background-color: #2a2a2a;
    }
    .category-score, .total-score {
        background-color: #2c3e50;
        border-color: #34495e;
    }
    .leaderboard-table th {
        background-color: #2c3e50;
    }
}

.section-na {
    opacity: 0.6;
}

.question-item.na {
    background-color: #f0f0f0;
    color: #666;
}

.dark .question-item.na {
    background-color: #2d2d2d;
    color: #999;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.status-badge {
    font-size: 0.9em;
    padding: 4px 8px;
    border-radius: 12px;
    font-weight: 500;
}

.status-badge.yes {
    background-color: #e6ffe6;
    color: #006600;
}

.status-badge.no {
    background-color: #ffe6e6;
    color: #990000;
}

.status-badge.n\/a {
    background-color: #f0f0f0;
    color: #666666;
}

.question-accordion {
    margin-top: 10px;
}

.question-accordion summary {
    cursor: pointer;
    padding: 8px;
    background-color: #f8f9fa;
    border-radius: 4px;
    margin-bottom: 10px;
    font-weight: 500;
}

.question-accordion summary:hover {
    background-color: #e9ecef;
}

.dark .status-badge.yes {
    background-color: #1a3a1a;
    color: #90EE90;
}

.dark .status-badge.no {
    background-color: #3a1a1a;
    color: #FFB6B6;
}

.dark .status-badge.n\/a {
    background-color: #2d2d2d;
    color: #999999;
}

.dark .question-accordion summary {
    background-color: #2a2a2a;
}

.dark .question-accordion summary:hover {
    background-color: #333333;
}
.metadata-card {
    margin-bottom: 30px;
    width: 100% !important;
}

.metadata-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.metadata-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    line-height: 1.5;
}

.metadata-label {
    font-weight: 600;
    min-width: 100px;
    color: #555;
}

.metadata-value {
    color: #333;
}

.metadata-link {
    color: #007bff;
    text-decoration: none;
}

.metadata-link:hover {
    text-decoration: underline;
}

.modality-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.modality-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    background-color: #f0f7ff;
    border: 1px solid #cce3ff;
    border-radius: 15px;
    font-size: 0.9em;
    color: #0066cc;
}

.dark .metadata-label {
    color: #aaa;
}

.dark .metadata-value {
    color: #ddd;
}

.dark .metadata-link {
    color: #66b3ff;
}

.dark .modality-badge {
    background-color: #1a2733;
    border-color: #2c3e50;
    color: #99ccff;
}

.summary-card {
    background-color: #f8f9fa;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 20px;
}

.summary-title {
    font-size: 1.2em;
    font-weight: bold;
    margin-bottom: 12px;
    color: #333;
}

.summary-section {
    margin-bottom: 16px;
}

.summary-subtitle {
    font-size: 1em;
    font-weight: 600;
    color: #555;
    margin-bottom: 8px;
}

.metric-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.metric-label {
    color: #666;
}

.metric-value {
    font-weight: 600;
    color: #333;
}

.coverage-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 8px;
    margin-top: 8px;
}

.coverage-item {
    padding: 8px;
    border-radius: 6px;
    text-align: center;
    font-size: 0.9em;
}

.coverage-item.covered {
    background-color: #e6ffe6;
    color: #006600;
    border: 1px solid #b3ffb3;
}

.coverage-item.not-covered {
    background-color: #f5f5f5;
    color: #666;
    border: 1px solid #ddd;
}

.status-pills {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.status-pill {
    padding: 4px 12px;
    border-radius: 16px;
    font-size: 0.9em;
    font-weight: 500;
}

.status-pill.yes {
    background-color: #e6ffe6;
    color: #006600;
    border: 1px solid #b3ffb3;
}

.status-pill.no {
    background-color: #ffe6e6;
    color: #990000;
    border: 1px solid #ffb3b3;
}

.status-pill.n\\/a {
    background-color: #f5f5f5;
    color: #666;
    border: 1px solid #ddd;
}

.dark .summary-card {
    background-color: #2a2a2a;
    border-color: #444;
}

.dark .summary-title,
.dark .summary-subtitle {
    color: #e0e0e0;
}

.dark .metric-label {
    color: #999;
}

.dark .metric-value {
    color: #fff;
}

.dark .coverage-item.covered {
    background-color: #1a3a1a;
    color: #90EE90;
    border-color: #2d5a2d;
}

.dark .coverage-item.not-covered {
    background-color: #333;
    color: #999;
    border-color: #444;
}

.dark .status-pill.yes {
    background-color: #1a3a1a;
    color: #90EE90;
    border-color: #2d5a2d;
}

.dark .status-pill.no {
    background-color: #3a1a1a;
    color: #FFB6B6;
    border-color: #5a2d2d;
}

.dark .status-pill.n\\/a {
    background-color: #333;
    color: #999;
    border-color: #444;
}

.overall-summary-card {
    width: 100% !important;
    margin-bottom: 30px;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}

.category-completion-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 16px;
    margin-top: 12px;
}

.category-completion-item {
    background-color: #f8f9fa;
    border-radius: 8px;
    padding: 12px;
}

.category-name {
    font-size: 0.9em;
    font-weight: 500;
    margin-bottom: 8px;
    color: #555;
}

.completion-bar-container {
    height: 24px;
    background-color: #eee;
    border-radius: 12px;
    position: relative;
    overflow: hidden;
}

.completion-bar {
    height: 100%;
    background-color: #4CAF50;
    transition: width 0.3s ease;
}

.completion-text {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.8em;
    font-weight: 600;
    color: #333;
}

.dark .category-completion-item {
    background-color: #2a2a2a;
}

.dark .category-name {
    color: #ccc;
}

.dark .completion-bar-container {
    background-color: #333;
}

.dark .completion-bar {
    background-color: #2e7d32;
}

.dark .completion-text {
    color: #fff;
}
.completion-bar-container.not-selected {
    opacity: 0.5;
    background-color: #f0f0f0;
}

.completion-bar-container.na {
    background-color: #f0f0f0;
}

.completion-bar-container.na .completion-bar {
    background-color: #999;
    width: 0 !important;  /* Ensure no bar shows for N/A */
}

.dark .completion-bar-container.na {
    background-color: #2d2d2d;
}

.dark .completion-bar-container.na .completion-bar {
    background-color: #666;
}

.category-completion-item {
    background-color: #f8f9fa;
    border-radius: 8px;
    padding: 12px;
    height: 80px;
    display: grid;
    grid-template-rows: 1fr auto;
    gap: 8px;
}

.category-name {
    font-size: 0.9em;
    font-weight: 500;
    color: #555;
    align-self: start;
    line-height: 1.3;
}

.completion-bar-container {
    height: 24px;
    background-color: #eee;
    border-radius: 12px;
    position: relative;
    overflow: hidden;
    align-self: end;
}
"""

first_model = next(iter(models.values()))
category_choices = list(first_model['scores'].keys())

with gr.Blocks(css=css) as demo:
    gr.Markdown("# AI Model Social Impact Scorecard Dashboard")
    
    with gr.Row():
        tab_selection = gr.Radio(["Leaderboard", "Category Analysis", "Detailed Scorecard"], 
                                label="Select Tab", value="Leaderboard")
    
    with gr.Row():
        model_chooser = gr.Dropdown(choices=[""] + list(models.keys()),
                                  label="Select Model for Details", 
                                  value="",
                                  interactive=True, visible=False)
        model_multi_chooser = gr.Dropdown(choices=list(models.keys()),
                                        label="Select Models for Comparison", 
                                        multiselect=True, interactive=True, visible=False)
        category_filter = gr.CheckboxGroup(choices=category_choices,
                                         label="Filter Categories", 
                                         value=category_choices,
                                         visible=False)
    
    with gr.Column(visible=True) as leaderboard_tab:
        leaderboard_output = gr.HTML()
    
    with gr.Column(visible=False) as category_analysis_tab:
        category_chart = gr.Plot()
    
    with gr.Column(visible=False) as detailed_scorecard_tab:
        model_metadata = gr.HTML()
        all_category_cards = gr.HTML()
        total_score = gr.Markdown()

    # Initialize the dashboard with the leaderboard
    leaderboard_output.value = create_leaderboard()
    
    def update_dashboard(tab, selected_models, selected_model, selected_categories):
            leaderboard_visibility = gr.update(visible=False)
            category_chart_visibility = gr.update(visible=False)
            detailed_scorecard_visibility = gr.update(visible=False)
            model_chooser_visibility = gr.update(visible=False)
            model_multi_chooser_visibility = gr.update(visible=False)
            category_filter_visibility = gr.update(visible=False)

            if tab == "Leaderboard":
                leaderboard_visibility = gr.update(visible=True)
                leaderboard_html = create_leaderboard()
                return [leaderboard_visibility, category_chart_visibility, detailed_scorecard_visibility,
                        model_chooser_visibility, model_multi_chooser_visibility, category_filter_visibility,
                        gr.update(value=leaderboard_html), gr.update(), gr.update(), gr.update(), gr.update()]
            
            elif tab == "Category Analysis":
                category_chart_visibility = gr.update(visible=True)
                model_multi_chooser_visibility = gr.update(visible=True)
                category_filter_visibility = gr.update(visible=True)
                category_plot = create_category_chart(selected_models or [], selected_categories)
                return [leaderboard_visibility, category_chart_visibility, detailed_scorecard_visibility,
                        model_chooser_visibility, model_multi_chooser_visibility, category_filter_visibility,
                        gr.update(), gr.update(value=category_plot), gr.update(), gr.update(), gr.update()]
            
            elif tab == "Detailed Scorecard":
                detailed_scorecard_visibility = gr.update(visible=True)
                model_chooser_visibility = gr.update(visible=True)
                category_filter_visibility = gr.update(visible=True)
                if selected_model:
                    scorecard_updates = update_detailed_scorecard(selected_model, selected_categories)
                else:
                    scorecard_updates = [
                        gr.update(value="Please select a model to view details.", visible=True),
                        gr.update(visible=False),
                        gr.update(visible=False)
                    ]
                return [leaderboard_visibility, category_chart_visibility, detailed_scorecard_visibility,
                        model_chooser_visibility, model_multi_chooser_visibility, category_filter_visibility,
                        gr.update(), gr.update()] + scorecard_updates

    # Set up event handlers
    tab_selection.change(
        fn=update_dashboard,
        inputs=[tab_selection, model_multi_chooser, model_chooser, category_filter],
        outputs=[leaderboard_tab, category_analysis_tab, detailed_scorecard_tab,
                model_chooser, model_multi_chooser, category_filter,
                leaderboard_output, category_chart, model_metadata,
                all_category_cards, total_score]
    )

    model_chooser.change(
        fn=update_dashboard,
        inputs=[tab_selection, model_multi_chooser, model_chooser, category_filter],
        outputs=[leaderboard_tab, category_analysis_tab, detailed_scorecard_tab,
                model_chooser, model_multi_chooser, category_filter,
                leaderboard_output, category_chart, model_metadata,
                all_category_cards, total_score]
    )

    model_multi_chooser.change(
        fn=update_dashboard,
        inputs=[tab_selection, model_multi_chooser, model_chooser, category_filter],
        outputs=[leaderboard_tab, category_analysis_tab, detailed_scorecard_tab,
                model_chooser, model_multi_chooser, category_filter,
                leaderboard_output, category_chart, model_metadata,
                all_category_cards, total_score]
    )

    category_filter.change(
        fn=update_dashboard,
        inputs=[tab_selection, model_multi_chooser, model_chooser, category_filter],
        outputs=[leaderboard_tab, category_analysis_tab, detailed_scorecard_tab,
                model_chooser, model_multi_chooser, category_filter,
                leaderboard_output, category_chart, model_metadata,
                all_category_cards, total_score]
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()