Asapyams commited on
Commit
4386f51
Β·
verified Β·
1 Parent(s): aeadedb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -127
app.py CHANGED
@@ -3,217 +3,204 @@ import os
3
  import gradio as gr
4
  import requests
5
  import openai
6
- from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
7
- from pathlib import Path
8
  import tempfile
9
- from smolagents.tools import PipelineTool, Tool
10
  import pathlib
11
- from typing import Union, Optional
12
- import pandas as pd
13
- from tabulate import tabulate # pragma: no cover – fallback path
14
  import re
 
 
15
 
16
- # ─── Set your OpenAI API Key ─────────────────────────────────────────────────
17
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
18
- if not OPENAI_API_KEY:
19
- raise RuntimeError(
20
- "Please set the OPENAI_API_KEY environment variable (e.g. export OPENAI_API_KEY=sk-…)"
21
- )
22
- openai.api_key = OPENAI_API_KEY
23
- # ──────────────────────────────────────────────────────────────────────────────
24
 
25
- # --- Constants ---
26
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
 
 
27
 
 
 
 
28
  class SpeechToTextTool(PipelineTool):
29
- """
30
- Transcribes an audio file to text using the OpenAI Whisper API.
31
- Only local file paths are supported.
32
- """
33
  default_checkpoint = "openai/whisper-1"
34
- description = (
35
- "This tool sends an audio file to OpenAI Whisper and returns the "
36
- "transcribed text."
37
- )
38
  name = "transcriber"
39
- inputs = {
40
- "audio": {
41
- "type": "string",
42
- "description": "Absolute or relative path to a local audio file.",
43
- }
44
- }
45
  output_type = "string"
46
-
47
  def __call__(self, audio: str) -> str:
48
  return self._transcribe(audio)
49
-
50
  @staticmethod
51
  def _transcribe(audio_path: str) -> str:
52
- if not isinstance(audio_path, str):
53
- raise TypeError("Parameter 'audio' must be a string containing the file path.")
54
- path = Path(audio_path).expanduser().resolve()
55
- if not path.is_file():
56
- raise FileNotFoundError(f"No such audio file: {path}")
57
- with path.open("rb") as fp:
58
- response = openai.audio.transcriptions.create(
59
- file=fp,
60
- model="whisper-1",
61
- response_format="text"
62
  )
63
- return response
64
 
 
 
 
65
  class ExcelToTextTool(Tool):
66
- """Render an Excel worksheet as Markdown text."""
67
  name = "excel_to_text"
68
- description = (
69
- "Read an Excel file and return a Markdown table of the requested sheet. "
70
- "Accepts either the sheet name or the zero-based index."
71
- )
72
  inputs = {
73
- "excel_path": {
74
- "type": "string",
75
- "description": "Path to the Excel file (.xlsx / .xls).",
76
- },
77
  "sheet_name": {
78
  "type": "string",
79
- "description": (
80
- "Worksheet name or zero‑based index *as a string* (optional; default first sheet)."
81
- ),
82
  "nullable": True,
83
  },
84
  }
85
  output_type = "string"
86
-
87
- def forward(self, excel_path: str, sheet_name: Optional[str] = None) -> str:
88
  path = pathlib.Path(excel_path).expanduser().resolve()
89
  if not path.exists():
90
  return f"Error: Excel file not found at {path}"
91
  try:
92
- sheet = 0 if not sheet_name else (int(sheet_name) if sheet_name.isdigit() else sheet_name)
93
- df = pd.read_excel(path, sheet_name=sheet)
94
- if hasattr(pd.DataFrame, "to_markdown"):
95
  return df.to_markdown(index=False)
 
96
  return tabulate(df, headers="keys", tablefmt="github", showindex=False)
97
- except Exception as exc:
98
- return f"Error reading Excel file: {exc}"
99
 
 
 
 
100
  def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
101
  url = f"{base_api_url}/files/{task_id}"
102
  resp = requests.get(url, timeout=30)
103
  if resp.status_code == 404:
104
  return None
105
  resp.raise_for_status()
106
- cdisp = resp.headers.get("content-disposition", "")
107
- filename = task_id
108
- if "filename=" in cdisp:
109
- m = re.search(r'filename="([^"]+)"', cdisp)
110
  if m:
111
- filename = m.group(1)
112
- tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
113
- tmp_dir.mkdir(exist_ok=True)
114
- file_path = tmp_dir / filename
115
- with open(file_path, "wb") as f:
116
- f.write(resp.content)
117
- return str(file_path)
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  class BasicAgent:
120
  def __init__(self):
121
  self.agent = CodeAgent(
122
  model=OpenAIServerModel(model_id="gpt-3.5-turbo"),
123
  tools=[
124
- DuckDuckGoSearchTool(),
125
  WikipediaSearchTool(),
126
  SpeechToTextTool(),
127
- ExcelToTextTool()
128
  ],
129
  add_base_tools=True,
130
- additional_authorized_imports=['pandas','numpy','csv','subprocess'],
131
- max_steps=5,
132
-
 
133
  )
134
- print("BasicAgent initialized.")
135
 
136
- def __call__(self, question: str) -> str:
137
- print(f"Agent received question (first 50 chars): {question[:50]}...")
138
- answer = self.agent.run(question)
139
- print(f"Agent returning answer: {answer}")
140
- return answer
141
 
142
  def run_and_submit_all(username: str):
143
- if not username:
144
- return "Please provide your username.", None
145
 
146
  api_url = DEFAULT_API_URL
147
  questions_url = f"{api_url}/questions"
148
  submit_url = f"{api_url}/submit"
149
 
150
  try:
151
- agent = BasicAgent()
152
  except Exception as e:
153
  return f"Error initializing agent: {e}", None
154
 
155
- agent_code = "https://huggingface.co/spaces/l3xv/Final_Assignment_Template/tree/main"
156
-
157
  try:
158
  resp = requests.get(questions_url, timeout=15)
159
  resp.raise_for_status()
160
- questions = resp.json()
161
  except Exception as e:
162
  return f"Error fetching questions: {e}", None
163
 
164
- results_log = []
165
- payload = []
166
- for item in questions:
167
- task_id = item.get("task_id")
168
- q = item.get("question")
169
- if not (task_id and q):
170
  continue
 
171
  try:
172
- file_path = download_file_if_any(api_url, task_id)
173
- except Exception as fe:
174
- print(f"[file fetch error] {task_id}: {fe}")
175
- file_path = None
176
- prompt = q + (f"\n\n---\nFile at: {file_path}\n---\n\n" if file_path else "")
177
- try:
178
- ans = agent(prompt)
179
- except Exception as e:
180
- ans = f"AGENT ERROR: {e}"
181
- payload.append({"task_id": task_id, "submitted_answer": ans})
182
- results_log.append({"Task ID": task_id, "Question": q, "Answer": ans})
183
-
184
- if not payload:
185
- return "No answers generated.", pd.DataFrame(results_log)
186
-
187
- submission = {
188
- "username": username.strip(),
189
- "agent_code": agent_code,
190
- "answers": payload
191
- }
192
  try:
193
- r = requests.post(submit_url, json=submission, timeout=60)
194
- r.raise_for_status()
195
- res = r.json()
196
  status = (
197
  f"Submission Successful!\n"
198
- f"Score: {res.get('score', 'N/A')}% "
199
- f"({res.get('correct_count','?')}/{res.get('total_attempted','?')})"
 
200
  )
201
  except Exception as e:
202
  status = f"Submission Failed: {e}"
203
 
204
- return status, pd.DataFrame(results_log)
205
 
 
 
 
206
  with gr.Blocks() as demo:
207
  gr.Markdown("# GAIA Agent Evaluation Runner")
208
- username = gr.Textbox(label="Username", placeholder="Your HF username")
209
- run_btn = gr.Button("Run Evaluation & Submit All Answers")
210
- status = gr.Textbox(label="Status / Submission Result", interactive=False)
 
211
  table = gr.DataFrame(label="Results")
212
 
213
- run_btn.click(fn=run_and_submit_all, inputs=[username], outputs=[status, table])
214
 
215
  if __name__ == "__main__":
216
- print("Launching GAIA Agent App…")
217
  demo.launch(debug=True, share=False)
218
 
219
 
 
3
  import gradio as gr
4
  import requests
5
  import openai
 
 
6
  import tempfile
 
7
  import pathlib
 
 
 
8
  import re
9
+ import pandas as pd
10
+ from concurrent.futures import ThreadPoolExecutor, TimeoutError
11
 
12
+ from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
13
+ from smolagents.tools import Tool, PipelineTool
 
 
 
 
 
 
14
 
15
+ # ──────────────────────────────────────────────────────────────────────────────
16
+ # 1) One‑shot DuckDuckGo wrapper (never retries on 202)
17
+ # ──────────────────────────────────────────────────────────────────────────────
18
+ class OneShotSearch(DuckDuckGoSearchTool):
19
+ def __call__(self, query: str) -> str:
20
+ try:
21
+ return super().__call__(query)
22
+ except Exception:
23
+ return ""
24
 
25
+ # ──────────────────────────────────────────────────────────────────────────────
26
+ # 2) Whisper‑based audio β†’ text
27
+ # ──────────────────────────────────────────────────────────────────────────────
28
  class SpeechToTextTool(PipelineTool):
 
 
 
 
29
  default_checkpoint = "openai/whisper-1"
 
 
 
 
30
  name = "transcriber"
31
+ description = "Transcribe a local audio file via OpenAI Whisper."
32
+ inputs = {"audio": {"type": "string", "description": "Local audio file path."}}
 
 
 
 
33
  output_type = "string"
 
34
  def __call__(self, audio: str) -> str:
35
  return self._transcribe(audio)
 
36
  @staticmethod
37
  def _transcribe(audio_path: str) -> str:
38
+ p = pathlib.Path(audio_path).expanduser().resolve()
39
+ if not p.is_file():
40
+ raise FileNotFoundError(f"No such audio file: {p}")
41
+ with p.open("rb") as fp:
42
+ return openai.audio.transcriptions.create(
43
+ file=fp, model="whisper-1", response_format="text"
 
 
 
 
44
  )
 
45
 
46
+ # ──────────────────────────────────────────────────────────────────────────────
47
+ # 3) Excel β†’ Markdown table
48
+ # ──────────────────────────────────────────────────────────────────────────────
49
  class ExcelToTextTool(Tool):
 
50
  name = "excel_to_text"
51
+ description = "Load an .xlsx/.xls and render a sheet as a Markdown table."
 
 
 
52
  inputs = {
53
+ "excel_path": {"type": "string", "description": "Path to .xlsx/.xls file"},
 
 
 
54
  "sheet_name": {
55
  "type": "string",
56
+ "description": "Sheet name or zero-based index (as string).",
 
 
57
  "nullable": True,
58
  },
59
  }
60
  output_type = "string"
61
+ def forward(self, excel_path: str, sheet_name: str | None = None) -> str:
 
62
  path = pathlib.Path(excel_path).expanduser().resolve()
63
  if not path.exists():
64
  return f"Error: Excel file not found at {path}"
65
  try:
66
+ idx = int(sheet_name) if sheet_name and sheet_name.isdigit() else sheet_name or 0
67
+ df = pd.read_excel(path, sheet_name=idx)
68
+ if hasattr(df, "to_markdown"):
69
  return df.to_markdown(index=False)
70
+ from tabulate import tabulate
71
  return tabulate(df, headers="keys", tablefmt="github", showindex=False)
72
+ except Exception as e:
73
+ return f"Error reading Excel file: {e}"
74
 
75
+ # ──────────────────────────────────────────────────────────────────────────────
76
+ # 4) Helper to download any attached file
77
+ # ──────────────────────────────────────────────────────────────────────────────
78
  def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
79
  url = f"{base_api_url}/files/{task_id}"
80
  resp = requests.get(url, timeout=30)
81
  if resp.status_code == 404:
82
  return None
83
  resp.raise_for_status()
84
+ disp = resp.headers.get("content-disposition", "")
85
+ fn = task_id
86
+ if "filename=" in disp:
87
+ m = re.search(r'filename="([^"]+)"', disp)
88
  if m:
89
+ fn = m.group(1)
90
+ tmp = pathlib.Path(tempfile.gettempdir()) / "gaia_files"
91
+ tmp.mkdir(exist_ok=True)
92
+ fp = tmp / fn
93
+ fp.write_bytes(resp.content)
94
+ return str(fp)
 
95
 
96
+ # ──────────────────────────────────────────────────────────────────────────────
97
+ # 5) Timeout wrapper around agent.run()
98
+ # ──────────────────────────────────────────────────────────────────────────────
99
+ def safe_run(agent: CodeAgent, question: str, timeout: float = 20.0) -> str:
100
+ with ThreadPoolExecutor(max_workers=1) as exe:
101
+ fut = exe.submit(agent.run, question)
102
+ try:
103
+ return fut.result(timeout=timeout)
104
+ except (TimeoutError, Exception):
105
+ return "N/A"
106
+
107
+ # ──────────────────────────────────────────────────────────────────────────────
108
+ # 6) Your BasicAgent, with max_steps and our custom tools
109
+ # ──────────────────────────────────────────────────────────────────────────────
110
  class BasicAgent:
111
  def __init__(self):
112
  self.agent = CodeAgent(
113
  model=OpenAIServerModel(model_id="gpt-3.5-turbo"),
114
  tools=[
115
+ OneShotSearch(),
116
  WikipediaSearchTool(),
117
  SpeechToTextTool(),
118
+ ExcelToTextTool(),
119
  ],
120
  add_base_tools=True,
121
+ additional_authorized_imports=[
122
+ "pandas", "numpy", "math", "re", "csv", "subprocess"
123
+ ],
124
+ max_steps=4,
125
  )
 
126
 
127
+ # ──────────────────────────────────────────────────────────────────────────────
128
+ # 7) Gradio runner (no login button, just a username box)
129
+ # ──────────────────────────────────────────────────────────────────────────────
130
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
131
 
132
  def run_and_submit_all(username: str):
133
+ if not username.strip():
134
+ return "Error: please enter your Hugging Face username.", None
135
 
136
  api_url = DEFAULT_API_URL
137
  questions_url = f"{api_url}/questions"
138
  submit_url = f"{api_url}/submit"
139
 
140
  try:
141
+ agent_wrapper = BasicAgent()
142
  except Exception as e:
143
  return f"Error initializing agent: {e}", None
144
 
 
 
145
  try:
146
  resp = requests.get(questions_url, timeout=15)
147
  resp.raise_for_status()
148
+ qs = resp.json()
149
  except Exception as e:
150
  return f"Error fetching questions: {e}", None
151
 
152
+ answers = []
153
+ log = []
154
+ for item in qs:
155
+ tid = item.get("task_id")
156
+ qtxt = item.get("question")
157
+ if not (tid and qtxt):
158
  continue
159
+ fp = None
160
  try:
161
+ fp = download_file_if_any(api_url, tid)
162
+ except:
163
+ pass
164
+ prompt = f"{qtxt}\n\n---\nFile at: {fp}\n---\n" if fp else qtxt
165
+ ans = safe_run(agent_wrapper.agent, prompt, timeout=20.0)
166
+ if not ans:
167
+ ans = "N/A"
168
+ answers.append({"task_id": tid, "submitted_answer": ans})
169
+ log.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
170
+
171
+ if not answers:
172
+ return "Agent produced no answers.", pd.DataFrame(log)
173
+
174
+ submission = {"username": username.strip(), "agent_code": "", "answers": answers}
 
 
 
 
 
 
175
  try:
176
+ r2 = requests.post(submit_url, json=submission, timeout=60)
177
+ r2.raise_for_status()
178
+ res = r2.json()
179
  status = (
180
  f"Submission Successful!\n"
181
+ f"User: {res.get('username')}\n"
182
+ f"Score: {res.get('score')}% "
183
+ f"({res.get('correct_count')}/{res.get('total_attempted')})"
184
  )
185
  except Exception as e:
186
  status = f"Submission Failed: {e}"
187
 
188
+ return status, pd.DataFrame(log)
189
 
190
+ # ──────────────────────────────────────────────────────────────────────────────
191
+ # 8) Gradio UI
192
+ # ──────────────────────────────────────────────────────────────────────────────
193
  with gr.Blocks() as demo:
194
  gr.Markdown("# GAIA Agent Evaluation Runner")
195
+ gr.Markdown("Enter your HF username and click **Run & Submit**")
196
+ username = gr.Textbox(label="Username", placeholder="huggingface.co username")
197
+ runbtn = gr.Button("Run & Submit")
198
+ status = gr.Textbox(label="Status", interactive=False)
199
  table = gr.DataFrame(label="Results")
200
 
201
+ runbtn.click(fn=run_and_submit_all, inputs=[username], outputs=[status, table])
202
 
203
  if __name__ == "__main__":
 
204
  demo.launch(debug=True, share=False)
205
 
206