Spaces:
Runtime error
Runtime error
Download script/ocr.py from whosouravsharma/paper-qa-rag: direct link, hf CLI and curl.
- Browser
- Download file 876 Bytes
-
https://huggingface.co/spaces/whosouravsharma/paper-qa-rag/resolve/main/script/ocr.py
- Command line
-
hf download hf://spaces/whosouravsharma/paper-qa-rag/script/ocr.py
-
curl -L -o ocr.py https://huggingface.co/spaces/whosouravsharma/paper-qa-rag/resolve/main/script/ocr.py
876 Bytes
| import os | |
| from pdf2image import convert_from_path | |
| import pytesseract | |
| from langchain_core.documents import Document | |
| from script.errors import IngestionError | |
| def extract_text_via_ocr(pdf_path: str) -> list[Document]: | |
| POPPLER_PATH = os.getenv("POPPLER_PATH", "/usr/bin") | |
| images = convert_from_path( | |
| pdf_path, | |
| poppler_path = POPPLER_PATH | |
| ) | |
| documents = [] | |
| for i, img in enumerate(images): | |
| text = pytesseract.image_to_string(img) | |
| if text.strip(): | |
| documents.append( | |
| Document( | |
| page_content=text, | |
| metadata={ | |
| "page": i, | |
| "source": "ocr" | |
| } | |
| ) | |
| ) | |
| if not documents: | |
| raise IngestionError( | |
| code="OCR_FAILED", | |
| message="OCR produced no text" | |
| ) | |
| return documents | |