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

smoldocling-ocr: skip whole batch when an image fails to decode

Browse files

Same class of bug as pp-doclayout's earlier fix today: 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.

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.

Coarser than per-image skip, but matches the existing batch-level
error handling and the dots-mocr.py pattern.

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

Files changed (1) hide show
  1. smoldocling-ocr.py +16 -2
smoldocling-ocr.py CHANGED
@@ -42,7 +42,7 @@ from typing import Any, Dict, Union
42
  import torch
43
  from datasets import load_dataset
44
  from huggingface_hub import DatasetCard, login
45
- from PIL import Image
46
  from toolz import partition_all
47
  from tqdm.auto import tqdm
48
  from vllm import LLM, SamplingParams
@@ -299,7 +299,21 @@ def main(
299
  desc="OCR processing",
300
  ):
301
  batch_indices = list(batch_indices)
302
- batch_images = [dataset[i][image_column] for i in batch_indices]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
 
304
  try:
305
  # Prepare inputs for batch
 
42
  import torch
43
  from datasets import load_dataset
44
  from huggingface_hub import DatasetCard, login
45
+ from PIL import Image, UnidentifiedImageError
46
  from toolz import partition_all
47
  from tqdm.auto import tqdm
48
  from vllm import LLM, SamplingParams
 
299
  desc="OCR processing",
300
  ):
301
  batch_indices = list(batch_indices)
302
+
303
+ # Fetch images first, with per-batch fallback for unreadable files.
304
+ # One corrupt image used to take down the entire run via the list
305
+ # comprehension; now we mark the whole batch as skipped and continue.
306
+ try:
307
+ batch_images = [dataset[i][image_column] for i in batch_indices]
308
+ except (UnidentifiedImageError, OSError) as e:
309
+ logger.warning(
310
+ f"Skipping batch of {len(batch_indices)} — unreadable image "
311
+ f"in batch: {type(e).__name__}: {e}"
312
+ )
313
+ all_output.extend(
314
+ ["[OCR SKIPPED — UNREADABLE IMAGE]"] * len(batch_indices)
315
+ )
316
+ continue
317
 
318
  try:
319
  # Prepare inputs for batch