from __future__ import annotations from pathlib import Path from backends.base import OCRBackend, OCRResult def maybe_ocr(backend: OCRBackend | None, path: Path) -> OCRResult | None: if backend is None: sidecar = path.with_suffix(".txt") if sidecar.is_file(): return OCRResult(text=sidecar.read_text(encoding="utf-8", errors="replace"), engine="sidecar") if path.suffix.lower() == ".txt": return OCRResult(text=path.read_text(encoding="utf-8", errors="replace"), engine="plaintext") return None return backend.ocr(path)