Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add page metadata on PDFMinerLoader #12277

Merged
merged 8 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions libs/langchain/langchain/document_loaders/parsers/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,29 @@ def _extract_images_from_page(self, page: pypdf._page.PageObject) -> str:
class PDFMinerParser(BaseBlobParser):
"""Parse `PDF` using `PDFMiner`."""

def __init__(self, extract_images: bool = False):
def __init__(self, extract_images: bool = False, load_per_pages: bool = False):
blue-hope marked this conversation as resolved.
Show resolved Hide resolved
self.extract_images = extract_images
self.load_per_pages = load_per_pages

def lazy_parse(self, blob: Blob) -> Iterator[Document]:
"""Lazily parse the blob."""

if not self.extract_images:
from pdfminer.high_level import extract_text

with blob.as_bytes_io() as pdf_file_obj:
text = extract_text(pdf_file_obj)
metadata = {"source": blob.source}
yield Document(page_content=text, metadata=metadata)
if self.load_per_pages:
from pdfminer.pdfpage import PDFPage

pages = PDFPage.get_pages(pdf_file_obj)
for i, _ in enumerate(pages):
text = extract_text(pdf_file_obj, page_numbers=[i])
metadata = {"source": blob.source, "page": str(i)}
yield Document(page_content=text, metadata=metadata)
else:
text = extract_text(pdf_file_obj)
metadata = {"source": blob.source}
yield Document(page_content=text, metadata=metadata)
else:
import io

Expand Down
5 changes: 4 additions & 1 deletion libs/langchain/langchain/document_loaders/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def __init__(
*,
headers: Optional[Dict] = None,
extract_images: bool = False,
load_per_pages: bool = False,
) -> None:
"""Initialize with file path."""
try:
Expand All @@ -262,7 +263,9 @@ def __init__(
)

super().__init__(file_path, headers=headers)
self.parser = PDFMinerParser(extract_images=extract_images)
self.parser = PDFMinerParser(
extract_images=extract_images, load_per_pages=load_per_pages
)

def load(self) -> List[Document]:
"""Eagerly load the content."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ def test_pdfminer_loader() -> None:
docs = loader.load()
assert len(docs) == 1

"""Test PDFMiner loader. (load per pages)"""
eyurtsev marked this conversation as resolved.
Show resolved Hide resolved
file_path = Path(__file__).parent.parent / "examples/hello.pdf"
loader = PDFMinerLoader(str(file_path), load_per_pages=True)
docs = loader.load()

assert len(docs) == 1

file_path = Path(__file__).parent.parent / "examples/layout-parser-paper.pdf"
loader = PDFMinerLoader(str(file_path), load_per_pages=True)

docs = loader.load()
assert len(docs) == 16


def test_pdfminer_pdf_as_html_loader() -> None:
"""Test PDFMinerPDFasHTMLLoader."""
Expand Down