File size: 3,777 Bytes
89f58ba
 
 
 
 
400d1bf
89f58ba
763d5c9
 
400d1bf
89f58ba
400d1bf
89f58ba
763d5c9
89f58ba
 
400d1bf
 
 
89f58ba
 
400d1bf
89f58ba
400d1bf
 
 
763d5c9
89f58ba
400d1bf
89f58ba
400d1bf
89f58ba
400d1bf
89f58ba
400d1bf
 
 
 
 
89f58ba
400d1bf
89f58ba
400d1bf
 
 
89f58ba
 
400d1bf
89f58ba
 
400d1bf
89f58ba
400d1bf
 
 
 
 
89f58ba
400d1bf
 
89f58ba
 
400d1bf
89f58ba
400d1bf
 
 
89f58ba
400d1bf
 
89f58ba
 
 
 
 
 
 
 
 
 
400d1bf
 
89f58ba
 
 
400d1bf
 
 
 
 
 
 
 
 
 
 
 
89f58ba
763d5c9
 
 
 
400d1bf
89f58ba
400d1bf
 
 
 
 
 
 
 
 
 
89f58ba
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
---
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
hf jobs uv run --flavor l4x1 \
    https://huggingface.co/datasets/uv-scripts/ocr/raw/main/nanonets-ocr.py \
    your-input-dataset your-output-dataset
```

That's it! The script will:

- ✅ Process all images in 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

### 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 ☐/☑

## 💻 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 \
    -e 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 64

# Private dataset with custom settings
hf jobs uv run --flavor l40sx1 \
    -e 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
```

## 📁 Works With

Any HuggingFace dataset containing images - documents, forms, receipts, books, handwriting.

## 🎛️ Configuration Options

| Option                     | Default | Description                 |
| -------------------------- | ------- | --------------------------- |
| `--image-column`           | `image` | Column containing images    |
| `--batch-size`             | `8`     | Images processed together   |
| `--max-model-len`          | `8192`  | Max context length          |
| `--max-tokens`             | `4096`  | Max output tokens           |
| `--gpu-memory-utilization` | `0.7`   | GPU memory usage            |
| `--split`                  | `train` | Dataset split to process    |
| `--max-samples`            | None    | Limit samples (for testing) |
| `--private`                | False   | Make output dataset private |

More OCR VLM Scripts coming soon! Stay tuned for updates!