gperdrizet commited on
Commit
4a7d0ab
·
verified ·
1 Parent(s): 5ffb76e
Files changed (1) hide show
  1. app.py +72 -36
app.py CHANGED
@@ -1,22 +1,32 @@
 
1
  '''HuggingFace Agents course final project GAIA agent benchmark'''
2
 
 
3
  import os
 
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
- from smolagents import (
8
- CodeAgent,
9
- DuckDuckGoSearchTool,
10
- InferenceClientModel,
11
- VisitWebpageTool
12
- )
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
16
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
17
 
18
- INSTRUCTIONS = '''
19
- YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.'''
 
 
 
 
 
 
20
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -41,19 +51,27 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
41
 
42
  # 1. Instantiate Agent ( modify this part to create your agent)
43
  try:
44
- model = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
 
 
 
45
  agent = CodeAgent(
46
- tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
47
  model=model
48
  )
49
 
50
- except Exception as e: # pyline: disable=W0703
51
  print(f"Error instantiating agent: {e}")
52
  return f"Error initializing agent: {e}", None
53
 
54
- # In the case of an app running as a hugging Face space, this link points toward your
55
- # codebase ( useful for others so please keep it public)
56
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
57
  print(agent_code)
58
 
59
  # 2. Fetch Questions
@@ -78,8 +96,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
78
  except requests.exceptions.RequestException as e:
79
  print(f"Error fetching questions: {e}")
80
  return f"Error fetching questions: {e}", None
81
-
82
- except Exception as e:
83
  print(f"An unexpected error occurred fetching questions: {e}")
84
  return f"An unexpected error occurred fetching questions: {e}", None
85
 
@@ -110,9 +128,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
110
  "Submitted Answer": submitted_answer
111
  })
112
 
113
- except Exception as e:
114
- print(f"Error running agent on task {task_id}: {e}")
115
- results_log.append({
116
  "Task ID": task_id,
117
  "Question": question_text,
118
  "Submitted Answer": f"AGENT ERROR: {e}"
@@ -122,9 +140,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
122
  print("Agent did not produce any answers to submit.")
123
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
124
 
125
- # 4. Prepare Submission
126
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
127
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
 
 
 
 
 
 
128
  print(status_update)
129
 
130
  # 5. Submit
@@ -137,13 +161,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
137
  f"Submission Successful!\n"
138
  f"User: {result_data.get('username')}\n"
139
  f"Overall Score: {result_data.get('score', 'N/A')}% "
140
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
 
141
  f"Message: {result_data.get('message', 'No message received.')}"
142
  )
143
  print("Submission successful.")
144
  results_df = pd.DataFrame(results_log)
145
  return final_status, results_df
146
-
147
  except requests.exceptions.HTTPError as e:
148
  error_detail = f"Server responded with status {e.response.status_code}."
149
 
@@ -158,20 +183,20 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
158
  print(status_message)
159
  results_df = pd.DataFrame(results_log)
160
  return status_message, results_df
161
-
162
  except requests.exceptions.Timeout:
163
  status_message = "Submission Failed: The request timed out."
164
  print(status_message)
165
  results_df = pd.DataFrame(results_log)
166
  return status_message, results_df
167
-
168
  except requests.exceptions.RequestException as e:
169
  status_message = f"Submission Failed: Network error - {e}"
170
  print(status_message)
171
  results_df = pd.DataFrame(results_log)
172
  return status_message, results_df
173
-
174
- except Exception as e:
175
  status_message = f"An unexpected error occurred during submission: {e}"
176
  print(status_message)
177
  results_df = pd.DataFrame(results_log)
@@ -185,14 +210,21 @@ with gr.Blocks() as demo:
185
  """
186
  **Instructions:**
187
 
188
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
189
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
190
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
 
 
191
 
192
  ---
193
  **Disclaimers:**
194
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
195
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a separate action or even to answer the questions in async.
 
 
 
 
196
  """
197
  )
198
 
@@ -211,6 +243,7 @@ with gr.Blocks() as demo:
211
 
212
  if __name__ == "__main__":
213
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
214
  # Check for SPACE_HOST and SPACE_ID at startup for information
215
  space_host_startup = os.getenv("SPACE_HOST")
216
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
@@ -226,9 +259,12 @@ if __name__ == "__main__":
226
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
227
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
228
  else:
229
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
 
230
 
231
  print("-"*(60 + len(" App Starting ")) + "\n")
232
 
233
  print("Launching Gradio Interface for Basic Agent Evaluation...")
234
- demo.launch(debug=True, share=False)
 
1
+
2
  '''HuggingFace Agents course final project GAIA agent benchmark'''
3
 
4
+ # Standard library
5
  import os
6
+
7
+ # Third-party
8
  import gradio as gr
9
  import requests
10
  import pandas as pd
11
+
12
+ # Local/Project
13
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool, Tool
14
+ from langchain.agents import load_tools
 
 
15
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
18
+ DEFAULT_API_URL = (
19
+ "https://agents-course-unit4-scoring.hf.space"
20
+ )
21
 
22
+ INSTRUCTIONS = (
23
+ """
24
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.\n"
25
+ "If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.\n"
26
+ "If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.\n"
27
+ "If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
28
+ """
29
+ )
30
 
31
 
32
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
51
 
52
  # 1. Instantiate Agent ( modify this part to create your agent)
53
  try:
54
+
55
+ wikipedia_tool = Tool.from_langchain(
56
+ load_tools(["wikipedia"])[0]
57
+ )
58
+ model = InferenceClientModel(
59
+ "Qwen/Qwen2.5-Coder-32B-Instruct"
60
+ )
61
  agent = CodeAgent(
62
+ tools=[wikipedia_tool, DuckDuckGoSearchTool(), VisitWebpageTool()],
63
  model=model
64
  )
65
 
66
+ except Exception as e: # pylint: disable=W0703
67
  print(f"Error instantiating agent: {e}")
68
  return f"Error initializing agent: {e}", None
69
 
70
+ # In the case of an app running as a hugging Face space, this link points toward your
71
+ # codebase (useful for others so please keep it public)
72
+ agent_code = (
73
+ f"https://huggingface.co/spaces/{space_id}/tree/main"
74
+ )
75
  print(agent_code)
76
 
77
  # 2. Fetch Questions
 
96
  except requests.exceptions.RequestException as e:
97
  print(f"Error fetching questions: {e}")
98
  return f"Error fetching questions: {e}", None
99
+
100
+ except Exception as e: # pylint: disable=W0703
101
  print(f"An unexpected error occurred fetching questions: {e}")
102
  return f"An unexpected error occurred fetching questions: {e}", None
103
 
 
128
  "Submitted Answer": submitted_answer
129
  })
130
 
131
+ except Exception as e: # pylint: disable=W0703
132
+ print(f"Error running agent on task {task_id}: {e}")
133
+ results_log.append({
134
  "Task ID": task_id,
135
  "Question": question_text,
136
  "Submitted Answer": f"AGENT ERROR: {e}"
 
140
  print("Agent did not produce any answers to submit.")
141
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
142
 
143
+ # 4. Prepare Submission
144
+ submission_data = {
145
+ "username": username.strip(),
146
+ "agent_code": agent_code,
147
+ "answers": answers_payload
148
+ }
149
+ status_update = (
150
+ f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
151
+ )
152
  print(status_update)
153
 
154
  # 5. Submit
 
161
  f"Submission Successful!\n"
162
  f"User: {result_data.get('username')}\n"
163
  f"Overall Score: {result_data.get('score', 'N/A')}% "
164
+ f"({result_data.get('correct_count', '?')}/"
165
+ f"{result_data.get('total_attempted', '?')} correct)\n"
166
  f"Message: {result_data.get('message', 'No message received.')}"
167
  )
168
  print("Submission successful.")
169
  results_df = pd.DataFrame(results_log)
170
  return final_status, results_df
171
+
172
  except requests.exceptions.HTTPError as e:
173
  error_detail = f"Server responded with status {e.response.status_code}."
174
 
 
183
  print(status_message)
184
  results_df = pd.DataFrame(results_log)
185
  return status_message, results_df
186
+
187
  except requests.exceptions.Timeout:
188
  status_message = "Submission Failed: The request timed out."
189
  print(status_message)
190
  results_df = pd.DataFrame(results_log)
191
  return status_message, results_df
192
+
193
  except requests.exceptions.RequestException as e:
194
  status_message = f"Submission Failed: Network error - {e}"
195
  print(status_message)
196
  results_df = pd.DataFrame(results_log)
197
  return status_message, results_df
198
+
199
+ except Exception as e: # pylint: disable=W0703
200
  status_message = f"An unexpected error occurred during submission: {e}"
201
  print(status_message)
202
  results_df = pd.DataFrame(results_log)
 
210
  """
211
  **Instructions:**
212
 
213
+ 1. Please clone this space, then modify the code to define your agent's logic,
214
+ the tools, the necessary packages, etc ...
215
+ 2. Log in to your Hugging Face account using the button below. This uses your
216
+ HF username for submission.
217
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your
218
+ agent, submit answers, and see the score.
219
 
220
  ---
221
  **Disclaimers:**
222
+ Once clicking on the "submit" button, it can take quite some time (this is the
223
+ time for the agent to go through all the questions).
224
+ This space provides a basic setup and is intentionally sub-optimal to encourage
225
+ you to develop your own, more robust solution. For instance, for the delay process
226
+ of the submit button, a solution could be to cache the answers and submit in a
227
+ separate action or even to answer the questions in async.
228
  """
229
  )
230
 
 
243
 
244
  if __name__ == "__main__":
245
  print("\n" + "-"*30 + " App Starting " + "-"*30)
246
+
247
  # Check for SPACE_HOST and SPACE_ID at startup for information
248
  space_host_startup = os.getenv("SPACE_HOST")
249
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
 
259
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
260
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
261
  else:
262
+ print(
263
+ "ℹ️ SPACE_ID environment variable not found (running locally?)." \
264
+ "Repo URL cannot be determined."
265
+ )
266
 
267
  print("-"*(60 + len(" App Starting ")) + "\n")
268
 
269
  print("Launching Gradio Interface for Basic Agent Evaluation...")
270
+ demo.launch(debug=True, share=False)