GionaZardini commited on
Commit
fc98b43
·
verified ·
1 Parent(s): d7b14ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -141
app.py CHANGED
@@ -1,139 +1,56 @@
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
- # --- Basic Agent Definition ---
14
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
-
16
- from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool
17
-
18
- # --- Basic Agent Definition ---
19
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
20
-
21
-
22
- class BasicAgent:
23
- def __init__(self):
24
- print("BasicAgent initialized.")
25
-
26
- self.model = InferenceClientModel(
27
- model_id="Qwen/Qwen2.5-7B-Instruct",
28
- token=os.getenv("HF_TOKEN"),
29
- )
30
-
31
- self.agent = CodeAgent(
32
- tools=[
33
- DuckDuckGoSearchTool(),
34
- VisitWebpageTool(),
35
- ],
36
- model=self.model,
37
- add_base_tools=True,
38
- max_steps=6,
39
- verbosity_level=1,
40
- )
41
-
42
- def __call__(self, question: str) -> str:
43
- print(f"Agent received question: {question}")
44
-
45
- prompt = f"""
46
- You are solving a GAIA-style benchmark task.
47
-
48
- Use tools when needed.
49
- Search the web for obscure factual questions.
50
- Use Python for calculations, tables, files, dates, counting, and exact transformations.
51
-
52
- Return only the final answer.
53
- No explanation.
54
- No markdown.
55
- No full sentence unless the question explicitly asks for one.
56
- Preserve exact requested formatting.
57
-
58
- Question:
59
- {question}
60
- """
61
-
62
- try:
63
- answer = self.agent.run(prompt)
64
- answer = str(answer).strip()
65
-
66
- prefixes = [
67
- "Final answer:",
68
- "Final Answer:",
69
- "Answer:",
70
- "The answer is",
71
- "The final answer is",
72
- ]
73
-
74
- for prefix in prefixes:
75
- if answer.startswith(prefix):
76
- answer = answer[len(prefix):].strip()
77
-
78
- answer = answer.strip().strip('"').strip("'").strip()
79
- print(f"Agent returning answer: {answer}")
80
- return answer
81
-
82
- except Exception as e:
83
- print(f"Agent error: {e}")
84
- return f"AGENT ERROR: {e}"
85
-
86
- def answer_question(self, question: str, task_id: str | None = None) -> str:
87
- return self.__call__(question)
88
-
89
-
90
- def run_and_submit_all( profile: gr.OAuthProfile | None):
91
- """
92
- Fetches all questions, runs the BasicAgent on them, submits all answers,
93
- and displays the results.
94
- """
95
- # --- Determine HF Space Runtime URL and Repo URL ---
96
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
97
-
98
- username = "GionaZardini"
99
  print(f"Using username: {username}")
100
 
101
  api_url = DEFAULT_API_URL
102
  questions_url = f"{api_url}/questions"
103
  submit_url = f"{api_url}/submit"
104
 
105
- # 1. Instantiate Agent ( modify this part to create your agent)
106
  try:
107
  agent = BasicAgent()
108
  except Exception as e:
109
  print(f"Error instantiating agent: {e}")
110
  return f"Error initializing agent: {e}", None
111
- # 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)
112
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
113
  print(agent_code)
114
 
115
- # 2. Fetch Questions
116
  print(f"Fetching questions from: {questions_url}")
117
  try:
118
  response = requests.get(questions_url, timeout=15)
119
  response.raise_for_status()
120
  questions_data = response.json()
121
  if not questions_data:
122
- print("Fetched questions list is empty.")
123
- return "Fetched questions list is empty or invalid format.", None
124
  print(f"Fetched {len(questions_data)} questions.")
125
  except requests.exceptions.RequestException as e:
126
  print(f"Error fetching questions: {e}")
127
  return f"Error fetching questions: {e}", None
128
  except requests.exceptions.JSONDecodeError as e:
129
- print(f"Error decoding JSON response from questions endpoint: {e}")
130
- print(f"Response text: {response.text[:500]}")
131
- return f"Error decoding server response for questions: {e}", None
132
  except Exception as e:
133
  print(f"An unexpected error occurred fetching questions: {e}")
134
  return f"An unexpected error occurred fetching questions: {e}", None
135
 
136
- # 3. Run your Agent
137
  results_log = []
138
  answers_payload = []
139
  print(f"Running agent on {len(questions_data)} questions...")
@@ -144,23 +61,30 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
144
  print(f"Skipping item with missing task_id or question: {item}")
145
  continue
146
  try:
147
- submitted_answer = agent(question_text)
148
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
149
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
150
  except Exception as e:
151
- print(f"Error running agent on task {task_id}: {e}")
152
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
153
 
 
154
  if not answers_payload:
155
  print("Agent did not produce any answers to submit.")
156
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
157
 
158
- # 4. Prepare Submission
159
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
160
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
161
  print(status_update)
162
 
163
- # 5. Submit
164
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
165
  try:
166
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -174,7 +98,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
174
  f"Message: {result_data.get('message', 'No message received.')}"
175
  )
176
  print("Submission successful.")
177
- results_df = pd.DataFrame(results_log)
178
  return final_status, results_df
179
  except requests.exceptions.HTTPError as e:
180
  error_detail = f"Server responded with status {e.response.status_code}."
@@ -185,76 +108,66 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
185
  error_detail += f" Response: {e.response.text[:500]}"
186
  status_message = f"Submission Failed: {error_detail}"
187
  print(status_message)
188
- results_df = pd.DataFrame(results_log)
189
  return status_message, results_df
190
  except requests.exceptions.Timeout:
191
  status_message = "Submission Failed: The request timed out."
192
  print(status_message)
193
- results_df = pd.DataFrame(results_log)
194
  return status_message, results_df
195
  except requests.exceptions.RequestException as e:
196
  status_message = f"Submission Failed: Network error - {e}"
197
  print(status_message)
198
- results_df = pd.DataFrame(results_log)
199
  return status_message, results_df
200
  except Exception as e:
201
  status_message = f"An unexpected error occurred during submission: {e}"
202
  print(status_message)
203
- results_df = pd.DataFrame(results_log)
204
  return status_message, results_df
205
 
206
 
207
- # --- Build Gradio Interface using Blocks ---
208
  with gr.Blocks() as demo:
209
  gr.Markdown("# Basic Agent Evaluation Runner")
210
  gr.Markdown(
211
  """
212
  **Instructions:**
213
 
214
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
215
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
216
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
217
 
218
  ---
219
  **Disclaimers:**
220
- 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).
221
- 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 seperate action or even to answer the questions in async.
222
  """
223
  )
224
 
225
  gr.LoginButton()
226
-
227
  run_button = gr.Button("Run Evaluation & Submit All Answers")
228
-
229
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
230
- # Removed max_rows=10 from DataFrame constructor
231
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
232
 
233
  run_button.click(
234
  fn=run_and_submit_all,
235
- outputs=[status_output, results_table]
236
  )
237
 
 
238
  if __name__ == "__main__":
239
- print("\n" + "-"*30 + " App Starting " + "-"*30)
240
- # Check for SPACE_HOST and SPACE_ID at startup for information
241
  space_host_startup = os.getenv("SPACE_HOST")
242
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
243
 
244
  if space_host_startup:
245
- print(f"SPACE_HOST found: {space_host_startup}")
246
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
247
  else:
248
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
249
 
250
- if space_id_startup: # Print repo URLs if SPACE_ID is found
251
- print(f"SPACE_ID found: {space_id_startup}")
252
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
253
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
254
  else:
255
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
256
-
257
- print("-"*(60 + len(" App Starting ")) + "\n")
258
 
 
259
  print("Launching Gradio Interface for Basic Agent Evaluation...")
260
  demo.launch(debug=True, share=False)
 
1
+ "Final answer:",
2
+ "Final Answer:",
3
+ "FINAL ANSWER:",
4
+ "Answer:",
5
+ "The answer is",
6
+ "The final answer is",
7
+ ]
8
+ for prefix in prefixes:
9
+ if answer.startswith(prefix):
10
+ answer = answer[len(prefix):].strip()
11
+
12
+ answer = answer.strip().strip('"').strip("'").strip()
13
+ return answer
14
+
15
+
16
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
17
+ space_id = os.getenv("SPACE_ID")
18
+ username = profile.username if profile and profile.username else os.getenv("HF_USERNAME", "GionaZardini")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  print(f"Using username: {username}")
20
 
21
  api_url = DEFAULT_API_URL
22
  questions_url = f"{api_url}/questions"
23
  submit_url = f"{api_url}/submit"
24
 
 
25
  try:
26
  agent = BasicAgent()
27
  except Exception as e:
28
  print(f"Error instantiating agent: {e}")
29
  return f"Error initializing agent: {e}", None
30
+
31
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
32
  print(agent_code)
33
 
 
34
  print(f"Fetching questions from: {questions_url}")
35
  try:
36
  response = requests.get(questions_url, timeout=15)
37
  response.raise_for_status()
38
  questions_data = response.json()
39
  if not questions_data:
40
+ print("Fetched questions list is empty.")
41
+ return "Fetched questions list is empty or invalid format.", None
42
  print(f"Fetched {len(questions_data)} questions.")
43
  except requests.exceptions.RequestException as e:
44
  print(f"Error fetching questions: {e}")
45
  return f"Error fetching questions: {e}", None
46
  except requests.exceptions.JSONDecodeError as e:
47
+ print(f"Error decoding JSON response from questions endpoint: {e}")
48
+ print(f"Response text: {response.text[:500]}")
49
+ return f"Error decoding server response for questions: {e}", None
50
  except Exception as e:
51
  print(f"An unexpected error occurred fetching questions: {e}")
52
  return f"An unexpected error occurred fetching questions: {e}", None
53
 
 
54
  results_log = []
55
  answers_payload = []
56
  print(f"Running agent on {len(questions_data)} questions...")
 
61
  print(f"Skipping item with missing task_id or question: {item}")
62
  continue
63
  try:
64
+ submitted_answer = agent.answer_question(question_text, task_id=task_id)
65
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
66
+ results_log.append(
67
+ {"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}
68
+ )
69
  except Exception as e:
70
+ print(f"Error running agent on task {task_id}: {e}")
71
+ results_log.append(
72
+ {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}
73
+ )
74
 
75
+ results_df = pd.DataFrame(results_log)
76
  if not answers_payload:
77
  print("Agent did not produce any answers to submit.")
78
+ return "Agent did not produce any answers to submit.", results_df
79
 
80
+ submission_data = {
81
+ "username": username.strip(),
82
+ "agent_code": agent_code,
83
+ "answers": answers_payload,
84
+ }
85
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
86
  print(status_update)
87
 
 
88
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
89
  try:
90
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
98
  f"Message: {result_data.get('message', 'No message received.')}"
99
  )
100
  print("Submission successful.")
 
101
  return final_status, results_df
102
  except requests.exceptions.HTTPError as e:
103
  error_detail = f"Server responded with status {e.response.status_code}."
 
108
  error_detail += f" Response: {e.response.text[:500]}"
109
  status_message = f"Submission Failed: {error_detail}"
110
  print(status_message)
 
111
  return status_message, results_df
112
  except requests.exceptions.Timeout:
113
  status_message = "Submission Failed: The request timed out."
114
  print(status_message)
 
115
  return status_message, results_df
116
  except requests.exceptions.RequestException as e:
117
  status_message = f"Submission Failed: Network error - {e}"
118
  print(status_message)
 
119
  return status_message, results_df
120
  except Exception as e:
121
  status_message = f"An unexpected error occurred during submission: {e}"
122
  print(status_message)
 
123
  return status_message, results_df
124
 
125
 
 
126
  with gr.Blocks() as demo:
127
  gr.Markdown("# Basic Agent Evaluation Runner")
128
  gr.Markdown(
129
  """
130
  **Instructions:**
131
 
132
+ 1. Clone this space, then modify the code to define your agent's logic, tools, and packages.
133
+ 2. Log in to your Hugging Face account using the button below.
134
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
135
 
136
  ---
137
  **Disclaimers:**
138
+ Once clicking on the submit button, it can take quite some time.
 
139
  """
140
  )
141
 
142
  gr.LoginButton()
 
143
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
144
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
145
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
146
 
147
  run_button.click(
148
  fn=run_and_submit_all,
149
+ outputs=[status_output, results_table],
150
  )
151
 
152
+
153
  if __name__ == "__main__":
154
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
 
155
  space_host_startup = os.getenv("SPACE_HOST")
156
+ space_id_startup = os.getenv("SPACE_ID")
157
 
158
  if space_host_startup:
159
+ print(f"SPACE_HOST found: {space_host_startup}")
160
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
161
  else:
162
+ print("SPACE_HOST environment variable not found (running locally?).")
163
 
164
+ if space_id_startup:
165
+ print(f"SPACE_ID found: {space_id_startup}")
166
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
167
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
168
  else:
169
+ print("SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
170
 
171
+ print("-" * (60 + len(" App Starting ")) + "\n")
172
  print("Launching Gradio Interface for Basic Agent Evaluation...")
173
  demo.launch(debug=True, share=False)