|
|
|
|
|
import os |
|
import requests |
|
import pandas as pd |
|
import gradio as gr |
|
import asyncio |
|
import tempfile |
|
|
|
from agent import ( |
|
answer_question, |
|
transcribe_audio, |
|
extract_excel_total_food_sales |
|
) |
|
|
|
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
|
class GAIALlamaAgent: |
|
def __call__(self, question: str, file) -> str: |
|
if file: |
|
ext = os.path.splitext(file.name)[1].lower() |
|
if ext == ".mp3": |
|
return transcribe_audio(file.name) |
|
if ext in (".xls", ".xlsx"): |
|
return extract_excel_total_food_sales(file.name) |
|
return asyncio.run(answer_question(question)) |
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
if not profile or not profile.username: |
|
return "Please log in to Hugging Face.", None |
|
username = profile.username |
|
space_id = os.getenv("SPACE_ID", "") |
|
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
|
|
try: |
|
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json() |
|
except Exception as e: |
|
return f"Error fetching questions: {e}", None |
|
|
|
agent = GAIALlamaAgent() |
|
payload, log = [], [] |
|
|
|
for q in questions: |
|
ans = agent(q["question"], None) |
|
payload.append({"task_id": q["task_id"], "submitted_answer": ans}) |
|
log.append({"Task ID": q["task_id"], "Question": q["question"], "Answer": ans}) |
|
|
|
try: |
|
res = requests.post(f"{DEFAULT_API_URL}/submit", |
|
json={"username": username, "agent_code": agent_code, "answers": payload}, |
|
timeout=60).json() |
|
status = f"β
Score: {res.get('score')}% ({res.get('correct_count')}/{res.get('total_attempted')})" |
|
except Exception as e: |
|
status = f"β Submission failed: {e}" |
|
|
|
return status, pd.DataFrame(log) |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# GAIA Agent") |
|
gr.LoginButton() |
|
with gr.Row(): |
|
question = gr.Textbox(label="Question") |
|
file_in = gr.File(label="Optional file (.mp3, .xlsx)") |
|
ask_btn = gr.Button("Ask") |
|
output = gr.Textbox(label="Answer") |
|
ask_btn.click(lambda q, f: GAIALlamaAgent()(q, f), inputs=[question, file_in], outputs=output) |
|
gr.Markdown("## Or run full GAIA evaluation") |
|
eval_btn = gr.Button("Run Evaluation & Submit") |
|
status = gr.Textbox(label="Status", lines=4) |
|
table = gr.DataFrame() |
|
eval_btn.click(run_and_submit_all, outputs=[status, table]) |
|
|
|
if __name__ == "__main__": |
|
print("π App starting") |
|
if os.getenv("SPACE_ID"): |
|
print("π Space:", os.getenv("SPACE_ID")) |
|
demo.launch(debug=True) |