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

Add warning when page_content is empty #25955

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Changes from 4 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
45 changes: 29 additions & 16 deletions libs/community/langchain_community/document_loaders/parsers/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@

def lazy_parse(self, blob: Blob) -> Iterator[Document]: # type: ignore[valid-type]
"""Lazily parse the blob."""
import fitz
import warnings

Check failure on line 271 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (F401)

langchain_community/document_loaders/parsers/pdf.py:271:16: F401 `warnings` imported but unused

Check failure on line 271 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (F401)

langchain_community/document_loaders/parsers/pdf.py:271:16: F401 `warnings` imported but unused

Check failure on line 272 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (I001)

langchain_community/document_loaders/parsers/pdf.py:270:1: I001 Import block is un-sorted or un-formatted

Check failure on line 272 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (I001)

langchain_community/document_loaders/parsers/pdf.py:270:1: I001 Import block is un-sorted or un-formatted
with blob.as_bytes_io() as file_path: # type: ignore[attr-defined]
if blob.data is None: # type: ignore[attr-defined]
Expand All @@ -277,25 +278,37 @@

yield from [
Document(
page_content=page.get_text(**self.text_kwargs)
+ self._extract_images_from_page(doc, page),
metadata=dict(
{
"source": blob.source, # type: ignore[attr-defined]
"file_path": blob.source, # type: ignore[attr-defined]
"page": page.number,
"total_pages": len(doc),
},
**{
k: doc.metadata[k]
for k in doc.metadata
if type(doc.metadata[k]) in [str, int]
},
),
page_content=self._get_page_content(doc, page, blob),
metadata=self._extract_metadata(doc, page, blob),
)
for page in doc
]


def _get_page_content(self, doc: fitz.fitz.Document, page: fitz.fitz.Page, blob: Blob) -> str:

Check failure on line 287 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:287:89: E501 Line too long (98 > 88)

Check failure on line 287 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:287:89: E501 Line too long (98 > 88)
"""Get the text of the page using PyMuPDF and RapidOCR and issue a warning if it is empty."""

Check failure on line 288 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:288:89: E501 Line too long (101 > 88)

Check failure on line 288 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:288:89: E501 Line too long (101 > 88)
content = page.get_text(**self.text_kwargs) + self._extract_images_from_page(doc, page)

Check failure on line 289 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:289:89: E501 Line too long (95 > 88)

Check failure on line 289 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:289:89: E501 Line too long (95 > 88)

if not content:
warnings.warn(f"Warning: Empty content on page {page.number} of document {blob.source}")

Check failure on line 292 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:292:89: E501 Line too long (100 > 88)

Check failure on line 292 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:292:89: E501 Line too long (100 > 88)

return content

def _extract_metadata(self, doc: fitz.fitz.Document, page: fitz.fitz.Page, blob: Blob) -> dict:

Check failure on line 296 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.12

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:296:89: E501 Line too long (99 > 88)

Check failure on line 296 in libs/community/langchain_community/document_loaders/parsers/pdf.py

View workflow job for this annotation

GitHub Actions / cd libs/community / make lint #3.9

Ruff (E501)

langchain_community/document_loaders/parsers/pdf.py:296:89: E501 Line too long (99 > 88)
"""Extract metadata from the document and page."""
return dict(
{
"source": blob.source, # type: ignore[attr-defined]
"file_path": blob.source, # type: ignore[attr-defined]
"page": page.number,
"total_pages": len(doc),
},
**{
k: doc.metadata[k]
for k in doc.metadata
if isinstance(doc.metadata[k], (str, int))
},
)

def _extract_images_from_page(
self, doc: fitz.fitz.Document, page: fitz.fitz.Page
) -> str:
Expand Down
Loading