|
import os |
|
import gradio as gr |
|
import requests |
|
import pandas as pd |
|
|
|
from agent import create_agent, fetch_random_question |
|
|
|
|
|
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
""" |
|
Fetch all questions, run the SmolAgent on them, submit all answers, |
|
and display the results. |
|
""" |
|
space_id = os.getenv("SPACE_ID") |
|
|
|
if profile: |
|
username = profile.username |
|
print(f"User logged in: {username}") |
|
else: |
|
print("User not logged in.") |
|
return "Please login to Hugging Face with the button.", None |
|
|
|
questions_url = f"{DEFAULT_API_URL}/questions" |
|
submit_url = f"{DEFAULT_API_URL}/submit" |
|
|
|
try: |
|
agent = create_agent() |
|
print("SmolAgent initialized.") |
|
except Exception as e: |
|
print(f"Error instantiating agent: {e}") |
|
return f"Error initializing agent: {e}", None |
|
|
|
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
print(f"Agent code URL: {agent_code}") |
|
|
|
try: |
|
response = requests.get(questions_url, timeout=15) |
|
response.raise_for_status() |
|
questions = response.json() |
|
if not questions: |
|
return "No questions fetched.", None |
|
print(f"Fetched {len(questions)} questions.") |
|
except Exception as e: |
|
print(f"Error fetching questions: {e}") |
|
return f"Error fetching questions: {e}", None |
|
|
|
results = [] |
|
payload = [] |
|
for q in questions: |
|
tid = q.get("task_id") |
|
text = q.get("question") |
|
if not tid or not text: |
|
continue |
|
try: |
|
ans = agent.run(question=text) |
|
except Exception as e: |
|
ans = f"ERROR: {e}" |
|
payload.append({"task_id": tid, "submitted_answer": ans}) |
|
results.append({"Task ID": tid, "Question": text, "Answer": ans}) |
|
|
|
if not payload: |
|
return "Agent returned no answers.", pd.DataFrame(results) |
|
|
|
submission = {"username": username, "agent_code": agent_code, "answers": payload} |
|
try: |
|
resp = requests.post(submit_url, json=submission, timeout=60) |
|
resp.raise_for_status() |
|
data = resp.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: |
|
print(f"Submission error: {e}") |
|
status = f"Submission Failed: {e}" |
|
|
|
return status, pd.DataFrame(results) |
|
|
|
|
|
def test_random_question(profile: gr.OAuthProfile | None): |
|
""" |
|
Fetch a random GAIA question and return the agent's answer. |
|
""" |
|
if not profile: |
|
return "Please login to test.", "" |
|
try: |
|
q = fetch_random_question() |
|
agent = create_agent() |
|
ans = agent.run(question=q.get("question", "")) |
|
return q.get("question", ""), ans |
|
except Exception as e: |
|
print(f"Test error: {e}") |
|
return f"Error: {e}", "" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# SmolAgent Evaluation Runner") |
|
gr.Markdown( |
|
""" |
|
**Istruzioni:** |
|
1. Clone questo space e definisci la logica in agent.py. |
|
2. Effettua il login con il tuo account Hugging Face. |
|
3. Usa 'Run Evaluation & Submit All Answers' o 'Test Random Question'. |
|
""" |
|
) |
|
login = gr.LoginButton() |
|
run_all = gr.Button("Run Evaluation & Submit All Answers") |
|
test = gr.Button("Test Random Question") |
|
status = gr.Textbox(label="Status / Risultato", lines=5, interactive=False) |
|
table = gr.DataFrame(label="Risultati Completi", wrap=True) |
|
qbox = gr.Textbox(label="Domanda Casuale", lines=3, interactive=False) |
|
abox = gr.Textbox(label="Risposta Agente", lines=3, interactive=False) |
|
|
|
run_all.click(fn=run_and_submit_all, inputs=[login], outputs=[status, table]) |
|
test.click(fn=test_random_question, inputs=[login], outputs=[qbox, abox]) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(debug=True, share=False) |
|
|