Thanh Vinh Vo commited on
Commit
763ca02
·
1 Parent(s): 2c91267

Do not use BeautifulSoup

Browse files
Files changed (1) hide show
  1. app.py +69 -31
app.py CHANGED
@@ -1,22 +1,30 @@
 
1
  import os
 
2
  import gradio as gr
3
- import requests
4
- import inspect
5
  import pandas as pd
6
- from smolagents import CodeAgent, InferenceClientModel, tool, DuckDuckGoSearchTool, load_tool
 
 
 
 
 
 
 
7
 
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
  class BasicAgent:
16
  def __init__(self):
17
  print("BasicAgent initialized.")
18
  self.agent = CodeAgent(
19
- tools=[],
20
  model=InferenceClientModel(),
21
  additional_authorized_imports=["requests", "bs4"],
22
  max_steps=10,
@@ -24,21 +32,26 @@ class BasicAgent:
24
 
25
  def __call__(self, question: str) -> str:
26
  print(f"Agent received question: {question}")
27
- result = self.agent.run(question)
 
 
 
 
28
  print(f"Agent responded with: {result}")
29
  return result
30
 
 
31
  def run_and_submit_all(questions_limit: str, profile: gr.OAuthProfile | None):
32
  """
33
  Fetches all questions, runs the BasicAgent on them, submits all answers,
34
  and displays the results.
35
  """
36
  # --- Determine HF Space Runtime URL and Repo URL ---
37
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
38
  LIMIT = int(questions_limit)
39
 
40
  if profile:
41
- username= f"{profile.username}"
42
  print(f"User logged in: {username}")
43
  else:
44
  print("User not logged in.")
@@ -60,21 +73,22 @@ def run_and_submit_all(questions_limit: str, profile: gr.OAuthProfile | None):
60
 
61
  # 2. Fetch Questions
62
  print(f"Fetching questions from: {questions_url}")
 
63
  try:
64
  response = requests.get(questions_url, timeout=15)
65
  response.raise_for_status()
66
  questions_data = response.json()[:LIMIT]
67
  if not questions_data:
68
- print("Fetched questions list is empty.")
69
- return "Fetched questions list is empty or invalid format.", None
70
  print(f"Fetched {len(questions_data)} questions.")
 
 
 
 
71
  except requests.exceptions.RequestException as e:
72
  print(f"Error fetching questions: {e}")
73
  return f"Error fetching questions: {e}", None
74
- except requests.exceptions.JSONDecodeError as e:
75
- print(f"Error decoding JSON response from questions endpoint: {e}")
76
- print(f"Response text: {response.text[:500]}")
77
- return f"Error decoding server response for questions: {e}", None
78
  except Exception as e:
79
  print(f"An unexpected error occurred fetching questions: {e}")
80
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -91,18 +105,36 @@ def run_and_submit_all(questions_limit: str, profile: gr.OAuthProfile | None):
91
  continue
92
  try:
93
  submitted_answer = agent(question_text)
94
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
95
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
96
  except Exception as e:
97
- print(f"Error running agent on task {task_id}: {e}")
98
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
99
 
100
  if not answers_payload:
101
  print("Agent did not produce any answers to submit.")
102
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
103
 
104
- # 4. Prepare Submission
105
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
106
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
107
  print(status_update)
108
 
@@ -170,25 +202,27 @@ with gr.Blocks() as demo:
170
 
171
  gr.LoginButton()
172
 
173
- questions_limit = gr.Textbox(label="How many questions to solve", lines=1, interactive=True)
174
-
 
175
  run_button = gr.Button("Run Evaluation & Submit All Answers")
176
-
177
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
178
  # Removed max_rows=10 from DataFrame constructor
179
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
180
 
181
  run_button.click(
182
  fn=run_and_submit_all,
183
  inputs=[questions_limit],
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 +230,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)
 
1
+ import inspect
2
  import os
3
+
4
  import gradio as gr
 
 
5
  import pandas as pd
6
+ import requests
7
+ from smolagents import (
8
+ CodeAgent,
9
+ DuckDuckGoSearchTool,
10
+ InferenceClientModel,
11
+ load_tool,
12
+ tool,
13
+ )
14
 
15
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
+
21
  # --- Basic Agent Definition ---
22
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
23
  class BasicAgent:
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
  self.agent = CodeAgent(
27
+ tools=[],
28
  model=InferenceClientModel(),
29
  additional_authorized_imports=["requests", "bs4"],
30
  max_steps=10,
 
32
 
33
  def __call__(self, question: str) -> str:
34
  print(f"Agent received question: {question}")
35
+ prompt = f"""
36
+ Answer the following question: {question}. Please follow the following rules:
37
+ 1. When there is need to parse HTML please use LLM to extract the relevant information instead of using BeautifulSoup.
38
+ """
39
+ result = self.agent.run(prompt)
40
  print(f"Agent responded with: {result}")
41
  return result
42
 
43
+
44
  def run_and_submit_all(questions_limit: str, profile: gr.OAuthProfile | None):
45
  """
46
  Fetches all questions, runs the BasicAgent on them, submits all answers,
47
  and displays the results.
48
  """
49
  # --- Determine HF Space Runtime URL and Repo URL ---
50
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
51
  LIMIT = int(questions_limit)
52
 
53
  if profile:
54
+ username = f"{profile.username}"
55
  print(f"User logged in: {username}")
56
  else:
57
  print("User not logged in.")
 
73
 
74
  # 2. Fetch Questions
75
  print(f"Fetching questions from: {questions_url}")
76
+ response = None
77
  try:
78
  response = requests.get(questions_url, timeout=15)
79
  response.raise_for_status()
80
  questions_data = response.json()[:LIMIT]
81
  if not questions_data:
82
+ print("Fetched questions list is empty.")
83
+ return "Fetched questions list is empty or invalid format.", None
84
  print(f"Fetched {len(questions_data)} questions.")
85
+ except requests.exceptions.JSONDecodeError as e:
86
+ print(f"Error decoding JSON response from questions endpoint: {e}")
87
+ print(f"Response: {response}")
88
+ return f"Error decoding server response for questions: {e}", None
89
  except requests.exceptions.RequestException as e:
90
  print(f"Error fetching questions: {e}")
91
  return f"Error fetching questions: {e}", None
 
 
 
 
92
  except Exception as e:
93
  print(f"An unexpected error occurred fetching questions: {e}")
94
  return f"An unexpected error occurred fetching questions: {e}", None
 
105
  continue
106
  try:
107
  submitted_answer = agent(question_text)
108
+ answers_payload.append(
109
+ {"task_id": task_id, "submitted_answer": submitted_answer}
110
+ )
111
+ results_log.append(
112
+ {
113
+ "Task ID": task_id,
114
+ "Question": question_text,
115
+ "Submitted Answer": submitted_answer,
116
+ }
117
+ )
118
  except Exception as e:
119
+ print(f"Error running agent on task {task_id}: {e}")
120
+ results_log.append(
121
+ {
122
+ "Task ID": task_id,
123
+ "Question": question_text,
124
+ "Submitted Answer": f"AGENT ERROR: {e}",
125
+ }
126
+ )
127
 
128
  if not answers_payload:
129
  print("Agent did not produce any answers to submit.")
130
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
131
 
132
+ # 4. Prepare Submission
133
+ submission_data = {
134
+ "username": username.strip(),
135
+ "agent_code": agent_code,
136
+ "answers": answers_payload,
137
+ }
138
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
139
  print(status_update)
140
 
 
202
 
203
  gr.LoginButton()
204
 
205
+ questions_limit = gr.Textbox(
206
+ label="How many questions to solve", lines=1, interactive=True, value="1"
207
+ )
208
  run_button = gr.Button("Run Evaluation & Submit All Answers")
209
+ status_output = gr.Textbox(
210
+ label="Run Status / Submission Result", lines=5, interactive=False
211
+ )
212
  # Removed max_rows=10 from DataFrame constructor
213
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
214
 
215
  run_button.click(
216
  fn=run_and_submit_all,
217
  inputs=[questions_limit],
218
+ outputs=[status_output, results_table],
219
  )
220
 
221
  if __name__ == "__main__":
222
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
223
  # Check for SPACE_HOST and SPACE_ID at startup for information
224
  space_host_startup = os.getenv("SPACE_HOST")
225
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
226
 
227
  if space_host_startup:
228
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
230
  else:
231
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
232
 
233
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
234
  print(f"✅ SPACE_ID found: {space_id_startup}")
235
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
236
+ print(
237
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
238
+ )
239
  else:
240
+ print(
241
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
242
+ )
243
 
244
+ print("-" * (60 + len(" App Starting ")) + "\n")
245
 
246
  print("Launching Gradio Interface for Basic Agent Evaluation...")
247
+ demo.launch(debug=True, share=False)