dawid-lorek commited on
Commit
f21f66c
Β·
verified Β·
1 Parent(s): 95da673

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -79
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py β€” updated for GAIA tools (file upload for audio/Excel)
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
- 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"
33
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
34
 
35
  try:
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
 
42
  agent = GAIALlamaAgent()
43
- answers_payload = []
44
- results_log = []
45
 
46
- for item in questions_data:
47
- qid = item.get("task_id")
48
- question = item.get("question")
49
- if not qid or not question:
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
- response = requests.post(submit_url, json=submission_data, timeout=60)
66
- response.raise_for_status()
67
- result_data = response.json()
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
- 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...")
108
  if os.getenv("SPACE_ID"):
109
- print(f"πŸ”— Space: https://huggingface.co/spaces/{os.getenv('SPACE_ID')}")
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)