Skip to main content

SparkOCR-VLM

CI Python 3.11 PySpark 3.5 License: MIT

Distributed VLM-based OCR at scale — PySpark + Vision-Language Models + Delta Lake.


The problem

Most teams OCR documents with a single-machine Python loop calling a VLM API. That breaks at scale:

  • A million-page document lake takes weeks on one machine
  • There is no retry, cost cap, or structured output — just a pile of text files
  • Every team writes the same boilerplate Spark glue from scratch

Databricks ai_parse_document solves part of this but is closed-source and Databricks-only.

What this does

SparkOCR-VLM wraps modern Vision-Language Models as PySpark pandas_udfs so you can:

  • Process millions of PDF pages in parallel across any Spark cluster
  • Land results directly in a Delta Lake silver table (structured, queryable, versioned)
  • Swap VLM backends (OpenRouter, Gemini, Together, Modal) with one config flag
  • Run on OSS Spark, Databricks Free Edition, or any cloud cluster — no vendor lock-in
  • Use the free OpenRouter tier to get started at $0.00

Install

git clone https://github.com/sabareeswarans11/SparkOCR-VLM.git
cd SparkOCR-VLM
pip install -e ".[dev]"
cp .env.template .env
# add OPENROUTER_API_KEY to .env

Quickstart

from sparkocr_vlm import OCRPipeline
from sparkocr_vlm.utils.spark_helpers import build_local_spark

spark = build_local_spark()

pipeline = OCRPipeline(
    backend="openrouter",
    model="nvidia/nemotron-nano-12b-vl:free",   # free tier, no credits needed
    input_path="./pdfs/",
    output_path="./output_delta/",
    max_cost_usd=1.0,
)

silver = pipeline.run(spark)
silver.show(truncate=80)

Results land in a Delta table with columns: filename, page_num, markdown, doc_type, confidence, prompt_tokens, completion_tokens, cost_usd, error.


Real results — Databricks Free Edition

Ran against 3 synthetic documents on Databricks serverless (Free Edition), writing to Unity Catalog workspace.default.ocr_silver. Total cost: $0.00.

synth_invoice.pdf — page 1

Invoice INV-2024-001

Bill to: ACME Corp
Date: 2024-01-15

| Item        | Qty | Price   | Total    |
|-------------|-----|---------|----------|
| Widget A    | 10  | $25.00  | $250.00  |
| Widget B    | 5   | $50.00  | $250.00  |
| Service Fee | 1   | $734.56 | $734.56  |

Total: **$1,234.56**

synth_report.pdf — page 1

# Q1 2025 Quarterly Report

Prepared by: Finance Team

## Executive Summary

Revenue grew 18% year over year, driven by enterprise contracts.
Operating margin improved to 22.4%.

synth_report.pdf — page 2

# Detailed Results

- Revenue: $42.1M
- Gross margin: 71%
- Net income: $9.4M
- Headcount: 312
- Key risks: foreign exchange, supplier consolidation.

synth_table.pdf — page 1

# Sales by Region

| Region | Q1  | Q2  | Q3  |
|:-------|:----|:----|:----|
| North  | 100 | 120 | 140 |
| South  | 80  | 90  | 110 |
| East   | 60  | 70  | 85  |
| West   | 150 | 160 | 175 |

Run stats

File Pages Tokens (in / out) Cost
synth_invoice.pdf 1 3402 / 138 $0.00
synth_report.pdf 2 3402 / 50 + 3402 / 52 $0.00
synth_table.pdf 1 3402 / 111 $0.00
Total 4 $0.00

Results written to workspace.default.ocr_silver Delta table in Unity Catalog.


Evaluation results

Scored against committed ground-truth goldens using 03_evaluation.ipynb. Metrics logged to MLflow.

Eval metrics chart

Per-page scores

File Page Edit Distance ↓ Anchor Recall ↑ Table F1 ↑ Reading Order ED ↓
synth_invoice.pdf 1 0.08 1.00 1.00 0.35
synth_report.pdf 1 0.46 0.67 1.00 0.35
synth_report.pdf 2 0.55 0.33 1.00 0.68
synth_table.pdf 1 0.06 1.00 1.00 0.28

Aggregate (mean across 4 pages)

Metric Score Meaning
Edit Distance ↓ 0.2859 Lower is better — character-level similarity to ground truth
Anchor Recall ↑ 0.75 Key entities (invoice numbers, totals, names) correctly extracted
Table F1 ↑ 1.00 All table cells matched perfectly across all documents
Reading Order ED ↓ 0.412 Line sequence preserved reasonably well

Table structure extraction is perfect (F1 = 1.0). The edit distance gap comes from minor formatting differences between the VLM output and the golden text (punctuation, whitespace). All critical entities are extractable.


Mock mode (unit tests — no API keys)

pipeline = OCRPipeline(backend="mock", input_path="./pdfs/", output_path="./out/")
pipeline.run(spark)

All 22 unit tests run on the mock backend — zero API spend, zero network calls.

Backends

Backend Recommended model Free tier? Notes
openrouter (default) nvidia/nemotron-nano-12b-v2-vl:free ✅ Yes Verified working, sign up at openrouter.ai
openrouter google/gemma-4-31b-it:free ✅ Yes Alt free vision model
gemini gemini-2.0-flash ✅ Yes (rate-limited) Google AI Studio free key
together meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo 💳 Pay-per-token Very cheap
modal Any HF vision model 💳 Pay-per-second Self-hosted GPU
mock n/a ✅ Free Unit tests + dry runs

Where this runs

  • Mac (Intel or Apple Silicon) — local PySpark, OpenRouter API, no GPU needed. See MAC_INTEL_SETUP.md.
  • Databricks Free Edition — upload the notebook to a Free workspace, run. See DATABRICKS_FREE.md.
  • Any Spark cluster — pip install sparkocr-vlm, set env vars, go.

Project layout

  • src/sparkocr_vlm/ — library source (backends, pipeline, evaluator, schema)
  • notebooks/ — quickstart, Databricks Free demo, eval benchmark
  • tests/ + tests/harness/ — pytest suite with deterministic synthetic-PDF harness
  • tasks/ — per-component build specs

What was built — end-to-end summary

Layer What Status
Library sparkocr_vlm Python package — pipeline, backends, evaluator, schema ✅
Backends OpenRouter, Gemini, Together, Modal, Mock — all behind one VLMBackend ABC ✅
PySpark UDF pandas_udf wrapping VLM calls; executor-safe key injection via closure ✅
Delta Lake Bronze → Silver pipeline; Unity Catalog table on Databricks Free ✅
Evaluator Edit distance, anchor recall, table F1, reading-order ED; MLflow logging ✅
Test harness 22 unit tests, deterministic synthetic PDFs, golden assertions — mock backend only ✅
CI/CD GitHub Actions — ruff lint + pytest on every push, Java 17 + Python 3.11 ✅
Notebooks Quickstart, Databricks Free Edition demo, evaluation ✅
Databricks Wheel deployed to Volume, pipeline runs on serverless, results in UC table ✅
Cost End-to-end run on 4 pages: $0.00 (OpenRouter free tier) ✅

Key design decisions

  • No GPU required — all inference is via API (OpenRouter, Gemini, Together). Runs on any Mac or cloud VM.
  • Spark-native — pages are distributed via mapInPandas, OCR via pandas_udf. No custom schedulers.
  • Backend-agnostic — switching models is one config flag; free and paid tiers both supported.
  • Retry-safe — exponential backoff on HTTP 429 and soft rate-limit errors (200 with error body).
  • Cost-capped — max_cost_usd hard-stops the pipeline before spending over budget.
  • Observable — every page logs prompt_tokens, completion_tokens, cost_usd, error to Delta.

License

MIT. See LICENSE.

Metadata

Release files for sparkocr-vlm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparkocr-vlm 0.1.0
File Size Uploaded Provenance Actions
sparkocr_vlm-0.1.0.tar.gz 146.8 kB Without provenance Details for sparkocr_vlm-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sparkocr-vlm 0.1.0
File Interpreter ABI Platform Actions
sparkocr_vlm-0.1.0-py3-none-any.whl Python 3 none any Details for sparkocr_vlm-0.1.0-py3-none-any.whl

Total release size: 173.9 kB

Release files / sparkocr_vlm-0.1.0.tar.gz

Download URL sparkocr_vlm-0.1.0.tar.gz
Size 146.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0977b11bc6f81f6b992de6c0727a65344af97e68a0be363b1df5d5e7d8b7829b
BLAKE2b-256 checksum
How to use checksums
b6750062a9d826ffa7d1d53f06869ed1ee4d85fc01e2084bacfa57b88aedcc24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / sparkocr_vlm-0.1.0-py3-none-any.whl

Download URL sparkocr_vlm-0.1.0-py3-none-any.whl
Size 27.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3fd5eea22f24cc1ad92f75ce8baa44026347fcce0fc19ca51de769aa5a0ba7c
BLAKE2b-256 checksum
How to use checksums
c0bdac05e71d17955790a586003f68f99a34325efd183991d4e6392bbb093fa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for sparkocr-vlm 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page