Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import requests | |
import pandas as pd | |
from agent import create_agent, fetch_random_question | |
# --- Constants --- | |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
def run_and_submit_all(profile): | |
""" | |
Fetch all questions, run the agent on them, submit all answers, | |
and return the status and results table. | |
""" | |
if profile is None: | |
return "Please login to Hugging Face with the login button.", None | |
username = profile.username | |
# Instantiate agent | |
try: | |
agent = create_agent() | |
except Exception as e: | |
return f"Error initializing agent: {e}", None | |
agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main" | |
# Fetch questions | |
try: | |
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15) | |
resp.raise_for_status() | |
questions = resp.json() | |
except Exception as e: | |
return f"Error fetching questions: {e}", None | |
if not questions: | |
return "No questions fetched.", None | |
# Run agent on each question | |
results = [] | |
payload = [] | |
for item in questions: | |
task_id = item.get("task_id") | |
question_text = item.get("question") | |
if not task_id or question_text is None: | |
continue | |
try: | |
ans = agent.run(question=question_text) | |
except Exception as e: | |
ans = f"ERROR: {e}" | |
results.append({"Task ID": task_id, "Question": question_text, "Answer": ans}) | |
payload.append({"task_id": task_id, "submitted_answer": ans}) | |
# Submit answers | |
submission = {"username": username, "agent_code": agent_code, "answers": payload} | |
try: | |
r = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60) | |
r.raise_for_status() | |
data = r.json() | |
status = ( | |
f"Submission Successful!\n" | |
f"User: {data.get('username')}\n" | |
f"Score: {data.get('score')}% ({data.get('correct_count')}/{data.get('total_attempted')})\n" | |
f"Message: {data.get('message')}" | |
) | |
except Exception as e: | |
status = f"Submission Failed: {e}" | |
return status, pd.DataFrame(results) | |
def test_random_question(profile): | |
""" | |
Fetch a random question and return the question and agent's answer. | |
""" | |
if profile is None: | |
return "Please login to Hugging Face with the login button.", "" | |
try: | |
q = fetch_random_question() | |
agent = create_agent() | |
ans = agent.run(question=q.get("question", "")) | |
return q.get("question", ""), ans | |
except Exception as e: | |
return f"Error: {e}", "" | |
# --- Gradio Interface --- | |
with gr.Blocks() as demo: | |
gr.Markdown("# SmolAgent Evaluation Runner") | |
gr.Markdown( | |
""" | |
**Instructions:** | |
1. Clone this space and implement your agent logic in agent.py. | |
2. Log in with your Hugging Face account using the login button below. | |
3. Use the buttons to run evaluation or test a random question. | |
""" | |
) | |
login = gr.LoginButton() | |
state = gr.State(value=None) | |
# Store OAuthProfile in state when user logs in | |
login.click(fn=lambda profile: profile, inputs=[], outputs=[state]) | |
run_all_btn = gr.Button("Run Evaluation & Submit All Answers") | |
test_btn = gr.Button("Test Random Question") | |
status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False) | |
results_table = gr.DataFrame(label="Results Table", wrap=True) | |
question_box = gr.Textbox(label="Random Question", lines=3, interactive=False) | |
answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False) | |
run_all_btn.click(fn=run_and_submit_all, inputs=[state], outputs=[status_output, results_table]) | |
test_btn.click(fn=test_random_question, inputs=[state], outputs=[question_box, answer_box]) | |
if __name__ == "__main__": | |
demo.launch(debug=True, share=False) | |