Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
-
import random
|
| 4 |
import os
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import datetime
|
| 7 |
from huggingface_hub import HfApi
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Configuration
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
@@ -12,67 +13,90 @@ DATASET_REPO = "FlameF0X/benchmark-results"
|
|
| 12 |
|
| 13 |
def run_benchmarks(model_name):
|
| 14 |
if not model_name:
|
| 15 |
-
return "
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"HumanEval": round(random.uniform(30, 85), 2),
|
| 23 |
-
"TruthfulQA": round(random.uniform(45, 80), 2)
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
# Calculate weighted average
|
| 27 |
-
avg_score = round(sum(results.values()) / len(results), 2)
|
| 28 |
-
|
| 29 |
-
# 2. Prepare the data entry
|
| 30 |
-
entry = {
|
| 31 |
-
"model_name": model_name,
|
| 32 |
-
"average": avg_score,
|
| 33 |
-
**results,
|
| 34 |
-
"timestamp": datetime.now().isoformat()
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
# 3. Save and Upload (Simulated file handling)
|
| 38 |
-
filename = f"result_{model_name.replace('/', '_')}_{datetime.now().strftime('%H%M%S')}.json"
|
| 39 |
-
with open(filename, 'w') as f:
|
| 40 |
-
json.dump(entry, f)
|
| 41 |
-
|
| 42 |
-
# Note: Ensure DATASET_REPO is valid for this to work
|
| 43 |
-
if HF_TOKEN and DATASET_REPO != "user/benchmark-results":
|
| 44 |
-
try:
|
| 45 |
-
api = HfApi()
|
| 46 |
-
api.upload_file(
|
| 47 |
-
path_or_fileobj=filename,
|
| 48 |
-
path_in_repo=f"results/{filename}",
|
| 49 |
-
repo_id=DATASET_REPO,
|
| 50 |
-
repo_type="dataset",
|
| 51 |
-
token=HF_TOKEN
|
| 52 |
-
)
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"Upload failed: {e}")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 63 |
-
gr.Markdown("# π AI Model Benchmarking Hub")
|
|
|
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1):
|
| 67 |
-
model_input = gr.Textbox(label="Model Name", placeholder="e.g., Llama-3-
|
| 68 |
submit_btn = gr.Button("π Run Full Evaluation", variant="primary")
|
| 69 |
|
| 70 |
with gr.Column(scale=2):
|
| 71 |
-
output_summary = gr.Markdown("Enter a model name to start.")
|
| 72 |
results_table = gr.DataFrame(
|
| 73 |
headers=["Model", "Avg", "MMLU", "GSM8K", "HumanEval"],
|
| 74 |
datatype=["str", "number", "number", "number", "number"],
|
| 75 |
-
label="
|
| 76 |
)
|
| 77 |
|
| 78 |
submit_btn.click(
|
|
@@ -81,4 +105,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 81 |
outputs=[output_summary, results_table]
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime
|
| 6 |
from huggingface_hub import HfApi
|
| 7 |
+
import lm_eval
|
| 8 |
+
from lm_eval.models.huggingface import HFLM
|
| 9 |
|
| 10 |
# Configuration
|
| 11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 13 |
|
| 14 |
def run_benchmarks(model_name):
|
| 15 |
if not model_name:
|
| 16 |
+
return "### β Error\nPlease enter a valid Hugging Face model name (e.g., 'meta-llama/Llama-3.2-1B')", None
|
| 17 |
|
| 18 |
+
try:
|
| 19 |
+
# 1. Initialize the model for evaluation
|
| 20 |
+
# We use 'pretrained' for the model weight path and 'device' to use GPU if available
|
| 21 |
+
print(f"Loading model: {model_name}...")
|
| 22 |
+
lm_obj = HFLM(pretrained=model_name, device="cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 2. Define the tasks to run
|
| 25 |
+
# Note: 'mmlu' is a group; 'gsm8k' is math; 'truthfulqa_mc2' is standard for TruthfulQA
|
| 26 |
+
tasks = ["mmlu", "gsm8k", "truthfulqa_mc2"]
|
| 27 |
+
|
| 28 |
+
print(f"Starting evaluation on tasks: {tasks}...")
|
| 29 |
+
results_raw = lm_eval.simple_evaluate(
|
| 30 |
+
model=lm_obj,
|
| 31 |
+
tasks=tasks,
|
| 32 |
+
num_fewshot=0, # 0-shot evaluation; increase for few-shot
|
| 33 |
+
batch_size="auto"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# 3. Extract scores (Converting to percentages 0-100)
|
| 37 |
+
# Results structure varies by task, usually we look for 'acc' or 'acc_norm'
|
| 38 |
+
results = {
|
| 39 |
+
"MMLU": round(results_raw["results"].get("mmlu", {}).get("acc,none", 0) * 100, 2),
|
| 40 |
+
"GSM8K": round(results_raw["results"].get("gsm8k", {}).get("exact_match,strict-match", 0) * 100, 2),
|
| 41 |
+
"HumanEval": round(results_raw["results"].get("humaneval", {}).get("pass@1", 0) * 100, 2),
|
| 42 |
+
"TruthfulQA": round(results_raw["results"].get("truthfulqa_mc2", {}).get("acc,none", 0) * 100, 2)
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Calculate weighted average
|
| 46 |
+
avg_score = round(sum(results.values()) / len(results), 2)
|
| 47 |
+
|
| 48 |
+
# 4. Prepare the data entry (Matches your existing storage logic)
|
| 49 |
+
entry = {
|
| 50 |
+
"model_name": model_name,
|
| 51 |
+
"average": avg_score,
|
| 52 |
+
**results,
|
| 53 |
+
"timestamp": datetime.now().isoformat()
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# 5. Save and Upload
|
| 57 |
+
filename = f"result_{model_name.replace('/', '_')}_{datetime.now().strftime('%H%M%S')}.json"
|
| 58 |
+
with open(filename, 'w') as f:
|
| 59 |
+
json.dump(entry, f)
|
| 60 |
+
|
| 61 |
+
if HF_TOKEN and DATASET_REPO != "user/benchmark-results":
|
| 62 |
+
try:
|
| 63 |
+
api = HfApi()
|
| 64 |
+
api.upload_file(
|
| 65 |
+
path_or_fileobj=filename,
|
| 66 |
+
path_in_repo=f"results/{filename}",
|
| 67 |
+
repo_id=DATASET_REPO,
|
| 68 |
+
repo_type="dataset",
|
| 69 |
+
token=HF_TOKEN
|
| 70 |
+
)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Upload failed: {e}")
|
| 73 |
+
|
| 74 |
+
if os.path.exists(filename):
|
| 75 |
+
os.remove(filename)
|
| 76 |
+
|
| 77 |
+
summary = f"### β
Results for {model_name}\n**Average Score: {avg_score}%**"
|
| 78 |
+
return summary, [[model_name, avg_score, results["MMLU"], results["GSM8K"], results["HumanEval"]]]
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
error_msg = f"### β Evaluation Failed\n**Error:** {str(e)}"
|
| 82 |
+
return error_msg, None
|
| 83 |
|
| 84 |
+
# --- Gradio UI remains the same ---
|
| 85 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 86 |
+
gr.Markdown("# π AI Model Benchmarking Hub (Live Eval)")
|
| 87 |
+
gr.Markdown("Note: Running this requires a GPU and time for model inference.")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
+
model_input = gr.Textbox(label="Model Name", placeholder="e.g., meta-llama/Llama-3.2-1B")
|
| 92 |
submit_btn = gr.Button("π Run Full Evaluation", variant="primary")
|
| 93 |
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
+
output_summary = gr.Markdown("Enter a model name to start actual evaluation.")
|
| 96 |
results_table = gr.DataFrame(
|
| 97 |
headers=["Model", "Avg", "MMLU", "GSM8K", "HumanEval"],
|
| 98 |
datatype=["str", "number", "number", "number", "number"],
|
| 99 |
+
label="Benchmark Results"
|
| 100 |
)
|
| 101 |
|
| 102 |
submit_btn.click(
|
|
|
|
| 105 |
outputs=[output_summary, results_table]
|
| 106 |
)
|
| 107 |
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
app.launch()
|