File size: 1,043 Bytes
583462f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from smolagents.tools import Tool
import pytubefix

class YouTubeTranscriptExtractor(Tool):
    description = "Extracts the transcript from a YouTube video."
    name = "youtube_transcript_extractor"
    inputs = {'video_url': {'type': 'string', 'description': 'The URL of the YouTube video.'}}
    output_type = "string"

    def forward(self, video_url: str) -> str:

        try:
          from pytubefix import YouTube
          # Create a YouTube object
          yt = YouTube(video_url)
          lang='en'

          # Get the video transcript
          if lang in yt.captions:
              transcript = yt.captions['en'].generate_srt_captions()

          else:
              transcript = yt.captions.all()[0].generate_srt_captions()
              lang=yt.captions.all()[0].code

          return lang + "transcript : " + transcript
        # return transcript


        except Exception as e:

          return f"An unexpected error occurred: {str(e)}"


    def __init__(self, *args, **kwargs):
        self.is_initialized = False