dawid-lorek's picture
Update app.py
4c200bf verified
raw
history blame
3.62 kB
# app.py
import os
import requests
import pandas as pd
import gradio as gr
from agent import answer_question
import asyncio
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class GAIALlamaAgent:
def __init__(self):
print("βœ… LangChain/LlamaIndex Agent initialized.")
def __call__(self, question: str) -> str:
print(f"πŸ“¨ Agent received: {question[:50]}...")
try:
return asyncio.run(answer_question(question))
except Exception as e:
return f"[ERROR] {str(e)}"
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 log in to Hugging Face.", None
print(f"πŸ‘€ User: {username}")
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
print(f"πŸ“₯ Fetched {len(questions_data)} questions")
except Exception as e:
return f"❌ Error fetching questions: {e}", None
agent = GAIALlamaAgent()
answers_payload = []
results_log = []
for item in questions_data:
qid = item.get("task_id")
question = item.get("question")
if not qid or not question:
continue
try:
answer = agent(question)
except Exception as e:
answer = f"[AGENT ERROR] {e}"
answers_payload.append({"task_id": qid, "submitted_answer": answer})
results_log.append({"Task ID": qid, "Question": question, "Submitted Answer": answer})
if not answers_payload:
return "No answers to submit.", pd.DataFrame(results_log)
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()
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)
# --- Build Gradio Interface using Blocks ---
with gr.Blocks() as demo:
gr.Markdown("""
# 🧠 GAIA Agent Evaluation
This app runs a LlamaIndex + LangChain powered agent through the GAIA benchmark.
1. Login to Hugging Face below
2. Click **Run Evaluation** to test all questions
3. Answers will be submitted and scored
""")
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Status", lines=5, interactive=False)
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
print("\nπŸ” App Starting Up...")
if os.getenv("SPACE_ID"):
print(f"πŸ”— Space: https://huggingface.co/spaces/{os.getenv('SPACE_ID')}")
demo.launch(debug=True)