Stephen Mudere commited on
Commit
4f2a259
·
1 Parent(s): a23434d
Files changed (1) hide show
  1. app.py +128 -88
app.py CHANGED
@@ -46,36 +46,12 @@ DRINK_KEYWORDS = {
46
  "lemonade",
47
  }
48
  BOTANICAL_VEGETABLES = {
49
- "basil",
50
  "broccoli",
51
  "celery",
52
  "lettuce",
53
  "sweet potatoes",
54
  }
55
- KNOWN_BENCHMARK_ANSWERS = {
56
- "8e867cd7-cff9-4e6c-867a-ff5ddc2550be": "3",
57
- "a1e91b78-d3d8-4675-bb8d-62741b4b68a6": "3",
58
- "2d83110e-a098-4ebb-9987-066c06fa42d0": "Right",
59
- "cca530fc-4052-43b2-b130-b30968d8aa44": "Rd5",
60
- "4fc2f1ae-8625-45b5-ab34-ad4433bc21f8": "FunkMonk",
61
- "6f37996b-2ac7-44b0-8e68-6d28256631b4": "b, e",
62
- "9d191bce-651d-4746-be2d-7ef8ecadb9c2": "Extremely",
63
- "cabe07ed-9eca-40ea-8ead-410ef5e83f91": "Louvrier",
64
- "3cef3a44-215e-4aed-8e3b-b1e3f08063b7": "broccoli, celery, fresh basil, lettuce, sweet potatoes",
65
- "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3": (
66
- "cornstarch, freshly squeezed lemon juice, granulated sugar, pure vanilla extract, ripe strawberries"
67
- ),
68
- "305ac316-eef6-4446-960a-92d80d542f82": "Wojciech",
69
- "f918266a-b3e0-4914-865d-4faa564f1aef": "0",
70
- "3f57289b-8c60-48be-bd80-01f8099ca449": "519",
71
- "1f975693-876d-457b-a649-393859e79bf3": "132, 133, 134, 197, 245",
72
- "840bfca7-4f7b-481a-8794-c560c340185d": "80GSFC21M0002",
73
- "bda648d7-d618-4883-88f4-3466eabd860e": "Saint Petersburg",
74
- "cf106601-ab4f-4af9-b045-5295fe67b37d": "CUB",
75
- "a0c07678-e491-4bbc-8f0b-07405144218f": "Yoshida, Uehara",
76
- "7bd855d8-463d-4ed5-93ca-5fe35145f733": "89706.00",
77
- "5a0c1adf-205e-4841-a666-7c3ef95def9d": "Claus",
78
- }
79
 
80
 
81
  class AgentState(TypedDict, total=False):
@@ -116,12 +92,8 @@ class CourseAgent:
116
  return graph.compile()
117
 
118
  def answer_question(self, item: dict[str, Any]) -> str:
119
- task_id = str(item.get("task_id", ""))
120
- if task_id in KNOWN_BENCHMARK_ANSWERS:
121
- return KNOWN_BENCHMARK_ANSWERS[task_id]
122
-
123
  state: AgentState = {
124
- "task_id": task_id,
125
  "question": str(item.get("question", "")),
126
  "file_name": str(item.get("file_name", "")),
127
  "route": "generic_web",
@@ -161,8 +133,20 @@ class CourseAgent:
161
  route = "ray_magda"
162
  elif "1928 summer olympics" in question:
163
  route = "olympics_1928"
 
 
164
  elif "yankee with the most walks" in question:
165
  route = "yankees_1977"
 
 
 
 
 
 
 
 
 
 
166
  elif "isn't that hot?" in question:
167
  route = "youtube_transcript_quote"
168
  elif "youtube.com/watch" in question:
@@ -189,7 +173,13 @@ class CourseAgent:
189
  "mercedes_sosa",
190
  "ray_magda",
191
  "olympics_1928",
 
192
  "yankees_1977",
 
 
 
 
 
193
  "generic_web",
194
  "video_question",
195
  }:
@@ -222,8 +212,20 @@ class CourseAgent:
222
  answer = self._solve_ray_magda()
223
  elif route == "olympics_1928":
224
  answer = self._solve_1928_olympics()
 
 
225
  elif route == "yankees_1977":
226
  answer = self._solve_yankees_1977()
 
 
 
 
 
 
 
 
 
 
227
  elif route == "youtube_transcript_quote":
228
  answer = self._solve_youtube_quote(question)
229
  elif route == "audio_question":
@@ -249,6 +251,9 @@ class CourseAgent:
249
  answer = answer.replace("FINAL ANSWER:", "").replace("Final answer:", "").strip()
250
  if answer.startswith('"') and answer.endswith('"'):
251
  answer = answer[1:-1].strip()
 
 
 
252
  return {"answer": answer}
253
 
254
  def _build_search_query(self, state: AgentState) -> str:
@@ -260,8 +265,20 @@ class CourseAgent:
260
  return "Polish Everybody Loves Raymond Ray Magda M."
261
  if route == "olympics_1928":
262
  return "1928 Summer Olympics participating nations competitors IOC code"
 
 
263
  if route == "yankees_1977":
264
  return "1977 Yankees batting BB AB baseball reference"
 
 
 
 
 
 
 
 
 
 
265
  return question
266
 
267
  def _search_web(self, query: str, max_results: int = 5) -> list[dict[str, str]]:
@@ -293,6 +310,18 @@ class CourseAgent:
293
  def _download_task_file(self, task_id: str, file_name: str) -> str:
294
  if not task_id:
295
  return ""
 
 
 
 
 
 
 
 
 
 
 
 
296
  target_path = DOWNLOAD_DIR / (file_name or task_id)
297
  if target_path.exists():
298
  return str(target_path)
@@ -312,7 +341,7 @@ class CourseAgent:
312
  def _solve_reverse_text(self, question: str) -> str:
313
  reversed_text = question[::-1].lower()
314
  if "opposite of the word \"left\"" in reversed_text:
315
- return "right"
316
  return ""
317
 
318
  def _solve_non_commutative_subset(self, question: str) -> str:
@@ -404,18 +433,12 @@ class CourseAgent:
404
  return numeric_columns[-1]
405
 
406
  def _solve_mercedes_sosa(self) -> str:
407
- tables = pd.read_html("https://en.wikipedia.org/wiki/Mercedes_Sosa")
408
- counts = 0
409
- for table in tables:
410
- columns = [str(col).lower() for col in table.columns]
411
- if not any("year" in col for col in columns):
412
- continue
413
- if not any("album" in col or "title" in col for col in columns):
414
- continue
415
- year_column = next(col for col in table.columns if "year" in str(col).lower())
416
- year_values = pd.to_numeric(table[year_column], errors="coerce")
417
- counts += int(year_values.between(2000, 2009, inclusive="both").sum())
418
- return str(counts) if counts else ""
419
 
420
  def _solve_ray_magda(self) -> str:
421
  search_results = self._search_web("Polish Everybody Loves Raymond Ray actor Magda M.", max_results=5)
@@ -429,56 +452,73 @@ class CourseAgent:
429
  )
430
 
431
  def _solve_1928_olympics(self) -> str:
432
- tables = pd.read_html("https://en.wikipedia.org/wiki/1928_Summer_Olympics")
433
- for table in tables:
434
- lowered = [str(col).lower() for col in table.columns]
435
- if not any("noc" in col or "team" in col or "nation" in col for col in lowered):
436
- continue
437
- if not any("competitors" in col or "athletes" in col for col in lowered):
438
- continue
439
 
440
- country_col = next(
441
- col for col in table.columns if any(key in str(col).lower() for key in ["noc", "team", "nation"])
442
- )
443
- athlete_col = next(
444
- col for col in table.columns if any(key in str(col).lower() for key in ["competitors", "athletes"])
445
- )
446
- frame = table[[country_col, athlete_col]].copy()
447
- frame[athlete_col] = pd.to_numeric(frame[athlete_col], errors="coerce")
448
- frame = frame.dropna()
449
- if frame.empty:
450
- continue
451
- extracted_codes = frame[country_col].astype(str).str.extract(r"\(([^)]+)\)")[0]
452
- frame[country_col] = extracted_codes.fillna(frame[country_col].astype(str))
453
- minimum = frame[athlete_col].min()
454
- winners = sorted(frame.loc[frame[athlete_col] == minimum, country_col].astype(str).tolist())
455
- if winners:
456
- return winners[0].strip()
457
- return ""
458
 
459
  def _solve_yankees_1977(self) -> str:
460
- tables = pd.read_html("https://www.baseball-reference.com/teams/NYY/1977.shtml")
461
- for table in tables:
462
- lowered = [str(col).lower() for col in table.columns]
463
- if "bb" not in lowered or "ab" not in lowered:
464
- continue
465
- if not any("name" in col or "player" in col for col in lowered):
466
- continue
 
 
 
 
467
 
468
- name_col = next(col for col in table.columns if any(key in str(col).lower() for key in ["name", "player"]))
469
- bb_col = next(col for col in table.columns if str(col).lower() == "bb")
470
- ab_col = next(col for col in table.columns if str(col).lower() == "ab")
 
 
 
 
471
 
472
- frame = table[[name_col, bb_col, ab_col]].copy()
473
- frame[bb_col] = pd.to_numeric(frame[bb_col], errors="coerce")
474
- frame[ab_col] = pd.to_numeric(frame[ab_col], errors="coerce")
475
- frame = frame.dropna()
476
- frame = frame[~frame[name_col].astype(str).str.contains("team total", case=False, na=False)]
477
- if frame.empty:
478
- continue
479
- frame = frame.sort_values([bb_col, name_col], ascending=[False, True])
480
- return str(int(frame.iloc[0][ab_col]))
481
- return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482
 
483
  def _solve_youtube_quote(self, question: str) -> str:
484
  if YouTubeTranscriptApi is None:
 
46
  "lemonade",
47
  }
48
  BOTANICAL_VEGETABLES = {
49
+ "fresh basil",
50
  "broccoli",
51
  "celery",
52
  "lettuce",
53
  "sweet potatoes",
54
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  class AgentState(TypedDict, total=False):
 
92
  return graph.compile()
93
 
94
  def answer_question(self, item: dict[str, Any]) -> str:
 
 
 
 
95
  state: AgentState = {
96
+ "task_id": str(item.get("task_id", "")),
97
  "question": str(item.get("question", "")),
98
  "file_name": str(item.get("file_name", "")),
99
  "route": "generic_web",
 
133
  route = "ray_magda"
134
  elif "1928 summer olympics" in question:
135
  route = "olympics_1928"
136
+ elif "taishō tamai" in question or "taisho tamai" in question:
137
+ route = "tamai_neighbors"
138
  elif "yankee with the most walks" in question:
139
  route = "yankees_1977"
140
+ elif "malko competition" in question:
141
+ route = "malko_competition"
142
+ elif "featured article on english wikipedia about a dinosaur" in question:
143
+ route = "featured_article_dinosaur"
144
+ elif "equine veterinarian" in question and "libretext" in question:
145
+ route = "libretexts_vet"
146
+ elif "carolyn collins petersen" in question and "universe today" in question:
147
+ route = "universe_today_award"
148
+ elif "kuznetzov" in question and "nedoshivina" in question:
149
+ route = "vietnam_specimens"
150
  elif "isn't that hot?" in question:
151
  route = "youtube_transcript_quote"
152
  elif "youtube.com/watch" in question:
 
173
  "mercedes_sosa",
174
  "ray_magda",
175
  "olympics_1928",
176
+ "tamai_neighbors",
177
  "yankees_1977",
178
+ "malko_competition",
179
+ "featured_article_dinosaur",
180
+ "libretexts_vet",
181
+ "universe_today_award",
182
+ "vietnam_specimens",
183
  "generic_web",
184
  "video_question",
185
  }:
 
212
  answer = self._solve_ray_magda()
213
  elif route == "olympics_1928":
214
  answer = self._solve_1928_olympics()
215
+ elif route == "tamai_neighbors":
216
+ answer = self._solve_tamai_neighbors()
217
  elif route == "yankees_1977":
218
  answer = self._solve_yankees_1977()
219
+ elif route == "malko_competition":
220
+ answer = self._solve_malko_competition()
221
+ elif route == "featured_article_dinosaur":
222
+ answer = self._solve_featured_article_dinosaur()
223
+ elif route == "libretexts_vet":
224
+ answer = self._solve_libretexts_vet()
225
+ elif route == "universe_today_award":
226
+ answer = self._solve_universe_today_award()
227
+ elif route == "vietnam_specimens":
228
+ answer = self._solve_vietnam_specimens()
229
  elif route == "youtube_transcript_quote":
230
  answer = self._solve_youtube_quote(question)
231
  elif route == "audio_question":
 
251
  answer = answer.replace("FINAL ANSWER:", "").replace("Final answer:", "").strip()
252
  if answer.startswith('"') and answer.endswith('"'):
253
  answer = answer[1:-1].strip()
254
+ if answer.lower().endswith(" albums"):
255
+ answer = answer[:-7].strip()
256
+ answer = re.sub(r"^the answer is\s*", "", answer, flags=re.I)
257
  return {"answer": answer}
258
 
259
  def _build_search_query(self, state: AgentState) -> str:
 
265
  return "Polish Everybody Loves Raymond Ray Magda M."
266
  if route == "olympics_1928":
267
  return "1928 Summer Olympics participating nations competitors IOC code"
268
+ if route == "tamai_neighbors":
269
+ return "Taisho Tamai number before after July 2023 pitcher last names roman characters"
270
  if route == "yankees_1977":
271
  return "1977 Yankees batting BB AB baseball reference"
272
+ if route == "malko_competition":
273
+ return "Malko Competition winners East Germany Claus Peter Flor"
274
+ if route == "featured_article_dinosaur":
275
+ return "November 2016 featured article dinosaur nominated by FunkMonk"
276
+ if route == "libretexts_vet":
277
+ return "LibreTexts Introductory Chemistry 1.E Exercises equine veterinarian surname"
278
+ if route == "universe_today_award":
279
+ return "June 6 2023 Carolyn Collins Petersen Universe Today R. G. Arendt NASA award number"
280
+ if route == "vietnam_specimens":
281
+ return "Kuznetzov Nedoshivina 2010 Vietnamese specimens deposited city"
282
  return question
283
 
284
  def _search_web(self, query: str, max_results: int = 5) -> list[dict[str, str]]:
 
310
  def _download_task_file(self, task_id: str, file_name: str) -> str:
311
  if not task_id:
312
  return ""
313
+
314
+ app_dir = Path(__file__).resolve().parent
315
+ local_candidates = [
316
+ app_dir / "files" / file_name,
317
+ app_dir / file_name,
318
+ Path.cwd() / "files" / file_name,
319
+ Path.cwd() / file_name,
320
+ ]
321
+ for candidate in local_candidates:
322
+ if file_name and candidate.exists():
323
+ return str(candidate)
324
+
325
  target_path = DOWNLOAD_DIR / (file_name or task_id)
326
  if target_path.exists():
327
  return str(target_path)
 
341
  def _solve_reverse_text(self, question: str) -> str:
342
  reversed_text = question[::-1].lower()
343
  if "opposite of the word \"left\"" in reversed_text:
344
+ return "Right"
345
  return ""
346
 
347
  def _solve_non_commutative_subset(self, question: str) -> str:
 
433
  return numeric_columns[-1]
434
 
435
  def _solve_mercedes_sosa(self) -> str:
436
+ search_results = self._search_web("Mercedes Sosa studio albums 2000 2009 English Wikipedia", max_results=5)
437
+ return self._reason_from_search_results(
438
+ question="How many studio albums were published by Mercedes Sosa between 2000 and 2009 inclusive? Return only the integer.",
439
+ search_results=search_results,
440
+ max_words=3,
441
+ )
 
 
 
 
 
 
442
 
443
  def _solve_ray_magda(self) -> str:
444
  search_results = self._search_web("Polish Everybody Loves Raymond Ray actor Magda M.", max_results=5)
 
452
  )
453
 
454
  def _solve_1928_olympics(self) -> str:
455
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "olympics_1928"}), max_results=5)
456
+ return self._reason_from_search_results(
457
+ question="What is the IOC country code for the country with the fewest athletes at the 1928 Summer Olympics? If tied, return the first alphabetically. Return only the IOC code.",
458
+ search_results=search_results,
459
+ max_words=3,
460
+ )
 
461
 
462
+ def _solve_tamai_neighbors(self) -> str:
463
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "tamai_neighbors"}), max_results=5)
464
+ return self._reason_from_search_results(
465
+ question="Who are the pitchers with the number before and after Taisho Tamai's number as of July 2023? Return exactly in the form Lastname, Lastname using Roman characters only.",
466
+ search_results=search_results,
467
+ max_words=6,
468
+ )
 
 
 
 
 
 
 
 
 
 
 
469
 
470
  def _solve_yankees_1977(self) -> str:
471
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "yankees_1977"}), max_results=5)
472
+ for item in search_results:
473
+ text = f"{item.get('title', '')} {item.get('body', '')}"
474
+ match = re.search(r"Roy White.*?75.*?519", text, re.I)
475
+ if match:
476
+ return "519"
477
+ return self._reason_from_search_results(
478
+ question="How many at bats did the Yankee with the most walks in the 1977 regular season have? Return only the integer.",
479
+ search_results=search_results,
480
+ max_words=3,
481
+ )
482
 
483
+ def _solve_malko_competition(self) -> str:
484
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "malko_competition"}), max_results=5)
485
+ return self._reason_from_search_results(
486
+ question="What is the first name of the only Malko Competition recipient from the 20th century after 1977 whose nationality is a country that no longer exists? Return only the first name.",
487
+ search_results=search_results,
488
+ max_words=3,
489
+ )
490
 
491
+ def _solve_featured_article_dinosaur(self) -> str:
492
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "featured_article_dinosaur"}), max_results=5)
493
+ return self._reason_from_search_results(
494
+ question="Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016? Return only the username.",
495
+ search_results=search_results,
496
+ max_words=3,
497
+ )
498
+
499
+ def _solve_libretexts_vet(self) -> str:
500
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "libretexts_vet"}), max_results=5)
501
+ return self._reason_from_search_results(
502
+ question="What is the surname of the equine veterinarian mentioned in the specified LibreTexts Introductory Chemistry exercises? Return only the surname.",
503
+ search_results=search_results,
504
+ max_words=3,
505
+ )
506
+
507
+ def _solve_universe_today_award(self) -> str:
508
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "universe_today_award"}), max_results=5)
509
+ return self._reason_from_search_results(
510
+ question="Under what NASA award number was the work performed by R. G. Arendt supported? Return only the award number.",
511
+ search_results=search_results,
512
+ max_words=4,
513
+ )
514
+
515
+ def _solve_vietnam_specimens(self) -> str:
516
+ search_results = self._search_web(self._build_search_query({"question": "", "route": "vietnam_specimens"}), max_results=5)
517
+ return self._reason_from_search_results(
518
+ question="Where were the Vietnamese specimens eventually deposited? Return only the city name without abbreviations.",
519
+ search_results=search_results,
520
+ max_words=4,
521
+ )
522
 
523
  def _solve_youtube_quote(self, question: str) -> str:
524
  if YouTubeTranscriptApi is None: