Skip to content

armgit / LEANN

Public Imported from GitHub
0

[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.

Implement OCR support and benchmark evaluation for PDF extraction (#190)

Toby Lib415df84ec49
diff --git a/apps/document_rag.py b/apps/document_rag.py
index fae1860..f20fd11 100644
--- a/apps/document_rag.py
+++ b/apps/document_rag.py
@@ -10,10 +10,73 @@ from typing import Any
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))
+import importlib.util
+
from base_rag_example import BaseRAGExample
from chunking import create_text_chunks
from llama_index.core import SimpleDirectoryReader
+# 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
+# that never touches a scanned PDF does not pay for it.
+OCR_AVAILABLE = importlib.util.find_spec("mineru") is not None
+
+
+def extract_pdf_with_ocr_fallback(pdf_path: str, use_ocr: bool = False) -> str:
+ """
+ Extract text from PDF with OCR fallback.
+ Used as a custom file extractor for SimpleDirectoryReader.
+
+ Args:
+ pdf_path: Path to PDF file
+ use_ocr: Whether to try OCR if standard extraction fails
+
+ Returns:
+ Extracted text string
+ """
+ # Try PyMuPDF first
+ try:
+ import fitz # PyMuPDF
+
+ doc = fitz.open(pdf_path)
+ text = ""
+ for page in doc:
+ text += page.get_text()
+ doc.close()
+
+ if text and len(text.strip()) > 100:
+ return text
+ except Exception:
+ pass
+
+ # Try pdfplumber
+ try:
+ import pdfplumber
+
+ text = ""
+ with pdfplumber.open(pdf_path) as pdf:
+ for page in pdf.pages:
+ text += page.extract_text() or ""
+
+ if text and len(text.strip()) > 100:
+ return text
+ except Exception:
+ pass
+
+ # Try OCR if enabled
+ if use_ocr and OCR_AVAILABLE:
+ try:
+ from mineru import parse
+
+ # 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 DocumentRAG(BaseRAGExample):
"""RAG example for document processing (PDF, TXT, MD, etc.)."""
@@ -52,6 +115,26 @@ class DocumentRAG(BaseRAGExample):
help="Enable AST-aware chunking for code files in the data directory",
)
+ # OCR parameters
+ ocr_group = parser.add_argument_group("OCR Parameters (for scanned PDFs)")
+ ocr_group.add_argument(
+ "--use-ocr",
+ action="store_true",
+ help="Force OCR processing for all PDFs (even if they contain text)",
+ )
+ ocr_group.add_argument(
+ "--auto-detect-scanned",
+ action="store_true",
+ default=True,
+ help="Automatically detect and OCR scanned PDFs (default: True)",
+ )
+ ocr_group.add_argument(
+ "--no-auto-detect-scanned",
+ dest="auto_detect_scanned",
+ action="store_false",
+ help="Disable automatic detection of scanned PDFs",
+ )
+
async def load_data(self, args) -> list[dict[str, Any]]:
"""Load documents and convert to text chunks."""
print(f"Loading documents from: {args.data_dir}")
@@ -65,13 +148,44 @@ class DocumentRAG(BaseRAGExample):
if not data_path.exists():
raise ValueError(f"Data directory not found: {args.data_dir}")
- # Load documents
- documents = SimpleDirectoryReader(
- args.data_dir,
- recursive=True,
- encoding="utf-8",
- required_exts=args.file_types if args.file_types else None,
- ).load_data(show_progress=True)
+ use_ocr_for_all = args.use_ocr
+ auto_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 extractor
+ reader_kwargs = {
+ "recursive": True,
+ "encoding": "utf-8",
+ }
+ if args.file_types:
+ reader_kwargs["required_exts"] = args.file_types
+
+ # Add custom PDF extractor if we need OCR
+ if use_ocr_for_all or auto_detect_scanned:
+ reader_kwargs["file_extractor"] = {".pdf": pdf_extractor}
+
+ documents = SimpleDirectoryReader(args.data_dir, **reader_kwargs).load_data(
+ show_progress=True
+ )
if not documents:
print(f"No documents found in {args.data_dir} with extensions {args.file_types}")
@@ -120,6 +234,16 @@ if __name__ == "__main__":
print("- Use --enable-code-chunking to enable AST-aware chunking for code files")
print("- Supports Python, Java, C#, TypeScript files")
print("- Better semantic understanding of code structure")
+ # Keep this file ASCII-only: tests/test_ci_minimal.py runs it as a subprocess,
+ # and Windows stdout defaults to cp1252, which cannot encode emoji. This banner
+ # prints before argparse, so one unencodable character breaks even --help.
+ if OCR_AVAILABLE:
+ print("\nOCR Support: Scanned PDF processing available!")
+ print("- Use --use-ocr to force OCR for all PDFs")
+ print("- Use --auto-detect-scanned (default) to automatically detect scanned PDFs")
+ else:
+ print("\nOCR Support: Install mineru for scanned PDF processing:")
+ print(' pip install "mineru>=4.0,<5"')
print("\nOr run without --query for interactive mode\n")
rag = DocumentRAG()