codekingpro/portable-devtools
114k
1from pathlib import Path2from typing import List, Union3 4from langchain_core.documents import Document5 6from langchain_community.document_loaders.base import BaseLoader7 8 9class SRTLoader(BaseLoader):10 """Load `.srt` (subtitle) files."""11 12 def __init__(self, file_path: Union[str, Path]):13 """Initialize with a file path."""14 try:15 import pysrt # noqa:F40116 except ImportError:17 raise ImportError(18 "package `pysrt` not found, please install it with `pip install pysrt`"19 )20 self.file_path = str(file_path)21 22 def load(self) -> List[Document]:23 """Load using pysrt file."""24 import pysrt25 26 parsed_info = pysrt.open(self.file_path)27 text = " ".join([t.text for t in parsed_info])28 metadata = {"source": self.file_path}29 return [Document(page_content=text, metadata=metadata)]30 