mistralM

Docs

sideBar

🏥 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 ExtractionMistral Document AI (Azure-hosted) extracts text, tables, and images from PDF documents
OCR Error CorrectionAutomatic fixing of common OCR errors in Invoice units
Completeness CheckingDetects skipped rows, empty cells, and sequential gaps in extracted tables
Invoice Value AnalysisExtracts structured lab values and classifies them against 60+ standard clinical reference ranges
Anomaly HighlightingColor-coded status badges: Normal (green), Low/High (orange), Critical (red)
Interactive Web UIGradio-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_ENDPOINTAzure endpointFull OCR endpoint URL
MISTRAL_API_KEY(set in config)Azure AI / Mistral API key
MISTRAL_OCR_MODELocrModel deployment name
MAX_PAGES0 (all)Limit pages to process

OCR Error Corrections

The system automatically fixes these common invoice OCR errors:

Error Pattern

Correction

Example

l ↔ 1 in unitsUnit-aware fixmg/1 → mg/l, g/d1 → g/dl
O ↔ 0 in numbersContext-aware fixO.5 → 0.5
Corrupted termsDictionary fixce11s → cells, p1ate1ets → platelets
mmH9 → mmHgUnit fixBlood pressure units
Whitespace issuesNormalization3 .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