File size: 2,717 Bytes
f21f66c
cfc7eb3
10e9b7d
3bce169
4c200bf
188585a
4c200bf
17268b7
f21f66c
 
 
 
 
 
cfc7eb3
188585a
 
3bce169
f21f66c
 
 
 
 
 
 
17268b7
188585a
 
f21f66c
 
 
 
 
188585a
 
f21f66c
188585a
f21f66c
188585a
4c200bf
f21f66c
188585a
f21f66c
 
 
 
188585a
 
f21f66c
 
 
 
188585a
f21f66c
188585a
f21f66c
4c200bf
f21f66c
 
188585a
17268b7
f21f66c
 
 
 
 
 
 
 
 
 
188585a
4c200bf
f21f66c
4c200bf
f21f66c
4c200bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# app.py β€” supports text, audio (.mp3) and Excel (.xlsx) inputs

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)