mirror of
https://github.com/run-llama/llama-hub.git
synced 2025-08-13 19:21:15 +00:00

* add option to pass languages to YoutubeTranscriptReader * Add optional type --------- Co-authored-by: lukas frischknecht <lukas.frischknecht@srf.ch> Co-authored-by: Jesse Zhang <jessetanzhang@gmail.com>
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
"""Simple Reader that reads transcript of youtube video."""
|
|
from typing import Any, List, Optional
|
|
|
|
from gpt_index.readers.base import BaseReader
|
|
from gpt_index.readers.schema.base import Document
|
|
|
|
|
|
class YoutubeTranscriptReader(BaseReader):
|
|
"""Youtube Transcript reader."""
|
|
|
|
def load_data(self, ytlinks: List[str], languages: Optional[List[str]] = ['en'], **load_kwargs: Any) -> List[Document]:
|
|
"""Load data from the input directory.
|
|
|
|
Args:
|
|
pages (List[str]): List of youtube links \
|
|
for which transcripts are to be read.
|
|
|
|
"""
|
|
from youtube_transcript_api import YouTubeTranscriptApi
|
|
|
|
results = []
|
|
for link in ytlinks:
|
|
video_id = link.split("?v=")[-1]
|
|
srt = YouTubeTranscriptApi.get_transcript(video_id, languages=languages)
|
|
transcript = ""
|
|
for chunk in srt:
|
|
transcript = transcript + chunk["text"] + "\n"
|
|
results.append(Document(transcript))
|
|
return results
|