AnhLee0 commited on
Commit
ab873b7
·
verified ·
1 Parent(s): 5c8db48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -158
app.py CHANGED
@@ -1,158 +1,44 @@
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
- from duckduckgo_search import DDGS
6
- import re
7
 
 
 
 
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
 
12
  class BasicAgent:
13
  def __init__(self):
14
- self.ddg_search = DDGS()
15
- self.api_url = DEFAULT_API_URL
16
- print("BasicAgent initialized with DDGS search.")
17
-
18
- def search_web(self, query: str) -> str:
19
- try:
20
- results = self.ddg_search.text(keywords=query, max_results=3)
21
- if results:
22
- return " ".join(result["body"] for result in results)
23
- return "No results found."
24
- except Exception as e:
25
- return f"Search error: {e}"
26
-
27
- def get_file(self, task_id: str) -> str:
28
- try:
29
- file_url = f"{self.api_url}/files/{task_id}"
30
- response = requests.get(file_url, timeout=15)
31
- response.raise_for_status()
32
- return response.text
33
- except requests.exceptions.RequestException as e:
34
- print(f"Error fetching file for task {task_id}: {e}")
35
- return "Error fetching file."
36
-
37
- def __call__(self, task_id: str, question: str) -> str:
38
  print(f"Agent received question (first 50 chars): {question[:50]}...")
39
- try:
40
- file_content = self.get_file(task_id)
41
- print(f"File content for task {task_id}: {file_content[:100]}...")
42
-
43
- # 1. Mercedes Sosa albums (2000-2009)
44
- if task_id == "8e867cd7-cff9-4e6c-867a-ff5ddc2550be":
45
- return "5" # Đã xác định: Misa Criolla, Voz y Sentimiento, Corazón Libre, Cantora 1, Cantora 2
46
-
47
- # 2. Số loài chim trong video
48
- if task_id == "a1e91b78-d3d8-4675-bb8d-62741b4b68a6":
49
- search_result = self.search_web("highest number of bird species in video L1vXCYZAYYM")
50
- numbers = re.findall(r"\b\d+\b", search_result)
51
- return numbers[0] if numbers else "2" # Giả định một số hợp lý
52
-
53
- # 3. Đảo ngược câu
54
- if task_id == "2d83110e-a098-4ebb-9987-066c06fa42d0":
55
- return "right"
56
-
57
- # 4. Nước đi cờ vua
58
- if task_id == "cca530fc-4052-43b2-b130-b30968d8aa44":
59
- # Giả định nước đi chiếu tướng cơ bản
60
- return "Qe8" # Một nước đi giả định (cần phân tích bàn cờ thực tế)
61
-
62
- # 5. Người đề cử bài viết Wikipedia
63
- if task_id == "4fc2f1ae-8625-45b5-ab34-ad4433bc21f8":
64
- return "FunkMonk" # Dựa trên lịch sử Wikipedia
65
-
66
- # 6. Toán tử không giao hoán
67
- if task_id == "6f37996b-2ac7-44b0-8e68-6d28256631b4":
68
- # Phân tích bảng: a*b = b, b*a = c (không giao hoán), v.v.
69
- return "a, b, c, d, e"
70
-
71
- # 7. Teal'c trong video
72
- if task_id == "9d191bce-651d-4746-be2d-7ef8ecadb9c2":
73
- return "Indeed" # Dựa trên Stargate SG-1
74
-
75
- # 8. Bác sĩ thú y
76
- if task_id == "cabe07ed-9eca-40ea-8ead-410ef5e83f91":
77
- return "Smith" # Dựa trên tài liệu LibreTexts
78
 
79
- # 9. Rau củ
80
- if task_id == "3cef3a44-215e-4aed-8e3b-b1e3f08063b7":
81
- return "broccoli, celery, fresh basil, green beans, lettuce, sweet potatoes, zucchini"
82
 
83
- # 10. Nguyên liệu làm bánh
84
- if task_id == "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3":
85
- return "lemon juice, ripe strawberries, salt, sugar" # Giả định
 
 
 
 
86
 
87
- # 11. Diễn viên trong Magda M.
88
- if task_id == "305ac316-eef6-4446-960a-92d80d542f82":
89
- search_result = self.search_web("actor who played Ray in Polish Everybody Loves Raymond in Magda M")
90
- return "Jacek" # Giả định dựa trên tìm kiếm
91
-
92
- # 12. Output mã Python
93
- if task_id == "f918266a-b3e0-4914-865d-4faa564f1aef":
94
- return "42" # Giả định (cần phân tích mã Python thực tế)
95
-
96
- # 13. Số lần đánh bóng (Yankees 1977)
97
- if task_id == "3f57289b-8c60-48be-bd80-01f8099ca449":
98
- search_result = self.search_web("Yankee with most walks 1977 regular season at bats")
99
- numbers = re.findall(r"\b\d+\b", search_result)
100
- return numbers[0] if numbers else "500" # Giả định
101
-
102
- # 14. Số trang bài tập
103
- if task_id == "1f975693-876d-457b-a649-393859e79bf3":
104
- return "10, 15, 20" # Giả định (cần file âm thanh)
105
-
106
- # 15. NASA award number
107
- if task_id == "840bfca7-4f7b-481a-8794-c560c340185d":
108
- search_result = self.search_web("R. G. Arendt NASA award number Universe Today June 6 2023")
109
- return "NNX17AJ88G" # Dựa trên bài báo
110
-
111
- # 16. Thành phố lưu trữ mẫu vật
112
- if task_id == "bda648d7-d618-4883-88f4-3466eabd860e":
113
- return "Hanoi" # Dựa trên bài báo của Nedoshivina
114
-
115
- # 17. Quốc gia ít vận động viên nhất 1928 Olympics
116
- if task_id == "cf106601-ab4f-4af9-b045-5295fe67b37d":
117
- return "MON" # Monaco, 2 vận động viên (ít nhất, xếp theo thứ tự alphabet)
118
-
119
- # 18. Pitchers trước và sau Taishō Tamai
120
- if task_id == "a0c07678-e491-4bbc-8f0b-07405144218f":
121
- return "Suzuki, Tanaka" # Giả định (cần dữ liệu thực tế)
122
-
123
- # 19. Tổng doanh thu từ thực phẩm
124
- if task_id == "7bd855d8-463d-4ed5-93ca-5fe35145f733":
125
- return "1500.00" # Giả định (cần file Excel)
126
-
127
- # 20. Người nhận Malko Competition
128
- if task_id == "5a0c1adf-205e-4841-a666-7c3ef95def9d":
129
- return "Vladimir" # Vladimir Verbitsky (USSR, sau 1977)
130
-
131
- # Các câu hỏi khác: Tìm kiếm thông tin chung
132
- search_result = self.search_web(question)
133
- if file_content != "Error fetching file.":
134
- search_result += " " + file_content
135
- answer = self.extract_short_answer(search_result)
136
- return answer
137
-
138
- except Exception as e:
139
- print(f"Error processing question: {e}")
140
- return "Error answering question."
141
-
142
- def extract_short_answer(self, text: str) -> str:
143
- numbers = re.findall(r"\b\d+\b", text)
144
- if numbers:
145
- return numbers[0]
146
- words = text.split()
147
- for word in words:
148
- if word[0].isupper() or len(word) < 10:
149
- return word
150
- return "Unknown"
151
-
152
- def run_and_submit_all(profile: gr.OAuthProfile | None):
153
- space_id = os.getenv("SPACE_ID")
154
  if profile:
155
- username = f"{profile.username}"
156
  print(f"User logged in: {username}")
157
  else:
158
  print("User not logged in.")
@@ -162,35 +48,38 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
162
  questions_url = f"{api_url}/questions"
163
  submit_url = f"{api_url}/submit"
164
 
 
165
  try:
166
  agent = BasicAgent()
167
  except Exception as e:
168
  print(f"Error instantiating agent: {e}")
169
  return f"Error initializing agent: {e}", None
170
-
171
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
172
  print(agent_code)
173
 
 
174
  print(f"Fetching questions from: {questions_url}")
175
  try:
176
  response = requests.get(questions_url, timeout=15)
177
  response.raise_for_status()
178
  questions_data = response.json()
179
  if not questions_data:
180
- print("Fetched questions list is empty.")
181
- return "Fetched questions list is empty or invalid format.", None
182
  print(f"Fetched {len(questions_data)} questions.")
183
  except requests.exceptions.RequestException as e:
184
  print(f"Error fetching questions: {e}")
185
  return f"Error fetching questions: {e}", None
186
  except requests.exceptions.JSONDecodeError as e:
187
- print(f"Error decoding JSON response from questions endpoint: {e}")
188
- print(f"Response text: {response.text[:500]}")
189
- return f"Error decoding server response for questions: {e}", None
190
  except Exception as e:
191
  print(f"An unexpected error occurred fetching questions: {e}")
192
  return f"An unexpected error occurred fetching questions: {e}", None
193
 
 
194
  results_log = []
195
  answers_payload = []
196
  print(f"Running agent on {len(questions_data)} questions...")
@@ -201,21 +90,23 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
201
  print(f"Skipping item with missing task_id or question: {item}")
202
  continue
203
  try:
204
- submitted_answer = agent(task_id, question_text)
205
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
206
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
207
  except Exception as e:
208
- print(f"Error running agent on task {task_id}: {e}")
209
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
210
 
211
  if not answers_payload:
212
  print("Agent did not produce any answers to submit.")
213
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
214
 
 
215
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
216
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
217
  print(status_update)
218
 
 
219
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
220
  try:
221
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -258,22 +149,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
258
  results_df = pd.DataFrame(results_log)
259
  return status_message, results_df
260
 
 
261
  # --- Build Gradio Interface using Blocks ---
262
  with gr.Blocks() as demo:
263
  gr.Markdown("# Basic Agent Evaluation Runner")
264
  gr.Markdown(
265
  """
266
  **Instructions:**
267
-
268
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
269
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
270
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
271
-
272
  ---
273
  **Disclaimers:**
274
- 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).
275
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
276
- 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.
277
  """
278
  )
279
 
@@ -282,6 +171,7 @@ with gr.Blocks() as demo:
282
  run_button = gr.Button("Run Evaluation & Submit All Answers")
283
 
284
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
285
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
286
 
287
  run_button.click(
@@ -291,8 +181,9 @@ with gr.Blocks() as demo:
291
 
292
  if __name__ == "__main__":
293
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
294
  space_host_startup = os.getenv("SPACE_HOST")
295
- space_id_startup = os.getenv("SPACE_ID")
296
 
297
  if space_host_startup:
298
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -300,7 +191,7 @@ if __name__ == "__main__":
300
  else:
301
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
302
 
303
- if space_id_startup:
304
  print(f"✅ SPACE_ID found: {space_id_startup}")
305
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
306
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
1
  import os
2
  import gradio as gr
3
  import requests
4
+ import inspect
5
  import pandas as pd
 
 
6
 
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
9
+
10
+ # (Keep Constants as is)
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
+ self.agent = CodeAgent(
19
+ model=OpenAIServerModel(model_id="gpt-4o-mini"),
20
+ tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()],
21
+ add_base_tools=True,
22
+ )
23
+ print("BasicAgent initialized.")
24
+ def __call__(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  print(f"Agent received question (first 50 chars): {question[:50]}...")
26
+ # fixed_answer = "This is a default answer."
27
+ fixed_answer = self.agent.run(question)
28
+ print(f"Agent returning fixed answer: {fixed_answer}")
29
+ return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
31
 
32
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
33
+ """
34
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
35
+ and displays the results.
36
+ """
37
+ # --- Determine HF Space Runtime URL and Repo URL ---
38
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  if profile:
41
+ username= f"{profile.username}"
42
  print(f"User logged in: {username}")
43
  else:
44
  print("User not logged in.")
 
48
  questions_url = f"{api_url}/questions"
49
  submit_url = f"{api_url}/submit"
50
 
51
+ # 1. Instantiate Agent ( modify this part to create your agent)
52
  try:
53
  agent = BasicAgent()
54
  except Exception as e:
55
  print(f"Error instantiating agent: {e}")
56
  return f"Error initializing agent: {e}", None
57
+ # 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)
58
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
59
  print(agent_code)
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()
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
81
 
82
+ # 3. Run your Agent
83
  results_log = []
84
  answers_payload = []
85
  print(f"Running agent on {len(questions_data)} questions...")
 
90
  print(f"Skipping item with missing task_id or question: {item}")
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
 
109
+ # 5. Submit
110
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
111
  try:
112
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
149
  results_df = pd.DataFrame(results_log)
150
  return status_message, results_df
151
 
152
+
153
  # --- Build Gradio Interface using Blocks ---
154
  with gr.Blocks() as demo:
155
  gr.Markdown("# Basic Agent Evaluation Runner")
156
  gr.Markdown(
157
  """
158
  **Instructions:**
159
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
160
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
161
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
 
162
  ---
163
  **Disclaimers:**
164
+ 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).
165
+ 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.
 
166
  """
167
  )
168
 
 
171
  run_button = gr.Button("Run Evaluation & Submit All Answers")
172
 
173
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
174
+ # Removed max_rows=10 from DataFrame constructor
175
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
176
 
177
  run_button.click(
 
181
 
182
  if __name__ == "__main__":
183
  print("\n" + "-"*30 + " App Starting " + "-"*30)
184
+ # Check for SPACE_HOST and SPACE_ID at startup for information
185
  space_host_startup = os.getenv("SPACE_HOST")
186
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
187
 
188
  if space_host_startup:
189
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
191
  else:
192
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
193
 
194
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
195
  print(f"✅ SPACE_ID found: {space_id_startup}")
196
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
197
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")