Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,40 @@ import pandas as pd
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Basic Agent Definition ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
| 13 |
self.math_tool = MathTool()
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Basic Agent Definition ---
|
| 11 |
+
import requests
|
| 12 |
+
import re
|
| 13 |
+
from math import sqrt
|
| 14 |
+
|
| 15 |
+
class MathTool:
|
| 16 |
+
def __call__(self, question: str) -> str:
|
| 17 |
+
try:
|
| 18 |
+
allowed = {"sqrt": sqrt}
|
| 19 |
+
result = eval(question, {"__builtins__": None}, allowed)
|
| 20 |
+
return str(result)
|
| 21 |
+
except:
|
| 22 |
+
return "I can't compute this."
|
| 23 |
+
|
| 24 |
+
class WikipediaTool:
|
| 25 |
+
def __call__(self, query: str) -> str:
|
| 26 |
+
try:
|
| 27 |
+
# Step 1: Use Wikipedia search
|
| 28 |
+
search_url = f"https://en.wikipedia.org/w/rest.php/v1/search/page?q={requests.utils.quote(query)}&limit=1"
|
| 29 |
+
res = requests.get(search_url, timeout=5).json()
|
| 30 |
+
if not res.get("pages"):
|
| 31 |
+
return "N/A"
|
| 32 |
+
|
| 33 |
+
title = res["pages"][0]["title"]
|
| 34 |
+
summary_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.quote(title)}"
|
| 35 |
+
summary = requests.get(summary_url, timeout=5).json()
|
| 36 |
+
extract = summary.get("extract", "")
|
| 37 |
+
|
| 38 |
+
# Step 2: Extract short fact — e.g. first date, number, or name
|
| 39 |
+
match = re.search(r'\b([A-Z][a-z]+(?:\s[A-Z][a-z]+)?)\b', extract)
|
| 40 |
+
return match.group(1) if match else extract.split(".")[0]
|
| 41 |
+
|
| 42 |
+
except Exception:
|
| 43 |
+
return "N/A"
|
| 44 |
+
|
| 45 |
class BasicAgent:
|
| 46 |
def __init__(self):
|
| 47 |
self.math_tool = MathTool()
|