gperdrizet commited on
Commit
5ffb76e
·
verified ·
1 Parent(s): a4af6d1

Added CodeAgent with web search and scraping tools, added GAIA prompt.

Browse files
Files changed (1) hide show
  1. app.py +62 -24
app.py CHANGED
@@ -1,23 +1,23 @@
 
 
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -30,6 +30,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
30
  if profile:
31
  username= f"{profile.username}"
32
  print(f"User logged in: {username}")
 
33
  else:
34
  print("User not logged in.")
35
  return "Please Login to Hugging Face with the button.", None
@@ -40,31 +41,44 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
- agent = BasicAgent()
44
- except Exception as e:
 
 
 
 
 
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
47
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
 
 
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
50
 
51
  # 2. Fetch Questions
52
  print(f"Fetching questions from: {questions_url}")
 
53
  try:
54
  response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
56
  questions_data = response.json()
 
57
  if not questions_data:
58
- print("Fetched questions list is empty.")
59
- return "Fetched questions list is empty or invalid format.", None
 
60
  print(f"Fetched {len(questions_data)} questions.")
 
 
 
 
 
 
61
  except requests.exceptions.RequestException as e:
62
  print(f"Error fetching questions: {e}")
63
  return f"Error fetching questions: {e}", None
64
- except requests.exceptions.JSONDecodeError as e:
65
- print(f"Error decoding JSON response from questions endpoint: {e}")
66
- print(f"Response text: {response.text[:500]}")
67
- return f"Error decoding server response for questions: {e}", None
68
  except Exception as e:
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -72,20 +86,37 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
72
  # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []
 
75
  print(f"Running agent on {len(questions_data)} questions...")
 
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
 
79
  if not task_id or question_text is None:
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
 
82
  try:
83
- submitted_answer = agent(question_text)
 
 
 
 
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
86
  except Exception as e:
87
  print(f"Error running agent on task {task_id}: {e}")
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
89
 
90
  if not answers_payload:
91
  print("Agent did not produce any answers to submit.")
@@ -112,27 +143,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
112
  print("Submission successful.")
113
  results_df = pd.DataFrame(results_log)
114
  return final_status, results_df
 
115
  except requests.exceptions.HTTPError as e:
116
  error_detail = f"Server responded with status {e.response.status_code}."
 
117
  try:
118
  error_json = e.response.json()
119
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
 
120
  except requests.exceptions.JSONDecodeError:
121
  error_detail += f" Response: {e.response.text[:500]}"
 
122
  status_message = f"Submission Failed: {error_detail}"
123
  print(status_message)
124
  results_df = pd.DataFrame(results_log)
125
  return status_message, results_df
 
126
  except requests.exceptions.Timeout:
127
  status_message = "Submission Failed: The request timed out."
128
  print(status_message)
129
  results_df = pd.DataFrame(results_log)
130
  return status_message, results_df
 
131
  except requests.exceptions.RequestException as e:
132
  status_message = f"Submission Failed: Network error - {e}"
133
  print(status_message)
134
  results_df = pd.DataFrame(results_log)
135
  return status_message, results_df
 
136
  except Exception as e:
137
  status_message = f"An unexpected error occurred during submission: {e}"
138
  print(status_message)
@@ -166,7 +204,7 @@ with gr.Blocks() as demo:
166
  # Removed max_rows=10 from DataFrame constructor
167
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
168
 
169
- run_button.click(
170
  fn=run_and_submit_all,
171
  outputs=[status_output, results_table]
172
  )
 
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):
23
  """
 
30
  if profile:
31
  username= f"{profile.username}"
32
  print(f"User logged in: {username}")
33
+
34
  else:
35
  print("User not logged in.")
36
  return "Please Login to Hugging Face with the button.", 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
60
  print(f"Fetching questions from: {questions_url}")
61
+
62
  try:
63
  response = requests.get(questions_url, timeout=15)
64
  response.raise_for_status()
65
  questions_data = response.json()
66
+
67
  if not questions_data:
68
+ print("Fetched questions list is empty.")
69
+ return "Fetched questions list is empty or invalid format.", None
70
+
71
  print(f"Fetched {len(questions_data)} questions.")
72
+
73
+ except requests.exceptions.JSONDecodeError as e:
74
+ print(f"Error decoding JSON response from questions endpoint: {e}")
75
+ print(f"Response text: {response.text[:500]}")
76
+ return f"Error decoding server response for questions: {e}", None
77
+
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
 
86
  # 3. Run your Agent
87
  results_log = []
88
  answers_payload = []
89
+
90
  print(f"Running agent on {len(questions_data)} questions...")
91
+
92
  for item in questions_data:
93
  task_id = item.get("task_id")
94
  question_text = item.get("question")
95
+
96
  if not task_id or question_text is None:
97
  print(f"Skipping item with missing task_id or question: {item}")
98
  continue
99
+
100
  try:
101
+ submitted_answer = agent.run(
102
+ INSTRUCTIONS,
103
+ additional_args={'user_prompt': question_text}
104
+ )
105
+
106
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
107
+ results_log.append({
108
+ "Task ID": task_id,
109
+ "Question": question_text,
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}"
119
+ })
120
 
121
  if not answers_payload:
122
  print("Agent did not produce any answers to submit.")
 
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
+
150
  try:
151
  error_json = e.response.json()
152
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
153
+
154
  except requests.exceptions.JSONDecodeError:
155
  error_detail += f" Response: {e.response.text[:500]}"
156
+
157
  status_message = f"Submission Failed: {error_detail}"
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)
 
204
  # Removed max_rows=10 from DataFrame constructor
205
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
206
 
207
+ run_button.click( # pylint: disable=E1101
208
  fn=run_and_submit_all,
209
  outputs=[status_output, results_table]
210
  )