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 2 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
Expand Up @@ -6,3 +6,4 @@ slowapi
starlette
tiktoken
uvicorn
PyPDF2
42 changes: 39 additions & 3 deletions src/gitingest/ingest_from_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from fnmatch import fnmatch
from typing import Any
from PyPDF2 import PdfReader
cyclotruc marked this conversation as resolved.
Show resolved Hide resolved

import tiktoken

Expand Down Expand Up @@ -97,6 +98,21 @@ def _is_safe_symlink(symlink_path: str, base_path: str) -> bool:
# If there's any error resolving the paths, consider it unsafe
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:
"""
Expand All @@ -123,14 +139,32 @@ def _is_text_file(file_path: str) -> bool:
except OSError:
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.
If an error occurs during reading (e.g., file is not found or permission error),
it returns an error message.
This function reads text files using UTF-8 encoding or extracts text from PDF files.
If an error occurs during reading, it returns an error message.

Parameters
----------
Expand All @@ -142,6 +176,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