Thanh Vinh Vo commited on
Commit
689c0dd
·
1 Parent(s): 86bb81c
Files changed (1) hide show
  1. app.py +64 -2
app.py CHANGED
@@ -128,6 +128,66 @@ def audio_to_text(file_path: str) -> str:
128
  return json.dumps({"transcript": transcript_data})
129
 
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  @tool
132
  def get_file(question_id: str, file_name: str) -> str:
133
  """
@@ -195,6 +255,7 @@ class BasicAgent:
195
  get_file,
196
  audio_to_text,
197
  WikipediaSearchTool(),
 
198
  ],
199
  model=OpenAIServerModel(model_id="gpt-4o"),
200
  additional_authorized_imports=[
@@ -212,7 +273,8 @@ class BasicAgent:
212
  "cv2",
213
  "numpy",
214
  "whisper",
215
- "openpyxl" "json",
 
216
  "wikipediaapi",
217
  ],
218
  name="multimodal_agent",
@@ -229,7 +291,7 @@ class BasicAgent:
229
  model=InferenceClientModel(
230
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
231
  ),
232
- tools=[get_file, audio_to_text],
233
  managed_agents=[self.multimodal_agent],
234
  additional_authorized_imports=[
235
  "requests",
 
128
  return json.dumps({"transcript": transcript_data})
129
 
130
 
131
+ @tool
132
+ def get_wikipedia_page_url_by_year(wikipedia_page_name: str, year: int) -> str:
133
+ """
134
+ Retrieve Wikipedia page URL for a specific year (latest revision in that year).
135
+
136
+ Args:
137
+ wikipedia_page_name (str): Name of the Wikipedia page
138
+ year (int): Year to get the page content from
139
+
140
+ Returns:
141
+ str: URL of the Wikipedia page from that year with revision included
142
+ """
143
+ import requests
144
+ import wikipediaapi
145
+
146
+ # Create Wikipedia API instance
147
+ wiki = wikipediaapi.Wikipedia(
148
+ user_agent="Final Project Agent Course (vthanhvinh@gmail.com)", language="en"
149
+ )
150
+
151
+ # Get the page
152
+ page = wiki.page(wikipedia_page_name)
153
+ if not page.exists():
154
+ raise ValueError(f"Wikipedia page '{wikipedia_page_name}' does not exist")
155
+
156
+ # Use Wikipedia API to get revisions from the specified year
157
+ api_url = "https://en.wikipedia.org/w/api.php"
158
+
159
+ # Get the latest revision from the specified year
160
+ params = {
161
+ "action": "query",
162
+ "format": "json",
163
+ "prop": "revisions",
164
+ "titles": wikipedia_page_name,
165
+ "rvprop": "ids|timestamp",
166
+ "rvstart": f"{year}-12-31T23:59:59Z",
167
+ "rvend": f"{year}-01-01T00:00:00Z",
168
+ "rvdir": "newer",
169
+ "rvlimit": 1,
170
+ }
171
+
172
+ response = requests.get(api_url, params=params)
173
+ data = response.json()
174
+
175
+ pages = data["query"]["pages"]
176
+ page_id = list(pages.keys())[0]
177
+ revisions = pages[page_id].get("revisions", [])
178
+
179
+ if not revisions:
180
+ raise ValueError(
181
+ f"No revisions found for '{wikipedia_page_name}' in year {year}"
182
+ )
183
+
184
+ # Get revision ID and construct URL
185
+ rev_id = revisions[0]["revid"]
186
+ url = f"https://en.wikipedia.org/w/index.php?title={wikipedia_page_name}&oldid={rev_id}"
187
+
188
+ return url
189
+
190
+
191
  @tool
192
  def get_file(question_id: str, file_name: str) -> str:
193
  """
 
255
  get_file,
256
  audio_to_text,
257
  WikipediaSearchTool(),
258
+ get_wikipedia_page_url_by_year,
259
  ],
260
  model=OpenAIServerModel(model_id="gpt-4o"),
261
  additional_authorized_imports=[
 
273
  "cv2",
274
  "numpy",
275
  "whisper",
276
+ "openpyxl",
277
+ "json",
278
  "wikipediaapi",
279
  ],
280
  name="multimodal_agent",
 
291
  model=InferenceClientModel(
292
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
293
  ),
294
+ tools=[get_file, audio_to_text, get_wikipedia_page_url_by_year],
295
  managed_agents=[self.multimodal_agent],
296
  additional_authorized_imports=[
297
  "requests",