Update tools.py
Browse files
tools.py
CHANGED
|
@@ -31,6 +31,9 @@ Classification of questions:
|
|
| 31 |
import wikipedia
|
| 32 |
@tool
|
| 33 |
def wiki_search(question: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 34 |
wikipedia.set_lang("en")
|
| 35 |
try:
|
| 36 |
search_results = wikipedia.search(question)
|
|
@@ -47,6 +50,9 @@ import re
|
|
| 47 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 48 |
@tool
|
| 49 |
def youtube_transcript(question: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 50 |
match = re.search(r"(?:v=|youtu\.be/)([\w\-]{11})", question)
|
| 51 |
if not match:
|
| 52 |
return "No YouTube video ID found."
|
|
@@ -61,6 +67,9 @@ def youtube_transcript(question: str) -> str:
|
|
| 61 |
|
| 62 |
@tool
|
| 63 |
def reverse_string(question: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
reversed_part = question.split('"')[1]
|
| 66 |
return reversed_part[::-1]
|
|
@@ -70,6 +79,9 @@ def reverse_string(question: str) -> str:
|
|
| 70 |
|
| 71 |
@tool
|
| 72 |
def python_repl(code: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 73 |
try:
|
| 74 |
local_vars = {}
|
| 75 |
exec(code, {}, local_vars)
|
|
@@ -84,6 +96,9 @@ from transformers import pipeline
|
|
| 84 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 85 |
@tool
|
| 86 |
def speech_recognition(audio_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 87 |
try:
|
| 88 |
result = asr_pipeline(audio_path)
|
| 89 |
return result["text"]
|
|
@@ -95,6 +110,9 @@ def speech_recognition(audio_path: str) -> str:
|
|
| 95 |
import pandas as pd
|
| 96 |
@tool
|
| 97 |
def excel_parser_tool(file_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 98 |
try:
|
| 99 |
df = pd.read_excel(file_path) # Ensure file has a 'Category' and 'Sales' column
|
| 100 |
food_sales = df[df["Category"] == "Food"]["Sales"].sum()
|
|
@@ -111,6 +129,9 @@ blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning
|
|
| 111 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 112 |
@tool
|
| 113 |
def chess_image_tool(image_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 114 |
try:
|
| 115 |
raw_image = Image.open(image_path).convert("RGB")
|
| 116 |
question = "What is the best move for black in this chess position?"
|
|
@@ -128,6 +149,9 @@ import requests
|
|
| 128 |
from bs4 import BeautifulSoup
|
| 129 |
@tool
|
| 130 |
def stat_api(question: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 131 |
try:
|
| 132 |
# Hardcoded example for the 1977 Yankees walks leader
|
| 133 |
url = "https://www.baseball-reference.com/teams/NYY/1977.shtml"
|
|
|
|
| 31 |
import wikipedia
|
| 32 |
@tool
|
| 33 |
def wiki_search(question: str) -> str:
|
| 34 |
+
"""
|
| 35 |
+
Search Wikipedia and return a short summary based on the input question.
|
| 36 |
+
"""
|
| 37 |
wikipedia.set_lang("en")
|
| 38 |
try:
|
| 39 |
search_results = wikipedia.search(question)
|
|
|
|
| 50 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 51 |
@tool
|
| 52 |
def youtube_transcript(question: str) -> str:
|
| 53 |
+
"""
|
| 54 |
+
Get the video fromyoutube and return a ttranscription of it.
|
| 55 |
+
"""
|
| 56 |
match = re.search(r"(?:v=|youtu\.be/)([\w\-]{11})", question)
|
| 57 |
if not match:
|
| 58 |
return "No YouTube video ID found."
|
|
|
|
| 67 |
|
| 68 |
@tool
|
| 69 |
def reverse_string(question: str) -> str:
|
| 70 |
+
"""
|
| 71 |
+
Reverse the input question and return it.
|
| 72 |
+
"""
|
| 73 |
try:
|
| 74 |
reversed_part = question.split('"')[1]
|
| 75 |
return reversed_part[::-1]
|
|
|
|
| 79 |
|
| 80 |
@tool
|
| 81 |
def python_repl(code: str) -> str:
|
| 82 |
+
"""
|
| 83 |
+
Execute a python code and return local variables.
|
| 84 |
+
"""
|
| 85 |
try:
|
| 86 |
local_vars = {}
|
| 87 |
exec(code, {}, local_vars)
|
|
|
|
| 96 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 97 |
@tool
|
| 98 |
def speech_recognition(audio_path: str) -> str:
|
| 99 |
+
"""
|
| 100 |
+
Extract a text from the input audio file and return a text.
|
| 101 |
+
"""
|
| 102 |
try:
|
| 103 |
result = asr_pipeline(audio_path)
|
| 104 |
return result["text"]
|
|
|
|
| 110 |
import pandas as pd
|
| 111 |
@tool
|
| 112 |
def excel_parser_tool(file_path: str) -> str:
|
| 113 |
+
"""
|
| 114 |
+
Sum sales of food category and return it.
|
| 115 |
+
"""
|
| 116 |
try:
|
| 117 |
df = pd.read_excel(file_path) # Ensure file has a 'Category' and 'Sales' column
|
| 118 |
food_sales = df[df["Category"] == "Food"]["Sales"].sum()
|
|
|
|
| 129 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 130 |
@tool
|
| 131 |
def chess_image_tool(image_path: str) -> str:
|
| 132 |
+
"""
|
| 133 |
+
Analyze chess position on the image and return the best move.
|
| 134 |
+
"""
|
| 135 |
try:
|
| 136 |
raw_image = Image.open(image_path).convert("RGB")
|
| 137 |
question = "What is the best move for black in this chess position?"
|
|
|
|
| 149 |
from bs4 import BeautifulSoup
|
| 150 |
@tool
|
| 151 |
def stat_api(question: str) -> str:
|
| 152 |
+
"""
|
| 153 |
+
Analyze statistics on the link https://www.baseball-reference.com/teams/NYY/1977.shtml and return an answer to the input question.
|
| 154 |
+
"""
|
| 155 |
try:
|
| 156 |
# Hardcoded example for the 1977 Yankees walks leader
|
| 157 |
url = "https://www.baseball-reference.com/teams/NYY/1977.shtml"
|