obe-matrix / obe /pdf_parser.py
Syed-Hassan's picture
Upload folder using huggingface_hub
378fe8c verified
Raw History Blame Contribute Delete
758 Bytes
"""PDF text extraction for chapter uploads."""
import pdfplumber
MAX_CHARS = 20_000
def extract_text_from_pdf(uploaded_file) -> tuple[str, bool]:
"""Extract text from an uploaded PDF file-like object.
Returns (text, truncated) where `truncated` is True if the extracted text
exceeded MAX_CHARS and had to be cut down to keep the LLM prompt within
the model's context window.
"""
pages = []
with pdfplumber.open(uploaded_file) as pdf:
for page in pdf.pages:
page_text = page.extract_text()
if page_text:
pages.append(page_text)
full_text = "\n\n".join(pages).strip()
if len(full_text) > MAX_CHARS:
return full_text[:MAX_CHARS], True
return full_text, False