Spaces:
Running
Running
Update customtools.py
Browse files- customtools.py +43 -15
customtools.py
CHANGED
|
@@ -20,9 +20,14 @@ from langchain_community.document_loaders import WikipediaLoader
|
|
| 20 |
from langchain_openrouter import ChatOpenRouter
|
| 21 |
from tavily import TavilyClient
|
| 22 |
from youtube_transcript_api import YouTubeTranscriptApi
|
|
|
|
|
|
|
| 23 |
|
| 24 |
from config import (
|
| 25 |
OPENROUTER_API_KEY,
|
|
|
|
|
|
|
|
|
|
| 26 |
TAVILY_API_KEY,
|
| 27 |
LLM_MODEL,
|
| 28 |
LLM_TEMPERATURE,
|
|
@@ -53,17 +58,21 @@ def wikisearch(query: str, max_pages: int = None) -> str:
|
|
| 53 |
|
| 54 |
|
| 55 |
@tool
|
| 56 |
-
def youtube_transcript(url: str
|
| 57 |
-
"""Fetch YouTube video transcript."""
|
| 58 |
-
chars =
|
| 59 |
video_id_match = re.search(r"[?&]v=([A-Za-z0-9_\-]{11})", url)
|
| 60 |
-
|
|
|
|
| 61 |
if not video_id_match:
|
| 62 |
return "Error: Could not extract video ID from URL"
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
-
transcript = YouTubeTranscriptApi
|
| 66 |
-
text = " ".join(
|
| 67 |
return text[:chars]
|
| 68 |
except Exception as exc:
|
| 69 |
print(f"Error fetching YouTube transcript: {exc}")
|
|
@@ -154,6 +163,16 @@ def division_tool(a: str, b: str) -> str:
|
|
| 154 |
|
| 155 |
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
@tool
|
| 159 |
def extract_text_from_image(image_path: str) -> str:
|
|
@@ -228,13 +247,22 @@ def load_and_analyze_excel_file(query: str, file_path: str) -> str:
|
|
| 228 |
data_summary=data_summary,
|
| 229 |
query=query
|
| 230 |
)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
message = HumanMessage(content=analysis_prompt)
|
| 240 |
llm_response = tool_llm.invoke([message])
|
|
@@ -252,7 +280,7 @@ def load_and_analyze_excel_file(query: str, file_path: str) -> str:
|
|
| 252 |
def transcribe_audio(audio_file: str) -> str:
|
| 253 |
"""Transcribe audio files and return the transcript."""
|
| 254 |
try:
|
| 255 |
-
model = whisper.load_model("
|
| 256 |
output = model.transcribe(audio=str(Path(audio_file)), language='en')
|
| 257 |
print(f"Audio transcription completed")
|
| 258 |
return output['text']
|
|
|
|
| 20 |
from langchain_openrouter import ChatOpenRouter
|
| 21 |
from tavily import TavilyClient
|
| 22 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 23 |
+
from langchain_nvidia_ai_endpoints import ChatNVIDIA
|
| 24 |
+
global llm
|
| 25 |
|
| 26 |
from config import (
|
| 27 |
OPENROUTER_API_KEY,
|
| 28 |
+
NVIDIA_MODEL,
|
| 29 |
+
NVIDIA,
|
| 30 |
+
NVIDIA_API_KEY,
|
| 31 |
TAVILY_API_KEY,
|
| 32 |
LLM_MODEL,
|
| 33 |
LLM_TEMPERATURE,
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
@tool
|
| 61 |
+
def youtube_transcript(url: str) -> str:
|
| 62 |
+
"""Fetch YouTube video transcript from given URL."""
|
| 63 |
+
chars = YOUTUBE_CHAR_LIMIT
|
| 64 |
video_id_match = re.search(r"[?&]v=([A-Za-z0-9_\-]{11})", url)
|
| 65 |
+
#print(f"Video is {video_id_match.group(1)}")
|
| 66 |
+
video_id = None
|
| 67 |
if not video_id_match:
|
| 68 |
return "Error: Could not extract video ID from URL"
|
| 69 |
+
else:
|
| 70 |
+
video_id=video_id_match.group(1)
|
| 71 |
+
#print(YouTubeTranscriptApi.__getattribute__())
|
| 72 |
+
print(video_id)
|
| 73 |
try:
|
| 74 |
+
transcript = YouTubeTranscriptApi().fetch(video_id)
|
| 75 |
+
text = " ".join([piece.text] for piece in transcript)
|
| 76 |
return text[:chars]
|
| 77 |
except Exception as exc:
|
| 78 |
print(f"Error fetching YouTube transcript: {exc}")
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
+
@tool
|
| 167 |
+
def modulus_tool(a: int, b: int) -> int:
|
| 168 |
+
"""Get the modulus of two numbers.
|
| 169 |
+
|
| 170 |
+
Args:
|
| 171 |
+
a: first int
|
| 172 |
+
b: second int
|
| 173 |
+
"""
|
| 174 |
+
return a % b
|
| 175 |
+
|
| 176 |
|
| 177 |
@tool
|
| 178 |
def extract_text_from_image(image_path: str) -> str:
|
|
|
|
| 247 |
data_summary=data_summary,
|
| 248 |
query=query
|
| 249 |
)
|
| 250 |
+
tool_llm=None
|
| 251 |
+
if NVIDIA:
|
| 252 |
+
tool_llm = ChatNVIDIA(
|
| 253 |
+
model=NVIDIA_MODEL,
|
| 254 |
+
api_key= NVIDIA_API_KEY,
|
| 255 |
+
temperature=1,
|
| 256 |
+
top_p=1,
|
| 257 |
+
|
| 258 |
+
)
|
| 259 |
+
else:
|
| 260 |
+
# Get LLM analysis
|
| 261 |
+
tool_llm = ChatOpenRouter(
|
| 262 |
+
model=LLM_MODEL,
|
| 263 |
+
temperature=LLM_TEMPERATURE,
|
| 264 |
+
api_key=OPENROUTER_API_KEY,
|
| 265 |
+
)
|
| 266 |
|
| 267 |
message = HumanMessage(content=analysis_prompt)
|
| 268 |
llm_response = tool_llm.invoke([message])
|
|
|
|
| 280 |
def transcribe_audio(audio_file: str) -> str:
|
| 281 |
"""Transcribe audio files and return the transcript."""
|
| 282 |
try:
|
| 283 |
+
model = whisper.load_model("small")
|
| 284 |
output = model.transcribe(audio=str(Path(audio_file)), language='en')
|
| 285 |
print(f"Audio transcription completed")
|
| 286 |
return output['text']
|