Update src/document_ingestion/document_processor.py
Browse files
src/document_ingestion/document_processor.py
CHANGED
|
@@ -1,104 +1,103 @@
|
|
| 1 |
-
"""Document processing module for loading and splitting documents"""
|
| 2 |
-
|
| 3 |
-
from typing import List
|
| 4 |
-
from langchain_community.document_loaders import WebBaseLoader
|
| 5 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
-
from langchain.schema import Document
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
self.
|
| 29 |
-
self.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
loader
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
loader
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
loader
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
loader
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
path
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
docs = self.load_documents(urls)
|
| 104 |
return self.split_documents(docs)
|
|
|
|
| 1 |
+
"""Document processing module for loading and splitting documents"""
|
| 2 |
+
|
| 3 |
+
from typing import List, Union
|
| 4 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 5 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
+
# from langchain.schema import Document
|
| 7 |
+
from langchain_core.documents import Document
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from langchain_community.document_loaders import (
|
| 10 |
+
WebBaseLoader,
|
| 11 |
+
PyPDFLoader,
|
| 12 |
+
TextLoader,
|
| 13 |
+
PyPDFDirectoryLoader
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
class DocumentProcessor:
|
| 17 |
+
"""Handles document loading and processing"""
|
| 18 |
+
|
| 19 |
+
def __init__(self, chunk_size: int = 500, chunk_overlap: int = 50):
|
| 20 |
+
"""
|
| 21 |
+
Initialize document processor
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
chunk_size: Size of text chunks
|
| 25 |
+
chunk_overlap: Overlap between chunks
|
| 26 |
+
"""
|
| 27 |
+
self.chunk_size = chunk_size
|
| 28 |
+
self.chunk_overlap = chunk_overlap
|
| 29 |
+
self.splitter = RecursiveCharacterTextSplitter(
|
| 30 |
+
chunk_size=chunk_size,
|
| 31 |
+
chunk_overlap=chunk_overlap
|
| 32 |
+
)
|
| 33 |
+
def load_from_url(self, url: str) -> List[Document]:
|
| 34 |
+
"""Load document(s) from a URL"""
|
| 35 |
+
loader = WebBaseLoader(url)
|
| 36 |
+
return loader.load()
|
| 37 |
+
|
| 38 |
+
def load_from_pdf_dir(self, directory: Union[str, Path]) -> List[Document]:
|
| 39 |
+
"""Load documents from all PDFs inside a directory"""
|
| 40 |
+
loader = PyPDFDirectoryLoader(str(directory))
|
| 41 |
+
return loader.load()
|
| 42 |
+
|
| 43 |
+
def load_from_txt(self, file_path: Union[str, Path]) -> List[Document]:
|
| 44 |
+
"""Load document(s) from a TXT file"""
|
| 45 |
+
loader = TextLoader(str(file_path), encoding="utf-8")
|
| 46 |
+
return loader.load()
|
| 47 |
+
|
| 48 |
+
def load_from_pdf(self, file_path: Union[str, Path]) -> List[Document]:
|
| 49 |
+
"""Load document(s) from a PDF file"""
|
| 50 |
+
loader = PyPDFDirectoryLoader(str("data"))
|
| 51 |
+
return loader.load()
|
| 52 |
+
|
| 53 |
+
def load_documents(self, sources: List[str]) -> List[Document]:
|
| 54 |
+
"""
|
| 55 |
+
Load documents from URLs, PDF directories, or TXT files
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
sources: List of URLs, PDF folder paths, or TXT file paths
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
List of loaded documents
|
| 62 |
+
"""
|
| 63 |
+
docs: List[Document] = []
|
| 64 |
+
for src in sources:
|
| 65 |
+
if src.startswith("http://") or src.startswith("https://"):
|
| 66 |
+
docs.extend(self.load_from_url(src))
|
| 67 |
+
|
| 68 |
+
path = Path("data")
|
| 69 |
+
if path.is_dir(): # PDF directory
|
| 70 |
+
docs.extend(self.load_from_pdf_dir(path))
|
| 71 |
+
elif path.suffix.lower() == ".txt":
|
| 72 |
+
docs.extend(self.load_from_txt(path))
|
| 73 |
+
else:
|
| 74 |
+
raise ValueError(
|
| 75 |
+
f"Unsupported source type: {src}. "
|
| 76 |
+
"Use URL, .txt file, or PDF directory."
|
| 77 |
+
)
|
| 78 |
+
return docs
|
| 79 |
+
|
| 80 |
+
def split_documents(self, documents: List[Document]) -> List[Document]:
|
| 81 |
+
"""
|
| 82 |
+
Split documents into chunks
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
documents: List of documents to split
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
List of split documents
|
| 89 |
+
"""
|
| 90 |
+
return self.splitter.split_documents(documents)
|
| 91 |
+
|
| 92 |
+
def process_urls(self, urls: List[str]) -> List[Document]:
|
| 93 |
+
"""
|
| 94 |
+
Complete pipeline to load and split documents
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
urls: List of URLs to process
|
| 98 |
+
|
| 99 |
+
Returns:
|
| 100 |
+
List of processed document chunks
|
| 101 |
+
"""
|
| 102 |
+
docs = self.load_documents(urls)
|
|
|
|
| 103 |
return self.split_documents(docs)
|