File size: 34,285 Bytes
d1078a3 |
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 |
"""
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
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.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()
self.db_path = db_manager.db_path # Store the path to create new connections in worker thread
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 _process_queue(self):
"""Process the evaluation queue in a separate thread."""
# Create a new database connection for this thread
thread_db = sqlite3.connect(self.db_path)
thread_db.row_factory = sqlite3.Row
while self.is_processing:
try:
# Get the next evaluation from the database using thread-local connection
cursor = thread_db.cursor()
cursor.execute("""
SELECT e.id as evaluation_id, e.model_id, e.benchmark_id, m.hf_model_id, b.dataset_id
FROM queue q
JOIN evaluations e ON q.evaluation_id = e.id
JOIN models m ON e.model_id = m.id
JOIN benchmarks b ON e.benchmark_id = b.id
WHERE e.status = 'pending'
ORDER BY q.priority DESC, q.added_at ASC
LIMIT 1
""")
row = cursor.fetchone()
if row:
next_eval = dict(row)
# Update status to running
cursor.execute("""
UPDATE evaluations
SET status = 'running', started_at = datetime('now')
WHERE id = ?
""", (next_eval['evaluation_id'],))
thread_db.commit()
# Set current evaluation and reset progress
with self.progress_lock:
self.current_evaluation = next_eval
self.progress = 0
try:
# Run the evaluation
results = self._run_evaluation(
next_eval['hf_model_id'],
next_eval['dataset_id']
)
# Calculate overall score
score = self._calculate_overall_score(results)
# Update status to completed with results
cursor.execute("""
UPDATE evaluations
SET status = 'completed',
completed_at = datetime('now'),
results = ?,
score = ?
WHERE id = ?
""", (json.dumps(results), score, next_eval['evaluation_id']))
thread_db.commit()
except Exception as e:
print(f"Evaluation error: {e}")
# Update status to failed
cursor.execute("""
UPDATE evaluations
SET status = 'failed', completed_at = datetime('now')
WHERE id = ?
""", (next_eval['evaluation_id'],))
thread_db.commit()
# 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)
# Close the thread-local database connection
thread_db.close()
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
if config:
dataset = load_dataset(dataset_id, config, split="test")
else:
dataset = load_dataset(dataset_id, split="test")
# Update progress
with self.progress_lock:
self.progress = 20 # Loading model
# Load the model (CPU only)
device = "cpu"
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map=device,
torch_dtype=torch.float32, # Use float32 for CPU
low_cpu_mem_usage=True
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# 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
# 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)
# Update progress
with self.progress_lock:
self.progress = 95 # Cleaning up
# Clean up to free memory
del model
del tokenizer
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
"""
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:
int: Evaluation ID if successful, None otherwise
"""
# 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:
# 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, "Evaluation submitted successfully."
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
}
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,
"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:
with gr.Tab("Submit Model"):
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"
)
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"
)
benchmark_dropdown = gr.Dropdown(
label="Benchmark",
info="Select a benchmark to evaluate your model on"
)
refresh_benchmarks_button = gr.Button("Refresh Benchmarks")
submit_model_button = gr.Button("Submit for Evaluation")
submission_status = 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%")
# Function to update progress display
def 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 eval_info, progress_text
return "No evaluation currently running", "Progress: 0%"
# Event handlers
def refresh_benchmarks_handler():
benchmarks = db_manager.get_benchmarks()
# Format for dropdown
choices = [(b["id"], b["name"]) for b in benchmarks]
return gr.update(choices=choices)
def submit_model_handler(model_id, model_name, model_description, model_parameters, model_tag, benchmark_id, request: gr.Request):
# Check if user is logged in
user = auth_manager.check_login(request)
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."
try:
# 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. Evaluation ID: {eval_id}"
else:
return message
except Exception as e:
return f"Error submitting model: {str(e)}"
def refresh_queue_handler():
# Get queue statistics
stats = evaluation_queue.get_queue_status()
# Get recent evaluations
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%"
# Connect event handlers
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
],
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=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]
)
# Manual refresh button with instructions
gr.Markdown("""
**Note:** Click the 'Refresh Queue' button periodically to update the progress display.
""")
return submission_ui
|