Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ HF_TOKEN = os.getenv("HF_TOKEN", None)
|
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def wiki_get_page(title: str) -> str:
|
|
|
|
| 18 |
API = "https://en.wikipedia.org/w/api.php"
|
| 19 |
params = {"action": "query", "format": "json", "prop": "revisions", "rvprop": "content", "rvslots": "*", "titles": title}
|
| 20 |
data = requests.get(API, params=params, timeout=10).json()
|
|
@@ -23,15 +24,18 @@ def wiki_get_page(title: str) -> str:
|
|
| 23 |
|
| 24 |
@tool
|
| 25 |
def youtube_transcript(video_id: str) -> str:
|
|
|
|
| 26 |
transcript = YouTubeTranscriptApi().fetch_transcript(video_id)
|
| 27 |
return " ".join(t["text"] for t in transcript)
|
| 28 |
|
| 29 |
@tool
|
| 30 |
def reverse_text(text: str) -> str:
|
|
|
|
| 31 |
return text[::-1]
|
| 32 |
|
| 33 |
@tool
|
| 34 |
def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
|
|
|
|
| 35 |
board = chess.Board(fen)
|
| 36 |
engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
|
| 37 |
result = engine.play(board, chess.engine.Limit(time=time_limit))
|
|
@@ -40,6 +44,7 @@ def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
|
|
| 40 |
|
| 41 |
@tool
|
| 42 |
def find_non_commutative(table: dict) -> list:
|
|
|
|
| 43 |
elems = set(x for x,_ in table.keys())
|
| 44 |
bad = set()
|
| 45 |
for x in elems:
|
|
@@ -50,30 +55,27 @@ def find_non_commutative(table: dict) -> list:
|
|
| 50 |
|
| 51 |
@tool
|
| 52 |
def libretext_extract(url: str, selector: str) -> str:
|
|
|
|
| 53 |
r = requests.get(url, timeout=10)
|
| 54 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 55 |
return soup.select_one(selector).get_text(strip=True)
|
| 56 |
|
| 57 |
@tool
|
| 58 |
def classify_vegetables(items: list) -> list:
|
|
|
|
| 59 |
VEGETABLE_SET = {"bell pepper","broccoli","celery","green beans","lettuce","zucchini","sweet potatoes"}
|
| 60 |
return sorted([i for i in items if i in VEGETABLE_SET])
|
| 61 |
|
| 62 |
@tool
|
| 63 |
def execute_code(code: str) -> str:
|
|
|
|
| 64 |
local_ns = {}
|
| 65 |
exec(code, {"__builtins__": {}}, local_ns)
|
| 66 |
return str(local_ns.get("output", ""))
|
| 67 |
|
| 68 |
-
@tool
|
| 69 |
-
def yankee_at_bats_most_walks(year: int) -> int:
|
| 70 |
-
leaders = statsapi.team_leaders("walks", season=year, team=147)
|
| 71 |
-
pid = leaders[0]["id"]
|
| 72 |
-
stats = statsapi.player_stats(pid, "hitting", "season", season=year)
|
| 73 |
-
return stats["batting"][0]["atBats"]
|
| 74 |
-
|
| 75 |
@tool
|
| 76 |
def least_athletes_olympics(year: int) -> str:
|
|
|
|
| 77 |
url = f"https://en.wikipedia.org/wiki/{year}_Summer_Olympics"
|
| 78 |
r = requests.get(url)
|
| 79 |
soup = BeautifulSoup(r.text,"html.parser")
|
|
@@ -86,6 +88,7 @@ def least_athletes_olympics(year: int) -> str:
|
|
| 86 |
|
| 87 |
@tool
|
| 88 |
def get_nasa_award_number(qid: str) -> str:
|
|
|
|
| 89 |
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
|
| 90 |
sparql.setQuery(f'SELECT ?award WHERE {{ wd:{qid} wdt:P496 ?award. }}')
|
| 91 |
sparql.setReturnFormat(JSON)
|
|
|
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def wiki_get_page(title: str) -> str:
|
| 18 |
+
"""Fetch raw wikitext for a given Wikipedia page title"""
|
| 19 |
API = "https://en.wikipedia.org/w/api.php"
|
| 20 |
params = {"action": "query", "format": "json", "prop": "revisions", "rvprop": "content", "rvslots": "*", "titles": title}
|
| 21 |
data = requests.get(API, params=params, timeout=10).json()
|
|
|
|
| 24 |
|
| 25 |
@tool
|
| 26 |
def youtube_transcript(video_id: str) -> str:
|
| 27 |
+
"""Retrieve transcript for a YouTube video ID"""
|
| 28 |
transcript = YouTubeTranscriptApi().fetch_transcript(video_id)
|
| 29 |
return " ".join(t["text"] for t in transcript)
|
| 30 |
|
| 31 |
@tool
|
| 32 |
def reverse_text(text: str) -> str:
|
| 33 |
+
"""Reverse the input string"""
|
| 34 |
return text[::-1]
|
| 35 |
|
| 36 |
@tool
|
| 37 |
def chess_best_move(fen: str, time_limit: float = 0.1) -> str:
|
| 38 |
+
"""Best move in UCI notation for given FEN"""
|
| 39 |
board = chess.Board(fen)
|
| 40 |
engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
|
| 41 |
result = engine.play(board, chess.engine.Limit(time=time_limit))
|
|
|
|
| 44 |
|
| 45 |
@tool
|
| 46 |
def find_non_commutative(table: dict) -> list:
|
| 47 |
+
"""Elements involved in non-commutativity"""
|
| 48 |
elems = set(x for x,_ in table.keys())
|
| 49 |
bad = set()
|
| 50 |
for x in elems:
|
|
|
|
| 55 |
|
| 56 |
@tool
|
| 57 |
def libretext_extract(url: str, selector: str) -> str:
|
| 58 |
+
"""Extract text via CSS selector"""
|
| 59 |
r = requests.get(url, timeout=10)
|
| 60 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 61 |
return soup.select_one(selector).get_text(strip=True)
|
| 62 |
|
| 63 |
@tool
|
| 64 |
def classify_vegetables(items: list) -> list:
|
| 65 |
+
"""Alphabetize true vegetables"""
|
| 66 |
VEGETABLE_SET = {"bell pepper","broccoli","celery","green beans","lettuce","zucchini","sweet potatoes"}
|
| 67 |
return sorted([i for i in items if i in VEGETABLE_SET])
|
| 68 |
|
| 69 |
@tool
|
| 70 |
def execute_code(code: str) -> str:
|
| 71 |
+
"""Execute code and return `output`"""
|
| 72 |
local_ns = {}
|
| 73 |
exec(code, {"__builtins__": {}}, local_ns)
|
| 74 |
return str(local_ns.get("output", ""))
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@tool
|
| 77 |
def least_athletes_olympics(year: int) -> str:
|
| 78 |
+
"""IOC code of least-athlete country"""
|
| 79 |
url = f"https://en.wikipedia.org/wiki/{year}_Summer_Olympics"
|
| 80 |
r = requests.get(url)
|
| 81 |
soup = BeautifulSoup(r.text,"html.parser")
|
|
|
|
| 88 |
|
| 89 |
@tool
|
| 90 |
def get_nasa_award_number(qid: str) -> str:
|
| 91 |
+
"""NASA award number for Wikidata QID"""
|
| 92 |
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
|
| 93 |
sparql.setQuery(f'SELECT ?award WHERE {{ wd:{qid} wdt:P496 ?award. }}')
|
| 94 |
sparql.setReturnFormat(JSON)
|