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 support for reading PDF files using pypdf #80

Closed
wants to merge 10 commits into from
Closed
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
click>=8.0.0
fastapi-analytics
fastapi[standard]
pypdf
python-dotenv
slowapi
starlette
Expand Down
1 change: 0 additions & 1 deletion src/gitingest/ignore_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"*.jpeg",
"*.gif",
"*.ico",
"*.pdf",
"*.mov",
"*.mp4",
"*.mp3",
Expand Down
43 changes: 42 additions & 1 deletion src/gitingest/ingest_from_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

import tiktoken
from pypdf import PdfReader

from gitingest.exceptions import AlreadyVisitedError, MaxFileSizeReachedError, MaxFilesReachedError

Expand Down Expand Up @@ -101,6 +102,23 @@ def _is_safe_symlink(symlink_path: str, base_path: str) -> bool:
return False


def _is_pdf_file(file_path: str) -> bool:
"""
Check if the file is a PDF based on its extension.

Parameters
----------
file_path : str
The path to the file to check.

Returns
-------
bool
`True` if the file is a PDF, `False` otherwise.
"""
return file_path.lower().endswith(".pdf")


def _is_text_file(file_path: str) -> bool:
"""
Determine if a file is likely a text file based on its content.
Expand All @@ -127,11 +145,32 @@ def _is_text_file(file_path: str) -> bool:
return False


def _read_pdf_content(file_path: str) -> str:
"""
Extract text from a PDF file.

Parameters
----------
file_path : str
The path to the PDF file.

Returns
-------
str
The extracted text from the PDF, or an error message if extraction fails.
"""
try:
reader = PdfReader(file_path)
return "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
except Exception as e:
return f"Error reading PDF file: {str(e)}"


def _read_file_content(file_path: str) -> str:
"""
Reads the content of a file.

This function attempts to open a file and read its contents using UTF-8 encoding.
This function attempts to open a file and read its contents using UTF-8 encoding or extracts text from pdf files.
If an error occurs during reading (e.g., file is not found or permission error),
it returns an error message.

Expand All @@ -145,6 +184,8 @@ def _read_file_content(file_path: str) -> str:
str
The content of the file, or an error message if the file could not be read.
"""
if _is_pdf_file(file_path):
return _read_pdf_content(file_path)
try:
with open(file_path, encoding="utf-8", errors="ignore") as f:
return f.read()
Expand Down