Spaces:
Paused
Paused
Download obe/pdf_parser.py from Syed-Hassan/obe-matrix: direct link, hf CLI and curl.
- Browser
- Download file 758 Bytes
-
https://huggingface.co/spaces/Syed-Hassan/obe-matrix/resolve/main/obe/pdf_parser.py
- Command line
-
hf download hf://spaces/Syed-Hassan/obe-matrix/obe/pdf_parser.py
-
curl -L -o pdf_parser.py https://huggingface.co/spaces/Syed-Hassan/obe-matrix/resolve/main/obe/pdf_parser.py
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 | |