File size: 27,250 Bytes
ec846c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "datasets>=4.0.0",
#     "huggingface-hub",
#     "pillow",
#     "requests",
# ]
# ///

"""
Convert document images to markdown using OvisOCR2 via an in-job vLLM server.

Same model, prompt, and outputs as ovis-ocr2.py, but serves the model behind
`vllm serve` inside the job and posts images concurrently — continuous batching
stays fed instead of draining at each offline `llm.generate()` batch barrier,
and a bad image fails one request instead of a whole batch of 16.

This script is the *driver* half: it expects the server on localhost (started
by the job command below), loads the input dataset, posts images concurrently,
postprocesses (repeat-trim + img-tag filter, as in ovis-ocr2.py), and pushes
the result dataset. The driver has no torch/vllm deps, so `uv run` starts in
seconds while the server warms up in parallel.

NOTE: OvisOCR2's card documents offline vLLM only (checked 2026-07-16) — this
server translation is ours, not the authors'. The serve flags below mirror the
card's offline args; treat A/B output parity with ovis-ocr2.py as part of any
benchmark run (same --max-samples slice, diff the markdown columns).

Run on HF Jobs (standard uv-run shape — the script starts `vllm serve` itself
as a subprocess when no server is already reachable; the only thing to get
right is the --image flag, which provides the `vllm` binary):

  hf jobs uv run --detach --flavor l4x1 -s HF_TOKEN --timeout 4h \\
      --image vllm/vllm-openai:v0.22.1 \\
      https://huggingface.co/datasets/uv-scripts/ocr/raw/main/ovis-ocr2-server.py \\
      <input-dataset> <output-dataset>

To use an already-running or remote endpoint instead, pass --server URL — the
script only spawns a server when the (default localhost) URL is unreachable.
The serve flags live in SERVE_ARGS below, the single source of truth.

Serve-flag provenance (the card only shows offline `LLM(...)` args):
- `--no-enable-prefix-caching --mm-processor-cache-gb 0`: the recurring official
  OCR-serving pattern (DeepSeek-OCR vLLM recipe, LightOnOCR, PaddleOCR-VL recipe)
  — OCR never reuses images, the caches only cost memory.
- `--mm-processor-kwargs`: server-side equivalent of the card's per-request
  `images_kwargs` pixel bounds. The driver ALSO downscales oversized images
  client-side to the same max_pixels, so outputs match even if the server flag
  is dropped.
- The card's offline `gdn_prefill_backend="triton"` (JIT/nvcc workaround for the
  bare uv image) is NOT needed here: the vllm-openai image ships the full CUDA
  toolchain. Add `--gdn-prefill-backend triton` to the serve command if the
  default backend misbehaves.
- `enable_thinking=False` (card-mandated, Qwen3.5 templates can inject a
  thinking preamble) is passed per-request via `chat_template_kwargs`.

Model: ATH-MaaS/OvisOCR2 (0.9B, Apache-2.0, 96.58 OmniDocBench v1.6)
vLLM: stock Qwen3_5ForConditionalGeneration arch, needs vllm >= 0.22.1.
"""

import argparse
import atexit
import base64
import concurrent.futures
import io
import json
import logging
import math
import os
import shutil
import subprocess
import sys
import threading
import time
from datetime import datetime
from typing import Any, Dict, Union
from urllib.parse import urlparse

import requests
from datasets import load_dataset
from huggingface_hub import DatasetCard, login
from PIL import Image

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger(__name__)

MODEL = "ATH-MaaS/OvisOCR2"

# Fixed instruction prompt, verbatim from the model card (including the leading newline).
# The card warns outputs are tuned to this exact wording — don't "improve" it.
OCR_PROMPT = (
    "\nExtract all readable content from the image in natural human reading order "
    "and output the result as a single Markdown document. For charts or images, "
    'represent them using an HTML image tag: <img src="images/bbox_{left}_{top}_'
    '{right}_{bottom}.jpg" />, where left, top, right, bottom are bounding box '
    "coordinates scaled to [0, 1000). Format formulas as LaTeX. Format tables as "
    "HTML: <table>...</table>. Transcribe all other text as standard Markdown. "
    "Preserve the original text without translation or paraphrasing."
)

# Image bounds from the model card's parser.
DEFAULT_MIN_PIXELS = 448 * 448  # 200,704
DEFAULT_MAX_PIXELS = 2880 * 2880  # 8,294,400

# The serve command this script spawns when no server is reachable — single
# source of truth for the serving configuration (see docstring for provenance).
SERVE_ARGS = [
    "vllm", "serve", MODEL,
    "--max-model-len", "32768",
    "--gpu-memory-utilization", "0.85",
    "--limit-mm-per-prompt", '{"image": 1}',
    "--no-enable-prefix-caching",
    "--mm-processor-cache-gb", "0",
    "--mm-processor-kwargs",
    f'{{"images_kwargs": {{"min_pixels": {DEFAULT_MIN_PIXELS}, "max_pixels": {DEFAULT_MAX_PIXELS}}}}}',
    "--port", "8000",
]

RUN_COMMAND = (
    "hf jobs uv run --detach --flavor l4x1 -s HF_TOKEN --timeout 4h \\\n"
    "    --image vllm/vllm-openai:v0.22.1 \\\n"
    "    https://huggingface.co/datasets/uv-scripts/ocr/raw/main/ovis-ocr2-server.py \\\n"
    "    <input-dataset> <output-dataset>"
)


def ensure_output_columns_free(dataset, columns, overwrite=False):
    """Fail fast if an output column would collide with an existing input column.

    Adding a column that already exists silently overwrites it (e.g. a ground-truth
    `text`/`markdown` column) or crashes on push with a duplicate-column error only
    *after* inference has run. Catch it up front. With overwrite=True, drop the clashing
    column(s) here instead (logged) so the later add_column is clean.
    """
    clash = [c for c in columns if c in dataset.column_names]
    if not clash:
        return dataset
    if overwrite:
        logger.warning(f"--overwrite: replacing existing column(s) {clash}")
        return dataset.remove_columns(clash)
    logger.error(
        f"Output column(s) {clash} already exist in the input dataset "
        f"(columns: {dataset.column_names})."
    )
    logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
    sys.exit(1)


def to_pil_image(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
    """Convert a dataset image cell (PIL image, bytes dict, or path) to RGB PIL."""
    if isinstance(image, Image.Image):
        pil_img = image
    elif isinstance(image, dict) and "bytes" in image:
        pil_img = Image.open(io.BytesIO(image["bytes"]))
    elif isinstance(image, str):
        pil_img = Image.open(image)
    else:
        raise ValueError(f"Unsupported image type: {type(image)}")
    return pil_img.convert("RGB")


def encode_image(image, max_pixels: int) -> str:
    """RGB-convert, downscale to max_pixels if oversized, return base64 JPEG.

    The server's processor clamps to the same bound, so this only changes where
    the downscale happens — doing it client-side shrinks the request payload
    (a 30MP scan is ~4x the bytes of its 8.3MP clamp) and keeps outputs
    identical even if the serve command omits --mm-processor-kwargs.
    """
    img = to_pil_image(image)
    w, h = img.size
    if w * h > max_pixels:
        scale = math.sqrt(max_pixels / (w * h))
        img = img.resize((max(1, int(w * scale)), max(1, int(h * scale))), Image.LANCZOS)
    buf = io.BytesIO()
    img.save(buf, format="JPEG", quality=95)
    return base64.b64encode(buf.getvalue()).decode()


def clean_truncated_repeats(
    text: str,
    min_text_len: int = 8000,
    max_period: int = 200,
    min_period: int = 1,
    min_repeat_chars: int = 100,
    min_repeat_times: int = 5,
) -> str:
    """Trim degenerate trailing repetition (verbatim port of the model card's cleanup).

    Long outputs that hit max_tokens can end in a repeated unit (a char, phrase, or
    table row); this detects the shortest repeating tail unit and keeps one copy.
    """
    n = len(text)
    if n < min_text_len:
        return text

    max_period = min(max_period, n - 1)
    for unit_len in range(min_period, max_period + 1):
        if text[n - 1] != text[n - 1 - unit_len]:
            continue

        match_len = 1
        idx = n - 2
        while idx >= unit_len and text[idx] == text[idx - unit_len]:
            match_len += 1
            idx -= 1

        total_len = match_len + unit_len
        repeat_times = total_len // unit_len
        tail_len = total_len % unit_len

        if repeat_times >= min_repeat_times and total_len >= min_repeat_chars:
            return text[: n - total_len + unit_len] + text[n - tail_len :]

    return text


def filter_image_tags(text: str) -> str:
    """Drop visual-region <img> blocks (upstream parser's default behaviour)."""
    return "\n\n".join(
        block
        for block in text.split("\n\n")
        if not block.strip().startswith('<img src="images/bbox_')
    )


def postprocess_output(text: str, keep_image_tags: bool) -> str:
    text = text.strip()
    if not keep_image_tags:
        text = filter_image_tags(text)
    return clean_truncated_repeats(text)


def server_alive(server: str) -> bool:
    try:
        return requests.get(f"{server}/health", timeout=5).status_code == 200
    except requests.RequestException:
        return False


def wait_for_server(server: str, timeout_s: int, proc: "subprocess.Popen | None" = None):
    logger.info(f"Waiting for server at {server}...")
    deadline = time.time() + timeout_s
    while time.time() < deadline:
        if server_alive(server):
            logger.info("Server is ready")
            return
        if proc is not None and proc.poll() is not None:
            logger.error(f"Spawned vllm serve exited with code {proc.returncode} before becoming ready")
            sys.exit(1)
        time.sleep(10)
    logger.error(f"Server did not become ready within {timeout_s}s")
    sys.exit(1)


def ensure_server(server: str, timeout_s: int = 1800):
    """Use a reachable server; otherwise spawn `vllm serve` ourselves; else fail fast.

    Spawning is only attempted for a localhost URL — a remote --server that is
    down is the user's to fix, not ours to shadow with a local model.
    """
    if server_alive(server):
        logger.info(f"Using already-running server at {server}")
        return

    host = urlparse(server).hostname or ""
    if host not in ("127.0.0.1", "localhost", "0.0.0.0"):
        logger.info(f"Remote server {server} not up yet — waiting for it")
        wait_for_server(server, timeout_s)
        return

    if shutil.which("vllm") is None:
        logger.error("No server is running and the `vllm` binary is not on PATH.")
        logger.error("Run this script on a vLLM image so it can start the server itself:\n")
        logger.error(RUN_COMMAND)
        logger.error("\n(or start `vllm serve` yourself / pass --server URL of a running endpoint)")
        sys.exit(1)

    logger.info(f"Starting server: {' '.join(SERVE_ARGS)}")
    proc = subprocess.Popen(SERVE_ARGS)  # logs interleave with ours on stdout/stderr
    atexit.register(proc.terminate)  # don't leave a GPU server behind on local runs
    wait_for_server(server, timeout_s, proc=proc)


def ocr_one(
    server: str,
    image,
    max_pixels: int,
    max_tokens: int,
    timeout_s: int,
    retries: int = 2,
) -> str:
    """OCR a single image via the chat completions API. Returns raw model text."""
    b64 = encode_image(image, max_pixels)
    payload = {
        "model": MODEL,
        "messages": [
            {
                "role": "user",
                "content": [
                    # Image first, then text — same order the card's chat template uses.
                    {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
                    {"type": "text", "text": OCR_PROMPT},
                ],
            }
        ],
        "temperature": 0.0,
        "max_tokens": max_tokens,
        # Card-mandated: Qwen3.5 templates can inject a thinking preamble otherwise.
        "chat_template_kwargs": {"enable_thinking": False},
    }
    last_err = None
    for attempt in range(retries + 1):
        try:
            resp = requests.post(
                f"{server}/v1/chat/completions", json=payload, timeout=timeout_s
            )
            resp.raise_for_status()
            return resp.json()["choices"][0]["message"]["content"]
        except Exception as e:
            last_err = e
            if attempt < retries:
                time.sleep(10 * (attempt + 1))
    raise RuntimeError(f"request failed after {retries + 1} attempts: {last_err}")


def create_dataset_card(
    source_dataset: str,
    model: str,
    num_samples: int,
    num_errors: int,
    processing_time: str,
    images_per_sec: float,
    concurrency: int,
    max_tokens: int,
    keep_image_tags: bool,
    image_column: str = "image",
    split: str = "train",
) -> str:
    """Create a dataset card documenting the OCR process."""
    model_name = model.split("/")[-1]

    # Canonical provenance stamp (see AGENTS.md): Jobs claim gated on JOB_ID, set by HF Jobs in-container.
    on_jobs = os.environ.get("JOB_ID") is not None
    hw = os.environ.get("ACCELERATOR") or ""
    origin = (
        "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
        + (f" (`{hw}`)" if hw else "")
    ) if on_jobs else "Generated"
    jobs_tag = "\n- hf-jobs" if on_jobs else ""

    return f"""---
tags:
- ocr
- document-processing
- ovis-ocr2
- markdown
- uv-script
- generated{jobs_tag}
---

# Document OCR using {model_name} (server mode)

This dataset contains OCR results from images in [{source_dataset}](https://huggingface.co/datasets/{source_dataset}) using OvisOCR2, a compact 0.9B document parsing model (96.58 on OmniDocBench v1.6), served behind an in-job vLLM server with concurrent requests (continuous batching).

## Processing Details

- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})
- **Model**: [{model}](https://huggingface.co/{model})
- **Number of Samples**: {num_samples:,}
- **Failed Requests**: {num_errors:,} (marked `[OCR ERROR]`)
- **Processing Time**: {processing_time}
- **Throughput**: {images_per_sec:.2f} images/sec
- **Processing Date**: {datetime.now().strftime("%Y-%m-%d %H:%M UTC")}

### Configuration

- **Mode**: vLLM server (`vllm serve`) + concurrent driver, {concurrency} concurrent requests
- **Image Column**: `{image_column}`
- **Dataset Split**: `{split}`
- **Max Output Tokens**: {max_tokens:,}
- **Temperature**: 0.0 (greedy, per model card)
- **Visual-region image tags**: {"kept" if keep_image_tags else "filtered (default)"}

## Dataset Structure

The dataset contains all original columns plus:
- `markdown`: The extracted text in markdown format
- `inference_info`: JSON list tracking all OCR models applied to this dataset

## Reproduction

{origin} with the [`ovis-ocr2-server.py`](https://huggingface.co/datasets/uv-scripts/ocr/raw/main/ovis-ocr2-server.py) recipe from [uv-scripts](https://huggingface.co/uv-scripts) — see the script docstring for the single `hf jobs run` command that starts the server and driver together. The offline-vLLM sibling recipe is [`ovis-ocr2.py`](https://huggingface.co/datasets/uv-scripts/ocr/raw/main/ovis-ocr2.py).
"""


def main(
    input_dataset: str,
    output_dataset: str,
    image_column: str = "image",
    server: str = "http://127.0.0.1:8000",
    concurrency: int = 32,
    max_tokens: int = 16384,
    max_pixels: int = DEFAULT_MAX_PIXELS,
    request_timeout: int = 1800,
    keep_image_tags: bool = False,
    hf_token: str = None,
    split: str = "train",
    max_samples: int = None,
    private: bool = False,
    shuffle: bool = False,
    seed: int = 42,
    output_column: str = "markdown",
    overwrite: bool = False,
    config: str = None,
    create_pr: bool = False,
):
    """Process images from HF dataset through an OvisOCR2 vLLM server."""

    start_time = datetime.now()

    HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
    if HF_TOKEN:
        login(token=HF_TOKEN)

    logger.info(f"Using model: {MODEL} via server {server}")

    logger.info(f"Loading dataset: {input_dataset}")
    dataset = load_dataset(input_dataset, split=split)

    if image_column not in dataset.column_names:
        raise ValueError(
            f"Column '{image_column}' not found. Available: {dataset.column_names}"
        )

    dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)

    if shuffle:
        logger.info(f"Shuffling dataset with seed {seed}")
        dataset = dataset.shuffle(seed=seed)

    if max_samples:
        dataset = dataset.select(range(min(max_samples, len(dataset))))
        logger.info(f"Limited to {len(dataset)} samples")

    # Reuse a reachable server, else spawn `vllm serve` (needs the vllm binary,
    # i.e. a vllm/vllm-openai image), else fail fast with the correct command.
    ensure_server(server)

    n = len(dataset)
    logger.info(f"Processing {n} images, concurrency {concurrency}")
    all_outputs = [None] * n
    errors = 0
    done = 0
    inference_start = time.time()
    lock = threading.Lock()

    def worker(i: int) -> None:
        nonlocal errors, done
        try:
            text = ocr_one(
                server,
                dataset[i][image_column],
                max_pixels,
                max_tokens,
                request_timeout,
            )
            all_outputs[i] = postprocess_output(text, keep_image_tags)
        except Exception as e:
            logger.error(f"Image {i} failed: {e}")
            all_outputs[i] = "[OCR ERROR]"
            with lock:
                errors += 1
        with lock:
            done += 1
            if done % 25 == 0 or done == n:
                rate = done / max(time.time() - inference_start, 1e-9)
                logger.info(f"{done}/{n} done ({rate:.2f} img/s, {errors} errors)")

    with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool:
        list(pool.map(worker, range(n)))

    inference_secs = time.time() - inference_start
    processing_duration = datetime.now() - start_time
    processing_time_str = f"{processing_duration.total_seconds() / 60:.1f} min"
    images_per_sec = n / inference_secs if inference_secs else 0.0

    logger.info(f"Adding '{output_column}' column to dataset")
    dataset = dataset.add_column(output_column, all_outputs)

    # Inference info tracking
    inference_entry = {
        "model_id": MODEL,
        "model_name": "OvisOCR2",
        "column_name": output_column,
        "timestamp": datetime.now().isoformat(),
        "temperature": 0.0,
        "max_tokens": max_tokens,
        "max_pixels": max_pixels,
        "keep_image_tags": keep_image_tags,
        "mode": "vllm-server",
        "concurrency": concurrency,
    }

    if "inference_info" in dataset.column_names:
        logger.info("Updating existing inference_info column")

        def update_inference_info(example):
            try:
                existing_info = (
                    json.loads(example["inference_info"])
                    if example["inference_info"]
                    else []
                )
            except (json.JSONDecodeError, TypeError):
                existing_info = []
            existing_info.append(inference_entry)
            return {"inference_info": json.dumps(existing_info)}

        dataset = dataset.map(update_inference_info)
    else:
        logger.info("Creating new inference_info column")
        inference_list = [json.dumps([inference_entry])] * len(dataset)
        dataset = dataset.add_column("inference_info", inference_list)

    # Push to hub with retry and XET fallback
    logger.info(f"Pushing to {output_dataset}")
    max_retries = 3
    for attempt in range(1, max_retries + 1):
        try:
            if attempt > 1:
                logger.warning("Disabling XET (fallback to HTTP upload)")
                os.environ["HF_HUB_DISABLE_XET"] = "1"
            dataset.push_to_hub(
                output_dataset,
                private=private,
                token=HF_TOKEN,
                max_shard_size="500MB",
                **({"config_name": config} if config else {}),
                create_pr=create_pr,
                commit_message=f"Add {MODEL} OCR results ({len(dataset)} samples, server mode)"
                + (f" [{config}]" if config else ""),
            )
            break
        except Exception as e:
            logger.error(f"Upload attempt {attempt}/{max_retries} failed: {e}")
            if attempt < max_retries:
                delay = 30 * (2 ** (attempt - 1))
                logger.info(f"Retrying in {delay}s...")
                time.sleep(delay)
            else:
                logger.error("All upload attempts failed. OCR results are lost.")
                sys.exit(1)

    logger.info("Creating dataset card")
    card_content = create_dataset_card(
        source_dataset=input_dataset,
        model=MODEL,
        num_samples=len(dataset),
        num_errors=errors,
        processing_time=processing_time_str,
        images_per_sec=images_per_sec,
        concurrency=concurrency,
        max_tokens=max_tokens,
        keep_image_tags=keep_image_tags,
        image_column=image_column,
        split=split,
    )

    card = DatasetCard(card_content)
    card.push_to_hub(output_dataset, token=HF_TOKEN)

    logger.info("Done! OvisOCR2 server-mode processing complete.")
    logger.info(
        f"Dataset available at: https://huggingface.co/datasets/{output_dataset}"
    )
    logger.info(f"Processing time: {processing_time_str}")
    logger.info(
        f"Throughput: {images_per_sec:.2f} images/sec "
        f"(inference only, excl. dataset load/push; {errors} errors)"
    )


if __name__ == "__main__":
    if len(sys.argv) == 1:
        print("=" * 70)
        print("OvisOCR2 Document Processing (vLLM server mode)")
        print("=" * 70)
        print("\nSame model + outputs as ovis-ocr2.py, but drives an in-job")
        print("`vllm serve` with concurrent requests — no batch barriers,")
        print("per-image (not per-batch) failure isolation.")
        print("\nThe server must already be running (the job command starts")
        print("both — see the module docstring for the full `hf jobs run`).")
        print("\nExamples:")
        print("\n1. Basic OCR (server on localhost:8000):")
        print("   uv run ovis-ocr2-server.py input-dataset output-dataset")
        print("\n2. Test with a small sample:")
        print("   uv run ovis-ocr2-server.py large-dataset test --max-samples 10 --shuffle")
        print("\n3. Throughput A/B vs the offline recipe:")
        print("   run both scripts on the same --max-samples slice and compare")
        print("   the images/sec lines + diff the markdown columns")
        print("\nFor full help: uv run ovis-ocr2-server.py --help")
        sys.exit(0)

    parser = argparse.ArgumentParser(
        description="Document OCR using OvisOCR2 via an in-job vLLM server",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  uv run ovis-ocr2-server.py my-docs analyzed-docs
  uv run ovis-ocr2-server.py large-dataset test --max-samples 50 --shuffle
See the module docstring for the full `hf jobs run` command (server + driver in one job).
        """,
    )

    parser.add_argument("input_dataset", help="Input dataset ID from Hugging Face Hub")
    parser.add_argument("output_dataset", help="Output dataset ID for Hugging Face Hub")
    parser.add_argument(
        "--image-column",
        default="image",
        help="Column containing images (default: image)",
    )
    parser.add_argument(
        "--server",
        default="http://127.0.0.1:8000",
        help="vLLM server base URL (default: in-job localhost:8000)",
    )
    parser.add_argument(
        "--concurrency",
        type=int,
        default=32,
        help="Concurrent OCR requests (default: 32; vLLM queues excess internally, "
        "so this mainly needs to be high enough to keep continuous batching fed)",
    )
    parser.add_argument(
        "--max-tokens",
        type=int,
        default=16384,
        help="Maximum tokens to generate (default: 16384, the model card value)",
    )
    parser.add_argument(
        "--max-pixels",
        type=int,
        default=DEFAULT_MAX_PIXELS,
        help=f"Maximum image pixels; larger images are downscaled client-side before "
        f"upload (default: {DEFAULT_MAX_PIXELS}, = 2880*2880, the model card value)",
    )
    parser.add_argument(
        "--request-timeout",
        type=int,
        default=1800,
        help="Per-request timeout in seconds (default: 1800)",
    )
    parser.add_argument(
        "--keep-image-tags",
        action="store_true",
        help="Keep visual-region <img src=\"images/bbox_...\"> tags in the output "
        "(default: filtered, matching the upstream parser)",
    )
    parser.add_argument("--hf-token", help="Hugging Face API token")
    parser.add_argument(
        "--split", default="train", help="Dataset split to use (default: train)"
    )
    parser.add_argument(
        "--max-samples",
        type=int,
        help="Maximum number of samples to process (for testing)",
    )
    parser.add_argument(
        "--private", action="store_true", help="Make output dataset private"
    )
    parser.add_argument(
        "--config",
        help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
    )
    parser.add_argument(
        "--create-pr",
        action="store_true",
        help="Create a pull request instead of pushing directly (for parallel benchmarking)",
    )
    parser.add_argument(
        "--shuffle", action="store_true", help="Shuffle dataset before processing"
    )
    parser.add_argument(
        "--seed",
        type=int,
        default=42,
        help="Random seed for shuffling (default: 42)",
    )
    parser.add_argument(
        "--output-column",
        default="markdown",
        help="Column name for output text (default: markdown)",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="Replace the output column if it already exists in the input dataset "
        "(default: error out to avoid clobbering an existing column).",
    )

    args = parser.parse_args()

    main(
        input_dataset=args.input_dataset,
        output_dataset=args.output_dataset,
        image_column=args.image_column,
        server=args.server,
        concurrency=args.concurrency,
        max_tokens=args.max_tokens,
        max_pixels=args.max_pixels,
        request_timeout=args.request_timeout,
        keep_image_tags=args.keep_image_tags,
        hf_token=args.hf_token,
        split=args.split,
        max_samples=args.max_samples,
        private=args.private,
        shuffle=args.shuffle,
        seed=args.seed,
        output_column=args.output_column,
        overwrite=args.overwrite,
        config=args.config,
        create_pr=args.create_pr,
    )