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 all 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
26 changes: 22 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,36 @@ 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, *, concatenate_pages: bool = True):
"""Initialize a parser based on PDFMiner.

Args:
extract_images: Whether to extract images from PDF.
concatenate_pages: If True, concatenate all PDF pages into one a single
document. Otherwise, return one document per page.
"""
self.extract_images = extract_images
self.concatenate_pages = concatenate_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.concatenate_pages:
text = extract_text(pdf_file_obj)
metadata = {"source": blob.source}
yield Document(page_content=text, metadata=metadata)
else:
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:
import io

Expand Down
13 changes: 11 additions & 2 deletions libs/langchain/langchain/document_loaders/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,15 @@ def __init__(
*,
headers: Optional[Dict] = None,
extract_images: bool = False,
concatenate_pages: bool = True,
) -> None:
"""Initialize with file path."""
"""Initialize with file path.

Args:
extract_images: Whether to extract images from PDF.
concatenate_pages: If True, concatenate all PDF pages into one a single
document. Otherwise, return one document per page.
"""
try:
from pdfminer.high_level import extract_text # noqa:F401
except ImportError:
Expand All @@ -262,7 +269,9 @@ def __init__(
)

super().__init__(file_path, headers=headers)
self.parser = PDFMinerParser(extract_images=extract_images)
self.parser = PDFMinerParser(
extract_images=extract_images, concatenate_pages=concatenate_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

# Verify that concatenating pages parameter works
file_path = Path(__file__).parent.parent / "examples/hello.pdf"
loader = PDFMinerLoader(str(file_path), concatenate_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), concatenate_pages=False)

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


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