dawid-lorek commited on
Commit
2c0ba2f
·
verified ·
1 Parent(s): 0e65f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -48
app.py CHANGED
@@ -1,77 +1,89 @@
1
- # app.py supports text, audio (.mp3) and Excel (.xlsx) inputs
2
 
3
  import os
4
  import requests
5
  import pandas as pd
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)
 
1
+ # app.py przywrócony layout oryginalnego benchmarku z integracją GAIA agent
2
 
3
  import os
4
  import requests
5
  import pandas as pd
6
  import gradio as gr
 
 
7
 
8
+ from agent import answer_question, transcribe_audio, extract_excel_total_food_sales
 
 
 
 
9
 
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  class GAIALlamaAgent:
13
+ def __call__(self, question: str) -> str:
14
+ return answer_question_sync(question)
 
 
 
 
 
 
15
 
16
  def run_and_submit_all(profile: gr.OAuthProfile | None):
17
+ space_id = os.getenv("SPACE_ID")
18
  if not profile or not profile.username:
19
+ return "Please Login to Hugging Face with the button.", None
20
+
21
+ username = profile.username.strip()
22
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
23
+ questions_url = f"{DEFAULT_API_URL}/questions"
24
+ submit_url = f"{DEFAULT_API_URL}/submit"
25
 
26
  try:
27
+ response = requests.get(questions_url, timeout=15)
28
+ response.raise_for_status()
29
+ questions_data = response.json()
30
  except Exception as e:
31
  return f"Error fetching questions: {e}", None
32
 
33
  agent = GAIALlamaAgent()
34
+ results_log = []
35
+ answers_payload = []
36
+
37
+ for item in questions_data:
38
+ task_id = item.get("task_id")
39
+ question_text = item.get("question")
40
+ if not task_id or question_text is None:
41
+ continue
42
+ try:
43
+ submitted_answer = agent(question_text)
44
+ except Exception as e:
45
+ submitted_answer = f"[ERROR] {e}"
46
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
47
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
48
 
49
+ submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
 
 
 
50
 
51
  try:
52
+ response = requests.post(submit_url, json=submission_data, timeout=60)
53
+ response.raise_for_status()
54
+ result_data = response.json()
55
+ final_status = (
56
+ f"Submission Successful!\n"
57
+ f"User: {result_data.get('username')}\n"
58
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
59
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
60
+ f"Message: {result_data.get('message', 'No message received.')}"
61
+ )
62
+ return final_status, pd.DataFrame(results_log)
63
  except Exception as e:
64
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
65
 
66
+ # --- Gradio Interface matching original benchmark ---
67
  with gr.Blocks() as demo:
68
+ gr.Markdown("# Basic Agent Evaluation Runner")
69
+ gr.Markdown("""
70
+ **Instructions:**
71
+ 1. Please clone this space and modify the agent logic.
72
+ 2. Log in to Hugging Face with the button.
73
+ 3. Click 'Run Evaluation & Submit All Answers' to run the full GAIA test.
74
+ """)
75
+
76
  gr.LoginButton()
77
+
78
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
79
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
80
+ results_table = gr.DataFrame(label="Questions and Agent Answers")
81
+
82
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
 
83
 
84
  if __name__ == "__main__":
85
+ print("\n===== Application Startup =====")
86
+ space_id = os.getenv("SPACE_ID")
87
+ if space_id:
88
+ print(f"🔗 Space: https://huggingface.co/spaces/{space_id}")
89
  demo.launch(debug=True)