[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.
Python aifaissgpt-osslangchainllama-indexllmlocalstorageoffline-firstollamaprivacypythonragretrieval-augmented-generationvector-databasevector-searchvectors
Fix --use-ocr PDF loading (#440)
Zhifei Li287859de0b53
diff --git a/apps/document_rag.py b/apps/document_rag.pyindex f20fd11..2e40cde 100644--- a/apps/document_rag.py+++ b/apps/document_rag.py@@ -14,7 +14,8 @@ import importlib.utilfrom base_rag_example import BaseRAGExamplefrom chunking import create_text_chunks-from llama_index.core import SimpleDirectoryReader+from llama_index.core import Document, SimpleDirectoryReader+from llama_index.core.readers.base import BaseReader# MinerU powers OCR for scanned PDFs. Probe for it rather than importing it:# the real import happens lazily in extract_pdf_with_ocr_fallback, so a run@@ -29,11 +30,22 @@ def extract_pdf_with_ocr_fallback(pdf_path: str, use_ocr: bool = False) -> str:Args:pdf_path: Path to PDF file- use_ocr: Whether to try OCR if standard extraction fails+ use_ocr: Whether to force MinerU OCR instead of trying text extractionReturns:Extracted text string"""+ if use_ocr:+ if not OCR_AVAILABLE:+ return ""+ try:+ from mineru import parse++ return parse(pdf_path, ocr_mode="ocr").markdown()+ except Exception as e:+ print(f" OCR failed for {pdf_path}: {e}")+ return ""+# Try PyMuPDF firsttry:import fitz # PyMuPDF@@ -63,19 +75,39 @@ def extract_pdf_with_ocr_fallback(pdf_path: str, use_ocr: bool = False) -> str:except Exception:pass- # Try OCR if enabled- if use_ocr and OCR_AVAILABLE:- try:- from mineru import parse+ return "" # Return empty if all fail- # ocr_mode="ocr" forces the OCR pipeline instead of letting MinerU- # pick: we only get here once the text layer has already proven- # unusable, so its "auto" heuristic has nothing left to decide.- return parse(pdf_path, ocr_mode="ocr").markdown()- except Exception as e:- print(f" OCR failed for {pdf_path}: {e}")- return "" # Return empty if all fail+class OcrPDFReader(BaseReader):+ def __init__(self, *, use_ocr: bool = False, auto_detect_scanned: bool = False):+ self.use_ocr = use_ocr+ self.auto_detect_scanned = auto_detect_scanned++ def load_data(+ self,+ file: Path,+ extra_info: dict[str, Any] | None = None,+ **kwargs: Any,+ ) -> list[Document]:+ file_path = str(file)+ used_ocr = self.use_ocr+ text = extract_pdf_with_ocr_fallback(file_path, use_ocr=used_ocr)++ if not used_ocr and self.auto_detect_scanned:+ if len(text.strip()) < 100:+ print(f"Detected scanned PDF: {Path(file_path).name}")+ text = extract_pdf_with_ocr_fallback(file_path, use_ocr=True)+ used_ocr = True++ if used_ocr and text:+ print(f"OCR complete: {Path(file_path).name}")+ if not text.strip():+ return []++ metadata = {"file_name": Path(file_path).name}+ if extra_info:+ metadata.update(extra_info)+ return [Document(text=text, metadata=metadata)]class DocumentRAG(BaseRAGExample):@@ -151,26 +183,6 @@ class DocumentRAG(BaseRAGExample):use_ocr_for_all = args.use_ocrauto_detect_scanned = args.auto_detect_scanned and OCR_AVAILABLE- # Create custom PDF extractor with OCR fallback- def pdf_extractor(file_path: str) -> str:- """Custom extractor for PDFs with OCR support."""- # Check if we should try OCR- try_ocr = use_ocr_for_all-- if not try_ocr and auto_detect_scanned:- # Quick check: try standard extraction first- text = extract_pdf_with_ocr_fallback(file_path, use_ocr=False)- # If we got very little text, it's likely scanned- if len(text.strip()) < 100:- try_ocr = True- print(f"Detected scanned PDF: {Path(file_path).name}")-- # Extract with OCR if needed- text = extract_pdf_with_ocr_fallback(file_path, use_ocr=try_ocr)- if try_ocr and text:- print(f"OCR complete: {Path(file_path).name}")- return text-# Load documents with custom PDF extractorreader_kwargs = {"recursive": True,@@ -181,7 +193,12 @@ class DocumentRAG(BaseRAGExample):# Add custom PDF extractor if we need OCRif use_ocr_for_all or auto_detect_scanned:- reader_kwargs["file_extractor"] = {".pdf": pdf_extractor}+ reader_kwargs["file_extractor"] = {+ ".pdf": OcrPDFReader(+ use_ocr=use_ocr_for_all,+ auto_detect_scanned=auto_detect_scanned,+ )+ }documents = SimpleDirectoryReader(args.data_dir, **reader_kwargs).load_data(show_progress=Truediff --git a/tests/test_document_rag_ocr.py b/tests/test_document_rag_ocr.pynew file mode 100644index 0000000..bb4679b--- /dev/null+++ b/tests/test_document_rag_ocr.py@@ -0,0 +1,142 @@+import importlib.util+import sys+import types+from pathlib import Path++REPO_ROOT = Path(__file__).resolve().parents[1]+++class _LlamaDocument:+ def __init__(self, text, metadata=None):+ self.text = text+ self.metadata = metadata or {}+++def _load_document_rag(monkeypatch):+ base_rag = types.SimpleNamespace(BaseRAGExample=type("BaseRAGExample", (), {}))+ chunking = types.SimpleNamespace(create_text_chunks=lambda *args, **kwargs: [])+ llama_index_core = types.SimpleNamespace(+ Document=_LlamaDocument,+ SimpleDirectoryReader=type("SimpleDirectoryReader", (), {}),+ )+ llama_index = types.SimpleNamespace(core=llama_index_core)+ readers_base = types.SimpleNamespace(BaseReader=type("BaseReader", (), {}))++ monkeypatch.setitem(sys.modules, "base_rag_example", base_rag)+ monkeypatch.setitem(sys.modules, "chunking", chunking)+ monkeypatch.setitem(sys.modules, "llama_index", llama_index)+ monkeypatch.setitem(sys.modules, "llama_index.core", llama_index_core)+ monkeypatch.setitem(sys.modules, "llama_index.core.readers", types.SimpleNamespace())+ monkeypatch.setitem(sys.modules, "llama_index.core.readers.base", readers_base)++ path = REPO_ROOT / "apps" / "document_rag.py"+ spec = importlib.util.spec_from_file_location("document_rag_under_test", path)+ assert spec is not None+ assert spec.loader is not None+ module = importlib.util.module_from_spec(spec)+ monkeypatch.setitem(sys.modules, spec.name, module)+ spec.loader.exec_module(module)+ return module+++class _Page:+ def __init__(self, text):+ self._text = text++ def get_text(self):+ return self._text+++class _Document:+ def __init__(self, text):+ self._page = _Page(text)++ def __iter__(self):+ return iter([self._page])++ def close(self):+ pass+++def test_use_ocr_skips_text_extractors(monkeypatch):+ module = _load_document_rag(monkeypatch)+ monkeypatch.setattr(module, "OCR_AVAILABLE", True)++ fitz_calls = []+ fitz = types.SimpleNamespace()++ def open_pdf(path):+ fitz_calls.append(path)+ return _Document("text layer " * 20)++ fitz.open = open_pdf+ monkeypatch.setitem(sys.modules, "fitz", fitz)++ parse_calls = []+ mineru = types.SimpleNamespace()++ class ParseResult:+ def markdown(self):+ return "ocr markdown"++ def parse(path, **kwargs):+ parse_calls.append((path, kwargs))+ return ParseResult()++ mineru.parse = parse+ monkeypatch.setitem(sys.modules, "mineru", mineru)++ assert module.extract_pdf_with_ocr_fallback("paper.pdf", use_ocr=True) == "ocr markdown"+ assert fitz_calls == []+ assert parse_calls == [("paper.pdf", {"ocr_mode": "ocr"})]+++def test_text_extraction_remains_the_default(monkeypatch):+ module = _load_document_rag(monkeypatch)+ monkeypatch.setattr(module, "OCR_AVAILABLE", True)++ text = "text layer " * 20+ fitz = types.SimpleNamespace(open=lambda path: _Document(text))+ monkeypatch.setitem(sys.modules, "fitz", fitz)++ mineru = types.SimpleNamespace()++ def unexpected_parse(*args, **kwargs):+ raise AssertionError("MinerU should not run when use_ocr is false")++ mineru.parse = unexpected_parse+ monkeypatch.setitem(sys.modules, "mineru", mineru)++ assert module.extract_pdf_with_ocr_fallback("paper.pdf") == text+++def test_ocr_reader_satisfies_simple_directory_reader_contract(monkeypatch):+ module = _load_document_rag(monkeypatch)+ monkeypatch.setattr(module, "OCR_AVAILABLE", True)++ fitz_calls = []+ fitz = types.SimpleNamespace()++ def open_pdf(path):+ fitz_calls.append(path)+ return _Document("text layer " * 20)++ fitz.open = open_pdf+ monkeypatch.setitem(sys.modules, "fitz", fitz)++ mineru = types.SimpleNamespace()++ class ParseResult:+ def markdown(self):+ return "ocr markdown"++ mineru.parse = lambda path, **kwargs: ParseResult()+ monkeypatch.setitem(sys.modules, "mineru", mineru)++ reader = module.OcrPDFReader(use_ocr=True)+ documents = reader.load_data(Path("paper.pdf"), extra_info={"source": "test"})++ assert len(documents) == 1+ assert documents[0].text == "ocr markdown"+ assert documents[0].metadata == {"file_name": "paper.pdf", "source": "test"}+ assert fitz_calls == []