File size: 7,573 Bytes
89f58ba 400d1bf 89f58ba 763d5c9 400d1bf 89f58ba 400d1bf 89f58ba 763d5c9 89f58ba f9358c9 400d1bf f9358c9 89f58ba 400d1bf 89f58ba 3ef9d1f 400d1bf 763d5c9 89f58ba 400d1bf 89f58ba 43324c8 400d1bf 89f58ba 400d1bf 89f58ba 400d1bf 89f58ba 7165fc4 cea7723 43324c8 400d1bf 89f58ba 400d1bf 89f58ba 400d1bf 89f58ba 400d1bf 89f58ba cea7723 400d1bf b3cfa7b 89f58ba 400d1bf 89f58ba cea7723 89f58ba 7165fc4 400d1bf b3cfa7b 89f58ba 400d1bf 89f58ba 400d1bf 89f58ba 400d1bf cea7723 43324c8 400d1bf 89f58ba 763d5c9 400d1bf 89f58ba 43324c8 cea7723 43324c8 3f59035 cea7723 43324c8 cea7723 43324c8 cea7723 400d1bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
---
viewer: false
tags: [uv-script, ocr, vision-language-model, document-processing]
---
# OCR UV Scripts
> Part of [uv-scripts](https://huggingface.co/uv-scripts) - ready-to-run ML tools powered by UV
Ready-to-run OCR scripts that work with `uv run` - no setup required!
## π Quick Start with HuggingFace Jobs
Run OCR on any dataset without needing your own GPU:
```bash
# Quick test with 10 samples
hf jobs uv run --flavor l4x1 \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
your-input-dataset your-output-dataset \
--max-samples 10
```
That's it! The script will:
- β
Process first 10 images from your dataset
- β
Add OCR results as a new `markdown` column
- β
Push the results to a new dataset
- π View results at: `https://huggingface.co/datasets/[your-output-dataset]`
## π Available Scripts
### RolmOCR (`rolm-ocr.py`)
Fast general-purpose OCR using [reducto/RolmOCR](https://huggingface.co/reducto/RolmOCR) based on Qwen2.5-VL-7B:
- π **Fast extraction** - Optimized for speed and efficiency
- π **Plain text output** - Clean, natural text representation
- πͺ **General-purpose** - Works well on various document types
- π₯ **Large context** - Handles up to 16K tokens
- β‘ **Batch optimized** - Efficient processing with vLLM
### Nanonets OCR (`nanonets-ocr.py`)
State-of-the-art document OCR using [nanonets/Nanonets-OCR-s](https://huggingface.co/nanonets/Nanonets-OCR-s) that handles:
- π **LaTeX equations** - Mathematical formulas preserved
- π **Tables** - Extracted as HTML format
- π **Document structure** - Headers, lists, formatting maintained
- πΌοΈ **Images** - Captions and descriptions included
- βοΈ **Forms** - Checkboxes rendered as β/β
### SmolDocling (`smoldocling-ocr.py`)
Ultra-compact document understanding using [ds4sd/SmolDocling-256M-preview](https://huggingface.co/ds4sd/SmolDocling-256M-preview) with only 256M parameters:
- π·οΈ **DocTags format** - Efficient XML-like representation
- π» **Code blocks** - Preserves indentation and syntax
- π’ **Formulas** - Mathematical expressions with layout
- π **Tables & charts** - Structured data extraction
- π **Layout preservation** - Bounding boxes and spatial info
- β‘ **Ultra-fast** - Tiny model size for quick inference
### NuMarkdown (`numarkdown-ocr.py`)
Advanced reasoning-based OCR using [numind/NuMarkdown-8B-Thinking](https://huggingface.co/numind/NuMarkdown-8B-Thinking) that analyzes documents before converting to markdown:
- π§ **Reasoning Process** - Thinks through document layout before generation
- π **Complex Tables** - Superior table extraction and formatting
- π **Mathematical Formulas** - Accurate LaTeX/math notation preservation
- π **Multi-column Layouts** - Handles complex document structures
- β¨ **Thinking Traces** - Optional inclusion of reasoning process with `--include-thinking`
## π New Features
### Multi-Model Comparison Support
All scripts now include `inference_info` tracking for comparing multiple OCR models:
```bash
# First model
uv run rolm-ocr.py my-dataset my-dataset --max-samples 100
# Second model (appends to same dataset)
uv run nanonets-ocr.py my-dataset my-dataset --max-samples 100
# View all models used
python -c "import json; from datasets import load_dataset; ds = load_dataset('my-dataset'); print(json.loads(ds[0]['inference_info']))"
```
### Random Sampling
Get representative samples with the new `--shuffle` flag:
```bash
# Random 50 samples instead of first 50
uv run rolm-ocr.py ordered-dataset output --max-samples 50 --shuffle
# Reproducible random sampling
uv run nanonets-ocr.py dataset output --max-samples 100 --shuffle --seed 42
```
### Automatic Dataset Cards
Every OCR run now generates comprehensive dataset documentation including:
- Model configuration and parameters
- Processing statistics
- Column descriptions
- Reproduction instructions
## π» Usage Examples
### Run on HuggingFace Jobs (Recommended)
No GPU? No problem! Run on HF infrastructure:
```bash
# Basic OCR job
hf jobs uv run --flavor l4x1 \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
your-input-dataset your-output-dataset
# Real example with UFO dataset πΈ
hf jobs uv run \
--flavor a10g-large \
--image vllm/vllm-openai:latest \
-s HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
davanstrien/ufo-ColPali \
your-username/ufo-ocr \
--image-column image \
--max-model-len 16384 \
--batch-size 128
# NuMarkdown with reasoning traces for complex documents
hf jobs uv run \
--image vllm/vllm-openai:latest \
--flavor l4x4 \
-s HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/numarkdown-ocr.py \
your-input-dataset your-output-dataset \
--max-samples 50 \
--include-thinking \
--shuffle
# Private dataset with custom settings
hf jobs uv run --flavor l40sx1 \
-s HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
private-input private-output \
--private \
--batch-size 32
```
### Python API
```python
from huggingface_hub import run_uv_job
job = run_uv_job(
"https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py",
args=["input-dataset", "output-dataset", "--batch-size", "16"],
flavor="l4x1"
)
```
### Run Locally (Requires GPU)
```bash
# Clone and run
git clone https://huggingface.co/datasets/uv-scripts/ocr
cd ocr
uv run nanonets-ocr.py input-dataset output-dataset
# Or run directly from URL
uv run https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
input-dataset output-dataset
# RolmOCR for fast text extraction
uv run rolm-ocr.py documents extracted-text
uv run rolm-ocr.py images texts --shuffle --max-samples 100 # Random sample
```
## π Works With
Any HuggingFace dataset containing images - documents, forms, receipts, books, handwriting.
## ποΈ Configuration Options
### Common Options (All Scripts)
| Option | Default | Description |
| -------------------------- | ------- | ----------------------------- |
| `--image-column` | `image` | Column containing images |
| `--batch-size` | `32`/`16`* | Images processed together |
| `--max-model-len` | `8192`/`16384`** | Max context length |
| `--max-tokens` | `4096`/`8192`** | Max output tokens |
| `--gpu-memory-utilization` | `0.8` | GPU memory usage (0.0-1.0) |
| `--split` | `train` | Dataset split to process |
| `--max-samples` | None | Limit samples (for testing) |
| `--private` | False | Make output dataset private |
| `--shuffle` | False | Shuffle dataset before processing |
| `--seed` | `42` | Random seed for shuffling |
*RolmOCR uses batch size 16
**RolmOCR uses 16384/8192
### RolmOCR Specific
- Output column is auto-generated from model name (e.g., `rolmocr_text`)
- Use `--output-column` to override the default name
π‘ **Performance tip**: Increase batch size for faster processing (e.g., `--batch-size 128` for A10G GPUs)
More OCR VLM Scripts coming soon! Stay tuned for updates!
|