Sborole commited on
Commit
35b3426
·
verified ·
1 Parent(s): 40f7c1a

Create YouTubeTool.py

Browse files
Files changed (1) hide show
  1. tools/YouTubeTool.py +17 -0
tools/YouTubeTool.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import Tool
2
+ from youtube_transcript_api import YoutubeTranscriptApi
3
+
4
+ class YoutubeTool(Tool):
5
+ name = "youtube reader"
6
+ description= "Fetches the transcript for the youtube video url"
7
+ inputs= {"video_url": {"type": "string"}, "description": "URL or ID of the Youtube video"}
8
+ output_string = "string"
9
+
10
+ def forward(self, video_url: str) -> str:
11
+ video_id = video_url.split("v=")[-1] # basic extraction
12
+ try:
13
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
14
+ return " ".join([t["text"] for t in transcript])
15
+ except Exception as e:
16
+ return f"Error fetching YouTube transcript: {e}"
17
+