"""Optional live gate: Arabic chars appear when PP-OCRv5 Arabic weights are present.""" from __future__ import annotations import os from pathlib import Path import pytest _WEIGHTS = Path(__file__).resolve().parents[2] / "models" / "ocr" / "ar" / "v5" / "rec.onnx" # Where the customer PDFs live. They are deliberately *not* in the repository, # so the path has to come from outside it — and it must not be one developer's # home directory hard-coded into a test, which is what it was: the gate could # only ever open on one machine. ``CONVERT_OCR_ARABIC_DOC_ROOT`` names the # folder; the historic location stays as the fallback so nobody's existing # checkout changes behaviour. _DOC_ROOT = Path(os.environ.get("CONVERT_OCR_ARABIC_DOC_ROOT") or Path.home() / "Downloads" / "DOC") def _find_rfp() -> Path | None: if not _DOC_ROOT.is_dir(): return None for p in _DOC_ROOT.glob("*.pdf"): if "مواصفات" in p.name or "هويتي" in p.name: return p return None @pytest.mark.skipif( os.environ.get("CONVERT_OCR_ARABIC_LIVE", "").strip() not in ("1", "true", "yes"), reason="opt-in live OCR (CONVERT_OCR_ARABIC_LIVE=1); customer PDFs stay out of the default suite", ) @pytest.mark.skipif(not _WEIGHTS.is_file(), reason="Arabic OCR weights not fetched") def test_arabic_ocr_recovers_script_on_rfp_page(): """When rec.onnx is present, page-1 OCR of the UAE RFP should yield Arabic letters.""" rfp = _find_rfp() if rfp is None: pytest.skip("UAE RFP PDF not found in Downloads/DOC") os.environ["CONVERT_OCR_ARABIC"] = "1" os.environ["CONVERT_OCR_ARABIC_WEIGHTS"] = str(_WEIGHTS) dict_path = _WEIGHTS.parent / "arabic_dict.txt" if dict_path.is_file(): os.environ["CONVERT_OCR_ARABIC_DICT"] = str(dict_path) # Force re-init of OCR engines with AR enabled (module may already be loaded EN-only). import app.services.ocr as ocr_mod ocr_mod._ocr_engine = None ocr_mod._ocr_engine_ar = None ocr_mod._ocr_available = False ocr_mod.load_ocr() assert ocr_mod.is_ocr_available() assert ocr_mod.is_ocr_arabic_available(), "expected ocr_ar=on with weights present" from app.services.convert.layout.page_raster import raster_page png_bytes = raster_page(rfp.read_bytes(), 0, dpi=150, timeout=60.0) result = ocr_mod.recognize_image_bytes(png_bytes) texts = " ".join(L.get("text") or "" for L in result.get("lines") or []) arabic_count = sum(1 for ch in texts if "\u0600" <= ch <= "\u06FF") assert arabic_count > 0, ( f"Expected Arabic chars from dual-pass OCR; got {arabic_count}. " f"Sample text: {texts[:240]!r}" ) assert result.get("ocrAr") is True