# app.py import os import asyncio import pandas as pd import requests import gradio as gr from agent import answer_question DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" class GAIALlamaAgent: def __init__(self): print("Initialized LlamaIndex Agent") def __call__(self, question: str) -> str: print(f"Received question: {question[:60]}...") return asyncio.run(answer_question(question)) def run_and_submit_all(profile: gr.OAuthProfile | None): space_id = os.getenv("SPACE_ID") username = profile.username if profile else None if not username: return "Please login to Hugging Face.", None agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "" api_url = DEFAULT_API_URL try: response = requests.get(f"{api_url}/questions", timeout=15) response.raise_for_status() questions_data = response.json() except Exception as e: return f"Error fetching questions: {e}", None answers_payload = [] results_log = [] agent = GAIALlamaAgent() for item in questions_data: q = item.get("question") task_id = item.get("task_id") try: a = agent(q) except Exception as e: a = f"ERROR: {e}" answers_payload.append({"task_id": task_id, "submitted_answer": a}) results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": a}) submission_data = { "username": username, "agent_code": agent_code, "answers": answers_payload } try: response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60) response.raise_for_status() result_data = response.json() status = ( f"✅ Submission Successful!\n" f"User: {result_data.get('username')}\n" f"Score: {result_data.get('score')}%\n" f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n" f"Message: {result_data.get('message')}" ) return 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("# LlamaIndex GAIA Agent – Evaluation Portal") gr.LoginButton() run_btn = gr.Button("Run Evaluation & Submit All Answers") status_box = gr.Textbox(label="Status", lines=5) result_table = gr.DataFrame(label="Agent Answers") run_btn.click(fn=run_and_submit_all, outputs=[status_box, result_table]) demo.launch(debug=True)