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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Model evaluation queue system for Dynamic Highscores.

This module handles the evaluation queue, CPU-only processing,
and enforces daily submission limits for users.
"""

import os
import json
import time
import threading
import queue as queue_module
from datetime import datetime, timedelta
import gradio as gr
from huggingface_hub import HfApi, hf_hub_download, snapshot_download
from datasets import load_dataset
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import sqlite3

class EvaluationQueue:
    """Manages the evaluation queue for model benchmarking."""
    
    def __init__(self, db_manager, auth_manager):
        """Initialize the evaluation queue manager.
        
        Args:
            db_manager: Database manager instance
            auth_manager: Authentication manager instance
        """
        self.db_manager = db_manager
        self.auth_manager = auth_manager
        self.hf_api = HfApi()
        self.queue = queue_module.Queue()
        self.is_processing = False
        self.worker_thread = None
        self.model_tags = ["Merge", "Agent", "Reasoning", "Coding", "General", "Specialized", "Instruction", "Chat"]
        self.current_evaluation = None
        self.progress = 0
        self.progress_lock = threading.Lock()
        # Memory limit for models in GB (leave 2GB for system)
        self.memory_limit_gb = 14.0
    
    def start_worker(self):
        """Start the worker thread for processing the evaluation queue."""
        if self.worker_thread is None or not self.worker_thread.is_alive():
            self.is_processing = True
            self.worker_thread = threading.Thread(target=self._process_queue)
            self.worker_thread.daemon = True
            self.worker_thread.start()
    
    def stop_worker(self):
        """Stop the worker thread."""
        self.is_processing = False
        if self.worker_thread and self.worker_thread.is_alive():
            self.worker_thread.join(timeout=1.0)
    
    def check_model_size(self, model_id):
        """Check if a model will fit within RAM limitations.
        
        Args:
            model_id: HuggingFace model ID
            
        Returns:
            tuple: (will_fit, message)
        """
        try:
            # Query model info from the HuggingFace API
            model_info_obj = self.hf_api.model_info(model_id)
            
            # Initialize total size
            total_size_gb = 0
            
            # Try different approaches to get model size based on API response structure
            if hasattr(model_info_obj, 'safetensors') and model_info_obj.safetensors:
                # New API format with safetensors dict
                for file_info in model_info_obj.safetensors.values():
                    if hasattr(file_info, 'size'):
                        total_size_gb += file_info.size / (1024 * 1024 * 1024)
                    elif isinstance(file_info, dict) and 'size' in file_info:
                        total_size_gb += file_info['size'] / (1024 * 1024 * 1024)
            
            # Fallback to siblings method
            if total_size_gb == 0 and hasattr(model_info_obj, 'siblings'):
                for sibling in model_info_obj.siblings:
                    if hasattr(sibling, 'size'):
                        if sibling.rfilename.endswith(('.bin', '.safetensors', '.pt')):
                            total_size_gb += sibling.size / (1024 * 1024 * 1024)
                    elif isinstance(sibling, dict) and 'size' in sibling:
                        if sibling.get('rfilename', '').endswith(('.bin', '.safetensors', '.pt')):
                            total_size_gb += sibling['size'] / (1024 * 1024 * 1024)
            
            # If we still couldn't determine size, try a reasonable guess based on model name
            if total_size_gb == 0:
                # Try to guess from model name (e.g., if it has "7b" in the name)
                model_name = model_id.lower()
                size_indicators = {
                    "1b": 1, "2b": 2, "3b": 3, "5b": 5, "7b": 7, "8b": 8,
                    "10b": 10, "13b": 13, "20b": 20, "30b": 30, "65b": 65, "70b": 70
                }
                
                for indicator, size in size_indicators.items():
                    if indicator in model_name.replace("-", "").replace("_", ""):
                        total_size_gb = size * 2  # Rough estimate: param count × 2 for size in GB
                        break
            
            # If we still couldn't determine size, use a default
            if total_size_gb == 0:
                # Try direct API method
                try:
                    print(f"Checking model size with direct method for {model_id}")
                    # Print out the entire structure for debugging
                    print(f"Model info: {model_info_obj.__dict__}")
                    
                    # Default to a conservative estimate
                    total_size_gb = 5  # Assume a 5GB model as default
                except Exception as e:
                    print(f"Direct size check failed: {e}")
                    return True, "Unable to determine model size accurately, but allowing submission with caution"
            
            # Account for memory overhead
            estimated_ram_needed = total_size_gb * 1.3  # 30% overhead
            
            # Check against limit
            if estimated_ram_needed > self.memory_limit_gb:
                return False, f"Model is too large (approximately {total_size_gb:.1f}GB, needs {estimated_ram_needed:.1f}GB RAM). Maximum allowed is {self.memory_limit_gb}GB."
            
            return True, f"Model size check passed ({total_size_gb:.1f}GB, estimated {estimated_ram_needed:.1f}GB RAM usage)"
        
        except Exception as e:
            print(f"Model size check error: {e}")
            # Log more details for debugging
            import traceback
            traceback.print_exc()
            
            # Allow submission with warning
            return True, f"Warning: Could not verify model size ({str(e)}). Please ensure your model is under {self.memory_limit_gb}GB."
    
    def _process_queue(self):
        """Process the evaluation queue in a separate thread."""
        while self.is_processing:
            try:
                # Get the next evaluation from the database
                pending_evals = self.db_manager.get_evaluation_results(status="pending")
                
                if pending_evals:
                    # Sort by priority and added_at
                    next_eval = pending_evals[0]
                    
                    # Update status to running
                    self.db_manager.update_evaluation_status(next_eval['id'], 'running')
                    
                    # Set current evaluation and reset progress
                    with self.progress_lock:
                        self.current_evaluation = next_eval
                        self.progress = 0
                    
                    try:
                        # Get model and benchmark details
                        model_info = self.db_manager.get_model(next_eval['model_id'])
                        benchmark_info = self.db_manager.get_benchmark(next_eval['benchmark_id'])
                        
                        if model_info and benchmark_info:
                            # Check if model will fit in memory
                            will_fit, message = self.check_model_size(model_info['hf_model_id'])
                            
                            if not will_fit:
                                raise Exception(f"Model too large for evaluation: {message}")
                                
                            # Run the evaluation
                            results = self._run_evaluation(
                                model_info['hf_model_id'],
                                benchmark_info['dataset_id']
                            )
                            
                            # Calculate overall score
                            score = self._calculate_overall_score(results)
                            
                            # Update status to completed with results
                            self.db_manager.update_evaluation_status(
                                next_eval['id'],
                                'completed',
                                results=results,
                                score=score
                            )
                        else:
                            raise Exception("Model or benchmark not found")
                    except Exception as e:
                        print(f"Evaluation error: {e}")
                        # Update status to failed with error message
                        error_results = {"error": str(e)}
                        self.db_manager.update_evaluation_status(
                            next_eval['id'], 
                            'failed',
                            results=error_results
                        )
                    
                    # Clear current evaluation
                    with self.progress_lock:
                        self.current_evaluation = None
                        self.progress = 0
                else:
                    # No evaluations in queue, sleep for a bit
                    time.sleep(5)
            except Exception as e:
                print(f"Queue processing error: {e}")
                time.sleep(5)
    
    def _run_evaluation(self, model_id, dataset_id):
        """Run an evaluation for a model on a benchmark.
        
        Args:
            model_id: HuggingFace model ID
            dataset_id: HuggingFace dataset ID (with optional config)
            
        Returns:
            dict: Evaluation results
        """
        # Update progress
        with self.progress_lock:
            self.progress = 5  # Starting evaluation
        
        # Parse dataset ID and config
        if ":" in dataset_id:
            dataset_id, config = dataset_id.split(":", 1)
        else:
            config = None
        
        # Update progress
        with self.progress_lock:
            self.progress = 10  # Loading dataset
        
        # Load the dataset
        try:
            if config:
                dataset = load_dataset(dataset_id, config, split="test")
            else:
                dataset = load_dataset(dataset_id, split="test")
        except Exception as e:
            return {"error": f"Failed to load dataset: {str(e)}"}
        
        # Update progress
        with self.progress_lock:
            self.progress = 20  # Loading model
        
        try:
            # Load the model with memory optimization settings
            device = "cpu"
            model = AutoModelForCausalLM.from_pretrained(
                model_id,
                device_map=device,
                torch_dtype=torch.float32,  # Use float32 for CPU
                low_cpu_mem_usage=True,     # Enable memory optimization
                offload_folder="offload",   # Enable offloading if needed
                offload_state_dict=True,    # Offload state dict for memory saving
                max_memory={0: f"{self.memory_limit_gb}GB"}  # Limit memory usage
            )
            tokenizer = AutoTokenizer.from_pretrained(model_id)
        except Exception as e:
            print(f"Model loading error: {e}")
            return {"error": f"Failed to load model: {str(e)}"}
        
        # Update progress
        with self.progress_lock:
            self.progress = 30  # Determining task type
        
        # Determine task type based on dataset features
        task_type = self._determine_task_type(dataset)
        
        # Update progress
        with self.progress_lock:
            self.progress = 40  # Starting evaluation
        
        try:
            # Run appropriate evaluation based on task type
            if task_type == "text-generation":
                results = self._evaluate_text_generation(model, tokenizer, dataset)
            elif task_type == "question-answering":
                results = self._evaluate_question_answering(model, tokenizer, dataset)
            elif task_type == "classification":
                results = self._evaluate_classification(model, tokenizer, dataset)
            elif task_type == "code-generation":
                results = self._evaluate_code_generation(model, tokenizer, dataset)
            else:
                # Default to general evaluation
                results = self._evaluate_general(model, tokenizer, dataset)
        except Exception as e:
            print(f"Evaluation task error: {e}")
            return {"error": f"Evaluation failed: {str(e)}"}
        
        # Update progress
        with self.progress_lock:
            self.progress = 95  # Cleaning up
        
        # Clean up to free memory
        del model
        del tokenizer
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        
        # Update progress
        with self.progress_lock:
            self.progress = 100  # Completed
        
        return results
    
    def get_current_progress(self):
        """Get the current evaluation progress.
        
        Returns:
            tuple: (current_evaluation, progress_percentage)
        """
        with self.progress_lock:
            return self.current_evaluation, self.progress
    
    def _determine_task_type(self, dataset):
        """Determine the task type based on dataset features.
        
        Args:
            dataset: HuggingFace dataset
            
        Returns:
            str: Task type
        """
        features = dataset.features
        
        # Check for common feature patterns
        if "question" in features and "answer" in features:
            return "question-answering"
        elif "code" in features or "solution" in features:
            return "code-generation"
        elif "label" in features or "class" in features:
            return "classification"
        elif "input" in features and "output" in features:
            return "text-generation"
        else:
            return "general"
    
    def _evaluate_text_generation(self, model, tokenizer, dataset):
        """Evaluate a model on text generation tasks.
        
        Args:
            model: HuggingFace model
            tokenizer: HuggingFace tokenizer
            dataset: HuggingFace dataset
            
        Returns:
            dict: Evaluation results
        """
        # Set up generation pipeline
        generator = pipeline(
            "text-generation",
            model=model,
            tokenizer=tokenizer,
            device="cpu"
        )
        
        # Sample a subset for evaluation (to keep runtime reasonable)
        if len(dataset) > 100:
            dataset = dataset.select(range(100))
        
        # Track metrics
        correct = 0
        total = 0
        generated_texts = []
        
        # Process each example
        for i, example in enumerate(dataset):
            # Update progress based on completion percentage
            with self.progress_lock:
                self.progress = 40 + int((i / len(dataset)) * 50)
            
            input_text = example.get("input", example.get("prompt", ""))
            expected_output = example.get("output", example.get("target", ""))
            
            if not input_text or not expected_output:
                continue
            
            # Generate text
            generated = generator(
                input_text,
                max_length=100,
                num_return_sequences=1
            )
            
            generated_text = generated[0]["generated_text"]
            generated_texts.append(generated_text)
            
            # Simple exact match check
            if expected_output.strip() in generated_text:
                correct += 1
            
            total += 1
        
        # Calculate metrics
        accuracy = correct / total if total > 0 else 0
        
        return {
            "accuracy": accuracy,
            "samples_evaluated": total,
            "generated_samples": generated_texts[:5]  # Include a few samples
        }
    
    def _evaluate_question_answering(self, model, tokenizer, dataset):
        """Evaluate a model on question answering tasks.
        
        Args:
            model: HuggingFace model
            tokenizer: HuggingFace tokenizer
            dataset: HuggingFace dataset
            
        Returns:
            dict: Evaluation results
        """
        # Set up QA pipeline
        qa_pipeline = pipeline(
            "question-answering",
            model=model,
            tokenizer=tokenizer,
            device="cpu"
        )
        
        # Sample a subset for evaluation
        if len(dataset) > 100:
            dataset = dataset.select(range(100))
        
        # Track metrics
        exact_matches = 0
        f1_scores = []
        total = 0
        
        # Process each example
        for i, example in enumerate(dataset):
            # Update progress based on completion percentage
            with self.progress_lock:
                self.progress = 40 + int((i / len(dataset)) * 50)
            
            question = example.get("question", "")
            context = example.get("context", "")
            answer = example.get("answer", "")
            
            if not question or not answer:
                continue
            
            # Get model prediction
            if context:
                result = qa_pipeline(question=question, context=context)
            else:
                # If no context provided, use the question as context
                result = qa_pipeline(question=question, context=question)
            
            predicted_answer = result["answer"]
            
            # Calculate exact match
            if predicted_answer.strip() == answer.strip():
                exact_matches += 1
            
            # Calculate F1 score
            f1 = self._calculate_f1(answer, predicted_answer)
            f1_scores.append(f1)
            
            total += 1
        
        # Calculate metrics
        exact_match_accuracy = exact_matches / total if total > 0 else 0
        avg_f1 = sum(f1_scores) / len(f1_scores) if f1_scores else 0
        
        return {
            "exact_match": exact_match_accuracy,
            "f1": avg_f1,
            "samples_evaluated": total
        }
    
    def _evaluate_classification(self, model, tokenizer, dataset):
        """Evaluate a model on classification tasks.
        
        Args:
            model: HuggingFace model
            tokenizer: HuggingFace tokenizer
            dataset: HuggingFace dataset
            
        Returns:
            dict: Evaluation results
        """
        # Set up classification pipeline
        classifier = pipeline(
            "text-classification",
            model=model,
            tokenizer=tokenizer,
            device="cpu"
        )
        
        # Sample a subset for evaluation
        if len(dataset) > 100:
            dataset = dataset.select(range(100))
        
        # Track metrics
        correct = 0
        total = 0
        
        # Process each example
        for i, example in enumerate(dataset):
            # Update progress based on completion percentage
            with self.progress_lock:
                self.progress = 40 + int((i / len(dataset)) * 50)
            
            text = example.get("text", example.get("sentence", ""))
            label = str(example.get("label", example.get("class", "")))
            
            if not text or not label:
                continue
            
            # Get model prediction
            result = classifier(text)
            predicted_label = result[0]["label"]
            
            # Check if correct
            if str(predicted_label) == label:
                correct += 1
            
            total += 1
        
        # Calculate metrics
        accuracy = correct / total if total > 0 else 0
        
        return {
            "accuracy": accuracy,
            "samples_evaluated": total
        }
    
    def _evaluate_code_generation(self, model, tokenizer, dataset):
        """Evaluate a model on code generation tasks.
        
        Args:
            model: HuggingFace model
            tokenizer: HuggingFace tokenizer
            dataset: HuggingFace dataset
            
        Returns:
            dict: Evaluation results
        """
        # Set up generation pipeline
        generator = pipeline(
            "text-generation",
            model=model,
            tokenizer=tokenizer,
            device="cpu"
        )
        
        # Sample a subset for evaluation
        if len(dataset) > 50:  # Smaller sample for code tasks
            dataset = dataset.select(range(50))
        
        # Track metrics
        exact_matches = 0
        functional_matches = 0
        total = 0
        
        # Process each example
        for i, example in enumerate(dataset):
            # Update progress based on completion percentage
            with self.progress_lock:
                self.progress = 40 + int((i / len(dataset)) * 50)
            
            prompt = example.get("prompt", example.get("input", ""))
            solution = example.get("solution", example.get("output", ""))
            
            if not prompt or not solution:
                continue
            
            # Generate code
            generated = generator(
                prompt,
                max_length=200,
                num_return_sequences=1
            )
            
            generated_code = generated[0]["generated_text"]
            
            # Extract code from generated text (remove prompt)
            if prompt in generated_code:
                generated_code = generated_code[len(prompt):].strip()
            
            # Check exact match
            if generated_code.strip() == solution.strip():
                exact_matches += 1
                functional_matches += 1
            else:
                # We would ideally check functional correctness here
                # but that requires executing code which is complex and potentially unsafe
                # For now, we'll use a simple heuristic
                if len(generated_code) > 0 and any(keyword in generated_code for keyword in ["def ", "function", "return", "class"]):
                    functional_matches += 0.5  # Partial credit
            
            total += 1
        
        # Calculate metrics
        exact_match_rate = exact_matches / total if total > 0 else 0
        functional_correctness = functional_matches / total if total > 0 else 0
        
        return {
            "exact_match": exact_match_rate,
            "functional_correctness": functional_correctness,
            "samples_evaluated": total
        }
    
    def _evaluate_general(self, model, tokenizer, dataset):
        """General evaluation for any dataset type.
        
        Args:
            model: HuggingFace model
            tokenizer: HuggingFace tokenizer
            dataset: HuggingFace dataset
            
        Returns:
            dict: Evaluation results
        """
        # Set up generation pipeline
        generator = pipeline(
            "text-generation",
            model=model,
            tokenizer=tokenizer,
            device="cpu"
        )
        
        # Sample a subset for evaluation
        if len(dataset) > 50:
            dataset = dataset.select(range(50))
        
        # Find input and output fields
        features = dataset.features
        input_field = None
        output_field = None
        
        for field in features:
            if field.lower() in ["input", "prompt", "question", "text"]:
                input_field = field
            elif field.lower() in ["output", "target", "answer", "response"]:
                output_field = field
        
        if not input_field:
            # Just use the first string field as input
            for field in features:
                if isinstance(features[field], (str, list)):
                    input_field = field
                    break
        
        # Track metrics
        total = 0
        generated_texts = []
        
        # Process each example
        for i, example in enumerate(dataset):
            # Update progress based on completion percentage
            with self.progress_lock:
                self.progress = 40 + int((i / len(dataset)) * 50)
            
            if input_field and input_field in example:
                input_text = str(example[input_field])
                
                # Generate text
                generated = generator(
                    input_text,
                    max_length=100,
                    num_return_sequences=1
                )
                
                generated_text = generated[0]["generated_text"]
                generated_texts.append({
                    "input": input_text,
                    "output": generated_text,
                    "expected": str(example[output_field]) if output_field and output_field in example else "N/A"
                })
                
                total += 1
        
        return {
            "samples_evaluated": total,
            "generated_samples": generated_texts[:5]  # Include a few samples
        }
    
    def _calculate_f1(self, answer, prediction):
        """Calculate F1 score between answer and prediction.
        
        Args:
            answer: Ground truth answer
            prediction: Model prediction
            
        Returns:
            float: F1 score
        """
        # Tokenize
        answer_tokens = answer.lower().split()
        prediction_tokens = prediction.lower().split()
        
        # Calculate precision and recall
        common_tokens = set(answer_tokens) & set(prediction_tokens)
        
        if not common_tokens:
            return 0.0
        
        precision = len(common_tokens) / len(prediction_tokens)
        recall = len(common_tokens) / len(answer_tokens)
        
        # Calculate F1
        if precision + recall == 0:
            return 0.0
        
        f1 = 2 * precision * recall / (precision + recall)
        return f1
    
    def _calculate_overall_score(self, results):
        """Calculate an overall score from evaluation results.
        
        Args:
            results: Evaluation results dictionary
            
        Returns:
            float: Overall score between 0 and 100
        """
        # If there was an error, return a low score
        if "error" in results:
            return 0.0
            
        score = 0.0
        
        # Check for common metrics and weight them
        if "accuracy" in results:
            score += results["accuracy"] * 100
        
        if "exact_match" in results:
            score += results["exact_match"] * 100
        
        if "f1" in results:
            score += results["f1"] * 100
        
        if "functional_correctness" in results:
            score += results["functional_correctness"] * 100
        
        # If multiple metrics were found, average them
        num_metrics = sum(1 for metric in ["accuracy", "exact_match", "f1", "functional_correctness"] if metric in results)
        
        if num_metrics > 0:
            score /= num_metrics
        else:
            # Default score if no metrics available
            score = 50.0
        
        return score
    
    def submit_evaluation(self, model_id, benchmark_id, user_id, priority=0):
        """Submit a model for evaluation on a benchmark.
        
        Args:
            model_id: Model ID in the database
            benchmark_id: Benchmark ID in the database
            user_id: User ID submitting the evaluation
            priority: Queue priority (higher = higher priority)
            
        Returns:
            tuple: (evaluation_id, message)
        """
        # Check if user can submit today
        if not self.auth_manager.can_submit_benchmark(user_id):
            return None, "Daily submission limit reached. Try again tomorrow."
        
        try:
            # Get model HuggingFace ID to check size
            model_info = self.db_manager.get_model(model_id)
            if not model_info:
                return None, "Model not found in database."
            
            # Check if model will fit in memory
            will_fit, message = self.check_model_size(model_info['hf_model_id'])
            
            if not will_fit:
                return None, message
                
            # Add evaluation to database and queue
            evaluation_id = self.db_manager.add_evaluation(
                model_id=model_id,
                benchmark_id=benchmark_id,
                priority=priority
            )
            
            # Update user's last submission date
            self.auth_manager.update_submission_date(user_id)
            
            # Make sure worker is running
            self.start_worker()
            
            return evaluation_id, f"Evaluation submitted successfully. {message}"
        except Exception as e:
            print(f"Submit evaluation error: {e}")
            return None, f"Failed to submit evaluation: {str(e)}"
    
    def get_queue_status(self):
        """Get the current status of the evaluation queue.
        
        Returns:
            dict: Queue status information
        """
        try:
            # Get evaluations from database
            pending_evals = self.db_manager.get_evaluation_results(status="pending")
            running_evals = self.db_manager.get_evaluation_results(status="running")
            completed_evals = self.db_manager.get_evaluation_results(status="completed")
            failed_evals = self.db_manager.get_evaluation_results(status="failed")
            
            # Get current evaluation progress
            current_eval, progress = self.get_current_progress()
            
            return {
                "pending": len(pending_evals),
                "running": len(running_evals),
                "completed": len(completed_evals),
                "failed": len(failed_evals),
                "is_processing": self.is_processing,
                "current_evaluation": current_eval,
                "progress": progress,
                "memory_limit_gb": self.memory_limit_gb
            }
        except Exception as e:
            print(f"Queue status error: {e}")
            return {
                "pending": 0,
                "running": 0,
                "completed": 0,
                "failed": 0,
                "is_processing": self.is_processing,
                "current_evaluation": None,
                "progress": 0,
                "memory_limit_gb": self.memory_limit_gb,
                "error": str(e)
            }

# Model submission UI components
def create_model_submission_ui(evaluation_queue, auth_manager, db_manager):
    """Create the model submission UI components.
    
    Args:
        evaluation_queue: Evaluation queue instance
        auth_manager: Authentication manager instance
        db_manager: Database manager instance
        
    Returns:
        gr.Blocks: Gradio Blocks component with model submission UI
    """
    with gr.Blocks() as submission_ui:
        # Store user authentication state
        user_state = gr.State(None)
        
        # Check authentication on load
        def check_auth_on_load(request: gr.Request):
            if request:
                # Special handling for HF Spaces OAuth
                if 'SPACE_ID' in os.environ:
                    username = request.headers.get("HF-User")
                    if username:
                        user = db_manager.get_user_by_username(username)
                        if user:
                            print(f"User authenticated via HF Spaces OAuth: {username}")
                            return user
                else:
                    # Standard token-based auth
                    user = auth_manager.check_login(request)
                    if user:
                        return user
            return None
        
        with gr.Tab("Submit Model"):
            gr.Markdown(f"""
            ### Model Size Restrictions
            
            Models must fit within {evaluation_queue.memory_limit_gb}GB of RAM for evaluation.
            Large models will be rejected to ensure all evaluations can complete successfully.
            """, elem_classes=["info-text"])
            
            with gr.Row():
                with gr.Column(scale=2):
                    model_id_input = gr.Textbox(
                        placeholder="HuggingFace model ID (e.g., 'gpt2', 'facebook/opt-350m')",
                        label="Model ID"
                    )
                    
                    check_size_button = gr.Button("Check Model Size")
                    size_check_result = gr.Markdown("")
                    model_name_input = gr.Textbox(
                        placeholder="Display name for your model",
                        label="Model Name"
                    )
                    
                    model_description_input = gr.Textbox(
                        placeholder="Brief description of your model",
                        label="Description",
                        lines=3
                    )
                    
                    model_parameters_input = gr.Number(
                        label="Number of Parameters (billions)",
                        precision=2
                    )
                
                with gr.Column(scale=1):
                    model_tag_input = gr.Dropdown(
                        choices=evaluation_queue.model_tags,
                        label="Model Tag",
                        info="Select one category that best describes your model"
                    )
                    
                    # Fixed benchmark dropdown to properly show names
                    benchmark_dropdown = gr.Dropdown(
                        label="Benchmark",
                        info="Select a benchmark to evaluate your model on",
                        choices=[("none", "Loading benchmarks...")],
                        value=None
                    )
                    
                    refresh_benchmarks_button = gr.Button("Refresh Benchmarks")
            
            submit_model_button = gr.Button("Submit for Evaluation")
            submission_status = gr.Markdown("")
            auth_message = gr.Markdown("")
        
        with gr.Tab("Evaluation Queue"):
            refresh_queue_button = gr.Button("Refresh Queue")
            
            with gr.Row():
                with gr.Column(scale=1):
                    queue_stats = gr.JSON(
                        label="Queue Statistics"
                    )
                
                with gr.Column(scale=2):
                    queue_status = gr.Dataframe(
                        headers=["ID", "Model", "Benchmark", "Status", "Submitted"],
                        label="Recent Evaluations"
                    )
            
            with gr.Row(visible=True) as progress_container:
                with gr.Column():
                    current_eval_info = gr.Markdown("No evaluation currently running")
                    # Use a simple text display for progress instead of Progress component
                    progress_display = gr.Markdown("Progress: 0%")
        
        # Event handlers
        def check_model_size_handler(model_id):
            if not model_id:
                return "Please enter a HuggingFace model ID."
            
            try:
                will_fit, message = evaluation_queue.check_model_size(model_id)
                
                if will_fit:
                    return f"✅ {message}"
                else:
                    return f"❌ {message}"
            except Exception as e:
                print(f"Model size check error: {e}")
                import traceback
                traceback.print_exc()
                return f"Error checking model size: {str(e)}"
            
        def refresh_benchmarks_handler():
            benchmarks = db_manager.get_benchmarks()
            
            # Format for dropdown - properly formatted to display names
            choices = []
            for b in benchmarks:
                # Add as tuple of (id, name) to ensure proper display
                choices.append((str(b["id"]), b["name"]))
            
            if not choices:
                choices = [("none", "No benchmarks available - add some first")]
            
            return gr.update(choices=choices)
        
        def submit_model_handler(model_id, model_name, model_description, model_parameters, model_tag, benchmark_id, user):
            # Check if user is logged in
            if not user:
                return "Please log in to submit a model."
            
            if not model_id or not model_name or not model_tag or not benchmark_id:
                return "Please fill in all required fields."
            
            if benchmark_id == "none":
                return "Please select a valid benchmark."
            
            try:
                # Check if model will fit in RAM
                will_fit, size_message = evaluation_queue.check_model_size(model_id)
                
                if not will_fit:
                    return f"❌ {size_message}"
                
                # Add model to database
                model_db_id = db_manager.add_model(
                    name=model_name,
                    hf_model_id=model_id,
                    user_id=user["id"],
                    tag=model_tag,
                    parameters=str(model_parameters) if model_parameters else None,
                    description=model_description
                )
                
                if not model_db_id:
                    return "Failed to add model to database."
                
                # Submit for evaluation
                eval_id, message = evaluation_queue.submit_evaluation(
                    model_id=model_db_id,
                    benchmark_id=benchmark_id,
                    user_id=user["id"]
                )
                
                if eval_id:
                    return f"✅ Model submitted successfully. {size_message}\nEvaluation ID: {eval_id}"
                else:
                    return message
            except Exception as e:
                print(f"Error submitting model: {str(e)}")
                import traceback
                traceback.print_exc()
                return f"Error submitting model: {str(e)}"
        
        def refresh_queue_handler():
            # Get queue statistics
            stats = evaluation_queue.get_queue_status()
            
            # Get recent evaluations (all statuses, limited to 20)
            evals = db_manager.get_evaluation_results(limit=20)
            
            # Format for dataframe
            eval_data = []
            for eval in evals:
                eval_data.append([
                    eval["id"],
                    eval["model_name"],
                    eval["benchmark_name"],
                    eval["status"],
                    eval["submitted_at"]
                ])
            
            # Also update progress display
            current_eval, progress = evaluation_queue.get_current_progress()
            if current_eval:
                model_info = db_manager.get_model(current_eval['model_id'])
                benchmark_info = db_manager.get_benchmark(current_eval['benchmark_id'])
                
                if model_info and benchmark_info:
                    eval_info = f"**Currently Evaluating:** {model_info['name']} on {benchmark_info['name']}"
                    progress_text = f"Progress: {progress}%"
                    return stats, eval_data, eval_info, progress_text
            
            return stats, eval_data, "No evaluation currently running", "Progress: 0%"
        
        # Update authentication status
        def update_auth_message(user):
            if user:
                return f"Logged in as {user['username']}"
            else:
                return "Please log in to submit a model."
        
        # Connect event handlers
        check_size_button.click(
            fn=check_model_size_handler,
            inputs=[model_id_input],
            outputs=[size_check_result]
        )
        
        refresh_benchmarks_button.click(
            fn=refresh_benchmarks_handler,
            inputs=[],
            outputs=[benchmark_dropdown]
        )
        
        submit_model_button.click(
            fn=submit_model_handler,
            inputs=[
                model_id_input,
                model_name_input,
                model_description_input,
                model_parameters_input,
                model_tag_input,
                benchmark_dropdown,
                user_state
            ],
            outputs=[submission_status]
        )
        
        refresh_queue_button.click(
            fn=refresh_queue_handler,
            inputs=[],
            outputs=[queue_stats, queue_status, current_eval_info, progress_display]
        )
        
        # Initialize on load
        submission_ui.load(
            fn=check_auth_on_load,
            inputs=[],
            outputs=[user_state]
        )
        
        submission_ui.load(
            fn=lambda user: update_auth_message(user),
            inputs=[user_state],
            outputs=[auth_message]
        )
        
        submission_ui.load(
            fn=refresh_benchmarks_handler,
            inputs=[],
            outputs=[benchmark_dropdown]
        )
        
        submission_ui.load(
            fn=refresh_queue_handler,
            inputs=[],
            outputs=[queue_stats, queue_status, current_eval_info, progress_display]
        )
    
    return submission_ui