dawid-lorek's picture
Update app.py
02035c1 verified
raw
history blame
3.3 kB
import os
import requests
import pandas as pd
import gradio as gr
from agent import BasicAgent
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class GAIAAgent:
def __init__(self):
self.agent = BasicAgent()
def __call__(self, question: str, task_id: str) -> str:
return self.agent(question, task_id)
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if not profile or not profile.username:
return "Please Login to Hugging Face with the button.", None
username = profile.username.strip()
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
questions_url = f"{DEFAULT_API_URL}/questions"
submit_url = f"{DEFAULT_API_URL}/submit"
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
except Exception as e:
return f"Error fetching questions: {e}", None
agent = GAIAAgent()
results_log = []
answers_payload = []
for item in questions_data:
task_id = item.get("task_id")
question_text = item.get("question")
if not task_id or question_text is None:
continue
try:
submitted_answer = agent(question_text, task_id)
except Exception as e:
submitted_answer = f"[ERROR] {e}"
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
response.raise_for_status()
result_data = response.json()
final_status = (
f"Submission Successful!\n"
f"User: {result_data.get('username')}\n"
f"Overall Score: {result_data.get('score', 'N/A')}% "
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
f"Message: {result_data.get('message', 'No message received.')}"
)
return final_status, pd.DataFrame(results_log)
except Exception as e:
return f"Submission Failed: {e}", pd.DataFrame(results_log)
with gr.Blocks() as demo:
gr.Markdown("# Basic Agent Evaluation Runner")
gr.Markdown("""
**Instructions:**
1. Please clone this space and modify the agent logic.
2. Log in to Hugging Face with the button.
3. Click 'Run Evaluation & Submit All Answers' to run the full GAIA test.
""")
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
results_table = gr.DataFrame(label="Questions and Agent Answers")
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
print("\n===== Application Startup =====")
space_id = os.getenv("SPACE_ID")
if space_id:
print(f"🔗 Space: https://huggingface.co/spaces/{space_id}")
demo.launch(debug=True)