davanstrien HF Staff Claude Opus 4.7 (1M context) commited on
Commit
1bb77ce
·
1 Parent(s): 8b99222

dots-mocr: skip whole batch when an image fails to decode

Browse files

Same class of bug as pp-doclayout's (15cd760) and smoldocling's (8b99222)
fixes from yesterday: the list comprehension
`[dataset[i][image_column] for i in batch_indices]` lives outside the
existing try/except, so one PIL.UnidentifiedImageError takes down the
entire run.

Hit by an overnight 5k BHL run on l4x1 — crashed at batch 150/313 (48%)
after ~10h on a corrupt JP2, no output saved.

Now wrapped in its own try/except. On a corrupt image, the whole batch
is marked `[OCR SKIPPED — UNREADABLE IMAGE]` (length-preserving so the
final add_column still aligns) and the run continues.

Smoke-tested via hf jobs uv run on a 50-image clean slice — confirmed
happy path still works end-to-end (14.2 min, dataset pushed cleanly).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. dots-mocr.py +16 -2
dots-mocr.py CHANGED
@@ -50,7 +50,7 @@ from typing import Any, Dict, List, Union
50
  import torch
51
  from datasets import load_dataset
52
  from huggingface_hub import DatasetCard, login
53
- from PIL import Image
54
  from toolz import partition_all
55
  from tqdm.auto import tqdm
56
  from vllm import LLM, SamplingParams
@@ -358,7 +358,21 @@ def main(
358
  desc="dots.mocr processing",
359
  ):
360
  batch_indices = list(batch_indices)
361
- batch_images = [dataset[i][image_column] for i in batch_indices]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
  try:
364
  # Create messages for batch
 
50
  import torch
51
  from datasets import load_dataset
52
  from huggingface_hub import DatasetCard, login
53
+ from PIL import Image, UnidentifiedImageError
54
  from toolz import partition_all
55
  from tqdm.auto import tqdm
56
  from vllm import LLM, SamplingParams
 
358
  desc="dots.mocr processing",
359
  ):
360
  batch_indices = list(batch_indices)
361
+
362
+ # Fetch images first, with per-batch fallback for unreadable files.
363
+ # One corrupt image used to take down the entire run via the list
364
+ # comprehension; now we mark the whole batch as skipped and continue.
365
+ try:
366
+ batch_images = [dataset[i][image_column] for i in batch_indices]
367
+ except (UnidentifiedImageError, OSError) as e:
368
+ logger.warning(
369
+ f"Skipping batch of {len(batch_indices)} — unreadable image "
370
+ f"in batch: {type(e).__name__}: {e}"
371
+ )
372
+ all_outputs.extend(
373
+ ["[OCR SKIPPED — UNREADABLE IMAGE]"] * len(batch_indices)
374
+ )
375
+ continue
376
 
377
  try:
378
  # Create messages for batch