Spaces:
Sleeping
Sleeping
File size: 1,388 Bytes
e27c97c | 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 51 52 53 54 55 56 57 58 59 60 61 62 63 | from langchain_core.documents import Document
from .combine import raw_document_text
from .lang_doc import get_langchain_docs
# Smart Chunks Documents
def smart_text_chunker(doc, max_chars=500):
chunks = []
buffer = ""
paragraphs = doc.page_content.split("\n\n")
for para in paragraphs:
if len(buffer) + len(para) <= max_chars:
buffer += para + "\n\n"
else:
chunks.append(
Document(
page_content=buffer.strip(),
metadata=doc.metadata
)
)
buffer = para + "\n\n"
if buffer.strip():
chunks.append(
Document(
page_content=buffer.strip(),
metadata=doc.metadata
)
)
return chunks
# Funtion for Raw Documents -> Langchain Document -> Smart Chunking Documents
def get_chunked_docs(pdf:str):
chunked_docs = []
docs = raw_document_text(pdf)
documents = get_langchain_docs(docs)
for doc in documents:
doc_type = doc.metadata["type"]
if doc_type == "text":
chunked_docs.extend(smart_text_chunker(doc))
elif doc_type == "table":
chunked_docs.append(doc)
elif doc_type == "image":
chunked_docs.append(doc)
return chunked_docs
|