Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
from datasets import load_dataset
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Cache to avoid reloading the model
|
7 |
+
model_cache = {}
|
8 |
+
|
9 |
+
def load_model(model_id):
|
10 |
+
if model_id in model_cache:
|
11 |
+
return model_cache[model_id]
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
15 |
+
model_cache[model_id] = generator
|
16 |
+
return generator
|
17 |
+
|
18 |
+
def format_prompt(item, source):
|
19 |
+
if source == "cais/mmlu":
|
20 |
+
prompt = f"{item['question']}\nA. {item['choices'][0]}\nB. {item['choices'][1]}\nC. {item['choices'][2]}\nD. {item['choices'][3]}\nAnswer:"
|
21 |
+
answer = item['answer']
|
22 |
+
elif source == "TIGER-Lab/MMLU-Pro":
|
23 |
+
prompt = f"{item['question']}\nA. {item['A']}\nB. {item['B']}\nC. {item['C']}\nD. {item['D']}\nAnswer:"
|
24 |
+
answer = item['answer']
|
25 |
+
elif source == "cais/hle":
|
26 |
+
prompt = f"{item['question']}\n{item['A']}\n{item['B']}\n{item['C']}\n{item['D']}\nAnswer:"
|
27 |
+
answer = item['answer']
|
28 |
+
else:
|
29 |
+
prompt, answer = "", ""
|
30 |
+
return prompt, answer
|
31 |
+
|
32 |
+
def evaluate(model_id, dataset_name, sample_count):
|
33 |
+
gen = load_model(model_id)
|
34 |
+
dataset = load_dataset(dataset_name)
|
35 |
+
if 'test' in dataset:
|
36 |
+
dataset = dataset['test']
|
37 |
+
else:
|
38 |
+
dataset = dataset[list(dataset.keys())[0]]
|
39 |
+
|
40 |
+
dataset = dataset.shuffle(seed=42).select(range(min(sample_count, len(dataset))))
|
41 |
+
correct = 0
|
42 |
+
results = []
|
43 |
+
|
44 |
+
for item in dataset:
|
45 |
+
prompt, answer = format_prompt(item, dataset_name)
|
46 |
+
output = gen(prompt, max_new_tokens=10, do_sample=False)[0]["generated_text"]
|
47 |
+
output_letter = next((char for char in output[::-1] if char in "ABCD"), None)
|
48 |
+
is_correct = output_letter == answer
|
49 |
+
correct += is_correct
|
50 |
+
results.append((prompt, output.strip(), answer, output_letter, is_correct))
|
51 |
+
|
52 |
+
accuracy = correct / len(dataset) * 100
|
53 |
+
return f"Accuracy: {accuracy:.2f}%", results
|
54 |
+
|
55 |
+
def run(model_id, benchmark, sample_count):
|
56 |
+
score, details = evaluate(model_id, benchmark, sample_count)
|
57 |
+
formatted = "\n\n".join([
|
58 |
+
f"### Question:\n{q}\n\n**Model Answer:** {o}\n**Expected:** {a}\n**Predicted:** {g}\n**Correct:** {c}"
|
59 |
+
for q, o, a, g, c in details
|
60 |
+
])
|
61 |
+
return score, formatted
|
62 |
+
|
63 |
+
with gr.Blocks(css="body {font-family: Inter, sans-serif; padding: 1em; max-width: 900px; margin: auto;}", analytics_enabled=False, custom_code=True) as demo:
|
64 |
+
gr.Markdown("""
|
65 |
+
# 🤖 LLM Benchmark Evaluator
|
66 |
+
|
67 |
+
Easily evaluate your Hugging Face-hosted model on:
|
68 |
+
- **MMLU** (`cais/mmlu`)
|
69 |
+
- **MMLU-Pro** (`TIGER-Lab/MMLU-Pro`)
|
70 |
+
- **Humanity's Last Exam** (`cais/hle`)
|
71 |
+
|
72 |
+
Enter your model ID, pick a benchmark, and hit evaluate.
|
73 |
+
""")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
model_id = gr.Textbox(label="Your Hugging Face Model ID", placeholder="e.g., your-org/your-model")
|
77 |
+
benchmark = gr.Dropdown(
|
78 |
+
label="Choose Benchmark",
|
79 |
+
choices=["cais/mmlu", "TIGER-Lab/MMLU-Pro", "cais/hle"],
|
80 |
+
value="cais/mmlu"
|
81 |
+
)
|
82 |
+
sample_count = gr.Slider(label="Number of Samples", minimum=1, maximum=100, value=10, step=1)
|
83 |
+
|
84 |
+
run_button = gr.Button("🚀 Run Evaluation")
|
85 |
+
acc_output = gr.Textbox(label="Benchmark Accuracy", interactive=False)
|
86 |
+
detail_output = gr.Textbox(label="Evaluation Details", lines=20, interactive=False)
|
87 |
+
|
88 |
+
run_button.click(run, inputs=[model_id, benchmark, sample_count], outputs=[acc_output, detail_output])
|
89 |
+
|
90 |
+
demo.launch()
|