File size: 42,714 Bytes
3232d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1225dd
3232d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1225dd
3232d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1225dd
3232d64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import os
import sys
import traceback
import logging

# Disable SSL verification for curl requests if needed
os.environ['CURL_CA_BUNDLE'] = ''

# Configure minimal logging first thing - before any imports
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logging.getLogger("httpx").setLevel(logging.ERROR)
logging.getLogger("urllib3").setLevel(logging.ERROR)
logging.getLogger("matplotlib").setLevel(logging.WARNING)
logging.getLogger("huggingface_hub").setLevel(logging.ERROR)

from gradio.oauth import OAuthProfile

from src.display.about import (
    CITATION_BUTTON_LABEL,
    CITATION_BUTTON_TEXT,
    EVALUATION_QUEUE_TEXT,
    INTRODUCTION_TEXT,
    LLM_BENCHMARKS_TEXT,
    TITLE,
)

from src.display.css_html_js import custom_css

from src.utils import (
    restart_space,
    load_benchmark_results,
    create_benchmark_plots,
    create_combined_leaderboard_table,
    create_evalmix_table,
    create_light_eval_table,
    create_raw_details_table,
    create_human_arena_table,
    update_supported_base_models
)

# Pipelines utils fonksiyonlarını import et
from pipelines.utils.common import search_and_filter
from pipelines.unified_benchmark import submit_unified_benchmark

# Evaluation types
EVAL_TYPES = ["EvalMix", "RAG-Judge", "Light-Eval", "Arena", "Snake-Bench"]

# Initialize OAuth configuration
OAUTH_CLIENT_ID = os.getenv("OAUTH_CLIENT_ID")
OAUTH_CLIENT_SECRET = os.getenv("OAUTH_CLIENT_SECRET")
OAUTH_SCOPES = os.getenv("OAUTH_SCOPES", "email")
OPENID_PROVIDER_URL = os.getenv("OPENID_PROVIDER_URL")
SESSION_TIMEOUT_MINUTES = int(os.getenv("HF_OAUTH_EXPIRATION_MINUTES", 30))

def format_dataframe(df, is_light_eval_detail=False):
    """
    Float değerleri 2 ondalık basamağa yuvarla,
    'file' sütununu kaldır ve kolon isimlerini düzgün formata getir
    
    Args:
        df: DataFrame to format
        is_light_eval_detail: If True, use 4 decimal places for light eval detail results
    """
    if df.empty:
        return df
    
    # 'file' sütununu kaldır
    if 'file' in df.columns:
        df = df.drop(columns=['file'])
    
    # Specifically remove problematic columns
    columns_to_remove = ["run_id", "user_id", "total_success_references", "Total Success References", "total_eval_samples", 
                         "total_samples", "samples_number"]
    for col in columns_to_remove:
        if col in df.columns:
            df = df.drop(columns=[col])
    
    # Float değerleri yuvarlama - light eval detail için 4 hane, diğerleri için 2 hane
    decimal_places = 4 if is_light_eval_detail else 2
    for column in df.columns:
        try:
            if pd.api.types.is_float_dtype(df[column]):
                df[column] = df[column].round(decimal_places)
        except:
            continue
    
    # Kolon isimlerini düzgün formata getir
    column_mapping = {}
    for col in df.columns:
        # Skip run_id and user_id fields
        if col.lower() in ["run_id", "user_id"]:
            continue
            
        # Special handling for Turkish Semantic column
        if "turkish_semantic" in col.lower():
            column_mapping[col] = "Turkish Semantic"
            continue
            
        # Special handling for Multilingual Semantic column
        if "multilingual_semantic" in col.lower():
            column_mapping[col] = "Multilingual Semantic"
            continue
            
        # Skip already well-formatted columns or columns that contain special characters
        if col == "Model Name" or " " in col:
            # Still process column if it contains "mean"
            if " mean" in col.lower():
                cleaned_col = col.replace(" mean", "").replace(" Mean", "")
                column_mapping[col] = cleaned_col
            continue
            
        # model_name column should be Model Name
        if col == "model_name":
            column_mapping[col] = "Model Name"
            continue
        
        # Remove the word "mean" from column names (case insensitive)
        cleaned_col = col.replace(" mean", "").replace("_mean", "")
        
        # Format column name by replacing underscores with spaces and capitalizing each word
        formatted_col = " ".join([word.capitalize() for word in cleaned_col.replace("_", " ").split()])
        column_mapping[col] = formatted_col
    
    # Rename columns with the mapping
    if column_mapping:
        df = df.rename(columns=column_mapping)
    
    return df

# User authentication function
def check_user_login(profile):
    if profile is None:
        return False, "Please log in with your Hugging Face account to submit models for benchmarking."
    
    # In some environments, profile may be a string instead of a profile object
    if isinstance(profile, str):
        if profile == "":
            return False, "Please log in with your Hugging Face account to submit models for benchmarking."
        return True, f"Logged in as {profile}"
        
    # Normal case where profile is an object with username attribute
    return True, f"Logged in as {profile.username}"

def create_demo():
    # Get logger for this function
    logger = logging.getLogger("mezura")
    
    with gr.Blocks(css=custom_css) as demo:
        # Update supported base models at startup
        logger.info("Updating supported base models at startup...")
        update_supported_base_models()
        logger.info("Base models updated successfully")
        
        gr.Markdown(TITLE)
        gr.Markdown(INTRODUCTION_TEXT)
        
        # Hidden session state to track login expiration
        session_expiry = gr.State(None)
        
        try:
            # Benchmark sonuçlarını yükle
            benchmark_results = load_benchmark_results()
            default_plots = create_benchmark_plots(benchmark_results, "avg")
            
            # State variable to track login state across page refreshes
            login_state = gr.State(value=False)
            
            with gr.Tabs() as tabs:
                with gr.TabItem("🏆 LLM Benchmark", elem_id="llm-benchmark-tab"):
                    gr.Markdown("## Model Evaluation Results")
                    gr.Markdown("This screen shows model performance across different evaluation categories.")
                    
                    # Remove the separate refresh button row
                    # Instead, combine search and refresh in one row
                    with gr.Row():
                        search_input = gr.Textbox(
                            label="🔍 Search for your model (separate multiple queries with `;`) and press ENTER...",
                            placeholder="Enter model name or evaluation information...",
                            show_label=False
                        )
                    #     # Update refresh button to be orange with "Refresh Results" text
                    #     refresh_button = gr.Button("🔄 Refresh Results", variant="primary")
                    
                    # # Status display for refresh results
                    # refresh_status = gr.Markdown("", visible=False)

                    # Benchmark tablarını semboller içeren tab grubuyla göster
                    with gr.Tabs() as benchmark_tabs:
                        with gr.TabItem("🏆 Leaderboard"):
                            # Birleşik leaderboard tablosu - avg_json dosyalarındaki tüm bilgileri göster
                            # Only use default data (avg files) for the leaderboard
                            combined_df = create_combined_leaderboard_table(benchmark_results)
                            # Float değerleri formatlama
                            combined_df = format_dataframe(combined_df)
                            
                            # Tüm sütunları göster
                            if not combined_df.empty:
                                leaderboard_df = combined_df.copy()
                            else:
                                leaderboard_df = pd.DataFrame({"Model Name": ["No data available"]})
                            
                            # Orijinal veriyi saklayacak state değişkeni
                            original_leaderboard_data = gr.State(value=leaderboard_df)
                            
                            combined_table = gr.DataFrame(
                                value=leaderboard_df,
                                label="Model Performance Comparison",
                                interactive=False,
                                column_widths=["300px", "165px" ,"165px", "120px", "120px", "180px", "220px", "100px", "100px", "120px"]

                            )
                            
                        with gr.TabItem("🏟️ Auto Arena"):
                            # Arena sonuçları - detail dosyalarını kullan
                            arena_details_df = create_raw_details_table(benchmark_results, "arena")
                            arena_details_df = format_dataframe(arena_details_df)
                            
                            if arena_details_df.empty:
                                arena_details_df = pd.DataFrame({"model_name": ["No data available"]})
                            
                            arena_table = gr.DataFrame(
                                value=arena_details_df,
                                label="Arena Detailed Results",
                                interactive=False,
                                column_widths=["300px", "150px", "110px", "110px", "180px", "100px", "120px"]

                            )
                            
                        with gr.TabItem("👥 Human Arena"):
                            # Human Arena sonuçları - detail dosyalarını kullan
                            human_arena_data = benchmark_results["raw"]["human_arena"]
                            if human_arena_data:
                                human_arena_df = create_human_arena_table(human_arena_data)
                            else:
                                human_arena_df = pd.DataFrame()
                                
                            human_arena_df = format_dataframe(human_arena_df)
                            
                            if human_arena_df.empty:
                                human_arena_df = pd.DataFrame({"Model Name": ["No data available"]})
                            
                            human_arena_table = gr.DataFrame(
                                value=human_arena_df,
                                label="Human Arena Results",
                                interactive=False,
                                column_widths=["300px", "150px", "110px", "110px", "110px", "156px", "169px", "100px", "120px"]

                            )
                            
                        with gr.TabItem("📚 Retrieval"):
                            # RAG Judge sonuçları - detail dosyalarını kullan
                            rag_details_df = create_raw_details_table(benchmark_results, "retrieval")
                            rag_details_df = format_dataframe(rag_details_df)
                            
                            if rag_details_df.empty:
                                rag_details_df = pd.DataFrame({"model_name": ["No data available"]})
                            
                            rag_table = gr.DataFrame(
                                value=rag_details_df,
                                label="Retrieval Detailed Results",
                                interactive=False,
                                column_widths=["280px", "120px", "140px", "140px", "140px", "120px", "160px", "100px", "120px"]

                            )
                            
                        with gr.TabItem("⚡ Light Eval"):
                            # Light Eval sonuçları - detail dosyalarını kullan
                            light_details_data = benchmark_results["raw"]["light_eval"]
                            if light_details_data:
                                light_details_df = create_light_eval_table(light_details_data, is_detail=True)
                            else:
                                light_details_df = pd.DataFrame()
                                
                            light_details_df = format_dataframe(light_details_df, is_light_eval_detail=True)
                            
                            if light_details_df.empty:
                                light_details_df = pd.DataFrame({"model_name": ["No data available"]})
                            
                            light_table = gr.DataFrame(
                                value=light_details_df,
                                label="Light Eval Detailed Results",
                                interactive=False,
                                column_widths=["300px", "110px", "110px", "143px", "130px", "130px", "110px", "110px", "100px", "120px"]

                            )
                            
                        with gr.TabItem("📋 EvalMix"):
                            # Hybrid Benchmark sonuçları - detail dosyalarını kullan
                            hybrid_details_df = create_raw_details_table(benchmark_results, "evalmix")
                            hybrid_details_df = format_dataframe(hybrid_details_df)
                            
                            if hybrid_details_df.empty:
                                hybrid_details_df = pd.DataFrame({"model_name": ["No data available"]})
                            
                            hybrid_table = gr.DataFrame(
                                value=hybrid_details_df,
                                label="EvalMix Detailed Results",
                                interactive=False,
                                column_widths=["300px", "180px", "230px", "143px", "110px", "110px", "110px", "110px", "169px", "220px" ,"100px", "120px"]

                            )
                            
                        with gr.TabItem("🐍 𝐒𝐧𝐚𝐤𝐞 𝐁𝐞𝐧𝐜𝐡"):
                            # Snake Benchmark sonuçları - detail dosyalarını kullan
                            snake_details_df = create_raw_details_table(benchmark_results, "snake")
                            snake_details_df = format_dataframe(snake_details_df)
                            
                            if snake_details_df.empty:
                                snake_details_df = pd.DataFrame({"model_name": ["No data available"]})
                            
                            snake_table = gr.DataFrame(
                                value=snake_details_df,
                                label="Snake Benchmark Detailed Results",
                                interactive=False,
                                column_widths=["300px", "130px", "110px", "117px", "110px", "110px", "110px", "117px", "100px", "120px"]

                            )
                            
                        # with gr.TabItem("📊 LM-Harness"):
                        #     # LM Harness sonuçları - detail dosyalarını kullan
                        #     lmharness_details_df = create_raw_details_table(benchmark_results, "lm_harness")
                        #     lmharness_details_df = format_dataframe(lmharness_details_df)
                        #     
                        #     if lmharness_details_df.empty:
                        #         lmharness_details_df = pd.DataFrame({"model_name": ["No data available"]})
                        #     
                        #     lmharness_table = gr.DataFrame(
                        #         value=lmharness_details_df,
                        #         label="LM Harness Detailed Results",
                        #         interactive=False
                        #     )
                    

                    # # Refresh butonu bağlantısı
                    # refresh_button.click(
                    #     refresh_leaderboard,
                    #     inputs=[],
                    #     outputs=[
                    #         refresh_status,
                    #         combined_table,
                    #         hybrid_table,
                    #         rag_table,
                    #         light_table,
                    #         arena_table,
                    #         lmharness_table,
                    #         snake_table
                    #     ]
                    # )

                    # Tüm sekmeler için ortak arama fonksiyonu
                    def search_all_tabs(query, original_data):
                        """
                        Tüm sekmelerde arama yapar
                        """
                        if not query or query.strip() == "":
                            # Boş arama - orijinal veriyi döndür
                            return (original_data, arena_details_df, human_arena_df, 
                                   rag_details_df, light_details_df, hybrid_details_df, snake_details_df)
                        
                        # Arama var - tüm sekmeleri filtrele
                        return (
                            search_and_filter(query, original_data, "All"),
                            search_and_filter(query, arena_details_df, "All"), 
                            search_and_filter(query, human_arena_df, "All"),
                            search_and_filter(query, rag_details_df, "All"),
                            search_and_filter(query, light_details_df, "All"),
                            search_and_filter(query, hybrid_details_df, "All"),
                            search_and_filter(query, snake_details_df, "All")
                        )
                    
                    # Arama fonksiyonu - tüm sekmeleri güncelle
                    search_input.change(
                        search_all_tabs,
                        inputs=[search_input, original_leaderboard_data],
                        outputs=[combined_table, arena_table, human_arena_table, rag_table, light_table, hybrid_table, snake_table]
                    )

                with gr.TabItem("ℹ️ About", elem_id="about-tab"):
                    gr.Markdown(LLM_BENCHMARKS_TEXT, elem_classes="markdown-text")
                
                with gr.TabItem("📊 Datasets", elem_id="datasets-tab"):
                    gr.Markdown("## Benchmark Datasets")
                    gr.Markdown("""
                    This section provides detailed information about the datasets used in our evaluation benchmarks.
                    Each dataset has been carefully selected and adapted to provide comprehensive model evaluation across different domains and capabilities.
                    """)
                    
                    # Create and display the datasets table
                    datasets_html = """
                    <div style="margin-top: 20px;">
                        <h3>Available Datasets for Evaluation</h3>
                        <table style="width: 100%; border-collapse: collapse; margin-top: 10px;">
                            <thead>
                                <tr style="background-color: var(--background-fill-secondary);">
                                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid var(--border-color-primary); width: 20%;">Dataset</th>
                                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid var(--border-color-primary); width: 18%;">Evaluation Task</th>
                                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid var(--border-color-primary); width: 10%;">Language</th>
                                    <th style="padding: 12px; text-align: left; border-bottom: 2px solid var(--border-color-primary); width: 52%;">Description</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/mmlu_tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/mmlu_tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval MMLU</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish adaptation of MMLU (Massive Multitask Language Understanding) v0.2 covering 57 academic subjects including mathematics, physics, chemistry, biology, history, law, and computer science. Tests knowledge and reasoning capabilities across multiple domains with multiple-choice questions.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/truthful_qa-tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/truthful_qa-tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval TruthfulQA</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish version of TruthfulQA (v0.2) designed to measure model truthfulness and resistance to generating false information. Contains questions where humans often answer incorrectly due to misconceptions or false beliefs, testing the model's ability to provide accurate information.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/winogrande-tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/winogrande-tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval WinoGrande</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish adaptation of WinoGrande (v0.2) focusing on commonsense reasoning through pronoun resolution tasks. Tests the model's ability to understand context, make logical inferences, and resolve ambiguous pronouns in everyday scenarios.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/hellaswag_tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/hellaswag_tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval HellaSwag</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish version of HellaSwag (v0.2) for commonsense reasoning evaluation. Tests the model's ability to predict plausible continuations of everyday scenarios and activities, requiring understanding of common sense and typical human behavior patterns.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/arc-tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/arc-tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval ARC</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish adaptation of ARC (AI2 Reasoning Challenge) v0.2 focusing on science reasoning and question answering. Contains grade school level science questions that require reasoning beyond simple factual recall, covering topics in physics, chemistry, biology, and earth science.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/malhajar/gsm8k_tr-v0.2" target="_blank" style="color: #0066cc; text-decoration: none;">malhajar/gsm8k_tr-v0.2</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Lighteval GSM8K</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">Turkish version of GSM8K (Grade School Math 8K) v0.2 for mathematical reasoning evaluation. Contains grade school level math word problems that require multi-step reasoning, arithmetic operations, and logical problem-solving skills to arrive at the correct numerical answer.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/newmindai/mezura-eval-data" target="_blank" style="color: #0066cc; text-decoration: none;">newmindai/mezura-eval-data</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Auto-Arena</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">mezura-eval dataset is a Turkish-language legal text dataset designed for evaluation tasks with RAG context support. The subsets include domains like Environmental Law, Tax Law, Data Protection Law and Health Law each containing annotated samples. Every row includes structured fields such as the category, concept, input and contextual information drawn from sources like official decisions.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/newmindai/mezura-eval-data" target="_blank" style="color: #0066cc; text-decoration: none;">newmindai/mezura-eval-data</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">EvalMix</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">mezura-eval dataset is a Turkish-language legal text dataset designed for evaluation tasks with RAG context support. The subsets include domains like Environmental Law, Tax Law, Data Protection Law and Health Law each containing annotated samples. Every row includes structured fields such as the category, concept, input and contextual information drawn from sources like official decisions.</td>
                                </tr>
                                <tr>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 20%;"><a href="https://huggingface.co/datasets/newmindai/mezura-eval-data" target="_blank" style="color: #0066cc; text-decoration: none;">newmindai/mezura-eval-data</a></td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 18%;">Retrieval</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 10%;">Turkish</td>
                                    <td style="padding: 10px; border-bottom: 1px solid var(--border-color-primary); width: 52%;">mezura-eval dataset is a Turkish-language legal text dataset designed for evaluation tasks with RAG context support. The subsets include domains like Environmental Law, Tax Law, Data Protection Law and Health Law each containing annotated samples. Every row includes structured fields such as the category, concept, input and contextual information drawn from sources like official decisions.</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    """
                    gr.HTML(datasets_html)
                
                with gr.TabItem("🔬 Evaluation", elem_id="evaluation-tab"):
                    gr.Markdown("""
                    <h2 align="center">Model Evaluation</h2>
                    
                    ### Evaluation Process:

                    1. **Login to Your Hugging Face Account**
                        - You must be logged in to submit models for evaluation
                    
                    2. **Enter Model Name**
                        - Input the HuggingFace model name or path you want to evaluate
                        - Example: meta-llama/Meta-Llama-3.1-70B-Instruct
                    
                    3. **Select Base Model**
                        - Choose the base model from the dropdown list
                        - The system will verify if your repository is a valid HuggingFace repository
                        - It will check if the model is trained from the selected base model
                    
                    4. **Start Evaluation**
                        - Click the "Start All Benchmarks" button to begin the evaluation
                        - If validation passes, your request will be processed
                        - If validation fails, you'll see an error message
                    
                    ### Important Limitations: 
                        - The model repository must be a maximum of 750 MB in size.
                        - For trained adapters, the maximum LoRA rank must be 32.
                    """)
                    
                    # Authentication Component (Always visible)
                    auth_container = gr.Group()
                    with auth_container:
                        # Simplified login button - Gradio will handle the OAuth
                        login_button = gr.LoginButton()
                    
                    # Get base models from API
                    from api.config import get_base_model_list
                    BASE_MODELS = get_base_model_list()
                    
                    # Fallback to static list if API list is empty
                    if not BASE_MODELS:
                        BASE_MODELS = [
                            "meta-llama/Meta-Llama-3.1-8B-Instruct",
                            "meta-llama/Llama-3.2-3B-Instruct",
                            "meta-llama/Llama-3.3-70B-Instruc",
                            "Qwen/Qwen2.5-72B-Instruct",
                            "Qwen/QwQ-32B",
                            "google/gemma-2-2b-it"
                        ]
                    
                    # Content that's only visible when logged in
                    login_dependent_content = gr.Group(visible=False)
                    with login_dependent_content:
                        gr.Markdown("### Model Submission")
                        
                        # Model input
                        model_to_evaluate = gr.Textbox(
                            label="Adapter Repo ID", 
                            placeholder="e.g., valadapt/llama-3-8b-turkish"
                        )
                        
                        # Add note about supported model types
                        gr.Markdown("""
                        **Note:** Currently, only adapter models are supported. Merged models are not yet supported.
                        """, elem_classes=["info-text"])
                        
                        # Base model selection
                        base_model_dropdown = gr.Dropdown(
                            choices=BASE_MODELS, 
                            label="Base Model", 
                            allow_custom_value=True
                        )
                        
                        # Reasoning capability checkbox
                        reasoning_checkbox = gr.Checkbox(
                            label="Reasoning",
                            value=False,
                            info="Enable reasoning capability during evaluation"
                        )
                        
                        # Email input
                        email_input = gr.Textbox(
                            label="Email Address", 
                            placeholder="example@domain.com",
                            info="You'll receive notification when benchmark is complete"
                        )
                        
                        # Submit button - CRITICAL: This submit button is only visible when logged in
                        submit_button = gr.Button("Start All Benchmarks", variant="primary")
                        
                        # Result area (initially empty)
                        result_output = gr.Markdown("")
                    
                    # Status area for authentication errors (initially hidden)
                    auth_error = gr.Markdown(visible=False)
                    
                    # Function to handle login visibility
                    def toggle_form_visibility(profile):
                        
                        # User is not logged in
                        if profile is None:
                            return (
                                gr.update(visible=False), 
                                gr.update(
                                    visible=True,
                                    value="<p style='color: red; text-align: center; font-weight: bold;'>Authentication required. Please log in with your Hugging Face account to submit models.</p>"
                                )
                            )
                        
                        # Log successful authentication
                        try:
                            
                            if hasattr(profile, 'name'):
                                username = profile.name
                            elif hasattr(profile, 'username'):
                                username = profile.username
                            else:
                                username = str(profile)
                            
                            logger.info(f"User authenticated: {username}")
                        except Exception as e:
                            logger.info(f"LOGIN - Error inspecting profile: {str(e)}")
                        
                        # User is logged in - show form, hide error
                        return (
                            gr.update(visible=True),
                            gr.update(visible=False, value="")
                        )
                    
                    # Connect login button to visibility toggle
                    login_button.click(
                        fn=toggle_form_visibility,
                        inputs=[login_button],
                        outputs=[login_dependent_content, auth_error]
                    )
                    
                    # Check visibility on page load
                    demo.load(
                        fn=toggle_form_visibility,
                        inputs=[login_button],
                        outputs=[login_dependent_content, auth_error]
                    )
                    
                    # Handle submission with authentication check
                    def submit_model(model, base_model, reasoning, email, profile):
                        # Authentication check
                        if profile is None:
                            logging.warning("Unauthorized submission attempt with no profile")
                            return "<p style='color: red; font-weight: bold;'>Authentication required. Please log in with your Hugging Face account.</p>"
                        
                        # IMPORTANT: In local development, Gradio returns "Sign in with Hugging Face" string
                        # This is NOT a real authentication, just a placeholder for local testing
                        if isinstance(profile, str) and profile == "Sign in with Hugging Face":
                            # Block submission in local dev with mock auth
                            return "<p style='color: orange; font-weight: bold;'>⚠️ HF authentication required.</p>"
                        
                        # Email is required
                        if not email or email.strip() == "":
                            return "<p style='color: red; font-weight: bold;'>Email address is required to receive benchmark results.</p>"
                        
                        # Check if the model is a merged model (not supported)
                        try:
                            from src.submission.check_validity import determine_model_type
                            model_type, _ = determine_model_type(model)
                            if model_type == "merged_model" or model_type == "merge":
                                return "<p style='color: red; font-weight: bold;'>Merged models are not supported yet. Please submit an adapter model instead.</p>"
                        except Exception as e:
                            # If error checking model type, continue with submission
                            logging.warning(f"Error checking model type: {str(e)}")
                        
                        # Call the benchmark function with profile information
                        # base_model validasyonunu kaldırdık ama parametre olarak yine de gönderiyoruz
                        result_message, _ = submit_unified_benchmark(model, base_model, reasoning, email, profile)
                        logging.info(f"Submission processed for model: {model}")
                        return result_message
                    
                    # Connect submit button
                    submit_button.click(
                        fn=submit_model,
                        inputs=[
                            model_to_evaluate, 
                            base_model_dropdown, 
                            reasoning_checkbox, 
                            email_input, 
                            login_button
                        ],
                        outputs=[result_output]
                    )

        except Exception as e:
            traceback.print_exc()
            gr.Markdown(f"## Error: An issue occurred while loading the LLM Benchmark screen")
            gr.Markdown(f"Error message: {str(e)}")
            gr.Markdown("Please check your configuration and try again.")
        
        # Citation information at the bottom
        gr.Markdown("---")
        with gr.Accordion(CITATION_BUTTON_LABEL, open=False):
            gr.Textbox(
                value=CITATION_BUTTON_TEXT,
                lines=10,
                show_copy_button=True,
                label=None
            )
            
    return demo

if __name__ == "__main__":
    # Get app logger
    logger = logging.getLogger("mezura")
    
    # Additional sensitive filter for remaining logs
    class SensitiveFilter(logging.Filter):
        def filter(self, record):
            msg = record.getMessage().lower()
            # Filter out messages with tokens, URLs with sign= in them, etc
            sensitive_patterns = ["token", "__sign=", "request", "auth", "http request"]
            return not any(pattern in msg.lower() for pattern in sensitive_patterns)
    
    # Apply the filter to all loggers
    for logger_name in logging.root.manager.loggerDict:
        logging.getLogger(logger_name).addFilter(SensitiveFilter())
    
    try:
        logger.info("Creating demo...")
        demo = create_demo()
        logger.info("Launching demo on 0.0.0.0...")
        
        # Add options to fix the session.pop error
        demo.launch(
            server_name="0.0.0.0", 
            server_port=7860
        )
        
    except FileNotFoundError as e:
        logger.critical(f"Configuration file not found: {e}")
        print(f"\n\nERROR: Configuration file not found. Please ensure config/api_config.yaml exists.\n{e}\n")
        sys.exit(1)
    except ValueError as e:
        logger.critical(f"Configuration error: {e}")
        print(f"\n\nERROR: Invalid configuration. Please check your config/api_config.yaml file.\n{e}\n")
        sys.exit(1)        
    except Exception as e:
        logger.critical(f"Could not launch demo: {e}", exc_info=True)