claudi47 commited on
Commit
5850246
·
1 Parent(s): ad427ea

added reading files from GAIA

Browse files
Files changed (1) hide show
  1. app.py +177 -32
app.py CHANGED
@@ -3,7 +3,14 @@ import requests
3
  import pandas as pd
4
  import gradio as gr
5
  from dotenv import load_dotenv
6
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, WikipediaSearchTool
 
 
 
 
 
 
 
7
 
8
  load_dotenv()
9
 
@@ -11,8 +18,86 @@ load_dotenv()
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  class BasicAgent:
17
  def __init__(self):
18
  print("BasicAgent initialized.")
@@ -20,28 +105,67 @@ class BasicAgent:
20
  model_id="Qwen/Qwen2.5-72B-Instruct",
21
  token=os.getenv("HF_TOKEN"),
22
  )
 
 
 
 
 
23
  self.agent = CodeAgent(
24
  model=model,
25
- tools=[DuckDuckGoSearchTool(), WikipediaSearchTool(user_agent="BasicAgent/1.0")],
26
- max_steps=8,
 
 
 
 
 
27
  verbosity_level=0,
 
 
 
 
 
 
 
 
 
 
 
28
  )
29
 
30
- def __call__(self, question: str) -> str:
31
- return str(self.agent.run(question))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
-
34
 
35
- def run_and_submit_all( profile: gr.OAuthProfile | None):
36
  """
37
  Fetches all questions, runs the BasicAgent on them, submits all answers,
38
  and displays the results.
39
  """
40
  # --- Determine HF Space Runtime URL and Repo URL ---
41
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
42
 
43
  if profile:
44
- username= f"{profile.username}"
45
  print(f"User logged in: {username}")
46
  else:
47
  print("User not logged in.")
@@ -68,16 +192,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
68
  response.raise_for_status()
69
  questions_data = response.json()
70
  if not questions_data:
71
- print("Fetched questions list is empty.")
72
- return "Fetched questions list is empty or invalid format.", None
73
  print(f"Fetched {len(questions_data)} questions.")
74
  except requests.exceptions.RequestException as e:
75
  print(f"Error fetching questions: {e}")
76
  return f"Error fetching questions: {e}", None
77
  except requests.exceptions.JSONDecodeError as e:
78
- print(f"Error decoding JSON response from questions endpoint: {e}")
79
- print(f"Response text: {response.text[:500]}")
80
- return f"Error decoding server response for questions: {e}", None
81
  except Exception as e:
82
  print(f"An unexpected error occurred fetching questions: {e}")
83
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -94,18 +218,36 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
94
  continue
95
  try:
96
  submitted_answer = agent(question_text)
97
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
98
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
99
  except Exception as e:
100
- print(f"Error running agent on task {task_id}: {e}")
101
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
102
 
103
  if not answers_payload:
104
  print("Agent did not produce any answers to submit.")
105
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
106
 
107
- # 4. Prepare Submission
108
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
109
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
110
  print(status_update)
111
 
@@ -175,20 +317,19 @@ with gr.Blocks() as demo:
175
 
176
  run_button = gr.Button("Run Evaluation & Submit All Answers")
177
 
178
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
 
179
  # Removed max_rows=10 from DataFrame constructor
180
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
181
 
182
- run_button.click(
183
- fn=run_and_submit_all,
184
- outputs=[status_output, results_table]
185
- )
186
 
187
  if __name__ == "__main__":
188
- print("\n" + "-"*30 + " App Starting " + "-"*30)
189
  # Check for SPACE_HOST and SPACE_ID at startup for information
190
  space_host_startup = os.getenv("SPACE_HOST")
191
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
192
 
193
  if space_host_startup:
194
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -196,14 +337,18 @@ if __name__ == "__main__":
196
  else:
197
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
198
 
199
- if space_id_startup: # Print repo URLs if SPACE_ID is found
200
  print(f"✅ SPACE_ID found: {space_id_startup}")
201
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
202
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
 
203
  else:
204
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
205
 
206
- print("-"*(60 + len(" App Starting ")) + "\n")
207
 
208
  print("Launching Gradio Interface for Basic Agent Evaluation...")
209
- demo.launch(debug=True, share=False)
 
3
  import pandas as pd
4
  import gradio as gr
5
  from dotenv import load_dotenv
6
+ from smolagents import (
7
+ CodeAgent,
8
+ DuckDuckGoSearchTool,
9
+ InferenceClientModel,
10
+ WikipediaSearchTool,
11
+ VisitWebpageTool,
12
+ Tool,
13
+ )
14
 
15
  load_dotenv()
16
 
 
18
  # --- Constants ---
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
 
21
+ # Format instructions appended to every question
22
+ # so that the agent returns exact-match-friendly
23
+ # answers via final_answer().
24
+ ANSWER_FORMAT_INSTRUCTIONS = """
25
+
26
+ IMPORTANT FORMAT INSTRUCTIONS:
27
+ Your final_answer must be as concise as possible:
28
+ - If the answer is a number, return ONLY the number
29
+ (no units, no commas, no $ or % unless asked).
30
+ - If the answer is a string, return ONLY the
31
+ essential words (no articles like "the"/"a",
32
+ no abbreviations for cities, write digits in
33
+ plain text unless told otherwise).
34
+ - If the answer is a comma separated list, apply
35
+ the rules above to each element.
36
+ Do NOT include explanations in your final_answer,
37
+ just the bare answer.
38
+ """
39
+
40
+
41
  # --- Basic Agent Definition ---
42
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
43
+ # --------------------------------------------------
44
+ # Custom tool: download a GAIA task file
45
+ # --------------------------------------------------
46
+ class GaiaFileFetcherTool(Tool):
47
+ """Downloads the file attached to a GAIA task."""
48
+
49
+ name = "fetch_task_file"
50
+ description = (
51
+ "Downloads the file attached to a GAIA task "
52
+ "given its task_id. Returns the local path "
53
+ "to the downloaded file so you can read it."
54
+ )
55
+ inputs = {
56
+ "task_id": {
57
+ "type": "string",
58
+ "description": (
59
+ "The task_id of the GAIA question "
60
+ "whose attached file you need."
61
+ ),
62
+ }
63
+ }
64
+ output_type = "string"
65
+
66
+ def __init__(self, api_url: str, **kwargs):
67
+ super().__init__(**kwargs)
68
+ self.api_url = api_url
69
+
70
+ def forward(self, task_id: str) -> str:
71
+ import requests as _req
72
+ import tempfile as _tmp
73
+ import mimetypes as _mt
74
+
75
+ url = f"{self.api_url}/files/{task_id}"
76
+ resp = _req.get(url, timeout=30)
77
+ resp.raise_for_status()
78
+
79
+ # Derive a sensible extension from headers
80
+ ct = resp.headers.get("Content-Type", "")
81
+ ext = _mt.guess_extension(ct.split(";")[0]) or ""
82
+
83
+ cd = resp.headers.get(
84
+ "Content-Disposition", ""
85
+ )
86
+ fname = ""
87
+ if "filename=" in cd:
88
+ fname = cd.split("filename=")[-1]
89
+ fname = fname.strip('"').strip("'")
90
+
91
+ if not fname:
92
+ fname = f"{task_id}{ext}"
93
+
94
+ path = os.path.join(
95
+ _tmp.gettempdir(), fname
96
+ )
97
+ with open(path, "wb") as f:
98
+ f.write(resp.content)
99
+ return path
100
+
101
  class BasicAgent:
102
  def __init__(self):
103
  print("BasicAgent initialized.")
 
105
  model_id="Qwen/Qwen2.5-72B-Instruct",
106
  token=os.getenv("HF_TOKEN"),
107
  )
108
+
109
+ self.file_tool = GaiaFileFetcherTool(
110
+ api_url=DEFAULT_API_URL,
111
+ )
112
+
113
  self.agent = CodeAgent(
114
  model=model,
115
+ tools=[
116
+ DuckDuckGoSearchTool(),
117
+ WikipediaSearchTool(user_agent="GaiaAgent/1.0"),
118
+ VisitWebpageTool(),
119
+ self.file_tool,
120
+ ],
121
+ max_steps=15,
122
  verbosity_level=0,
123
+ additional_authorized_imports=[
124
+ "json",
125
+ "re",
126
+ "csv",
127
+ "math",
128
+ "statistics",
129
+ "datetime",
130
+ "collections",
131
+ "itertools",
132
+ "os",
133
+ ],
134
  )
135
 
136
+ def __call__(
137
+ self,
138
+ question: str,
139
+ task_id: str,
140
+ has_file: bool = False,
141
+ ) -> str:
142
+ # Build the prompt for the agent
143
+ prompt = question
144
+
145
+ if has_file:
146
+ prompt += (
147
+ f"\n\n[This question has an attached "
148
+ f"file. Use the fetch_task_file tool "
149
+ f"with task_id='{task_id}' to "
150
+ f"download and read it.]"
151
+ )
152
+
153
+ prompt += ANSWER_FORMAT_INSTRUCTIONS
154
+
155
+ raw = str(self.agent.run(prompt))
156
+ return raw.strip()
157
 
 
158
 
159
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
160
  """
161
  Fetches all questions, runs the BasicAgent on them, submits all answers,
162
  and displays the results.
163
  """
164
  # --- Determine HF Space Runtime URL and Repo URL ---
165
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
166
 
167
  if profile:
168
+ username = f"{profile.username}"
169
  print(f"User logged in: {username}")
170
  else:
171
  print("User not logged in.")
 
192
  response.raise_for_status()
193
  questions_data = response.json()
194
  if not questions_data:
195
+ print("Fetched questions list is empty.")
196
+ return "Fetched questions list is empty or invalid format.", None
197
  print(f"Fetched {len(questions_data)} questions.")
198
  except requests.exceptions.RequestException as e:
199
  print(f"Error fetching questions: {e}")
200
  return f"Error fetching questions: {e}", None
201
  except requests.exceptions.JSONDecodeError as e:
202
+ print(f"Error decoding JSON response from questions endpoint: {e}")
203
+ print(f"Response text: {response.text[:500]}")
204
+ return f"Error decoding server response for questions: {e}", None
205
  except Exception as e:
206
  print(f"An unexpected error occurred fetching questions: {e}")
207
  return f"An unexpected error occurred fetching questions: {e}", None
 
218
  continue
219
  try:
220
  submitted_answer = agent(question_text)
221
+ answers_payload.append(
222
+ {"task_id": task_id, "submitted_answer": submitted_answer}
223
+ )
224
+ results_log.append(
225
+ {
226
+ "Task ID": task_id,
227
+ "Question": question_text,
228
+ "Submitted Answer": submitted_answer,
229
+ }
230
+ )
231
  except Exception as e:
232
+ print(f"Error running agent on task {task_id}: {e}")
233
+ results_log.append(
234
+ {
235
+ "Task ID": task_id,
236
+ "Question": question_text,
237
+ "Submitted Answer": f"AGENT ERROR: {e}",
238
+ }
239
+ )
240
 
241
  if not answers_payload:
242
  print("Agent did not produce any answers to submit.")
243
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
244
 
245
+ # 4. Prepare Submission
246
+ submission_data = {
247
+ "username": username.strip(),
248
+ "agent_code": agent_code,
249
+ "answers": answers_payload,
250
+ }
251
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
252
  print(status_update)
253
 
 
317
 
318
  run_button = gr.Button("Run Evaluation & Submit All Answers")
319
 
320
+ status_output = gr.Textbox(
321
+ label="Run Status / Submission Result", lines=5, interactive=False
322
+ )
323
  # Removed max_rows=10 from DataFrame constructor
324
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
325
 
326
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
327
 
328
  if __name__ == "__main__":
329
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
330
  # Check for SPACE_HOST and SPACE_ID at startup for information
331
  space_host_startup = os.getenv("SPACE_HOST")
332
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
333
 
334
  if space_host_startup:
335
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
337
  else:
338
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
339
 
340
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
341
  print(f"✅ SPACE_ID found: {space_id_startup}")
342
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
343
+ print(
344
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
345
+ )
346
  else:
347
+ print(
348
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
349
+ )
350
 
351
+ print("-" * (60 + len(" App Starting ")) + "\n")
352
 
353
  print("Launching Gradio Interface for Basic Agent Evaluation...")
354
+ demo.launch(debug=True, share=False)