Spaces:
Sleeping
Sleeping
Download utils/pdf_extractor.py from aryan195a/Legal-classifier: direct link, hf CLI and curl.
- Browser
- Download file 582 Bytes
-
https://huggingface.co/spaces/aryan195a/Legal-classifier/resolve/main/utils/pdf_extractor.py
- Command line
-
hf download hf://spaces/aryan195a/Legal-classifier/utils/pdf_extractor.py
-
curl -L -o pdf_extractor.py https://huggingface.co/spaces/aryan195a/Legal-classifier/resolve/main/utils/pdf_extractor.py
582 Bytes
| import pdfplumber | |
| import pytesseract | |
| from PIL import Image | |
| import io | |
| def extract_pdf_text(file_bytes: bytes): | |
| """Extracts text page-wise from PDF, falls back to OCR if needed.""" | |
| pages_text = [] | |
| with pdfplumber.open(io.BytesIO(file_bytes)) as pdf: | |
| for page in pdf.pages: | |
| text = page.extract_text() | |
| if not text: # fallback to OCR | |
| pil_img = page.to_image(resolution=300).original | |
| text = pytesseract.image_to_string(pil_img) | |
| pages_text.append(text.strip() if text else "") | |
| return pages_text | |