Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,32 @@
|
|
1 |
-
# app.py
|
2 |
|
3 |
import os
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
import gradio as gr
|
7 |
-
from agent import answer_question
|
8 |
import asyncio
|
|
|
|
|
9 |
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
class GAIALlamaAgent:
|
13 |
-
def
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
print(f"π¨ Agent received: {question[:50]}...")
|
18 |
-
try:
|
19 |
-
return asyncio.run(answer_question(question))
|
20 |
-
except Exception as e:
|
21 |
-
return f"[ERROR] {str(e)}"
|
22 |
|
23 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
24 |
space_id = os.getenv("SPACE_ID")
|
25 |
username = profile.username if profile else None
|
26 |
if not username:
|
27 |
-
return "Please
|
28 |
|
29 |
-
print(f"π€ User: {username}")
|
30 |
api_url = DEFAULT_API_URL
|
31 |
questions_url = f"{api_url}/questions"
|
32 |
submit_url = f"{api_url}/submit"
|
@@ -36,7 +36,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
36 |
response = requests.get(questions_url, timeout=15)
|
37 |
response.raise_for_status()
|
38 |
questions_data = response.json()
|
39 |
-
print(f"π₯ Fetched {len(questions_data)} questions")
|
40 |
except Exception as e:
|
41 |
return f"β Error fetching questions: {e}", None
|
42 |
|
@@ -56,9 +55,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
56 |
answers_payload.append({"task_id": qid, "submitted_answer": answer})
|
57 |
results_log.append({"Task ID": qid, "Question": question, "Submitted Answer": answer})
|
58 |
|
59 |
-
if not answers_payload:
|
60 |
-
return "No answers to submit.", pd.DataFrame(results_log)
|
61 |
-
|
62 |
submission_data = {
|
63 |
"username": username,
|
64 |
"agent_code": agent_code,
|
@@ -80,23 +76,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
80 |
except Exception as e:
|
81 |
return f"β Submission failed: {e}", pd.DataFrame(results_log)
|
82 |
|
83 |
-
# ---
|
84 |
with gr.Blocks() as demo:
|
85 |
gr.Markdown("""
|
86 |
# π§ GAIA Agent Evaluation
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
1. Login to Hugging Face below
|
91 |
-
2. Click **Run Evaluation** to test all questions
|
92 |
-
3. Answers will be submitted and scored
|
93 |
-
""")
|
94 |
gr.LoginButton()
|
95 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
96 |
-
status_output = gr.Textbox(label="Status", lines=5, interactive=False)
|
97 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
98 |
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
print("\nπ App Starting Up...")
|
|
|
1 |
+
# app.py β updated for GAIA tools (file upload for audio/Excel)
|
2 |
|
3 |
import os
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
import gradio as gr
|
|
|
7 |
import asyncio
|
8 |
+
import tempfile
|
9 |
+
from agent import answer_question, transcribe_audio, extract_excel_total_food_sales
|
10 |
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
class GAIALlamaAgent:
|
14 |
+
def __call__(self, question: str, file_path: str = None) -> str:
|
15 |
+
# Shortcut logic: if file exists and question matches specific types
|
16 |
+
if file_path:
|
17 |
+
if "mp3" in file_path:
|
18 |
+
return transcribe_audio(file_path)
|
19 |
+
elif "xlsx" in file_path or "xls" in file_path:
|
20 |
+
return extract_excel_total_food_sales(file_path)
|
21 |
|
22 |
+
return asyncio.run(answer_question(question))
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
25 |
space_id = os.getenv("SPACE_ID")
|
26 |
username = profile.username if profile else None
|
27 |
if not username:
|
28 |
+
return "Please login to Hugging Face.", None
|
29 |
|
|
|
30 |
api_url = DEFAULT_API_URL
|
31 |
questions_url = f"{api_url}/questions"
|
32 |
submit_url = f"{api_url}/submit"
|
|
|
36 |
response = requests.get(questions_url, timeout=15)
|
37 |
response.raise_for_status()
|
38 |
questions_data = response.json()
|
|
|
39 |
except Exception as e:
|
40 |
return f"β Error fetching questions: {e}", None
|
41 |
|
|
|
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,
|
|
|
76 |
except Exception as e:
|
77 |
return f"β Submission failed: {e}", pd.DataFrame(results_log)
|
78 |
|
79 |
+
# --- Gradio UI ---
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.Markdown("""
|
82 |
# π§ GAIA Agent Evaluation
|
83 |
|
84 |
+
Upload your files if needed and evaluate the agent's answers.
|
85 |
+
""")
|
|
|
|
|
|
|
|
|
86 |
gr.LoginButton()
|
|
|
|
|
|
|
87 |
|
88 |
+
with gr.Row():
|
89 |
+
question_box = gr.Textbox(label="Manual Question (optional)", lines=3)
|
90 |
+
file_input = gr.File(label="Optional File (.mp3 or .xlsx)", file_types=[".mp3", ".xlsx"])
|
91 |
+
submit_btn = gr.Button("Ask Agent")
|
92 |
+
|
93 |
+
output_text = gr.Textbox(label="Answer")
|
94 |
+
submit_btn.click(
|
95 |
+
fn=lambda q, f: GAIALlamaAgent()(q, f.name if f else None),
|
96 |
+
inputs=[question_box, file_input],
|
97 |
+
outputs=output_text
|
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("\nπ App Starting Up...")
|