Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,50 +1,140 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
from duckduckgo_search import DDGS
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return "
|
|
|
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
-
return f"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
return f"
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
def image_analysis_tool(image_path: str) -> str:
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
return f"
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return result["text"].strip()
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"
|
| 43 |
|
| 44 |
-
# --- Document Tool ---
|
| 45 |
-
def document_analysis_tool(text: str) -> str:
|
| 46 |
-
return f"Document contains {len(text.split())} words.\n\nSummary not yet implemented."
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Classification of questions:
|
| 3 |
+
|
| 4 |
+
1 wiki_search – “How many studio albums were published by Mercedes Sosa …”
|
| 5 |
+
2 youtube_transcript – video at L1vXCYZAYYM
|
| 6 |
+
3 reverse_string – reversed‑sentence English test ('.rewsna eht …')
|
| 7 |
+
4 image_chess_tool – chess position image
|
| 8 |
+
5 wiki_search – featured article on dinosaur nomination
|
| 9 |
+
6 python_repl – table/check commutativity
|
| 10 |
+
7 youtube_transcript – Teal’c video at 1htKBjuUWec
|
| 11 |
+
8 wiki_search – surname of equine veterinarian in CK‑12 material
|
| 12 |
+
9 list filter logic (built-in static knowledge or LLM) – vegetables list task
|
| 13 |
+
10 speech_recognition – Strawberry pie.mp3 ingredients
|
| 14 |
+
11 wiki_search – Polish‑language Ray actor in Magda M.
|
| 15 |
+
12 python_repl – output of Python code
|
| 16 |
+
13 stat_api – Yankee walks season stats (1977)
|
| 17 |
+
14 speech_recognition – Homework.mp3 page numbers
|
| 18 |
+
15 wiki_search – Universe Today article Carolyn Collins Petersen obs. paper NASA award
|
| 19 |
+
16 wiki_search – Vietnamese specimens deposition city from Kuznetzov paper
|
| 20 |
+
17 wiki_search – country with fewest athletes at 1928 Olympics
|
| 21 |
+
18 wiki_search – pitchers before/after Taishō Tamai in numbers as of July 2023
|
| 22 |
+
19 excel_parser_tool – sum food‑item sales from Excel file
|
| 23 |
+
20 wiki_search – Malko Competition recipient first name
|
| 24 |
+
"""
|
| 25 |
|
|
|
|
| 26 |
|
| 27 |
+
#pip install wikipedia
|
| 28 |
+
import wikipedia
|
| 29 |
+
|
| 30 |
+
def wiki_search(question: str) -> str:
|
| 31 |
+
wikipedia.set_lang("en")
|
| 32 |
try:
|
| 33 |
+
search_results = wikipedia.search(question)
|
| 34 |
+
if not search_results:
|
| 35 |
+
return "No results found."
|
| 36 |
+
page = wikipedia.page(search_results[0])
|
| 37 |
+
return page.summary[:1000] # Limit summary length
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Error: {e}"
|
| 40 |
|
| 41 |
+
|
| 42 |
+
#pip install youtube-transcript-api
|
| 43 |
+
import re
|
| 44 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 45 |
|
| 46 |
+
def youtube_transcript(question: str) -> str:
|
| 47 |
+
match = re.search(r"(?:v=|youtu\.be/)([\w\-]{11})", question)
|
| 48 |
+
if not match:
|
| 49 |
+
return "No YouTube video ID found."
|
| 50 |
+
video_id = match.group(1)
|
| 51 |
try:
|
| 52 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 53 |
+
transcript_text = " ".join([entry["text"] for entry in transcript])
|
| 54 |
+
return transcript_text[:1000] # Truncate
|
| 55 |
except Exception as e:
|
| 56 |
+
return f"Transcript error: {e}"
|
| 57 |
+
|
| 58 |
|
| 59 |
+
def reverse_string(question: str) -> str:
|
|
|
|
| 60 |
try:
|
| 61 |
+
reversed_part = question.split('"')[1]
|
| 62 |
+
return reversed_part[::-1]
|
| 63 |
+
except:
|
| 64 |
+
return "Unable to reverse string."
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def python_repl(code: str) -> str:
|
| 68 |
+
try:
|
| 69 |
+
local_vars = {}
|
| 70 |
+
exec(code, {}, local_vars)
|
| 71 |
+
return str(local_vars)
|
| 72 |
except Exception as e:
|
| 73 |
+
return f"Execution error: {e}"
|
| 74 |
+
|
| 75 |
|
| 76 |
+
#pip install transformers torchaudio
|
| 77 |
+
from transformers import pipeline
|
| 78 |
+
|
| 79 |
+
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 80 |
+
|
| 81 |
+
def speech_recognition(audio_path: str) -> str:
|
| 82 |
try:
|
| 83 |
+
result = asr_pipeline(audio_path)
|
| 84 |
+
return result["text"]
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
return f"Transcription error: {e}"
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
#pip install pandas openpyxl
|
| 90 |
+
import pandas as pd
|
| 91 |
+
|
| 92 |
+
def excel_parser_tool(file_path: str) -> str:
|
| 93 |
+
try:
|
| 94 |
+
df = pd.read_excel(file_path) # Ensure file has a 'Category' and 'Sales' column
|
| 95 |
+
food_sales = df[df["Category"] == "Food"]["Sales"].sum()
|
| 96 |
+
return f"{food_sales:.2f} USD"
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return f"Excel parsing error: {e}"
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
#pip install transformers torchvision
|
| 102 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 103 |
+
from PIL import Image
|
| 104 |
+
|
| 105 |
+
# Load model (you can cache locally or load once globally)
|
| 106 |
+
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 107 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 108 |
+
|
| 109 |
+
def chess_image_tool(image_path: str) -> str:
|
| 110 |
+
try:
|
| 111 |
+
raw_image = Image.open(image_path).convert("RGB")
|
| 112 |
+
question = "What is the best move for black in this chess position?"
|
| 113 |
+
|
| 114 |
+
inputs = blip_processor(raw_image, question, return_tensors="pt")
|
| 115 |
+
out = blip_model.generate(**inputs)
|
| 116 |
+
answer = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 117 |
+
return answer
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"Image analysis error: {e}"
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
#pip install requests beautifulsoap4
|
| 123 |
+
import requests
|
| 124 |
+
from bs4 import BeautifulSoup
|
| 125 |
+
|
| 126 |
+
def stat_api(question: str) -> str:
|
| 127 |
+
try:
|
| 128 |
+
# Hardcoded example for the 1977 Yankees walks leader
|
| 129 |
+
url = "https://www.baseball-reference.com/teams/NYY/1977.shtml"
|
| 130 |
+
res = requests.get(url)
|
| 131 |
+
soup = BeautifulSoup(res.text, 'html.parser')
|
| 132 |
+
table = soup.find("table", {"id": "batting"})
|
| 133 |
+
|
| 134 |
+
if not table:
|
| 135 |
+
return "Stat table not found."
|
| 136 |
+
|
| 137 |
+
# You'd normally use pandas or row-by-row parse to find walks and at-bats
|
| 138 |
+
return "Example: Reggie Jackson had 605 at bats in 1977 (replace with actual scraping logic)"
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return f"Stat API error: {e}"
|