feat(ocr): replace Google Vision with Mistral OCR as PDF fallback #333

Merged
chaim merged 1 commits from worktree-mistral-ocr-fallback into main 2026-06-27 10:15:13 +00:00
Owner

סיכום

  • Google Cloud Vision הוחלף ב-Mistral OCR (mistral-ocr-latest) כ-fallback לדפים סרוקים
  • PyMuPDF נשמר לדפים דיגיטליים (חינמי, ~50ms, ללא API call)
  • ניתוב ברמת-מסמך: אם אפילו דף אחד נכשל ב-_text_quality_ok() → Mistral קורא את כל ה-PDF בקריאה אחת
  • פלט Markdown נשמר: ## כותרות, |טבלאות| — ה-chunker עודכן לזהות כותרות ATX כגבולות-סקציה
  • MISTRAL_API_KEY נוסף ל-Coolify

שינויים

קובץ מה השתנה
config.py GOOGLE_CLOUD_VISION_API_KEYMISTRAL_API_KEY; allowlist: vision.googleapis.comapi.mistral.ai
extractor.py הוסר כל קוד Vision; נוסף _call_mistral_ocr() (async, base64 PDF → per-page Markdown); _extract_pdf() עבר לניתוב document-level
chunker.py ב-_split_into_sections(): הוסף (?:#{1,3}\s+)? לאנקור כדי ש-## נימוקי הערר ימופה לסקציה הנכונה
scripts/SCRIPTS.md נוסף ocr_benchmark_mistral.py (בנצ'מרק שהוביל להחלטה)

למה Mistral?

בנצ'מרק על 5 תיקים (כתבי ערר בלבד):

  • תיק 1044-03-26: Vision החזיר English garbage (OCR layer שבור בקובץ); Mistral חילץ עברית מלאה
  • בממוצע: Mistral מנצח ב-4/5 תיקים בכיסוי ובניקיון
  • Mistral מטפל בגרשיים נכון natively (אין צורך ב-quote-fixer)

Invariants

  • G1 — מסלול OCR יחיד (לא פרלל): PyMuPDF → Mistral, לא שניהם
  • G2 — אין extractor מקביל: הוסר כל קוד Vision
  • INV-AH — quote-fixer נשאר רק ב-PyMuPDF path; Mistral מטפל בזה natively

🤖 Generated with Claude Code

## סיכום - **Google Cloud Vision הוחלף ב-Mistral OCR** (`mistral-ocr-latest`) כ-fallback לדפים סרוקים - **PyMuPDF נשמר** לדפים דיגיטליים (חינמי, ~50ms, ללא API call) - **ניתוב ברמת-מסמך**: אם אפילו דף אחד נכשל ב-`_text_quality_ok()` → Mistral קורא את כל ה-PDF בקריאה אחת - **פלט Markdown נשמר**: `## כותרות`, `|טבלאות|` — ה-chunker עודכן לזהות כותרות ATX כגבולות-סקציה - `MISTRAL_API_KEY` נוסף ל-Coolify ## שינויים | קובץ | מה השתנה | |------|-----------| | `config.py` | `GOOGLE_CLOUD_VISION_API_KEY` → `MISTRAL_API_KEY`; allowlist: `vision.googleapis.com` → `api.mistral.ai` | | `extractor.py` | הוסר כל קוד Vision; נוסף `_call_mistral_ocr()` (async, base64 PDF → per-page Markdown); `_extract_pdf()` עבר לניתוב document-level | | `chunker.py` | ב-`_split_into_sections()`: הוסף `(?:#{1,3}\s+)?` לאנקור כדי ש-`## נימוקי הערר` ימופה לסקציה הנכונה | | `scripts/SCRIPTS.md` | נוסף `ocr_benchmark_mistral.py` (בנצ'מרק שהוביל להחלטה) | ## למה Mistral? בנצ'מרק על 5 תיקים (כתבי ערר בלבד): - **תיק 1044-03-26**: Vision החזיר English garbage (OCR layer שבור בקובץ); Mistral חילץ עברית מלאה - בממוצע: Mistral מנצח ב-4/5 תיקים בכיסוי ובניקיון - Mistral מטפל בגרשיים נכון natively (אין צורך ב-quote-fixer) ## Invariants - **G1** — מסלול OCR יחיד (לא פרלל): PyMuPDF → Mistral, לא שניהם - **G2** — אין extractor מקביל: הוסר כל קוד Vision - **INV-AH** — quote-fixer נשאר רק ב-PyMuPDF path; Mistral מטפל בזה natively 🤖 Generated with [Claude Code](https://claude.com/claude-code)
chaim added 1 commit 2026-06-27 10:12:15 +00:00
feat(ocr): replace Google Vision with Mistral OCR as PDF fallback
All checks were successful
G12 Leak-Guard / leak-guard (pull_request) Successful in 4s
Lint — undefined names / undefined-names (pull_request) Successful in 10s
9ae7304d44
Switches the scanned-PDF fallback from Google Cloud Vision to
Mistral OCR (mistral-ocr-latest) for better Hebrew accuracy and
robustness against broken embedded OCR layers (e.g. case 1044-03-26
which returned English garbage through Vision).

Routing strategy (document-level, not per-page):
- PyMuPDF extracts all pages; pages that pass _text_quality_ok()
  use PyMuPDF output directly (free, ~50ms).
- If ANY page fails quality → Mistral OCR called once for the whole
  PDF, returning per-page Markdown for all pages (consistent format,
  no plain-text/Markdown mix within a document).

Markdown output preserved: Mistral returns ## headers and |tables|;
chunker updated to recognise ATX Markdown headers (##/###) as section
boundaries in _split_into_sections().

Config: GOOGLE_CLOUD_VISION_API_KEY → MISTRAL_API_KEY; allowlist
updated vision.googleapis.com → api.mistral.ai.
MISTRAL_API_KEY added to Coolify container env.

Invariants: G1 (single OCR fallback path, not parallel), G2 (no
duplicate extractor route), INV-AH (Mistral handles gershayim
natively; quote-fix only on PyMuPDF path).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
chaim merged commit b4a68cf5da into main 2026-06-27 10:15:13 +00:00
chaim deleted branch worktree-mistral-ocr-fallback 2026-06-27 10:15:14 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ezer-mishpati/legal-ai#333