Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py β
|
2 |
|
3 |
import os
|
4 |
import requests
|
@@ -6,105 +6,72 @@ import pandas as pd
|
|
6 |
import gradio as gr
|
7 |
import asyncio
|
8 |
import tempfile
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
class GAIALlamaAgent:
|
14 |
-
def __call__(self, question: str,
|
15 |
-
|
16 |
-
|
17 |
-
if "mp3"
|
18 |
-
return transcribe_audio(
|
19 |
-
|
20 |
-
return extract_excel_total_food_sales(
|
21 |
-
|
22 |
return asyncio.run(answer_question(question))
|
23 |
|
24 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
api_url = DEFAULT_API_URL
|
31 |
-
questions_url = f"{api_url}/questions"
|
32 |
-
submit_url = f"{api_url}/submit"
|
33 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
34 |
|
35 |
try:
|
36 |
-
|
37 |
-
response.raise_for_status()
|
38 |
-
questions_data = response.json()
|
39 |
except Exception as e:
|
40 |
-
return f"
|
41 |
|
42 |
agent = GAIALlamaAgent()
|
43 |
-
|
44 |
-
results_log = []
|
45 |
|
46 |
-
for
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
continue
|
51 |
-
try:
|
52 |
-
answer = agent(question)
|
53 |
-
except Exception as e:
|
54 |
-
answer = f"[AGENT ERROR] {e}"
|
55 |
-
answers_payload.append({"task_id": qid, "submitted_answer": answer})
|
56 |
-
results_log.append({"Task ID": qid, "Question": question, "Submitted Answer": answer})
|
57 |
-
|
58 |
-
submission_data = {
|
59 |
-
"username": username,
|
60 |
-
"agent_code": agent_code,
|
61 |
-
"answers": answers_payload
|
62 |
-
}
|
63 |
|
64 |
try:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
status = (
|
69 |
-
f"β
Submission Successful!\n"
|
70 |
-
f"User: {result_data.get('username')}\n"
|
71 |
-
f"Score: {result_data.get('score')}%\n"
|
72 |
-
f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n"
|
73 |
-
f"Message: {result_data.get('message')}"
|
74 |
-
)
|
75 |
-
return status, pd.DataFrame(results_log)
|
76 |
except Exception as e:
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
with gr.Blocks() as demo:
|
81 |
-
gr.Markdown("""
|
82 |
-
# π§ GAIA Agent Evaluation
|
83 |
|
84 |
-
|
85 |
-
""
|
86 |
gr.LoginButton()
|
87 |
-
|
88 |
with gr.Row():
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
)
|
99 |
-
|
100 |
-
gr.Markdown("## Or run full benchmark submission")
|
101 |
-
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
102 |
-
run_out = gr.Textbox(label="Status", lines=4)
|
103 |
-
run_table = gr.DataFrame(label="Questions and Agent Answers")
|
104 |
-
run_btn.click(fn=run_and_submit_all, outputs=[run_out, run_table])
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
-
print("
|
108 |
if os.getenv("SPACE_ID"):
|
109 |
-
print(
|
110 |
demo.launch(debug=True)
|
|
|
1 |
+
# app.py β supports text, audio (.mp3) and Excel (.xlsx) inputs
|
2 |
|
3 |
import os
|
4 |
import requests
|
|
|
6 |
import gradio as gr
|
7 |
import asyncio
|
8 |
import tempfile
|
9 |
+
|
10 |
+
from agent import (
|
11 |
+
answer_question,
|
12 |
+
transcribe_audio,
|
13 |
+
extract_excel_total_food_sales
|
14 |
+
)
|
15 |
|
16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
17 |
|
18 |
class GAIALlamaAgent:
|
19 |
+
def __call__(self, question: str, file) -> str:
|
20 |
+
if file:
|
21 |
+
ext = os.path.splitext(file.name)[1].lower()
|
22 |
+
if ext == ".mp3":
|
23 |
+
return transcribe_audio(file.name)
|
24 |
+
if ext in (".xls", ".xlsx"):
|
25 |
+
return extract_excel_total_food_sales(file.name)
|
|
|
26 |
return asyncio.run(answer_question(question))
|
27 |
|
28 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
29 |
+
if not profile or not profile.username:
|
30 |
+
return "Please log in to Hugging Face.", None
|
31 |
+
username = profile.username
|
32 |
+
space_id = os.getenv("SPACE_ID", "")
|
33 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
|
|
|
|
|
34 |
|
35 |
try:
|
36 |
+
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
|
|
|
|
37 |
except Exception as e:
|
38 |
+
return f"Error fetching questions: {e}", None
|
39 |
|
40 |
agent = GAIALlamaAgent()
|
41 |
+
payload, log = [], []
|
|
|
42 |
|
43 |
+
for q in questions:
|
44 |
+
ans = agent(q["question"], None)
|
45 |
+
payload.append({"task_id": q["task_id"], "submitted_answer": ans})
|
46 |
+
log.append({"Task ID": q["task_id"], "Question": q["question"], "Answer": ans})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
try:
|
49 |
+
res = requests.post(f"{DEFAULT_API_URL}/submit",
|
50 |
+
json={"username": username, "agent_code": agent_code, "answers": payload},
|
51 |
+
timeout=60).json()
|
52 |
+
status = f"β
Score: {res.get('score')}% ({res.get('correct_count')}/{res.get('total_attempted')})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except Exception as e:
|
54 |
+
status = f"β Submission failed: {e}"
|
55 |
|
56 |
+
return status, pd.DataFrame(log)
|
|
|
|
|
|
|
57 |
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("# GAIA Agent")
|
60 |
gr.LoginButton()
|
|
|
61 |
with gr.Row():
|
62 |
+
question = gr.Textbox(label="Question")
|
63 |
+
file_in = gr.File(label="Optional file (.mp3, .xlsx)")
|
64 |
+
ask_btn = gr.Button("Ask")
|
65 |
+
output = gr.Textbox(label="Answer")
|
66 |
+
ask_btn.click(lambda q, f: GAIALlamaAgent()(q, f), inputs=[question, file_in], outputs=output)
|
67 |
+
gr.Markdown("## Or run full GAIA evaluation")
|
68 |
+
eval_btn = gr.Button("Run Evaluation & Submit")
|
69 |
+
status = gr.Textbox(label="Status", lines=4)
|
70 |
+
table = gr.DataFrame()
|
71 |
+
eval_btn.click(run_and_submit_all, outputs=[status, table])
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
if __name__ == "__main__":
|
74 |
+
print("π App starting")
|
75 |
if os.getenv("SPACE_ID"):
|
76 |
+
print("π Space:", os.getenv("SPACE_ID"))
|
77 |
demo.launch(debug=True)
|