-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_directory_loaders.py
50 lines (44 loc) · 1.69 KB
/
custom_directory_loaders.py
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
39
40
41
42
43
44
45
46
47
48
49
50
from langchain_core.document_loaders import BaseLoader
from langchain_community.document_loaders import Docx2txtLoader
from pathlib import Path
from typing import Union
import logging
logger = logging.getLogger(__file__)
class DocxDirectoryLoader(BaseLoader):
"""Load a directory with `docx` files using `docx2txt` and chunks at character level."""
def __init__(
self,
path: Union[str, Path],
glob: str = "**/[!.]*.docx",
silent_errors: bool = False,
load_hidden: bool = False,
recursive: bool = False,
):
self.path = path
self.glob = glob
self.load_hidden = load_hidden
self.recursive = recursive
self.silent_errors = silent_errors
@staticmethod
def _is_visible(path: Path) -> bool:
return not any(part.startswith(".") for part in path.parts)
def load(self) -> str:
"""Load the docx files in the directory and return the concatenated text."""
p = Path(self.path)
docs = []
items = p.rglob(self.glob) if self.recursive else p.glob(self.glob)
for i in items:
if i.is_file():
if self._is_visible(i.relative_to(p)) or self.load_hidden:
try:
loader = Docx2txtLoader(str(i))
sub_docs = loader.load()
for doc in sub_docs:
doc.metadata["source"] = str(i)
docs.extend(sub_docs)
except Exception as e:
if self.silent_errors:
logger.warning(e)
else:
raise e
return docs