Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import json
|
|
| 8 |
import pandas as pd
|
| 9 |
import traceback
|
| 10 |
import spaces
|
|
|
|
| 11 |
|
| 12 |
# --- Environment and Caching ---
|
| 13 |
|
|
@@ -46,10 +47,10 @@ def get_all_benchmark_options():
|
|
| 46 |
try:
|
| 47 |
# Fetching dataset configurations requires authentication if the dataset is private
|
| 48 |
subjects = get_dataset_config_names(dataset_id, token=HF_TOKEN)
|
| 49 |
-
benchmark_subject_cache[key] = ["ALL"] + subjects
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Warning: Could not load configs for {key} ({dataset_id}). It might be private or unavailable. Error: {e}")
|
| 52 |
-
benchmark_subject_cache[key] = []
|
| 53 |
print("Benchmark configurations cached.")
|
| 54 |
return benchmark_subject_cache
|
| 55 |
|
|
@@ -80,7 +81,8 @@ def load_model(model_id):
|
|
| 80 |
model_id,
|
| 81 |
token=HF_TOKEN,
|
| 82 |
torch_dtype=dtype,
|
| 83 |
-
trust_remote_code=True
|
|
|
|
| 84 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 85 |
|
| 86 |
# Create the pipeline for text generation
|
|
@@ -115,8 +117,17 @@ def extract_predicted_letter(output_text):
|
|
| 115 |
Extracts the predicted letter from the model's output.
|
| 116 |
It looks for a letter (A, B, C, D) immediately following 'Answer:'.
|
| 117 |
"""
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
def evaluate_single_subject(generator, dataset_id, subject, sample_count, progress):
|
| 122 |
"""
|
|
@@ -140,23 +151,28 @@ def evaluate_single_subject(generator, dataset_id, subject, sample_count, progre
|
|
| 140 |
prompt, correct_answer_idx = format_prompt(item)
|
| 141 |
expected_letter = get_choice_letter(correct_answer_idx)
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
# Generate a short response, aiming for a single letter answer.
|
| 144 |
# do_sample=False (greedy decoding) is crucial for reproducibility.
|
| 145 |
-
raw_output = generator(prompt, max_new_tokens=5, do_sample=False)[0]["generated_text"]
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
is_correct = (predicted_letter == expected_letter)
|
| 149 |
|
| 150 |
if is_correct:
|
| 151 |
correct_predictions += 1
|
| 152 |
|
| 153 |
results_details.append({
|
| 154 |
-
"
|
| 155 |
-
"
|
| 156 |
-
"
|
| 157 |
-
"
|
| 158 |
-
"
|
| 159 |
-
"is_correct": is_correct,
|
| 160 |
})
|
| 161 |
|
| 162 |
accuracy = (correct_predictions / num_samples) * 100 if num_samples > 0 else 0
|
|
@@ -168,7 +184,7 @@ def run_evaluation(model_id, benchmark_category, subject_name, sample_count, pro
|
|
| 168 |
"""
|
| 169 |
Main function to orchestrate the entire evaluation process.
|
| 170 |
Handles single subject or 'ALL' subjects evaluation.
|
| 171 |
-
Returns
|
| 172 |
"""
|
| 173 |
try:
|
| 174 |
gr.Info("Starting evaluation...")
|
|
@@ -185,15 +201,19 @@ def run_evaluation(model_id, benchmark_category, subject_name, sample_count, pro
|
|
| 185 |
|
| 186 |
subjects_to_run = []
|
| 187 |
if subject_name == "ALL":
|
| 188 |
-
|
| 189 |
-
if "ALL"
|
| 190 |
-
subjects_to_run.remove("ALL") # Remove 'ALL' from the list of subjects to run
|
| 191 |
else:
|
| 192 |
subjects_to_run = [subject_name]
|
| 193 |
|
| 194 |
if not subjects_to_run:
|
| 195 |
gr.Warning(f"No subjects found for '{benchmark_category}'.")
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
for i, subject in enumerate(subjects_to_run):
|
| 199 |
gr.Info(f"Evaluating {benchmark_category} - {subject} ({i+1}/{len(subjects_to_run)})...")
|
|
@@ -201,7 +221,7 @@ def run_evaluation(model_id, benchmark_category, subject_name, sample_count, pro
|
|
| 201 |
accuracy, subject_details = evaluate_single_subject(generator, dataset_id, subject, sample_count, progress)
|
| 202 |
|
| 203 |
all_results_details.extend(subject_details)
|
| 204 |
-
num_correct = sum(
|
| 205 |
num_evaluated = len(subject_details)
|
| 206 |
|
| 207 |
total_correct += num_correct
|
|
@@ -209,58 +229,59 @@ def run_evaluation(model_id, benchmark_category, subject_name, sample_count, pro
|
|
| 209 |
summary_lines.append(f"- **{subject}**: {accuracy:.2f}% ({num_correct}/{num_evaluated})")
|
| 210 |
|
| 211 |
except Exception as e:
|
|
|
|
| 212 |
gr.Error(f"Skipping {subject} due to an error: {e}")
|
| 213 |
-
summary_lines.append(f"- **{subject}**: Evaluation failed.")
|
| 214 |
continue
|
| 215 |
|
| 216 |
overall_accuracy = (total_correct / total_samples) * 100 if total_samples > 0 else 0
|
| 217 |
|
| 218 |
# --- Prepare Outputs ---
|
| 219 |
if subject_name == "ALL":
|
| 220 |
-
result_summary = f"### Overall Average Accuracy
|
| 221 |
-
result_summary += "across {:,} total samples.\n\n---\n\n**Breakdown by Subject:**\n"
|
| 222 |
result_summary += "\n".join(summary_lines)
|
| 223 |
else:
|
| 224 |
result_summary = f"### Accuracy for {benchmark_category} - {subject_name}: {overall_accuracy:.2f}%\n"
|
| 225 |
-
result_summary += "({:,}/{:,} correct)"
|
| 226 |
|
| 227 |
-
# Create a detailed DataFrame for inspection
|
| 228 |
-
df_details = pd.DataFrame(all_results_details)
|
| 229 |
-
|
| 230 |
# Save results for leaderboard
|
| 231 |
record = {
|
| 232 |
"model_id": model_id,
|
| 233 |
"benchmark": benchmark_category,
|
| 234 |
"accuracy": overall_accuracy,
|
| 235 |
-
"subject": subject_name,
|
| 236 |
"sample_count": total_samples,
|
| 237 |
-
"timestamp":
|
| 238 |
}
|
| 239 |
with open(EVAL_FILE, "a") as f:
|
| 240 |
f.write(json.dumps(record) + "\n")
|
| 241 |
|
| 242 |
gr.Info("Evaluation completed successfully!")
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
gr.update(value=
|
| 249 |
-
gr.update(
|
| 250 |
-
|
|
|
|
|
|
|
| 251 |
|
| 252 |
except Exception as e:
|
| 253 |
-
error_message = f"An unexpected error occurred: {e}"
|
| 254 |
error_details = traceback.format_exc()
|
| 255 |
gr.Error(error_message)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
gr.update(
|
| 260 |
-
gr.update(value=error_message
|
| 261 |
-
gr.update(value=error_details
|
| 262 |
-
gr.update(
|
| 263 |
-
|
|
|
|
| 264 |
|
| 265 |
# --- UI Helper Functions ---
|
| 266 |
|
|
@@ -270,171 +291,141 @@ def update_subject_dropdown(benchmark_category):
|
|
| 270 |
default_value = "ALL" if "ALL" in choices else (choices[0] if choices else None)
|
| 271 |
return gr.update(choices=choices, value=default_value)
|
| 272 |
|
| 273 |
-
def load_leaderboard(benchmark_filter):
|
| 274 |
"""
|
| 275 |
Loads and processes evaluation data to display on the leaderboard.
|
| 276 |
It now correctly averages scores for models that were evaluated on 'ALL' subjects.
|
| 277 |
"""
|
|
|
|
| 278 |
try:
|
| 279 |
if not os.path.exists(EVAL_FILE):
|
| 280 |
-
return pd.DataFrame(columns=["Model ID", "Avg. Accuracy (%)", "Total Samples"])
|
| 281 |
|
| 282 |
df = pd.read_json(EVAL_FILE, lines=True)
|
| 283 |
if df.empty:
|
| 284 |
-
return pd.DataFrame(columns=["Model ID", "Avg. Accuracy (%)", "Total Samples"])
|
| 285 |
|
| 286 |
# Coerce accuracy to numeric and filter valid entries
|
| 287 |
df['accuracy'] = pd.to_numeric(df['accuracy'], errors='coerce')
|
| 288 |
df.dropna(subset=['accuracy'], inplace=True)
|
| 289 |
|
| 290 |
# Filter by the selected benchmark (e.g., MMLU or MMLU-Pro)
|
| 291 |
-
df_filtered = df[df['benchmark'] == benchmark_filter].copy()
|
| 292 |
|
| 293 |
if df_filtered.empty:
|
| 294 |
-
return pd.DataFrame(columns=["Model ID", "Avg. Accuracy (%)", "Total Samples"])
|
| 295 |
|
| 296 |
-
# We are interested in the 'ALL' subject evaluations for the main leaderboard
|
| 297 |
-
df_all = df_filtered[df_filtered['subject'] == 'ALL'].copy()
|
| 298 |
-
|
| 299 |
-
if df_all.empty:
|
| 300 |
-
return pd.DataFrame(columns=["Model ID", "Avg. Accuracy (%)", "Total Samples"]).to_dict('records')
|
| 301 |
-
|
| 302 |
# Find the latest evaluation for each model
|
| 303 |
-
|
| 304 |
-
latest_evals =
|
| 305 |
|
| 306 |
-
leaderboard_df = latest_evals
|
| 307 |
-
leaderboard_df.columns = ["Model ID", "Avg. Accuracy (%)", "Total Samples"]
|
| 308 |
|
| 309 |
-
#
|
| 310 |
-
leaderboard_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
|
| 313 |
-
leaderboard_df = leaderboard_df.
|
| 314 |
|
| 315 |
-
|
|
|
|
| 316 |
|
| 317 |
except Exception as e:
|
| 318 |
gr.Error(f"Error loading leaderboard: {e}")
|
| 319 |
traceback.print_exc()
|
| 320 |
-
return pd.DataFrame(columns=["Model ID", "Avg. Accuracy (%)", "Total Samples"])
|
| 321 |
|
| 322 |
|
| 323 |
# --- Gradio Interface Definition ---
|
| 324 |
|
| 325 |
-
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 326 |
/* --- Global & Layout --- */
|
| 327 |
body { font-family: 'Inter', sans-serif; background-color: #f8f9fa; }
|
| 328 |
.gradio-container { max-width: 1280px !important; margin: auto; }
|
| 329 |
-
.gr-
|
| 330 |
|
| 331 |
/* --- Typography --- */
|
| 332 |
-
h1 {
|
| 333 |
-
|
| 334 |
-
font-size: 2.5rem !important;
|
| 335 |
-
font-weight: 700;
|
| 336 |
-
color: #212529;
|
| 337 |
-
margin-bottom: 0.5rem;
|
| 338 |
-
letter-spacing: -1px;
|
| 339 |
-
}
|
| 340 |
-
.subtitle {
|
| 341 |
-
text-align: center; color: #6c757d; font-size: 1.1rem; margin-bottom: 2.5rem;
|
| 342 |
-
}
|
| 343 |
|
| 344 |
/* --- Buttons & Inputs --- */
|
| 345 |
-
.gr-button {
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
padding: 10px 20px !important;
|
| 349 |
-
transition: all 0.2s ease;
|
| 350 |
-
}
|
| 351 |
-
.gr-button-primary { box-shadow: 0 4px 10px rgba(0, 123, 255, 0.2); }
|
| 352 |
-
.gr-button-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 15px rgba(0, 123, 255, 0.3); }
|
| 353 |
|
| 354 |
/* --- Custom Radio Buttons (Segmented Control) --- */
|
| 355 |
-
#leaderboard-toggle
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
}
|
| 362 |
-
#leaderboard-toggle div.gr-form, #eval-benchmark-selection div.gr-form {
|
| 363 |
-
display: flex;
|
| 364 |
-
gap: 5px;
|
| 365 |
-
}
|
| 366 |
-
#leaderboard-toggle input[type='radio'], #eval-benchmark-selection input[type='radio'] { display: none; }
|
| 367 |
-
#leaderboard-toggle label, #eval-benchmark-selection label {
|
| 368 |
-
padding: 8px 16px;
|
| 369 |
-
border-radius: 8px;
|
| 370 |
-
cursor: pointer;
|
| 371 |
-
transition: background-color 0.3s, color 0.3s, box-shadow 0.3s;
|
| 372 |
-
font-weight: 500;
|
| 373 |
-
color: #495057;
|
| 374 |
-
background: transparent;
|
| 375 |
-
border: none;
|
| 376 |
-
box-shadow: none;
|
| 377 |
-
}
|
| 378 |
-
#leaderboard-toggle input[type='radio']:checked + label, #eval-benchmark-selection input[type='radio']:checked + label {
|
| 379 |
-
background-color: white;
|
| 380 |
-
color: #007bff;
|
| 381 |
-
font-weight: 600;
|
| 382 |
-
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 383 |
-
}
|
| 384 |
|
| 385 |
/* --- Dataframe / Table Styling --- */
|
| 386 |
-
.leaderboard-table .gr-dataframe table {
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
}
|
| 390 |
-
.leaderboard-table .gr-dataframe
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
font-weight: 600 !important;
|
| 394 |
-
text-align: left;
|
| 395 |
-
padding: 12px 15px;
|
| 396 |
-
border-bottom: 2px solid #dee2e6;
|
| 397 |
-
}
|
| 398 |
-
.leaderboard-table .gr-dataframe tbody tr:nth-of-type(even) {
|
| 399 |
-
background-color: #f8f9fa;
|
| 400 |
-
}
|
| 401 |
-
.leaderboard-table .gr-dataframe tbody tr:hover {
|
| 402 |
-
background-color: #e9ecef;
|
| 403 |
-
}
|
| 404 |
-
.leaderboard-table .gr-dataframe tbody td {
|
| 405 |
-
padding: 12px 15px;
|
| 406 |
-
border-bottom: 1px solid #dee2e6;
|
| 407 |
-
}
|
| 408 |
|
| 409 |
/* --- Error & Result Panes --- */
|
| 410 |
-
#error-display-box { background-color: #fff3f3; border-color: #ffc9c9; }
|
| 411 |
-
#
|
| 412 |
-
#result-summary-box { background-color: #f3f9ff; border-color: #cde4ff; }
|
| 413 |
""") as demo:
|
| 414 |
-
gr.Markdown("<h1
|
| 415 |
-
gr.Markdown("<p class='subtitle'>Benchmark leading models on MMLU and MMLU-Pro. Your results contribute to a live leaderboard.</p>")
|
| 416 |
|
| 417 |
-
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
# --- Evaluation Tab ---
|
| 419 |
-
with gr.TabItem("π Run Evaluation"):
|
| 420 |
-
with gr.Row():
|
| 421 |
with gr.Column(scale=2):
|
| 422 |
-
with gr.
|
| 423 |
gr.Markdown("### 1. Configure Evaluation")
|
| 424 |
model_id_input = gr.Textbox(
|
| 425 |
label="Hugging Face Model ID",
|
| 426 |
placeholder="e.g., meta-llama/Meta-Llama-3-8B-Instruct",
|
| 427 |
interactive=True
|
| 428 |
)
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
elem_id="eval-benchmark-selection",
|
| 436 |
-
container=False
|
| 437 |
-
)
|
| 438 |
with gr.Row():
|
| 439 |
benchmark_subject_dropdown = gr.Dropdown(
|
| 440 |
label="Subject",
|
|
@@ -453,44 +444,25 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
| 453 |
gr.Markdown("### 2. View Results")
|
| 454 |
|
| 455 |
# Panel for displaying the summary of results
|
| 456 |
-
with gr.
|
| 457 |
-
result_summary_output = gr.Markdown()
|
| 458 |
|
| 459 |
# Panel for displaying errors
|
| 460 |
-
with gr.
|
| 461 |
-
error_output = gr.Textbox(label="Error Message", interactive=False)
|
| 462 |
error_details_output = gr.Textbox(label="Error Details (Traceback)", interactive=False, lines=8)
|
| 463 |
|
| 464 |
# Panel for detailed, row-by-row results
|
| 465 |
-
with gr.
|
| 466 |
gr.Markdown("#### Detailed Evaluation Log")
|
| 467 |
-
detailed_results_df = gr.
|
| 468 |
-
headers=["Question", "Correct", "Expected", "Predicted", "
|
| 469 |
-
datatype=["str", "
|
| 470 |
interactive=False,
|
| 471 |
row_count=10,
|
| 472 |
-
col_count=5
|
|
|
|
| 473 |
)
|
| 474 |
-
|
| 475 |
-
# --- Leaderboard Tab ---
|
| 476 |
-
with gr.TabItem("π Leaderboard"):
|
| 477 |
-
with gr.Column():
|
| 478 |
-
gr.Markdown("<div style='display: flex; justify-content: center; width: 100%; margin-bottom: 20px;'></div>", elem_id="leaderboard-toggle-container")
|
| 479 |
-
leaderboard_type_toggle = gr.Radio(
|
| 480 |
-
["MMLU", "MMLU-Pro"],
|
| 481 |
-
label="Select Benchmark",
|
| 482 |
-
value="MMLU",
|
| 483 |
-
interactive=True,
|
| 484 |
-
elem_id="leaderboard-toggle",
|
| 485 |
-
container=False
|
| 486 |
-
)
|
| 487 |
-
leaderboard_table_output = gr.Dataframe(
|
| 488 |
-
headers=["Model ID", "Avg. Accuracy (%)", "Total Samples"],
|
| 489 |
-
interactive=False,
|
| 490 |
-
datatype=["str", "str", "number"],
|
| 491 |
-
row_count=15,
|
| 492 |
-
elem_classes="leaderboard-table"
|
| 493 |
-
)
|
| 494 |
|
| 495 |
# --- Event Handlers & Logic ---
|
| 496 |
|
|
@@ -505,12 +477,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
| 505 |
run_button.click(
|
| 506 |
fn=run_evaluation,
|
| 507 |
inputs=[model_id_input, benchmark_selection_radio, benchmark_subject_dropdown, sample_count_slider],
|
| 508 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 509 |
).then(
|
| 510 |
-
|
| 511 |
-
lambda r, e, d, df: (r, e, d, df.to_dict('records')),
|
| 512 |
-
inputs=[result_summary_box, error_box, error_details_output, details_box],
|
| 513 |
-
outputs=[result_summary_output, error_output, error_details_output, detailed_results_df]
|
| 514 |
)
|
| 515 |
|
| 516 |
# Leaderboard loading logic
|
|
@@ -522,14 +494,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
| 522 |
leaderboard_type_toggle.change(
|
| 523 |
fn=load_leaderboard,
|
| 524 |
inputs=[leaderboard_type_toggle],
|
| 525 |
-
outputs=[leaderboard_table_output]
|
|
|
|
| 526 |
)
|
| 527 |
-
|
| 528 |
-
# When the run button is clicked again, refresh the leaderboard
|
| 529 |
-
run_button.click(
|
| 530 |
fn=load_leaderboard,
|
| 531 |
inputs=[leaderboard_type_toggle],
|
| 532 |
-
outputs=[leaderboard_table_output]
|
|
|
|
| 533 |
)
|
| 534 |
|
| 535 |
# Launch the Gradio app
|
|
|
|
| 8 |
import pandas as pd
|
| 9 |
import traceback
|
| 10 |
import spaces
|
| 11 |
+
from datetime import datetime
|
| 12 |
|
| 13 |
# --- Environment and Caching ---
|
| 14 |
|
|
|
|
| 47 |
try:
|
| 48 |
# Fetching dataset configurations requires authentication if the dataset is private
|
| 49 |
subjects = get_dataset_config_names(dataset_id, token=HF_TOKEN)
|
| 50 |
+
benchmark_subject_cache[key] = ["ALL"] + sorted([s for s in subjects if s != 'all']) # Sort subjects
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Warning: Could not load configs for {key} ({dataset_id}). It might be private or unavailable. Error: {e}")
|
| 53 |
+
benchmark_subject_cache[key] = ["ALL"] # Provide a default
|
| 54 |
print("Benchmark configurations cached.")
|
| 55 |
return benchmark_subject_cache
|
| 56 |
|
|
|
|
| 81 |
model_id,
|
| 82 |
token=HF_TOKEN,
|
| 83 |
torch_dtype=dtype,
|
| 84 |
+
trust_remote_code=True,
|
| 85 |
+
low_cpu_mem_usage=True, # Optimization for large models
|
| 86 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 87 |
|
| 88 |
# Create the pipeline for text generation
|
|
|
|
| 117 |
Extracts the predicted letter from the model's output.
|
| 118 |
It looks for a letter (A, B, C, D) immediately following 'Answer:'.
|
| 119 |
"""
|
| 120 |
+
# Look for "Answer: X" and capture X
|
| 121 |
+
match = re.search(r"Answer:\s*([ABCD])", output_text.strip(), re.IGNORECASE)
|
| 122 |
+
if match:
|
| 123 |
+
return match.group(1).upper()
|
| 124 |
+
|
| 125 |
+
# Fallback: if the model just outputs a letter
|
| 126 |
+
match = re.search(r"^\s*([ABCD])\b", output_text.strip())
|
| 127 |
+
if match:
|
| 128 |
+
return match.group(1).upper()
|
| 129 |
+
|
| 130 |
+
return None
|
| 131 |
|
| 132 |
def evaluate_single_subject(generator, dataset_id, subject, sample_count, progress):
|
| 133 |
"""
|
|
|
|
| 151 |
prompt, correct_answer_idx = format_prompt(item)
|
| 152 |
expected_letter = get_choice_letter(correct_answer_idx)
|
| 153 |
|
| 154 |
+
# The generated text is often just after the prompt. We need to slice it.
|
| 155 |
+
full_prompt_text = generator.tokenizer.decode(generator.tokenizer.encode(prompt), skip_special_tokens=True)
|
| 156 |
+
|
| 157 |
# Generate a short response, aiming for a single letter answer.
|
| 158 |
# do_sample=False (greedy decoding) is crucial for reproducibility.
|
| 159 |
+
raw_output = generator(prompt, max_new_tokens=5, do_sample=False, pad_token_id=generator.tokenizer.eos_token_id)[0]["generated_text"]
|
| 160 |
|
| 161 |
+
# Isolate the newly generated part
|
| 162 |
+
generated_text_only = raw_output[len(full_prompt_text):].strip()
|
| 163 |
+
|
| 164 |
+
predicted_letter = extract_predicted_letter(generated_text_only)
|
| 165 |
is_correct = (predicted_letter == expected_letter)
|
| 166 |
|
| 167 |
if is_correct:
|
| 168 |
correct_predictions += 1
|
| 169 |
|
| 170 |
results_details.append({
|
| 171 |
+
"Question": item['question'],
|
| 172 |
+
"Correct": "β
" if is_correct else "β",
|
| 173 |
+
"Expected": expected_letter,
|
| 174 |
+
"Predicted": predicted_letter or "N/A",
|
| 175 |
+
"Model Output": generated_text_only
|
|
|
|
| 176 |
})
|
| 177 |
|
| 178 |
accuracy = (correct_predictions / num_samples) * 100 if num_samples > 0 else 0
|
|
|
|
| 184 |
"""
|
| 185 |
Main function to orchestrate the entire evaluation process.
|
| 186 |
Handles single subject or 'ALL' subjects evaluation.
|
| 187 |
+
Returns a dictionary of Gradio updates.
|
| 188 |
"""
|
| 189 |
try:
|
| 190 |
gr.Info("Starting evaluation...")
|
|
|
|
| 201 |
|
| 202 |
subjects_to_run = []
|
| 203 |
if subject_name == "ALL":
|
| 204 |
+
# Exclude the "ALL" placeholder from the list of subjects to run
|
| 205 |
+
subjects_to_run = [s for s in ALL_BENCHMARK_SUBJECTS.get(benchmark_category, []) if s != "ALL"]
|
|
|
|
| 206 |
else:
|
| 207 |
subjects_to_run = [subject_name]
|
| 208 |
|
| 209 |
if not subjects_to_run:
|
| 210 |
gr.Warning(f"No subjects found for '{benchmark_category}'.")
|
| 211 |
+
# Return an empty but valid structure
|
| 212 |
+
return {
|
| 213 |
+
result_summary_output: gr.update(value="No subjects found to evaluate.", visible=True),
|
| 214 |
+
error_box: gr.update(visible=False),
|
| 215 |
+
details_box: gr.update(visible=False),
|
| 216 |
+
}
|
| 217 |
|
| 218 |
for i, subject in enumerate(subjects_to_run):
|
| 219 |
gr.Info(f"Evaluating {benchmark_category} - {subject} ({i+1}/{len(subjects_to_run)})...")
|
|
|
|
| 221 |
accuracy, subject_details = evaluate_single_subject(generator, dataset_id, subject, sample_count, progress)
|
| 222 |
|
| 223 |
all_results_details.extend(subject_details)
|
| 224 |
+
num_correct = sum(1 for d in subject_details if d['Correct'] == "β
")
|
| 225 |
num_evaluated = len(subject_details)
|
| 226 |
|
| 227 |
total_correct += num_correct
|
|
|
|
| 229 |
summary_lines.append(f"- **{subject}**: {accuracy:.2f}% ({num_correct}/{num_evaluated})")
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
+
error_trace = traceback.format_exc()
|
| 233 |
gr.Error(f"Skipping {subject} due to an error: {e}")
|
| 234 |
+
summary_lines.append(f"- **{subject}**: Evaluation failed. See logs for details:\n```\n{error_trace}\n```")
|
| 235 |
continue
|
| 236 |
|
| 237 |
overall_accuracy = (total_correct / total_samples) * 100 if total_samples > 0 else 0
|
| 238 |
|
| 239 |
# --- Prepare Outputs ---
|
| 240 |
if subject_name == "ALL":
|
| 241 |
+
result_summary = f"### Overall Average Accuracy: {overall_accuracy:.2f}%\n"
|
| 242 |
+
result_summary += f"across {total_samples:,} total samples from {len(subjects_to_run)} subjects.\n\n---\n\n**Breakdown by Subject:**\n"
|
| 243 |
result_summary += "\n".join(summary_lines)
|
| 244 |
else:
|
| 245 |
result_summary = f"### Accuracy for {benchmark_category} - {subject_name}: {overall_accuracy:.2f}%\n"
|
| 246 |
+
result_summary += f"({total_correct:,}/{total_samples:,} correct)"
|
| 247 |
|
|
|
|
|
|
|
|
|
|
| 248 |
# Save results for leaderboard
|
| 249 |
record = {
|
| 250 |
"model_id": model_id,
|
| 251 |
"benchmark": benchmark_category,
|
| 252 |
"accuracy": overall_accuracy,
|
| 253 |
+
"subject": subject_name, # Record if it was an 'ALL' run
|
| 254 |
"sample_count": total_samples,
|
| 255 |
+
"timestamp": datetime.now().isoformat()
|
| 256 |
}
|
| 257 |
with open(EVAL_FILE, "a") as f:
|
| 258 |
f.write(json.dumps(record) + "\n")
|
| 259 |
|
| 260 |
gr.Info("Evaluation completed successfully!")
|
| 261 |
|
| 262 |
+
df_details = pd.DataFrame(all_results_details)
|
| 263 |
+
|
| 264 |
+
# Return a dictionary of component updates
|
| 265 |
+
return {
|
| 266 |
+
result_summary_output: gr.update(value=result_summary, visible=True),
|
| 267 |
+
error_box: gr.update(visible=False),
|
| 268 |
+
details_box: gr.update(visible=True),
|
| 269 |
+
detailed_results_df: gr.update(value=df_details)
|
| 270 |
+
}
|
| 271 |
|
| 272 |
except Exception as e:
|
| 273 |
+
error_message = f"An unexpected error occurred during setup: {e}"
|
| 274 |
error_details = traceback.format_exc()
|
| 275 |
gr.Error(error_message)
|
| 276 |
|
| 277 |
+
return {
|
| 278 |
+
result_summary_output: gr.update(visible=False),
|
| 279 |
+
error_box: gr.update(visible=True),
|
| 280 |
+
error_output: gr.update(value=error_message),
|
| 281 |
+
error_details_output: gr.update(value=error_details),
|
| 282 |
+
details_box: gr.update(visible=False)
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
|
| 286 |
# --- UI Helper Functions ---
|
| 287 |
|
|
|
|
| 291 |
default_value = "ALL" if "ALL" in choices else (choices[0] if choices else None)
|
| 292 |
return gr.update(choices=choices, value=default_value)
|
| 293 |
|
| 294 |
+
def load_leaderboard(benchmark_filter, progress=gr.Progress()):
|
| 295 |
"""
|
| 296 |
Loads and processes evaluation data to display on the leaderboard.
|
| 297 |
It now correctly averages scores for models that were evaluated on 'ALL' subjects.
|
| 298 |
"""
|
| 299 |
+
progress(0, desc="Loading Leaderboard...")
|
| 300 |
try:
|
| 301 |
if not os.path.exists(EVAL_FILE):
|
| 302 |
+
return pd.DataFrame(columns=["Rank", "Model ID", "Avg. Accuracy (%)", "Total Samples", "Date"])
|
| 303 |
|
| 304 |
df = pd.read_json(EVAL_FILE, lines=True)
|
| 305 |
if df.empty:
|
| 306 |
+
return pd.DataFrame(columns=["Rank", "Model ID", "Avg. Accuracy (%)", "Total Samples", "Date"])
|
| 307 |
|
| 308 |
# Coerce accuracy to numeric and filter valid entries
|
| 309 |
df['accuracy'] = pd.to_numeric(df['accuracy'], errors='coerce')
|
| 310 |
df.dropna(subset=['accuracy'], inplace=True)
|
| 311 |
|
| 312 |
# Filter by the selected benchmark (e.g., MMLU or MMLU-Pro)
|
| 313 |
+
df_filtered = df[(df['benchmark'] == benchmark_filter) & (df['subject'] == 'ALL')].copy()
|
| 314 |
|
| 315 |
if df_filtered.empty:
|
| 316 |
+
return pd.DataFrame(columns=["Rank", "Model ID", "Avg. Accuracy (%)", "Total Samples", "Date"])
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
# Find the latest evaluation for each model
|
| 319 |
+
df_filtered['timestamp'] = pd.to_datetime(df_filtered['timestamp'])
|
| 320 |
+
latest_evals = df_filtered.loc[df_filtered.groupby('model_id')['timestamp'].idxmax()].copy()
|
| 321 |
|
| 322 |
+
leaderboard_df = latest_evals.sort_values(by="accuracy", ascending=False).copy()
|
|
|
|
| 323 |
|
| 324 |
+
# Add Rank
|
| 325 |
+
leaderboard_df.insert(0, 'Rank', range(1, len(leaderboard_df) + 1))
|
| 326 |
+
|
| 327 |
+
# Rename and format columns
|
| 328 |
+
leaderboard_df.rename(columns={
|
| 329 |
+
'model_id': 'Model ID',
|
| 330 |
+
'accuracy': 'Avg. Accuracy (%)',
|
| 331 |
+
'sample_count': 'Total Samples',
|
| 332 |
+
'timestamp': 'Date'
|
| 333 |
+
}, inplace=True)
|
| 334 |
|
| 335 |
+
leaderboard_df['Avg. Accuracy (%)'] = leaderboard_df['Avg. Accuracy (%)'].map('{:.2f}'.format)
|
| 336 |
+
leaderboard_df['Date'] = leaderboard_df['Date'].dt.strftime('%Y-%m-%d')
|
| 337 |
|
| 338 |
+
progress(1, desc="Done.")
|
| 339 |
+
return leaderboard_df[['Rank', 'Model ID', 'Avg. Accuracy (%)', 'Total Samples', 'Date']]
|
| 340 |
|
| 341 |
except Exception as e:
|
| 342 |
gr.Error(f"Error loading leaderboard: {e}")
|
| 343 |
traceback.print_exc()
|
| 344 |
+
return pd.DataFrame(columns=["Rank", "Model ID", "Avg. Accuracy (%)", "Total Samples", "Date"])
|
| 345 |
|
| 346 |
|
| 347 |
# --- Gradio Interface Definition ---
|
| 348 |
|
| 349 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), css="""
|
| 350 |
/* --- Global & Layout --- */
|
| 351 |
body { font-family: 'Inter', sans-serif; background-color: #f8f9fa; }
|
| 352 |
.gradio-container { max-width: 1280px !important; margin: auto; }
|
| 353 |
+
.gr-group { border-radius: 12px !important; box-shadow: 0 4px 12px rgba(0,0,0,0.05) !important; border: 1px solid #e9ecef !important; background-color: white; }
|
| 354 |
|
| 355 |
/* --- Typography --- */
|
| 356 |
+
h1 { text-align: center; font-size: 2.5rem !important; font-weight: 800; color: #212529; margin-bottom: 0.5rem; letter-spacing: -1.5px; }
|
| 357 |
+
.subtitle { text-align: center; color: #6c757d; font-size: 1.1rem; margin-bottom: 2.5rem; max-width: 800px; margin-left: auto; margin-right: auto;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
/* --- Buttons & Inputs --- */
|
| 360 |
+
.gr-button { font-weight: 600 !important; transition: all 0.2s ease; }
|
| 361 |
+
.gr-button-primary { box-shadow: 0 4px 10px rgba(59, 130, 246, 0.2); }
|
| 362 |
+
.gr-button-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 15px rgba(59, 130, 246, 0.3); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
/* --- Custom Radio Buttons (Segmented Control) --- */
|
| 365 |
+
#leaderboard-toggle-group { display: flex; justify-content: center; align-items: center; gap: 1rem; margin-bottom: 1.5rem; }
|
| 366 |
+
#leaderboard-toggle { background-color: #e9ecef; padding: 5px; border-radius: 10px; display: inline-flex; }
|
| 367 |
+
#leaderboard-toggle div.gr-form { display: flex; gap: 5px; }
|
| 368 |
+
#leaderboard-toggle input[type='radio'] { display: none; }
|
| 369 |
+
#leaderboard-toggle label { padding: 8px 16px; border-radius: 8px; cursor: pointer; transition: all 0.3s ease; font-weight: 500; color: #495057; background: transparent; border: none; box-shadow: none; }
|
| 370 |
+
#leaderboard-toggle input[type='radio']:checked + label { background-color: white; color: #0d6efd; font-weight: 600; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
/* --- Dataframe / Table Styling --- */
|
| 373 |
+
.leaderboard-table .gr-dataframe table { border-collapse: collapse; width: 100%; }
|
| 374 |
+
.leaderboard-table .gr-dataframe thead th { background-color: #f8f9fa !important; color: #495057 !important; font-weight: 600 !important; text-align: left; padding: 12px 15px; border-bottom: 2px solid #dee2e6; }
|
| 375 |
+
.leaderboard-table .gr-dataframe tbody tr:nth-of-type(even) { background-color: #fdfdff; }
|
| 376 |
+
.leaderboard-table .gr-dataframe tbody tr:hover { background-color: #f0f6ff; }
|
| 377 |
+
.leaderboard-table .gr-dataframe tbody td { padding: 12px 15px; border-bottom: 1px solid #e9ecef; }
|
| 378 |
+
.leaderboard-table .gr-dataframe tbody td:first-child { font-weight: 700; color: #495057; }
|
| 379 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
/* --- Error & Result Panes --- */
|
| 382 |
+
#error-display-box { background-color: #fff3f3 !important; border-color: #ffc9c9 !important; }
|
| 383 |
+
#result-summary-box { background-color: #f3f9ff !important; border-color: #cde4ff !important; }
|
|
|
|
| 384 |
""") as demo:
|
| 385 |
+
gr.Markdown("<h1>π Open LLM Evaluator</h1>")
|
| 386 |
+
gr.Markdown("<p class='subtitle'>Benchmark leading models on MMLU and MMLU-Pro. Your results contribute to a live leaderboard. Select a benchmark and run an evaluation, or view the current standings.</p>")
|
| 387 |
|
| 388 |
+
with gr.Tabs() as tabs:
|
| 389 |
+
# --- Leaderboard Tab ---
|
| 390 |
+
with gr.TabItem("π Leaderboard", id=0):
|
| 391 |
+
with gr.Column():
|
| 392 |
+
with gr.Row(elem_id="leaderboard-toggle-group"):
|
| 393 |
+
leaderboard_type_toggle = gr.Radio(
|
| 394 |
+
["MMLU", "MMLU-Pro"],
|
| 395 |
+
label="Select Benchmark",
|
| 396 |
+
value="MMLU",
|
| 397 |
+
interactive=True,
|
| 398 |
+
elem_id="leaderboard-toggle",
|
| 399 |
+
container=False,
|
| 400 |
+
show_label=False,
|
| 401 |
+
)
|
| 402 |
+
refresh_button = gr.Button("π Refresh", size="sm")
|
| 403 |
+
|
| 404 |
+
leaderboard_table_output = gr.DataFrame(
|
| 405 |
+
headers=["Rank", "Model ID", "Avg. Accuracy (%)", "Total Samples", "Date"],
|
| 406 |
+
interactive=False,
|
| 407 |
+
datatype=["number", "str", "str", "number", "str"],
|
| 408 |
+
row_count=15,
|
| 409 |
+
elem_classes="leaderboard-table"
|
| 410 |
+
)
|
| 411 |
+
|
| 412 |
# --- Evaluation Tab ---
|
| 413 |
+
with gr.TabItem("π Run Evaluation", id=1):
|
| 414 |
+
with gr.Row(variant='panel'):
|
| 415 |
with gr.Column(scale=2):
|
| 416 |
+
with gr.Group():
|
| 417 |
gr.Markdown("### 1. Configure Evaluation")
|
| 418 |
model_id_input = gr.Textbox(
|
| 419 |
label="Hugging Face Model ID",
|
| 420 |
placeholder="e.g., meta-llama/Meta-Llama-3-8B-Instruct",
|
| 421 |
interactive=True
|
| 422 |
)
|
| 423 |
+
benchmark_selection_radio = gr.Radio(
|
| 424 |
+
["MMLU", "MMLU-Pro"],
|
| 425 |
+
label="Benchmark",
|
| 426 |
+
value="MMLU",
|
| 427 |
+
interactive=True,
|
| 428 |
+
)
|
|
|
|
|
|
|
|
|
|
| 429 |
with gr.Row():
|
| 430 |
benchmark_subject_dropdown = gr.Dropdown(
|
| 431 |
label="Subject",
|
|
|
|
| 444 |
gr.Markdown("### 2. View Results")
|
| 445 |
|
| 446 |
# Panel for displaying the summary of results
|
| 447 |
+
with gr.Group(visible=False) as result_summary_box:
|
| 448 |
+
result_summary_output = gr.Markdown(elem_id="result-summary-box")
|
| 449 |
|
| 450 |
# Panel for displaying errors
|
| 451 |
+
with gr.Group(visible=False) as error_box:
|
| 452 |
+
error_output = gr.Textbox(label="Error Message", interactive=False, elem_id="error-display-box")
|
| 453 |
error_details_output = gr.Textbox(label="Error Details (Traceback)", interactive=False, lines=8)
|
| 454 |
|
| 455 |
# Panel for detailed, row-by-row results
|
| 456 |
+
with gr.Group(visible=False) as details_box:
|
| 457 |
gr.Markdown("#### Detailed Evaluation Log")
|
| 458 |
+
detailed_results_df = gr.DataFrame(
|
| 459 |
+
headers=["Question", "Correct", "Expected", "Predicted", "Model Output"],
|
| 460 |
+
datatype=["str", "str", "str", "str", "str"],
|
| 461 |
interactive=False,
|
| 462 |
row_count=10,
|
| 463 |
+
col_count=5,
|
| 464 |
+
wrap=True,
|
| 465 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
# --- Event Handlers & Logic ---
|
| 468 |
|
|
|
|
| 477 |
run_button.click(
|
| 478 |
fn=run_evaluation,
|
| 479 |
inputs=[model_id_input, benchmark_selection_radio, benchmark_subject_dropdown, sample_count_slider],
|
| 480 |
+
outputs=[result_summary_output, error_box, error_output, error_details_output, details_box, detailed_results_df]
|
| 481 |
+
).then(
|
| 482 |
+
# After evaluation, switch to the leaderboard tab and refresh it
|
| 483 |
+
lambda: gr.update(selected=0), outputs=[tabs]
|
| 484 |
).then(
|
| 485 |
+
load_leaderboard, inputs=[leaderboard_type_toggle], outputs=[leaderboard_table_output]
|
|
|
|
|
|
|
|
|
|
| 486 |
)
|
| 487 |
|
| 488 |
# Leaderboard loading logic
|
|
|
|
| 494 |
leaderboard_type_toggle.change(
|
| 495 |
fn=load_leaderboard,
|
| 496 |
inputs=[leaderboard_type_toggle],
|
| 497 |
+
outputs=[leaderboard_table_output],
|
| 498 |
+
show_progress='minimal'
|
| 499 |
)
|
| 500 |
+
refresh_button.click(
|
|
|
|
|
|
|
| 501 |
fn=load_leaderboard,
|
| 502 |
inputs=[leaderboard_type_toggle],
|
| 503 |
+
outputs=[leaderboard_table_output],
|
| 504 |
+
show_progress='full'
|
| 505 |
)
|
| 506 |
|
| 507 |
# Launch the Gradio app
|