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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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "surya-ocr",
#     "datasets>=3.1.0",
#     "huggingface-hub",
#     "pillow",
#     "toolz",
#     "tqdm",
# ]
# ///
"""
Document intelligence on images OR multi-page PDFs with Datalab's **Surya OCR 2**
(`datalab-to/surya-ocr-2`, 650M, Qwen3.5-style).

Surya is *structured* OCR: instead of a flat markdown blob, it returns per-block
HTML with bounding boxes, reading order, and labels (equations in `<math>`). This
recipe writes **both**:

  --output-column   (default `markdown`)  flattened, reading-order text per row
  surya_blocks                            the full structured result as JSON
                                          (bbox / polygon / label / reading_order /
                                          confidence / html per block), one entry
                                          per page.

Three tasks via `--task`:
  ocr     (default)  full-page OCR  -> text + per-block HTML/bboxes
  layout             layout regions -> labelled boxes + reading order
  table              table structure -> HTML (mode `full`) or rows/cols/cells
                     (mode `simple`, via --table-mode)

Input is one document per row:
  --image-column COL   (default `image`)  one image per row
  --pdf-column COL                        PDF bytes per row (multi-page; honors
                                          --page-range). Pages are concatenated in
                                          the text column and kept per-page in
                                          `surya_blocks`.

ENGINE: Surya normally spawns a vLLM **server** (Docker) — which can't run inside
an HF Job. This script instead does **offline batch inference**: it injects a
custom in-process backend into Surya's `SuryaInferenceManager` that runs vLLM's
offline `LLM().chat()` engine (no server, no HTTP). Surya still owns all the
prompting, image preprocessing, and HTML/bbox parsing — we only swap the
transport. Run on the **`vllm/vllm-openai:v0.20.1`** image (Surya's known-good
vLLM build; the model is the recent, version-sensitive `qwen3_5` architecture).

LICENSE NOTE: Surya's *code* is Apache-2.0 but the *weights* are a modified
OpenRAIL-M license — free for research, personal use, and startups under $5M
funding/revenue, but restricted from competitive use against Datalab's API.
Confirm you are within those terms. https://huggingface.co/datalab-to/surya-ocr-2

HF Jobs (use the pinned vLLM image so vLLM + qwen3_5 support are present):

    hf jobs uv run --flavor l4x1 -s HF_TOKEN \\
        --image vllm/vllm-openai:v0.20.1 --python /usr/local/bin/python3 \\
        -e PYTHONPATH=/usr/local/lib/python3.12/site-packages \\
        https://huggingface.co/datasets/uv-scripts/ocr/raw/main/surya-ocr.py \\
        INPUT_DATASET OUTPUT_DATASET \\
        --max-samples 5 --shuffle --seed 42

Model: datalab-to/surya-ocr-2  (package: surya-ocr, https://github.com/datalab-to/surya)
"""

import argparse
import io
import json
import logging
import math
import os
import sys
import tempfile
import time
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Tuple
from urllib.request import urlopen

from datasets import load_dataset
from huggingface_hub import DatasetCard, login
from PIL import Image
from toolz import partition_all
from tqdm import tqdm

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

DEFAULT_MODEL = "datalab-to/surya-ocr-2"
# Surya's own vision-tiling bounds (from its vLLM backend), applied to the
# offline engine too so preprocessing matches the server path exactly.
MM_PROCESSOR_KWARGS = {"min_pixels": 3136, "max_pixels": 6291456}
TASKS = ("ocr", "layout", "table")


def check_cuda_availability() -> None:
    """Exit early with a clear message if there's no GPU."""
    import torch

    if not torch.cuda.is_available():
        logger.error("CUDA is not available. This script requires a GPU.")
        logger.error(
            "Run on Hugging Face Jobs with: hf jobs uv run --flavor l4x1 "
            "--image vllm/vllm-openai:v0.20.1 ..."
        )
        sys.exit(1)
    logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")


def check_vllm_available() -> None:
    """Fail fast (before loading 400 rows) if vLLM isn't importable.

    Surya-2 runs its VLM through vLLM's offline engine, but `vllm` is deliberately
    NOT a PEP723 dependency: the recent hybrid `qwen3_5` architecture is only in the
    pinned `vllm/vllm-openai:v0.20.1` image, which also provides torch/transformers via
    PYTHONPATH. Launched on the bare uv image (no `--image`), the import fails per-batch
    and every row silently gets "[SURYA GENERATE ERROR]". Detect that up front instead.
    """
    import importlib.util

    if importlib.util.find_spec("vllm") is None:
        logger.error("vLLM is not importable — this recipe cannot run on the bare uv image.")
        logger.error(
            "Surya-2 needs the pinned vLLM build; re-run with the image + interpreter flags:"
        )
        logger.error(
            "  hf jobs uv run --flavor l4x1 -s HF_TOKEN \\\n"
            "      --image vllm/vllm-openai:v0.20.1 --python /usr/local/bin/python3 \\\n"
            "      -e PYTHONPATH=/usr/local/lib/python3.12/site-packages \\\n"
            "      <script_url> INPUT_DATASET OUTPUT_DATASET ..."
        )
        sys.exit(1)


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 parse_page_range(spec: Optional[str]) -> Optional[List[int]]:
    """Turn '0-3,5' into [0,1,2,3,5]. None/empty -> None (all pages)."""
    if not spec:
        return None
    pages: List[int] = []
    for part in spec.split(","):
        part = part.strip()
        if not part:
            continue
        if "-" in part:
            lo, hi = part.split("-", 1)
            pages.extend(range(int(lo), int(hi) + 1))
        else:
            pages.append(int(part))
    return pages or None


def cell_to_bytes(cell: Any) -> bytes:
    """Normalize an HF dataset cell (image or document) to raw file bytes."""
    if isinstance(cell, Image.Image):
        buf = io.BytesIO()
        cell.convert("RGB").save(buf, format="PNG")
        return buf.getvalue()
    if isinstance(cell, dict):
        if cell.get("bytes"):
            return cell["bytes"]
        if cell.get("path"):
            with open(cell["path"], "rb") as f:
                return f.read()
        raise ValueError(
            f"Unsupported image/document dict (no bytes/path): {list(cell)}"
        )
    if isinstance(cell, (bytes, bytearray)):
        return bytes(cell)
    if isinstance(cell, str):
        if cell.startswith(("http://", "https://")):
            return urlopen(cell).read()  # noqa: S310
        with open(cell, "rb") as f:
            return f.read()
    raise ValueError(f"Unsupported cell type: {type(cell)}")


def cell_to_pil(cell: Any) -> Image.Image:
    """One image cell -> RGB PIL image."""
    if isinstance(cell, Image.Image):
        return cell.convert("RGB")
    return Image.open(io.BytesIO(cell_to_bytes(cell))).convert("RGB")


def load_pdf_images(
    load_pdf, cell: Any, page_indices: Optional[List[int]], dpi: int
) -> List[Image.Image]:
    """Render one PDF cell into page images via Surya's own pypdfium2 loader."""
    data = cell_to_bytes(cell)
    with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
        tmp.write(data)
        path = tmp.name
    try:
        images, _ = load_pdf(path, page_indices, dpi=dpi)
        return [im.convert("RGB") for im in images]
    finally:
        os.unlink(path)


# --- structured-output shim (vLLM API moved between versions) ---
def build_structured_outputs(schema: Dict[str, Any]) -> Dict[str, Any]:
    """SamplingParams kwargs for guided JSON, across vLLM versions (layout uses this)."""
    try:
        from vllm.sampling_params import StructuredOutputsParams  # vLLM >= 0.12

        return {"structured_outputs": StructuredOutputsParams(json=schema)}
    except (ImportError, TypeError):
        pass
    try:
        from vllm.sampling_params import GuidedDecodingParams  # older vLLM

        return {"guided_decoding": GuidedDecodingParams(json=schema)}
    except (ImportError, TypeError):
        pass
    logger.warning(
        "Guided JSON unavailable in this vLLM version; relying on the model."
    )
    return {}


def _mean_token_prob(completion_output) -> Optional[float]:
    """Mean exp(logprob) of the sampled tokens -> Surya's per-block `confidence`."""
    lps = getattr(completion_output, "logprobs", None)
    if not lps:
        return None
    probs: List[float] = []
    for tid, lp_dict in zip(completion_output.token_ids, lps):
        if not lp_dict:
            continue
        entry = lp_dict.get(tid)
        if (
            entry is None
        ):  # sampled token not in the returned top-k; use the best we have
            entry = max(lp_dict.values(), key=lambda e: e.logprob)
        probs.append(math.exp(entry.logprob))
    return sum(probs) / len(probs) if probs else None


class OfflineVLLMBackend:
    """Surya `Backend` (duck-typed) that runs vLLM's offline `LLM().chat()` engine.

    Surya's predictors call `manager.generate(batch)` -> `backend.generate(batch)`;
    we satisfy that contract in-process (no server). Surya keeps ownership of the
    prompts (`PROMPT_MAPPING`), image scaling (`scale_to_fit`), and output parsing.
    """

    name = "offline-vllm"

    def __init__(
        self,
        model: str,
        max_model_len: int,
        gpu_memory_utilization: float,
        dtype: str = "bfloat16",
        max_tokens_default: int = 2048,
        logprobs_default: bool = True,
    ):
        self.model = model
        self.max_model_len = max_model_len
        self.gpu_memory_utilization = gpu_memory_utilization
        self.dtype = dtype
        self.max_tokens_default = max_tokens_default
        self.logprobs_default = logprobs_default
        self.llm = None
        self._build_messages = None
        self._scale_to_fit = None
        self._prompt_mapping = None

    def start(self):
        from vllm import LLM

        logger.info(
            f"Loading {self.model} into vLLM offline engine (dtype={self.dtype})..."
        )
        self.llm = LLM(
            model=self.model,
            dtype=self.dtype,
            max_model_len=self.max_model_len,
            gpu_memory_utilization=self.gpu_memory_utilization,
            mm_processor_kwargs=MM_PROCESSOR_KWARGS,
            limit_mm_per_prompt={"image": 1},
        )
        # Reuse Surya's exact request shaping so the offline path matches the server.
        from surya.inference.backends.openai_client import _build_messages
        from surya.inference.prompts import PROMPT_MAPPING
        from surya.inference.util import scale_to_fit

        self._build_messages = _build_messages
        self._scale_to_fit = scale_to_fit
        self._prompt_mapping = PROMPT_MAPPING
        return None

    def stop(self) -> None:
        self.llm = None

    def _sampling_params(self, item):
        from vllm import SamplingParams

        max_tokens = item.max_tokens or self.max_tokens_default
        want_logprobs = item.request_logprobs or self.logprobs_default
        kwargs: Dict[str, Any] = dict(temperature=0.0, top_p=0.1, max_tokens=max_tokens)
        if want_logprobs:
            kwargs["logprobs"] = 1
        if item.guided_json is not None:
            kwargs.update(build_structured_outputs(item.guided_json))
        return SamplingParams(**kwargs)

    def generate(self, batch):
        from surya.inference.schema import BatchOutputItem

        if self.llm is None:
            self.start()
        if not batch:
            return []

        conversations = []
        sampling_params = []
        for item in batch:
            prompt = item.prompt or self._prompt_mapping[item.prompt_type]
            image = self._scale_to_fit(item.image)
            conversations.append(self._build_messages(image, prompt))
            sampling_params.append(self._sampling_params(item))

        outputs = self.llm.chat(
            conversations,
            sampling_params,
            chat_template_content_format="openai",
            use_tqdm=False,
        )

        results = []
        for item, out in zip(batch, outputs):
            comp = out.outputs[0]
            results.append(
                BatchOutputItem(
                    raw=comp.text,
                    token_count=len(comp.token_ids),
                    error=False,
                    mean_token_prob=_mean_token_prob(comp),
                    logprobs=None,
                    metadata=item.metadata,  # carries page_idx/block_idx — must round-trip
                )
            )
        return results


def make_manager(backend: OfflineVLLMBackend):
    """A SuryaInferenceManager wired to our offline backend (bypassing autodetect)."""
    from surya.inference import SuryaInferenceManager

    manager = SuryaInferenceManager.__new__(SuryaInferenceManager)
    manager.method = backend.name
    manager.backend = backend
    return manager


# --- result serialization (text column + structured surya_blocks) ---
def _html_to_text(html: str) -> str:
    from bs4 import BeautifulSoup

    return BeautifulSoup(html, "html.parser").get_text(" ", strip=True)


def serialize_pages(task: str, pages: List[Any]) -> Tuple[str, List[Dict[str, Any]]]:
    """(text, structured-per-page) for one row's page results."""
    structured = [p.model_dump(mode="json") for p in pages]
    page_texts: List[str] = []
    for page in pages:
        if task == "ocr":
            parts = []
            for b in sorted(page.blocks, key=lambda b: b.reading_order):
                if b.skipped or not b.html:
                    continue
                txt = _html_to_text(b.html)
                if txt:
                    parts.append(txt)
            page_texts.append("\n".join(parts))
        elif task == "layout":
            # No OCR text in layout mode — emit a reading-order outline of labels.
            page_texts.append(
                "\n".join(
                    f"{b.position}: {b.label}"
                    for b in sorted(page.bboxes, key=lambda b: b.position)
                )
            )
        else:  # table
            if page.html:  # mode="full"
                page_texts.append(page.html)
            else:  # mode="simple"
                page_texts.append(f"{len(page.rows)} rows x {len(page.cols)} cols")
    return "\n\n".join(page_texts), structured


def create_dataset_card(
    source_dataset: str,
    model: str,
    task: str,
    table_mode: str,
    num_samples: int,
    n_ok: int,
    source_column: str,
    is_pdf: bool,
    page_range: Optional[str],
    output_column: str,
    blocks_column: str,
    split: str,
    processing_time: str,
) -> str:
    input_kind = "PDF documents" if is_pdf else "images"
    col_desc = "PDF" if is_pdf else "image"
    if page_range:
        col_desc += f", pages {page_range}"
    task_desc = {
        "ocr": "full-page OCR (structured HTML + bounding boxes)",
        "layout": "layout analysis (labelled regions + reading order)",
        "table": f"table recognition (mode `{table_mode}`)",
    }[task]
    return f"""---
tags:
- ocr
- document-processing
- surya
- structured
- uv-script
- generated
---

# Surya OCR 2 ({task}) on {source_dataset}

{task_desc.capitalize()} over {input_kind} in
[{source_dataset}](https://huggingface.co/datasets/{source_dataset}) using
[Surya OCR 2](https://huggingface.co/{model}) (650M, Qwen3.5-based) by Datalab, via the
[`surya-ocr`](https://github.com/datalab-to/surya) package, run as **offline vLLM batch
inference** on Hugging Face Jobs.

## Processing Details

- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})
- **Model**: [{model}](https://huggingface.co/{model})
- **Task**: `{task}`{f" (table mode `{table_mode}`)" if task == "table" else ""}
- **Input column**: `{source_column}` ({col_desc})
- **Text column**: `{output_column}` (flattened, reading-order text per row)
- **Structured column**: `{blocks_column}` (JSON: per-page blocks with bbox / polygon / label / reading_order / confidence / html)
- **Split**: `{split}`
- **Samples**: {num_samples:,}
- **Processed OK**: {n_ok:,} / {num_samples:,}
- **Processing time**: {processing_time}
- **Date**: {datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")}

## License note

Surya's code is Apache-2.0, but the model **weights** use a modified OpenRAIL-M
license: free for research, personal use, and startups under $5M funding/revenue,
restricted from competitive use against Datalab's API. See the
[model card](https://huggingface.co/{model}).

## Dataset Structure

Original columns plus:
- `{output_column}`: flattened text (OCR), label outline (layout), or table HTML (table)
- `{blocks_column}`: structured result as a JSON string (one entry per page)
- `inference_info`: JSON list tracking models applied to this dataset

Generated with [UV Scripts](https://huggingface.co/uv-scripts).
"""


def main(
    input_dataset: str,
    output_dataset: str,
    task: str = "ocr",
    table_mode: str = "full",
    image_column: str = "image",
    pdf_column: Optional[str] = None,
    output_column: str = "markdown",
    overwrite: bool = False,
    blocks_column: str = "surya_blocks",
    page_range: Optional[str] = None,
    split: str = "train",
    max_samples: Optional[int] = None,
    shuffle: bool = False,
    seed: int = 42,
    batch_size: int = 16,
    max_model_len: int = 18000,
    gpu_memory_utilization: float = 0.85,
    dtype: str = "bfloat16",
    model: str = DEFAULT_MODEL,
    private: bool = False,
    config: Optional[str] = None,
    create_pr: bool = False,
    hf_token: Optional[str] = None,
    verbose: bool = False,
) -> None:
    # Unlock full Xet bandwidth for the model download (repo convention).
    os.environ["HF_XET_HIGH_PERFORMANCE"] = "1"
    # Surya reads settings from env at import; pin the checkpoint and forbid any
    # server autostart (we inject our own offline backend instead).
    os.environ["SURYA_MODEL_CHECKPOINT"] = model
    os.environ["SURYA_INFERENCE_AUTOSTART"] = "False"

    check_cuda_availability()
    check_vllm_available()
    start_time = datetime.now(timezone.utc)

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

    # Import Surya only after env is set.
    from surya.input.load import load_pdf
    from surya.settings import settings

    source_column = pdf_column or image_column
    is_pdf = pdf_column is not None
    page_indices = parse_page_range(page_range)
    pdf_dpi = settings.IMAGE_DPI_HIGHRES

    logger.info(
        f"Model: {model}  Task: {task}"
        + (f" (mode {table_mode})" if task == "table" else "")
    )
    logger.info(f"Loading dataset: {input_dataset} (split={split})")
    dataset = load_dataset(input_dataset, split=split)
    if source_column not in dataset.column_names:
        logger.error(
            f"Column '{source_column}' not found. Available: {dataset.column_names}"
        )
        sys.exit(1)
    # Fail fast if the output column would collide with an existing input column
    dataset = ensure_output_columns_free(
        dataset, [output_column, blocks_column], overwrite=overwrite
    )
    if shuffle:
        dataset = dataset.shuffle(seed=seed)
    if max_samples:
        dataset = dataset.select(range(min(max_samples, len(dataset))))
    n = len(dataset)
    logger.info(f"Processing {n} documents from column '{source_column}'")

    # Build the offline engine + inject it into a Surya manager, then pick the predictor.
    backend = OfflineVLLMBackend(
        model=model,
        max_model_len=max_model_len,
        gpu_memory_utilization=gpu_memory_utilization,
        dtype=dtype,
    )
    manager = make_manager(backend)

    if task == "ocr":
        from surya.recognition import RecognitionPredictor

        predictor = RecognitionPredictor(manager)

        def run(images):
            return predictor(images, full_page=True)
    elif task == "layout":
        from surya.layout import LayoutPredictor

        predictor = LayoutPredictor(manager)

        def run(images):
            return predictor(images)
    else:  # table
        from surya.table_rec import TableRecPredictor

        predictor = TableRecPredictor(manager)

        def run(images):
            return predictor(images, mode=table_mode)

    texts: List[Optional[str]] = [None] * n
    blocks: List[Optional[str]] = [None] * n
    error_flags: List[bool] = [True] * n

    for chunk in tqdm(list(partition_all(batch_size, range(n))), desc=f"Surya {task}"):
        chunk = list(chunk)
        flat_images: List[Image.Image] = []
        spans: List[Tuple[int, int, int]] = []  # (row_idx, start, count)
        for i in chunk:
            try:
                if is_pdf:
                    imgs = load_pdf_images(
                        load_pdf, dataset[i][source_column], page_indices, pdf_dpi
                    )
                else:
                    imgs = [cell_to_pil(dataset[i][source_column])]
            except Exception as e:
                logger.warning(f"Row {i}: failed to load document: {e}")
                texts[i] = f"[SURYA LOAD ERROR] {e}"
                blocks[i] = None
                continue
            if not imgs:
                texts[i] = "[SURYA EMPTY DOCUMENT]"
                continue
            spans.append((i, len(flat_images), len(imgs)))
            flat_images.extend(imgs)

        if not flat_images:
            continue
        try:
            results = run(flat_images)
        except Exception as e:
            logger.error(f"Batch generate failed: {e}")
            for i, _, _ in spans:
                texts[i] = "[SURYA GENERATE ERROR]"
                blocks[i] = None
            continue

        for i, start, count in spans:
            page_results = results[start : start + count]
            text, structured = serialize_pages(task, page_results)
            texts[i] = text
            blocks[i] = json.dumps(structured, ensure_ascii=False)
            error_flags[i] = False

    n_ok = sum(not f for f in error_flags)
    logger.info(f"Processed OK: {n_ok}/{n}")

    dataset = dataset.add_column(output_column, texts)
    dataset = dataset.add_column(blocks_column, blocks)

    inference_entry = {
        "model": model,
        "model_name": "surya-ocr-2",
        "column_name": output_column,
        "blocks_column": blocks_column,
        "task": task,
        "table_mode": table_mode if task == "table" else None,
        "backend": "vllm-offline",
        "page_range": page_range,
        "error_rate": (n - n_ok) / n if n else 0.0,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "script": "surya-ocr.py",
    }
    if "inference_info" in dataset.column_names:

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

        dataset = dataset.map(update_info)
    else:
        dataset = dataset.add_column(
            "inference_info", [json.dumps([inference_entry])] * n
        )

    processing_time = (
        f"{(datetime.now(timezone.utc) - start_time).total_seconds() / 60:.1f} min"
    )

    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",
                create_pr=create_pr,
                **({"config_name": config} if config else {}),
                commit_message=f"Add Surya OCR 2 {task} results ({n} samples)"
                + (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. Results are lost.")
                sys.exit(1)

    try:
        card = DatasetCard(
            create_dataset_card(
                source_dataset=input_dataset,
                model=model,
                task=task,
                table_mode=table_mode,
                num_samples=n,
                n_ok=n_ok,
                source_column=source_column,
                is_pdf=is_pdf,
                page_range=page_range,
                output_column=output_column,
                blocks_column=blocks_column,
                split=split,
                processing_time=processing_time,
            )
        )
        card.push_to_hub(output_dataset, token=HF_TOKEN)
    except Exception as e:
        logger.warning(f"Could not push dataset card: {e}")

    logger.info("Done! Surya OCR 2 complete.")
    logger.info(f"Dataset: https://huggingface.co/datasets/{output_dataset}")
    logger.info(f"Processing time: {processing_time}")

    if verbose:
        import importlib.metadata

        logger.info("--- Resolved package versions ---")
        for pkg in ["surya-ocr", "vllm", "transformers", "torch", "datasets", "pillow"]:
            try:
                logger.info(f"  {pkg}=={importlib.metadata.version(pkg)}")
            except importlib.metadata.PackageNotFoundError:
                logger.info(f"  {pkg}: not installed")


if __name__ == "__main__":
    if len(sys.argv) == 1:
        print(
            "Surya OCR 2 — structured OCR / layout / tables from images & PDFs (650M)"
        )
        print("\nUsage:")
        print("  uv run surya-ocr.py INPUT OUTPUT [--task ocr|layout|table] [options]")
        print("\nExamples:")
        print("  # full-page OCR -> text + structured surya_blocks")
        print("  uv run surya-ocr.py my-images my-ocr")
        print("\n  # layout regions / table structure")
        print("  uv run surya-ocr.py my-images my-layout --task layout")
        print("  uv run surya-ocr.py my-tables my-tables-out --task table")
        print("\n  # multi-page PDFs")
        print("  uv run surya-ocr.py my-pdfs my-ocr --pdf-column pdf --page-range 0-5")
        print("\nRun on the vllm/vllm-openai:v0.20.1 image (offline vLLM batch).")
        print("For full help: uv run surya-ocr.py --help")
        sys.exit(0)

    parser = argparse.ArgumentParser(
        description="Surya OCR 2 (650M): structured OCR / layout / tables, offline vLLM batch",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Tasks (--task):
  ocr     full-page OCR -> reading-order text + per-block HTML/bboxes (default)
  layout  layout regions -> labelled boxes + reading order
  table   table structure -> HTML (--table-mode full) or rows/cols/cells (simple)

Output columns:
  --output-column   flattened text per row (default: markdown)
  surya_blocks      structured JSON per row (bbox/label/reading_order/confidence/html)

Input (one document per row):
  --image-column COL   one image per row   (default: image)
  --pdf-column COL     PDF bytes per row    (multi-page; honors --page-range)

Run on the vllm/vllm-openai:v0.20.1 image:
  --image vllm/vllm-openai:v0.20.1 --python /usr/local/bin/python3 \\
    -e PYTHONPATH=/usr/local/lib/python3.12/site-packages
""",
    )
    parser.add_argument(
        "input_dataset", help="Input dataset ID from the Hugging Face Hub"
    )
    parser.add_argument(
        "output_dataset", help="Output dataset ID for the Hugging Face Hub"
    )
    parser.add_argument(
        "--task", choices=TASKS, default="ocr", help="Task (default: ocr)"
    )
    parser.add_argument(
        "--table-mode",
        choices=["full", "simple"],
        default="full",
        help="Table task: 'full' = HTML, 'simple' = rows/cols/cells (default: full)",
    )
    parser.add_argument(
        "--image-column", default="image", help="Image column (default: image)"
    )
    parser.add_argument(
        "--pdf-column",
        default=None,
        help="PDF column (bytes/path). Mutually exclusive with --image-column.",
    )
    parser.add_argument(
        "--output-column",
        default="markdown",
        help="Text output column (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).",
    )
    parser.add_argument(
        "--blocks-column",
        default="surya_blocks",
        help="Structured JSON output column (default: surya_blocks)",
    )
    parser.add_argument(
        "--page-range",
        default=None,
        help="Pages from PDFs, e.g. '0-5,7' (PDF column only)",
    )
    parser.add_argument(
        "--split", default="train", help="Dataset split (default: train)"
    )
    parser.add_argument(
        "--max-samples", type=int, help="Limit number of documents (for testing)"
    )
    parser.add_argument(
        "--shuffle", action="store_true", help="Shuffle before sampling"
    )
    parser.add_argument(
        "--seed", type=int, default=42, help="Shuffle seed (default: 42)"
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=16,
        help="Rows (images) per offline llm.chat batch (default: 16)",
    )
    parser.add_argument(
        "--max-model-len",
        type=int,
        default=18000,
        help="vLLM context length (default: 18000)",
    )
    parser.add_argument(
        "--gpu-memory-utilization",
        type=float,
        default=0.85,
        help="vLLM GPU memory fraction (default: 0.85)",
    )
    parser.add_argument(
        "--dtype",
        default="bfloat16",
        help="vLLM dtype (default: bfloat16; use float16 on T4/Turing)",
    )
    parser.add_argument(
        "--model", default=DEFAULT_MODEL, help=f"Model ID (default: {DEFAULT_MODEL})"
    )
    parser.add_argument(
        "--private", action="store_true", help="Make output dataset private"
    )
    parser.add_argument(
        "--config",
        default=None,
        help="Config/subset name when pushing (for benchmarking in one repo)",
    )
    parser.add_argument(
        "--create-pr",
        action="store_true",
        help="Push as a pull request instead of directly",
    )
    parser.add_argument("--hf-token", help="Hugging Face API token (or set HF_TOKEN)")
    parser.add_argument(
        "--verbose",
        action="store_true",
        help="Log resolved package versions after processing",
    )

    args = parser.parse_args()

    if args.pdf_column and args.image_column != "image":
        parser.error("--image-column and --pdf-column are mutually exclusive.")

    main(
        input_dataset=args.input_dataset,
        output_dataset=args.output_dataset,
        task=args.task,
        table_mode=args.table_mode,
        image_column=args.image_column,
        pdf_column=args.pdf_column,
        output_column=args.output_column,
        overwrite=args.overwrite,
        blocks_column=args.blocks_column,
        page_range=args.page_range,
        split=args.split,
        max_samples=args.max_samples,
        shuffle=args.shuffle,
        seed=args.seed,
        batch_size=args.batch_size,
        max_model_len=args.max_model_len,
        gpu_memory_utilization=args.gpu_memory_utilization,
        dtype=args.dtype,
        model=args.model,
        private=args.private,
        config=args.config,
        create_pr=args.create_pr,
        hf_token=args.hf_token,
        verbose=args.verbose,
    )