Update tools.py
Browse files
tools.py
CHANGED
|
@@ -4,8 +4,8 @@ from langchain_core.messages import HumanMessage
|
|
| 4 |
from langchain.tools import tool
|
| 5 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 7 |
-
from langchain_core.video import VideoFile
|
| 8 |
import yt_dlp
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
@tool
|
|
@@ -208,6 +208,7 @@ class TranscribeAudio:
|
|
| 208 |
return ""
|
| 209 |
|
| 210 |
|
|
|
|
| 211 |
def download_youtube_video(youtube_url: str, output_path: str) -> str:
|
| 212 |
"""
|
| 213 |
Download a YouTube video as an MP4 file.
|
|
@@ -230,44 +231,30 @@ def download_youtube_video(youtube_url: str, output_path: str) -> str:
|
|
| 230 |
return output_path
|
| 231 |
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
def __call__(self, video_path: str, query: str) -> str:
|
| 238 |
-
try:
|
| 239 |
-
with open(video_path, "rb") as video_file:
|
| 240 |
-
video_bytes = video_file.read()
|
| 241 |
-
|
| 242 |
-
video_data = VideoFile(
|
| 243 |
-
mime_type="video/mp4",
|
| 244 |
-
data=video_bytes
|
| 245 |
-
)
|
| 246 |
-
|
| 247 |
-
message = [
|
| 248 |
-
HumanMessage(
|
| 249 |
-
content=[
|
| 250 |
-
{
|
| 251 |
-
"type": "text",
|
| 252 |
-
"text": (
|
| 253 |
-
f"In relation to this video, answer the following request: {query} "
|
| 254 |
-
),
|
| 255 |
-
},
|
| 256 |
-
{
|
| 257 |
-
"type": "video",
|
| 258 |
-
"video": video_data,
|
| 259 |
-
},
|
| 260 |
-
]
|
| 261 |
-
)
|
| 262 |
-
]
|
| 263 |
-
|
| 264 |
-
response = self.multimodal_model.invoke(message)
|
| 265 |
-
return response.content.strip()
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
@tool
|
| 273 |
def wiki_search(query: str) -> str:
|
|
|
|
| 4 |
from langchain.tools import tool
|
| 5 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
|
|
|
| 7 |
import yt_dlp
|
| 8 |
+
import ffmpeg
|
| 9 |
|
| 10 |
|
| 11 |
@tool
|
|
|
|
| 208 |
return ""
|
| 209 |
|
| 210 |
|
| 211 |
+
@tool
|
| 212 |
def download_youtube_video(youtube_url: str, output_path: str) -> str:
|
| 213 |
"""
|
| 214 |
Download a YouTube video as an MP4 file.
|
|
|
|
| 231 |
return output_path
|
| 232 |
|
| 233 |
|
| 234 |
+
@tool
|
| 235 |
+
def extract_audio_from_video(video_path: str, audio_output: str) -> str:
|
| 236 |
+
"""
|
| 237 |
+
Extracts audio from an MP4 video file and saves it as MP3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
Args:
|
| 240 |
+
video_path: Path to the input MP4 video file.
|
| 241 |
+
audio_output: Path for the output MP3 file.
|
| 242 |
|
| 243 |
+
Returns:
|
| 244 |
+
Path to the audio file.
|
| 245 |
+
"""
|
| 246 |
+
try:
|
| 247 |
+
(
|
| 248 |
+
ffmpeg
|
| 249 |
+
.input(video_path)
|
| 250 |
+
.output(audio_output, format='mp3', acodec='libmp3lame', t=60) # limit to 60 sec
|
| 251 |
+
.overwrite_output()
|
| 252 |
+
.run(quiet=True)
|
| 253 |
+
)
|
| 254 |
+
return audio_output
|
| 255 |
+
except ffmpeg.Error as e:
|
| 256 |
+
raise RuntimeError(f"FFmpeg error: {e.stderr.decode()}") from e
|
| 257 |
+
|
| 258 |
|
| 259 |
@tool
|
| 260 |
def wiki_search(query: str) -> str:
|