-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_processor.py
More file actions
65 lines (54 loc) · 2.33 KB
/
pdf_processor.py
File metadata and controls
65 lines (54 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import List
import PyPDF2
from io import BytesIO
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from config import Config
import logging
logger = logging.getLogger(__name__)
class PDFProcessor:
def __init__(self):
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=Config.CHUNK_SIZE,
chunk_overlap=Config.CHUNK_OVERLAP,
length_function=len,
)
def extract_text_from_pdf(self, pdf_content: bytes, filename: str) -> str:
text = ""
try:
pdf_reader = PyPDF2.PdfReader(BytesIO(pdf_content))
for page_num, page in enumerate(pdf_reader.pages):
page_text = page.extract_text()
if page_text.strip():
text += f"\n--- Page {page_num + 1} ---\n{page_text}\n"
if not text.strip():
raise ValueError("No text could be extracted from the PDF")
logger.info(f"Extracted text from {filename}: {len(text)} characters")
return text
except Exception as e:
logger.error(f"Error extracting text from {filename}: {e}")
raise
def create_documents(self, text: str, filename: str) -> List[Document]:
try:
chunks = self.text_splitter.split_text(text)
documents = []
for i, chunk in enumerate(chunks):
if chunk.strip():
doc = Document(
page_content=chunk,
metadata={
"source": filename,
"chunk_index": i,
"total_chunks": len(chunks)
}
)
documents.append(doc)
logger.info(f"Created {len(documents)} documents from {filename}")
return documents
except Exception as e:
logger.error(f"Error creating documents from {filename}: {e}")
raise
def process_pdf(self, pdf_content: bytes, filename: str) -> List[Document]:
text = self.extract_text_from_pdf(pdf_content, filename)
documents = self.create_documents(text, filename)
return documents