- Ressources
Cookbooks
đ„ Invoice OCR Analyzer
Hands-on notebooks and guides for building with the Mistral API.
đ„ Invoice OCR Analyzer
Mistral Document AI-powered OCR extraction, correction, and invoice analysis of PDF documents.
Features
Feature | Description |
|---|---|
| OCR Extraction | Mistral Document AI (Azure-hosted) extracts text, tables, and images from PDF documents |
| OCR Error Correction | Automatic fixing of common OCR errors in Invoice units |
| Completeness Checking | Detects skipped rows, empty cells, and sequential gaps in extracted tables |
| Invoice Value Analysis | Extracts structured lab values and classifies them against 60+ standard clinical reference ranges |
| Anomaly Highlighting | Color-coded status badges: Normal (green), Low/High (orange), Critical (red) |
| Interactive Web UI | Gradio-based interface with PDF viewer, results panel, corrections log, and reference table |
Architecture
PDF Upload
â
âŒ
ââââââââââââââââââââââ
â Mistral OCR â â Azure endpoint (REST + SDK fallback)
â (ocr_engine.py) â
ââââââââââŹââââââââââââ
â raw markdown / HTML tables
âŒ
ââââââââââââââââââââââ
â Error Correction â â regex rules for Invoice OCR errors
â (ocr_correction) â
ââââââââââŹââââââââââââ
â corrected text
âŒ
ââââââââââââââââââââââ
â Completeness â â Skip detection, empty-row checks
â Check â
ââââââââââŹââââââââââââ
â
âŒ
ââââââââââââââââââââââ
â Invoice Analysis â â Extract lab values, compare to reference ranges
â (invoice_analyzer)â
ââââââââââŹââââââââââââ
â
âŒ
ââââââââââââââââââââââ
â Gradio Web UI â â Results, PDF viewer, corrections log
â (app.py) â
ââââââââââââââââââââââ
Quick Start
# Install dependencies
pip install -r requirements.txt
# Run the web application
python app.py
Then open http://localhost:7860 in your browser.
Configuration
Set via environment variables or edit config.py:
Variable | Default | Description |
|---|---|---|
| MISTRAL_OCR_ENDPOINT | Azure endpoint | Full OCR endpoint URL |
| MISTRAL_API_KEY | (set in config) | Azure AI / Mistral API key |
| MISTRAL_OCR_MODEL | ocr | Model deployment name |
| MAX_PAGES | 0 (all) | Limit pages to process |
OCR Error Corrections
The system automatically fixes these common invoice OCR errors:
Error Pattern | Correction | Example |
|---|---|---|
| l â 1 in units | Unit-aware fix | mg/1 â mg/l, g/d1 â g/dl |
| O â 0 in numbers | Context-aware fix | O.5 â 0.5 |
| Corrupted terms | Dictionary fix | ce11s â cells, p1ate1ets â platelets |
| mmH9 â mmHg | Unit fix | Blood pressure units |
| Whitespace issues | Normalization | 3 .5 â 3.5 |
Project Structure
âââ app.py # Gradio web UI (entry point)
âââ config.py # Configuration (endpoint, keys)
âââ ocr_engine.py # Mistral Document AI OCR extraction
âââ ocr_correction.py # Error correction + completeness checking
âââ invoice_analyzer.py # Reference ranges + value analysis
âââ requirements.txt # Python dependencies
âââ data/ # Sample PDFs
âââ uploads/ # Temporary upload storage