Sync from GitHub via hub-sync
Browse files- abot-ocr.py +44 -4
- deepseek-ocr-vllm.py +44 -4
- deepseek-ocr.py +45 -5
- deepseek-ocr2-vllm.py +33 -0
- dots-mocr.py +33 -0
- falcon-ocr.py +33 -0
- firered-ocr.py +33 -0
- glm-ocr-v2.py +35 -0
- hunyuan-ocr.py +33 -0
- lfm2-extract.py +34 -0
- lfm2-vl-extract.py +34 -0
- lift-extract.py +34 -0
- lighton-ocr.py +33 -0
- nanonets-ocr.py +49 -7
- nuextract3.py +37 -0
- numarkdown-ocr.py +33 -0
- olmocr2-vllm.py +33 -0
- paddleocr-vl-1.5.py +33 -0
- paddleocr-vl-1.6.py +33 -0
- paddleocr-vl.py +33 -0
- pp-doclayout.py +30 -1
- qianfan-ocr.py +33 -0
- rolm-ocr.py +33 -0
- smoldocling-ocr.py +33 -0
- unlimited-ocr-vllm.py +33 -0
abot-ocr.py
CHANGED
|
@@ -96,6 +96,28 @@ def check_cuda_availability():
|
|
| 96 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def post_process_text(text: str, threshold: int = 8000) -> str:
|
| 100 |
"""Trim runaway repetition loops that VLM OCR can fall into on dense pages.
|
| 101 |
|
|
@@ -258,6 +280,8 @@ def main(
|
|
| 258 |
private: bool = False,
|
| 259 |
shuffle: bool = False,
|
| 260 |
seed: int = 42,
|
|
|
|
|
|
|
| 261 |
verbose: bool = False,
|
| 262 |
):
|
| 263 |
"""Process images from a HF dataset through the ABot-OCR model."""
|
|
@@ -286,6 +310,9 @@ def main(
|
|
| 286 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 287 |
)
|
| 288 |
|
|
|
|
|
|
|
|
|
|
| 289 |
# Shuffle if requested
|
| 290 |
if shuffle:
|
| 291 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -335,9 +362,9 @@ def main(
|
|
| 335 |
logger.error(f"Error processing batch: {e}")
|
| 336 |
all_markdown.extend(["[OCR FAILED]"] * len(batch_images))
|
| 337 |
|
| 338 |
-
# Add
|
| 339 |
-
logger.info("Adding
|
| 340 |
-
dataset = dataset.add_column(
|
| 341 |
|
| 342 |
# Handle inference_info tracking
|
| 343 |
logger.info("Updating inference_info...")
|
|
@@ -345,7 +372,7 @@ def main(
|
|
| 345 |
inference_entry = {
|
| 346 |
"model_id": model,
|
| 347 |
"model_name": "ABot-OCR",
|
| 348 |
-
"column_name":
|
| 349 |
"timestamp": datetime.now().isoformat(),
|
| 350 |
"batch_size": batch_size,
|
| 351 |
"max_tokens": max_tokens,
|
|
@@ -533,6 +560,17 @@ Examples:
|
|
| 533 |
default=42,
|
| 534 |
help="Random seed for shuffling (default: 42)",
|
| 535 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
parser.add_argument(
|
| 537 |
"--verbose",
|
| 538 |
action="store_true",
|
|
@@ -556,5 +594,7 @@ Examples:
|
|
| 556 |
private=args.private,
|
| 557 |
shuffle=args.shuffle,
|
| 558 |
seed=args.seed,
|
|
|
|
|
|
|
| 559 |
verbose=args.verbose,
|
| 560 |
)
|
|
|
|
| 96 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 97 |
|
| 98 |
|
| 99 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 100 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 101 |
+
|
| 102 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 103 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 104 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 105 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 106 |
+
"""
|
| 107 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 108 |
+
if not clash:
|
| 109 |
+
return dataset
|
| 110 |
+
if overwrite:
|
| 111 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 112 |
+
return dataset.remove_columns(clash)
|
| 113 |
+
logger.error(
|
| 114 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 115 |
+
f"(columns: {dataset.column_names})."
|
| 116 |
+
)
|
| 117 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 118 |
+
sys.exit(1)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
def post_process_text(text: str, threshold: int = 8000) -> str:
|
| 122 |
"""Trim runaway repetition loops that VLM OCR can fall into on dense pages.
|
| 123 |
|
|
|
|
| 280 |
private: bool = False,
|
| 281 |
shuffle: bool = False,
|
| 282 |
seed: int = 42,
|
| 283 |
+
output_column: str = "markdown",
|
| 284 |
+
overwrite: bool = False,
|
| 285 |
verbose: bool = False,
|
| 286 |
):
|
| 287 |
"""Process images from a HF dataset through the ABot-OCR model."""
|
|
|
|
| 310 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 311 |
)
|
| 312 |
|
| 313 |
+
# Fail fast if the output column would collide with an existing input column
|
| 314 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 315 |
+
|
| 316 |
# Shuffle if requested
|
| 317 |
if shuffle:
|
| 318 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 362 |
logger.error(f"Error processing batch: {e}")
|
| 363 |
all_markdown.extend(["[OCR FAILED]"] * len(batch_images))
|
| 364 |
|
| 365 |
+
# Add output column to dataset
|
| 366 |
+
logger.info(f"Adding '{output_column}' column to dataset")
|
| 367 |
+
dataset = dataset.add_column(output_column, all_markdown)
|
| 368 |
|
| 369 |
# Handle inference_info tracking
|
| 370 |
logger.info("Updating inference_info...")
|
|
|
|
| 372 |
inference_entry = {
|
| 373 |
"model_id": model,
|
| 374 |
"model_name": "ABot-OCR",
|
| 375 |
+
"column_name": output_column,
|
| 376 |
"timestamp": datetime.now().isoformat(),
|
| 377 |
"batch_size": batch_size,
|
| 378 |
"max_tokens": max_tokens,
|
|
|
|
| 560 |
default=42,
|
| 561 |
help="Random seed for shuffling (default: 42)",
|
| 562 |
)
|
| 563 |
+
parser.add_argument(
|
| 564 |
+
"--output-column",
|
| 565 |
+
default="markdown",
|
| 566 |
+
help="Column name for the OCR output text (default: markdown)",
|
| 567 |
+
)
|
| 568 |
+
parser.add_argument(
|
| 569 |
+
"--overwrite",
|
| 570 |
+
action="store_true",
|
| 571 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 572 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 573 |
+
)
|
| 574 |
parser.add_argument(
|
| 575 |
"--verbose",
|
| 576 |
action="store_true",
|
|
|
|
| 594 |
private=args.private,
|
| 595 |
shuffle=args.shuffle,
|
| 596 |
seed=args.seed,
|
| 597 |
+
output_column=args.output_column,
|
| 598 |
+
overwrite=args.overwrite,
|
| 599 |
verbose=args.verbose,
|
| 600 |
)
|
deepseek-ocr-vllm.py
CHANGED
|
@@ -78,6 +78,28 @@ def check_cuda_availability():
|
|
| 78 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 82 |
"""Convert various image formats to PIL Image."""
|
| 83 |
if isinstance(image, Image.Image):
|
|
@@ -211,6 +233,8 @@ def main(
|
|
| 211 |
private: bool = False,
|
| 212 |
shuffle: bool = False,
|
| 213 |
seed: int = 42,
|
|
|
|
|
|
|
| 214 |
config: str = None,
|
| 215 |
create_pr: bool = False,
|
| 216 |
verbose: bool = False,
|
|
@@ -253,6 +277,9 @@ def main(
|
|
| 253 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 254 |
)
|
| 255 |
|
|
|
|
|
|
|
|
|
|
| 256 |
# Shuffle if requested
|
| 257 |
if shuffle:
|
| 258 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -326,9 +353,9 @@ def main(
|
|
| 326 |
processing_duration = datetime.now() - start_time
|
| 327 |
processing_time_str = f"{processing_duration.total_seconds() / 60:.1f} min"
|
| 328 |
|
| 329 |
-
# Add
|
| 330 |
-
logger.info("Adding
|
| 331 |
-
dataset = dataset.add_column(
|
| 332 |
|
| 333 |
# Handle inference_info tracking
|
| 334 |
logger.info("Updating inference_info...")
|
|
@@ -336,7 +363,7 @@ def main(
|
|
| 336 |
inference_entry = {
|
| 337 |
"model_id": model,
|
| 338 |
"model_name": "DeepSeek-OCR",
|
| 339 |
-
"column_name":
|
| 340 |
"timestamp": datetime.now().isoformat(),
|
| 341 |
"prompt_mode": prompt_mode if prompt is None else "custom",
|
| 342 |
"batch_size": batch_size,
|
|
@@ -569,6 +596,17 @@ Examples:
|
|
| 569 |
default=42,
|
| 570 |
help="Random seed for shuffling (default: 42)",
|
| 571 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
parser.add_argument(
|
| 573 |
"--verbose",
|
| 574 |
action="store_true",
|
|
@@ -594,6 +632,8 @@ Examples:
|
|
| 594 |
private=args.private,
|
| 595 |
shuffle=args.shuffle,
|
| 596 |
seed=args.seed,
|
|
|
|
|
|
|
| 597 |
config=args.config,
|
| 598 |
create_pr=args.create_pr,
|
| 599 |
verbose=args.verbose,
|
|
|
|
| 78 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 79 |
|
| 80 |
|
| 81 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 82 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 83 |
+
|
| 84 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 85 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 86 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 87 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 88 |
+
"""
|
| 89 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 90 |
+
if not clash:
|
| 91 |
+
return dataset
|
| 92 |
+
if overwrite:
|
| 93 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 94 |
+
return dataset.remove_columns(clash)
|
| 95 |
+
logger.error(
|
| 96 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 97 |
+
f"(columns: {dataset.column_names})."
|
| 98 |
+
)
|
| 99 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 100 |
+
sys.exit(1)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 104 |
"""Convert various image formats to PIL Image."""
|
| 105 |
if isinstance(image, Image.Image):
|
|
|
|
| 233 |
private: bool = False,
|
| 234 |
shuffle: bool = False,
|
| 235 |
seed: int = 42,
|
| 236 |
+
output_column: str = "markdown",
|
| 237 |
+
overwrite: bool = False,
|
| 238 |
config: str = None,
|
| 239 |
create_pr: bool = False,
|
| 240 |
verbose: bool = False,
|
|
|
|
| 277 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 278 |
)
|
| 279 |
|
| 280 |
+
# Fail fast if the output column would collide with an existing input column
|
| 281 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 282 |
+
|
| 283 |
# Shuffle if requested
|
| 284 |
if shuffle:
|
| 285 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 353 |
processing_duration = datetime.now() - start_time
|
| 354 |
processing_time_str = f"{processing_duration.total_seconds() / 60:.1f} min"
|
| 355 |
|
| 356 |
+
# Add output column to dataset
|
| 357 |
+
logger.info(f"Adding '{output_column}' column to dataset")
|
| 358 |
+
dataset = dataset.add_column(output_column, all_markdown)
|
| 359 |
|
| 360 |
# Handle inference_info tracking
|
| 361 |
logger.info("Updating inference_info...")
|
|
|
|
| 363 |
inference_entry = {
|
| 364 |
"model_id": model,
|
| 365 |
"model_name": "DeepSeek-OCR",
|
| 366 |
+
"column_name": output_column,
|
| 367 |
"timestamp": datetime.now().isoformat(),
|
| 368 |
"prompt_mode": prompt_mode if prompt is None else "custom",
|
| 369 |
"batch_size": batch_size,
|
|
|
|
| 596 |
default=42,
|
| 597 |
help="Random seed for shuffling (default: 42)",
|
| 598 |
)
|
| 599 |
+
parser.add_argument(
|
| 600 |
+
"--output-column",
|
| 601 |
+
default="markdown",
|
| 602 |
+
help="Column name for the OCR output text (default: markdown)",
|
| 603 |
+
)
|
| 604 |
+
parser.add_argument(
|
| 605 |
+
"--overwrite",
|
| 606 |
+
action="store_true",
|
| 607 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 608 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 609 |
+
)
|
| 610 |
parser.add_argument(
|
| 611 |
"--verbose",
|
| 612 |
action="store_true",
|
|
|
|
| 632 |
private=args.private,
|
| 633 |
shuffle=args.shuffle,
|
| 634 |
seed=args.seed,
|
| 635 |
+
output_column=args.output_column,
|
| 636 |
+
overwrite=args.overwrite,
|
| 637 |
config=args.config,
|
| 638 |
create_pr=args.create_pr,
|
| 639 |
verbose=args.verbose,
|
deepseek-ocr.py
CHANGED
|
@@ -76,6 +76,28 @@ def check_cuda_availability():
|
|
| 76 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 77 |
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def create_dataset_card(
|
| 80 |
source_dataset: str,
|
| 81 |
model: str,
|
|
@@ -243,6 +265,8 @@ def main(
|
|
| 243 |
private: bool = False,
|
| 244 |
shuffle: bool = False,
|
| 245 |
seed: int = 42,
|
|
|
|
|
|
|
| 246 |
):
|
| 247 |
"""Process images from HF dataset through DeepSeek-OCR model."""
|
| 248 |
|
|
@@ -294,6 +318,9 @@ def main(
|
|
| 294 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 295 |
)
|
| 296 |
|
|
|
|
|
|
|
|
|
|
| 297 |
# Shuffle if requested
|
| 298 |
if shuffle:
|
| 299 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -379,12 +406,12 @@ def main(
|
|
| 379 |
try:
|
| 380 |
shutil.rmtree(temp_dir)
|
| 381 |
shutil.rmtree(temp_output_dir)
|
| 382 |
-
except:
|
| 383 |
pass
|
| 384 |
|
| 385 |
-
# Add
|
| 386 |
-
logger.info("Adding
|
| 387 |
-
dataset = dataset.add_column(
|
| 388 |
|
| 389 |
# Handle inference_info tracking
|
| 390 |
logger.info("Updating inference_info...")
|
|
@@ -403,7 +430,7 @@ def main(
|
|
| 403 |
|
| 404 |
# Add new inference info
|
| 405 |
new_info = {
|
| 406 |
-
"column_name":
|
| 407 |
"model_id": model,
|
| 408 |
"processing_date": datetime.now().isoformat(),
|
| 409 |
"resolution_mode": resolution_mode,
|
|
@@ -581,6 +608,17 @@ Examples:
|
|
| 581 |
default=42,
|
| 582 |
help="Random seed for shuffling (default: 42)",
|
| 583 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
|
| 585 |
args = parser.parse_args()
|
| 586 |
|
|
@@ -600,4 +638,6 @@ Examples:
|
|
| 600 |
private=args.private,
|
| 601 |
shuffle=args.shuffle,
|
| 602 |
seed=args.seed,
|
|
|
|
|
|
|
| 603 |
)
|
|
|
|
| 76 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 77 |
|
| 78 |
|
| 79 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 80 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 81 |
+
|
| 82 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 83 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 84 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 85 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 86 |
+
"""
|
| 87 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 88 |
+
if not clash:
|
| 89 |
+
return dataset
|
| 90 |
+
if overwrite:
|
| 91 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 92 |
+
return dataset.remove_columns(clash)
|
| 93 |
+
logger.error(
|
| 94 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 95 |
+
f"(columns: {dataset.column_names})."
|
| 96 |
+
)
|
| 97 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 98 |
+
sys.exit(1)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
def create_dataset_card(
|
| 102 |
source_dataset: str,
|
| 103 |
model: str,
|
|
|
|
| 265 |
private: bool = False,
|
| 266 |
shuffle: bool = False,
|
| 267 |
seed: int = 42,
|
| 268 |
+
output_column: str = "markdown",
|
| 269 |
+
overwrite: bool = False,
|
| 270 |
):
|
| 271 |
"""Process images from HF dataset through DeepSeek-OCR model."""
|
| 272 |
|
|
|
|
| 318 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 319 |
)
|
| 320 |
|
| 321 |
+
# Fail fast if the output column would collide with an existing input column
|
| 322 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 323 |
+
|
| 324 |
# Shuffle if requested
|
| 325 |
if shuffle:
|
| 326 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 406 |
try:
|
| 407 |
shutil.rmtree(temp_dir)
|
| 408 |
shutil.rmtree(temp_output_dir)
|
| 409 |
+
except Exception:
|
| 410 |
pass
|
| 411 |
|
| 412 |
+
# Add output column to dataset
|
| 413 |
+
logger.info(f"Adding '{output_column}' column to dataset")
|
| 414 |
+
dataset = dataset.add_column(output_column, all_markdown)
|
| 415 |
|
| 416 |
# Handle inference_info tracking
|
| 417 |
logger.info("Updating inference_info...")
|
|
|
|
| 430 |
|
| 431 |
# Add new inference info
|
| 432 |
new_info = {
|
| 433 |
+
"column_name": output_column,
|
| 434 |
"model_id": model,
|
| 435 |
"processing_date": datetime.now().isoformat(),
|
| 436 |
"resolution_mode": resolution_mode,
|
|
|
|
| 608 |
default=42,
|
| 609 |
help="Random seed for shuffling (default: 42)",
|
| 610 |
)
|
| 611 |
+
parser.add_argument(
|
| 612 |
+
"--output-column",
|
| 613 |
+
default="markdown",
|
| 614 |
+
help="Column name for the OCR output text (default: markdown)",
|
| 615 |
+
)
|
| 616 |
+
parser.add_argument(
|
| 617 |
+
"--overwrite",
|
| 618 |
+
action="store_true",
|
| 619 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 620 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 621 |
+
)
|
| 622 |
|
| 623 |
args = parser.parse_args()
|
| 624 |
|
|
|
|
| 638 |
private=args.private,
|
| 639 |
shuffle=args.shuffle,
|
| 640 |
seed=args.seed,
|
| 641 |
+
output_column=args.output_column,
|
| 642 |
+
overwrite=args.overwrite,
|
| 643 |
)
|
deepseek-ocr2-vllm.py
CHANGED
|
@@ -86,6 +86,28 @@ def check_cuda_availability():
|
|
| 86 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 90 |
"""Convert various image formats to PIL Image."""
|
| 91 |
if isinstance(image, Image.Image):
|
|
@@ -225,6 +247,7 @@ def main(
|
|
| 225 |
shuffle: bool = False,
|
| 226 |
seed: int = 42,
|
| 227 |
output_column: str = "markdown",
|
|
|
|
| 228 |
config: str = None,
|
| 229 |
create_pr: bool = False,
|
| 230 |
verbose: bool = False,
|
|
@@ -267,6 +290,9 @@ def main(
|
|
| 267 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 268 |
)
|
| 269 |
|
|
|
|
|
|
|
|
|
|
| 270 |
# Shuffle if requested
|
| 271 |
if shuffle:
|
| 272 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -574,6 +600,12 @@ Examples:
|
|
| 574 |
default="markdown",
|
| 575 |
help="Column name for OCR output (default: markdown). Use a different name to add alongside existing OCR.",
|
| 576 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
parser.add_argument(
|
| 578 |
"--shuffle",
|
| 579 |
action="store_true",
|
|
@@ -620,6 +652,7 @@ Examples:
|
|
| 620 |
shuffle=args.shuffle,
|
| 621 |
seed=args.seed,
|
| 622 |
output_column=args.output_column,
|
|
|
|
| 623 |
config=args.config,
|
| 624 |
create_pr=args.create_pr,
|
| 625 |
verbose=args.verbose,
|
|
|
|
| 86 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 87 |
|
| 88 |
|
| 89 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 90 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 91 |
+
|
| 92 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 93 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 94 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 95 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 96 |
+
"""
|
| 97 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 98 |
+
if not clash:
|
| 99 |
+
return dataset
|
| 100 |
+
if overwrite:
|
| 101 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 102 |
+
return dataset.remove_columns(clash)
|
| 103 |
+
logger.error(
|
| 104 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 105 |
+
f"(columns: {dataset.column_names})."
|
| 106 |
+
)
|
| 107 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 108 |
+
sys.exit(1)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 112 |
"""Convert various image formats to PIL Image."""
|
| 113 |
if isinstance(image, Image.Image):
|
|
|
|
| 247 |
shuffle: bool = False,
|
| 248 |
seed: int = 42,
|
| 249 |
output_column: str = "markdown",
|
| 250 |
+
overwrite: bool = False,
|
| 251 |
config: str = None,
|
| 252 |
create_pr: bool = False,
|
| 253 |
verbose: bool = False,
|
|
|
|
| 290 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 291 |
)
|
| 292 |
|
| 293 |
+
# Fail fast if the output column would collide with an existing input column
|
| 294 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 295 |
+
|
| 296 |
# Shuffle if requested
|
| 297 |
if shuffle:
|
| 298 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 600 |
default="markdown",
|
| 601 |
help="Column name for OCR output (default: markdown). Use a different name to add alongside existing OCR.",
|
| 602 |
)
|
| 603 |
+
parser.add_argument(
|
| 604 |
+
"--overwrite",
|
| 605 |
+
action="store_true",
|
| 606 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 607 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 608 |
+
)
|
| 609 |
parser.add_argument(
|
| 610 |
"--shuffle",
|
| 611 |
action="store_true",
|
|
|
|
| 652 |
shuffle=args.shuffle,
|
| 653 |
seed=args.seed,
|
| 654 |
output_column=args.output_column,
|
| 655 |
+
overwrite=args.overwrite,
|
| 656 |
config=args.config,
|
| 657 |
create_pr=args.create_pr,
|
| 658 |
verbose=args.verbose,
|
dots-mocr.py
CHANGED
|
@@ -115,6 +115,28 @@ def check_cuda_availability():
|
|
| 115 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def make_ocr_message(
|
| 119 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 120 |
prompt: str = PROMPT_TEMPLATES["ocr"],
|
|
@@ -281,6 +303,7 @@ def main(
|
|
| 281 |
prompt_mode: str = "ocr",
|
| 282 |
custom_prompt: str = None,
|
| 283 |
output_column: str = "markdown",
|
|
|
|
| 284 |
config: str = None,
|
| 285 |
create_pr: bool = False,
|
| 286 |
temperature: float = 0.1,
|
|
@@ -318,6 +341,9 @@ def main(
|
|
| 318 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 319 |
)
|
| 320 |
|
|
|
|
|
|
|
|
|
|
| 321 |
# Shuffle if requested
|
| 322 |
if shuffle:
|
| 323 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -665,6 +691,12 @@ Examples:
|
|
| 665 |
default="markdown",
|
| 666 |
help="Column name for output text (default: markdown)",
|
| 667 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
parser.add_argument(
|
| 669 |
"--config",
|
| 670 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
@@ -712,6 +744,7 @@ Examples:
|
|
| 712 |
prompt_mode=args.prompt_mode,
|
| 713 |
custom_prompt=args.custom_prompt,
|
| 714 |
output_column=args.output_column,
|
|
|
|
| 715 |
config=args.config,
|
| 716 |
create_pr=args.create_pr,
|
| 717 |
temperature=args.temperature,
|
|
|
|
| 115 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 116 |
|
| 117 |
|
| 118 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 119 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 120 |
+
|
| 121 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 122 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 123 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 124 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 125 |
+
"""
|
| 126 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 127 |
+
if not clash:
|
| 128 |
+
return dataset
|
| 129 |
+
if overwrite:
|
| 130 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 131 |
+
return dataset.remove_columns(clash)
|
| 132 |
+
logger.error(
|
| 133 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 134 |
+
f"(columns: {dataset.column_names})."
|
| 135 |
+
)
|
| 136 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 137 |
+
sys.exit(1)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
def make_ocr_message(
|
| 141 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 142 |
prompt: str = PROMPT_TEMPLATES["ocr"],
|
|
|
|
| 303 |
prompt_mode: str = "ocr",
|
| 304 |
custom_prompt: str = None,
|
| 305 |
output_column: str = "markdown",
|
| 306 |
+
overwrite: bool = False,
|
| 307 |
config: str = None,
|
| 308 |
create_pr: bool = False,
|
| 309 |
temperature: float = 0.1,
|
|
|
|
| 341 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 342 |
)
|
| 343 |
|
| 344 |
+
# Fail fast if the output column would collide with an existing input column
|
| 345 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 346 |
+
|
| 347 |
# Shuffle if requested
|
| 348 |
if shuffle:
|
| 349 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 691 |
default="markdown",
|
| 692 |
help="Column name for output text (default: markdown)",
|
| 693 |
)
|
| 694 |
+
parser.add_argument(
|
| 695 |
+
"--overwrite",
|
| 696 |
+
action="store_true",
|
| 697 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 698 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 699 |
+
)
|
| 700 |
parser.add_argument(
|
| 701 |
"--config",
|
| 702 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
|
|
| 744 |
prompt_mode=args.prompt_mode,
|
| 745 |
custom_prompt=args.custom_prompt,
|
| 746 |
output_column=args.output_column,
|
| 747 |
+
overwrite=args.overwrite,
|
| 748 |
config=args.config,
|
| 749 |
create_pr=args.create_pr,
|
| 750 |
temperature=args.temperature,
|
falcon-ocr.py
CHANGED
|
@@ -74,6 +74,28 @@ def check_cuda_availability():
|
|
| 74 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def prepare_image(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 78 |
if isinstance(image, Image.Image):
|
| 79 |
pil_img = image
|
|
@@ -145,6 +167,7 @@ def main(
|
|
| 145 |
shuffle: bool = False,
|
| 146 |
seed: int = 42,
|
| 147 |
output_column: str = "markdown",
|
|
|
|
| 148 |
config: str = None,
|
| 149 |
create_pr: bool = False,
|
| 150 |
compile: bool = True,
|
|
@@ -176,6 +199,9 @@ def main(
|
|
| 176 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 177 |
)
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
if shuffle:
|
| 180 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 181 |
dataset = dataset.shuffle(seed=seed)
|
|
@@ -386,6 +412,12 @@ if __name__ == "__main__":
|
|
| 386 |
"--output-column", default="markdown",
|
| 387 |
help="Column name for output text (default: markdown)",
|
| 388 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
parser.add_argument(
|
| 390 |
"--config",
|
| 391 |
help="Config/subset name for Hub (for benchmarking multiple models)",
|
|
@@ -424,6 +456,7 @@ if __name__ == "__main__":
|
|
| 424 |
shuffle=args.shuffle,
|
| 425 |
seed=args.seed,
|
| 426 |
output_column=args.output_column,
|
|
|
|
| 427 |
config=args.config,
|
| 428 |
create_pr=args.create_pr,
|
| 429 |
compile=not args.no_compile,
|
|
|
|
| 74 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 75 |
|
| 76 |
|
| 77 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 78 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 79 |
+
|
| 80 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 81 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 82 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 83 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 84 |
+
"""
|
| 85 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 86 |
+
if not clash:
|
| 87 |
+
return dataset
|
| 88 |
+
if overwrite:
|
| 89 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 90 |
+
return dataset.remove_columns(clash)
|
| 91 |
+
logger.error(
|
| 92 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 93 |
+
f"(columns: {dataset.column_names})."
|
| 94 |
+
)
|
| 95 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 96 |
+
sys.exit(1)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
def prepare_image(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 100 |
if isinstance(image, Image.Image):
|
| 101 |
pil_img = image
|
|
|
|
| 167 |
shuffle: bool = False,
|
| 168 |
seed: int = 42,
|
| 169 |
output_column: str = "markdown",
|
| 170 |
+
overwrite: bool = False,
|
| 171 |
config: str = None,
|
| 172 |
create_pr: bool = False,
|
| 173 |
compile: bool = True,
|
|
|
|
| 199 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 200 |
)
|
| 201 |
|
| 202 |
+
# Fail fast if the output column would collide with an existing input column
|
| 203 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 204 |
+
|
| 205 |
if shuffle:
|
| 206 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 207 |
dataset = dataset.shuffle(seed=seed)
|
|
|
|
| 412 |
"--output-column", default="markdown",
|
| 413 |
help="Column name for output text (default: markdown)",
|
| 414 |
)
|
| 415 |
+
parser.add_argument(
|
| 416 |
+
"--overwrite",
|
| 417 |
+
action="store_true",
|
| 418 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 419 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 420 |
+
)
|
| 421 |
parser.add_argument(
|
| 422 |
"--config",
|
| 423 |
help="Config/subset name for Hub (for benchmarking multiple models)",
|
|
|
|
| 456 |
shuffle=args.shuffle,
|
| 457 |
seed=args.seed,
|
| 458 |
output_column=args.output_column,
|
| 459 |
+
overwrite=args.overwrite,
|
| 460 |
config=args.config,
|
| 461 |
create_pr=args.create_pr,
|
| 462 |
compile=not args.no_compile,
|
firered-ocr.py
CHANGED
|
@@ -90,6 +90,28 @@ def check_cuda_availability():
|
|
| 90 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def make_ocr_message(
|
| 94 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 95 |
prompt: str = FIRERED_OCR_PROMPT,
|
|
@@ -242,6 +264,7 @@ def main(
|
|
| 242 |
shuffle: bool = False,
|
| 243 |
seed: int = 42,
|
| 244 |
output_column: str = "markdown",
|
|
|
|
| 245 |
config: str = None,
|
| 246 |
create_pr: bool = False,
|
| 247 |
):
|
|
@@ -268,6 +291,9 @@ def main(
|
|
| 268 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 269 |
)
|
| 270 |
|
|
|
|
|
|
|
|
|
|
| 271 |
# Shuffle if requested
|
| 272 |
if shuffle:
|
| 273 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -528,6 +554,12 @@ Examples:
|
|
| 528 |
default="markdown",
|
| 529 |
help="Column name for output text (default: markdown)",
|
| 530 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
parser.add_argument(
|
| 532 |
"--config",
|
| 533 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
@@ -556,6 +588,7 @@ Examples:
|
|
| 556 |
shuffle=args.shuffle,
|
| 557 |
seed=args.seed,
|
| 558 |
output_column=args.output_column,
|
|
|
|
| 559 |
config=args.config,
|
| 560 |
create_pr=args.create_pr,
|
| 561 |
)
|
|
|
|
| 90 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 91 |
|
| 92 |
|
| 93 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 94 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 95 |
+
|
| 96 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 97 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 98 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 99 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 100 |
+
"""
|
| 101 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 102 |
+
if not clash:
|
| 103 |
+
return dataset
|
| 104 |
+
if overwrite:
|
| 105 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 106 |
+
return dataset.remove_columns(clash)
|
| 107 |
+
logger.error(
|
| 108 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 109 |
+
f"(columns: {dataset.column_names})."
|
| 110 |
+
)
|
| 111 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 112 |
+
sys.exit(1)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
def make_ocr_message(
|
| 116 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 117 |
prompt: str = FIRERED_OCR_PROMPT,
|
|
|
|
| 264 |
shuffle: bool = False,
|
| 265 |
seed: int = 42,
|
| 266 |
output_column: str = "markdown",
|
| 267 |
+
overwrite: bool = False,
|
| 268 |
config: str = None,
|
| 269 |
create_pr: bool = False,
|
| 270 |
):
|
|
|
|
| 291 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 292 |
)
|
| 293 |
|
| 294 |
+
# Fail fast if the output column would collide with an existing input column
|
| 295 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 296 |
+
|
| 297 |
# Shuffle if requested
|
| 298 |
if shuffle:
|
| 299 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 554 |
default="markdown",
|
| 555 |
help="Column name for output text (default: markdown)",
|
| 556 |
)
|
| 557 |
+
parser.add_argument(
|
| 558 |
+
"--overwrite",
|
| 559 |
+
action="store_true",
|
| 560 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 561 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 562 |
+
)
|
| 563 |
parser.add_argument(
|
| 564 |
"--config",
|
| 565 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
|
|
| 588 |
shuffle=args.shuffle,
|
| 589 |
seed=args.seed,
|
| 590 |
output_column=args.output_column,
|
| 591 |
+
overwrite=args.overwrite,
|
| 592 |
config=args.config,
|
| 593 |
create_pr=args.create_pr,
|
| 594 |
)
|
glm-ocr-v2.py
CHANGED
|
@@ -136,6 +136,28 @@ def check_cuda_availability():
|
|
| 136 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 137 |
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
def make_ocr_message(
|
| 140 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 141 |
task: str = "ocr",
|
|
@@ -386,6 +408,7 @@ def main(
|
|
| 386 |
shuffle: bool = False,
|
| 387 |
seed: int = 42,
|
| 388 |
output_column: str = "markdown",
|
|
|
|
| 389 |
verbose: bool = False,
|
| 390 |
config: str = None,
|
| 391 |
create_pr: bool = False,
|
|
@@ -438,6 +461,11 @@ def main(
|
|
| 438 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 439 |
)
|
| 440 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
if shuffle:
|
| 442 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 443 |
dataset = dataset.shuffle(seed=seed)
|
|
@@ -955,6 +983,12 @@ Examples:
|
|
| 955 |
default="markdown",
|
| 956 |
help="Column name for output text (default: markdown)",
|
| 957 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 958 |
parser.add_argument(
|
| 959 |
"--verbose",
|
| 960 |
action="store_true",
|
|
@@ -999,6 +1033,7 @@ Examples:
|
|
| 999 |
shuffle=args.shuffle,
|
| 1000 |
seed=args.seed,
|
| 1001 |
output_column=args.output_column,
|
|
|
|
| 1002 |
verbose=args.verbose,
|
| 1003 |
config=args.config,
|
| 1004 |
create_pr=args.create_pr,
|
|
|
|
| 136 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 137 |
|
| 138 |
|
| 139 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 140 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 141 |
+
|
| 142 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 143 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 144 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 145 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 146 |
+
"""
|
| 147 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 148 |
+
if not clash:
|
| 149 |
+
return dataset
|
| 150 |
+
if overwrite:
|
| 151 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 152 |
+
return dataset.remove_columns(clash)
|
| 153 |
+
logger.error(
|
| 154 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 155 |
+
f"(columns: {dataset.column_names})."
|
| 156 |
+
)
|
| 157 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 158 |
+
sys.exit(1)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
def make_ocr_message(
|
| 162 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 163 |
task: str = "ocr",
|
|
|
|
| 408 |
shuffle: bool = False,
|
| 409 |
seed: int = 42,
|
| 410 |
output_column: str = "markdown",
|
| 411 |
+
overwrite: bool = False,
|
| 412 |
verbose: bool = False,
|
| 413 |
config: str = None,
|
| 414 |
create_pr: bool = False,
|
|
|
|
| 461 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 462 |
)
|
| 463 |
|
| 464 |
+
# Fail fast if the output column would collide with an existing input column.
|
| 465 |
+
# The incremental path derives batch datasets via dataset.select(...), so dropping
|
| 466 |
+
# it here (on --overwrite) also keeps those per-batch add_columns clean.
|
| 467 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 468 |
+
|
| 469 |
if shuffle:
|
| 470 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 471 |
dataset = dataset.shuffle(seed=seed)
|
|
|
|
| 983 |
default="markdown",
|
| 984 |
help="Column name for output text (default: markdown)",
|
| 985 |
)
|
| 986 |
+
parser.add_argument(
|
| 987 |
+
"--overwrite",
|
| 988 |
+
action="store_true",
|
| 989 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 990 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 991 |
+
)
|
| 992 |
parser.add_argument(
|
| 993 |
"--verbose",
|
| 994 |
action="store_true",
|
|
|
|
| 1033 |
shuffle=args.shuffle,
|
| 1034 |
seed=args.seed,
|
| 1035 |
output_column=args.output_column,
|
| 1036 |
+
overwrite=args.overwrite,
|
| 1037 |
verbose=args.verbose,
|
| 1038 |
config=args.config,
|
| 1039 |
create_pr=args.create_pr,
|
hunyuan-ocr.py
CHANGED
|
@@ -150,6 +150,28 @@ def check_cuda_availability():
|
|
| 150 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 151 |
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def get_prompt(
|
| 154 |
prompt_mode: str,
|
| 155 |
use_chinese: bool = False,
|
|
@@ -366,6 +388,7 @@ def main(
|
|
| 366 |
use_chinese: bool = False,
|
| 367 |
custom_prompt: str = None,
|
| 368 |
output_column: str = "markdown",
|
|
|
|
| 369 |
clean_output: bool = True,
|
| 370 |
config: str = None,
|
| 371 |
create_pr: bool = False,
|
|
@@ -409,6 +432,9 @@ def main(
|
|
| 409 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 410 |
)
|
| 411 |
|
|
|
|
|
|
|
|
|
|
| 412 |
# Shuffle if requested
|
| 413 |
if shuffle:
|
| 414 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -786,6 +812,12 @@ Examples:
|
|
| 786 |
default="markdown",
|
| 787 |
help="Column name for output text (default: markdown)",
|
| 788 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
parser.add_argument(
|
| 790 |
"--no-clean-output",
|
| 791 |
action="store_true",
|
|
@@ -835,6 +867,7 @@ Examples:
|
|
| 835 |
use_chinese=args.use_chinese_prompts,
|
| 836 |
custom_prompt=args.custom_prompt,
|
| 837 |
output_column=args.output_column,
|
|
|
|
| 838 |
clean_output=not args.no_clean_output,
|
| 839 |
config=args.config,
|
| 840 |
create_pr=args.create_pr,
|
|
|
|
| 150 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 151 |
|
| 152 |
|
| 153 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 154 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 155 |
+
|
| 156 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 157 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 158 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 159 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 160 |
+
"""
|
| 161 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 162 |
+
if not clash:
|
| 163 |
+
return dataset
|
| 164 |
+
if overwrite:
|
| 165 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 166 |
+
return dataset.remove_columns(clash)
|
| 167 |
+
logger.error(
|
| 168 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 169 |
+
f"(columns: {dataset.column_names})."
|
| 170 |
+
)
|
| 171 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 172 |
+
sys.exit(1)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
def get_prompt(
|
| 176 |
prompt_mode: str,
|
| 177 |
use_chinese: bool = False,
|
|
|
|
| 388 |
use_chinese: bool = False,
|
| 389 |
custom_prompt: str = None,
|
| 390 |
output_column: str = "markdown",
|
| 391 |
+
overwrite: bool = False,
|
| 392 |
clean_output: bool = True,
|
| 393 |
config: str = None,
|
| 394 |
create_pr: bool = False,
|
|
|
|
| 432 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 433 |
)
|
| 434 |
|
| 435 |
+
# Fail fast if the output column would collide with an existing input column
|
| 436 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 437 |
+
|
| 438 |
# Shuffle if requested
|
| 439 |
if shuffle:
|
| 440 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 812 |
default="markdown",
|
| 813 |
help="Column name for output text (default: markdown)",
|
| 814 |
)
|
| 815 |
+
parser.add_argument(
|
| 816 |
+
"--overwrite",
|
| 817 |
+
action="store_true",
|
| 818 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 819 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 820 |
+
)
|
| 821 |
parser.add_argument(
|
| 822 |
"--no-clean-output",
|
| 823 |
action="store_true",
|
|
|
|
| 867 |
use_chinese=args.use_chinese_prompts,
|
| 868 |
custom_prompt=args.custom_prompt,
|
| 869 |
output_column=args.output_column,
|
| 870 |
+
overwrite=args.overwrite,
|
| 871 |
clean_output=not args.no_clean_output,
|
| 872 |
config=args.config,
|
| 873 |
create_pr=args.create_pr,
|
lfm2-extract.py
CHANGED
|
@@ -77,6 +77,28 @@ def check_cuda_availability() -> None:
|
|
| 77 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name()}")
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def load_text_arg(value: str) -> str:
|
| 81 |
"""Resolve --schema (inline text/JSON, URL, or file path) into a string."""
|
| 82 |
text = value.strip()
|
|
@@ -116,6 +138,7 @@ def main(
|
|
| 116 |
schema: str,
|
| 117 |
text_column: str = "text",
|
| 118 |
output_column: str = "extraction",
|
|
|
|
| 119 |
output_format: str = "json",
|
| 120 |
split: str = "train",
|
| 121 |
max_samples: Optional[int] = None,
|
|
@@ -142,6 +165,10 @@ def main(
|
|
| 142 |
|
| 143 |
logger.info(f"Loading dataset: {input_dataset} (split={split})")
|
| 144 |
dataset = load_dataset(input_dataset, split=split)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
if shuffle:
|
| 146 |
dataset = dataset.shuffle(seed=seed)
|
| 147 |
if max_samples:
|
|
@@ -257,6 +284,12 @@ if __name__ == "__main__":
|
|
| 257 |
)
|
| 258 |
parser.add_argument("--text-column", default="text", help="Text column (default: text)")
|
| 259 |
parser.add_argument("--output-column", default="extraction", help="Output column (default: extraction)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
parser.add_argument(
|
| 261 |
"--format", dest="output_format", default="json", choices=list(FORMATS),
|
| 262 |
help="Output format (default: json)",
|
|
@@ -279,6 +312,7 @@ if __name__ == "__main__":
|
|
| 279 |
schema=args.schema,
|
| 280 |
text_column=args.text_column,
|
| 281 |
output_column=args.output_column,
|
|
|
|
| 282 |
output_format=args.output_format,
|
| 283 |
split=args.split,
|
| 284 |
max_samples=args.max_samples,
|
|
|
|
| 77 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name()}")
|
| 78 |
|
| 79 |
|
| 80 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 81 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 82 |
+
|
| 83 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 84 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 85 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 86 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 87 |
+
"""
|
| 88 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 89 |
+
if not clash:
|
| 90 |
+
return dataset
|
| 91 |
+
if overwrite:
|
| 92 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 93 |
+
return dataset.remove_columns(clash)
|
| 94 |
+
logger.error(
|
| 95 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 96 |
+
f"(columns: {dataset.column_names})."
|
| 97 |
+
)
|
| 98 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 99 |
+
sys.exit(1)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
def load_text_arg(value: str) -> str:
|
| 103 |
"""Resolve --schema (inline text/JSON, URL, or file path) into a string."""
|
| 104 |
text = value.strip()
|
|
|
|
| 138 |
schema: str,
|
| 139 |
text_column: str = "text",
|
| 140 |
output_column: str = "extraction",
|
| 141 |
+
overwrite: bool = False,
|
| 142 |
output_format: str = "json",
|
| 143 |
split: str = "train",
|
| 144 |
max_samples: Optional[int] = None,
|
|
|
|
| 165 |
|
| 166 |
logger.info(f"Loading dataset: {input_dataset} (split={split})")
|
| 167 |
dataset = load_dataset(input_dataset, split=split)
|
| 168 |
+
|
| 169 |
+
# Fail fast if the output column would collide with an existing input column
|
| 170 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 171 |
+
|
| 172 |
if shuffle:
|
| 173 |
dataset = dataset.shuffle(seed=seed)
|
| 174 |
if max_samples:
|
|
|
|
| 284 |
)
|
| 285 |
parser.add_argument("--text-column", default="text", help="Text column (default: text)")
|
| 286 |
parser.add_argument("--output-column", default="extraction", help="Output column (default: extraction)")
|
| 287 |
+
parser.add_argument(
|
| 288 |
+
"--overwrite",
|
| 289 |
+
action="store_true",
|
| 290 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 291 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 292 |
+
)
|
| 293 |
parser.add_argument(
|
| 294 |
"--format", dest="output_format", default="json", choices=list(FORMATS),
|
| 295 |
help="Output format (default: json)",
|
|
|
|
| 312 |
schema=args.schema,
|
| 313 |
text_column=args.text_column,
|
| 314 |
output_column=args.output_column,
|
| 315 |
+
overwrite=args.overwrite,
|
| 316 |
output_format=args.output_format,
|
| 317 |
split=args.split,
|
| 318 |
max_samples=args.max_samples,
|
lfm2-vl-extract.py
CHANGED
|
@@ -80,6 +80,28 @@ def check_cuda_availability() -> None:
|
|
| 80 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name()}")
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def load_schema_arg(value: str) -> Dict[str, str]:
|
| 84 |
"""Resolve --schema (inline JSON, URL, or file path) into a {field: description} dict."""
|
| 85 |
text = value.strip()
|
|
@@ -152,6 +174,7 @@ def main(
|
|
| 152 |
schema: str,
|
| 153 |
image_column: str = "image",
|
| 154 |
output_column: str = "extraction",
|
|
|
|
| 155 |
split: str = "train",
|
| 156 |
max_samples: Optional[int] = None,
|
| 157 |
shuffle: bool = False,
|
|
@@ -175,6 +198,10 @@ def main(
|
|
| 175 |
|
| 176 |
logger.info(f"Loading dataset: {input_dataset} (split={split})")
|
| 177 |
dataset = load_dataset(input_dataset, split=split)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
if shuffle:
|
| 179 |
dataset = dataset.shuffle(seed=seed)
|
| 180 |
if max_samples:
|
|
@@ -293,6 +320,12 @@ if __name__ == "__main__":
|
|
| 293 |
)
|
| 294 |
parser.add_argument("--image-column", default="image", help="Image column (default: image)")
|
| 295 |
parser.add_argument("--output-column", default="extraction", help="Output column (default: extraction)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
parser.add_argument("--split", default="train", help="Dataset split (default: train)")
|
| 297 |
parser.add_argument("--max-samples", type=int, help="Limit number of samples")
|
| 298 |
parser.add_argument("--shuffle", action="store_true", help="Shuffle before sampling")
|
|
@@ -311,6 +344,7 @@ if __name__ == "__main__":
|
|
| 311 |
schema=args.schema,
|
| 312 |
image_column=args.image_column,
|
| 313 |
output_column=args.output_column,
|
|
|
|
| 314 |
split=args.split,
|
| 315 |
max_samples=args.max_samples,
|
| 316 |
shuffle=args.shuffle,
|
|
|
|
| 80 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name()}")
|
| 81 |
|
| 82 |
|
| 83 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 84 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 85 |
+
|
| 86 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 87 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 88 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 89 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 90 |
+
"""
|
| 91 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 92 |
+
if not clash:
|
| 93 |
+
return dataset
|
| 94 |
+
if overwrite:
|
| 95 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 96 |
+
return dataset.remove_columns(clash)
|
| 97 |
+
logger.error(
|
| 98 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 99 |
+
f"(columns: {dataset.column_names})."
|
| 100 |
+
)
|
| 101 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 102 |
+
sys.exit(1)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
def load_schema_arg(value: str) -> Dict[str, str]:
|
| 106 |
"""Resolve --schema (inline JSON, URL, or file path) into a {field: description} dict."""
|
| 107 |
text = value.strip()
|
|
|
|
| 174 |
schema: str,
|
| 175 |
image_column: str = "image",
|
| 176 |
output_column: str = "extraction",
|
| 177 |
+
overwrite: bool = False,
|
| 178 |
split: str = "train",
|
| 179 |
max_samples: Optional[int] = None,
|
| 180 |
shuffle: bool = False,
|
|
|
|
| 198 |
|
| 199 |
logger.info(f"Loading dataset: {input_dataset} (split={split})")
|
| 200 |
dataset = load_dataset(input_dataset, split=split)
|
| 201 |
+
|
| 202 |
+
# Fail fast if the output column would collide with an existing input column
|
| 203 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 204 |
+
|
| 205 |
if shuffle:
|
| 206 |
dataset = dataset.shuffle(seed=seed)
|
| 207 |
if max_samples:
|
|
|
|
| 320 |
)
|
| 321 |
parser.add_argument("--image-column", default="image", help="Image column (default: image)")
|
| 322 |
parser.add_argument("--output-column", default="extraction", help="Output column (default: extraction)")
|
| 323 |
+
parser.add_argument(
|
| 324 |
+
"--overwrite",
|
| 325 |
+
action="store_true",
|
| 326 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 327 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 328 |
+
)
|
| 329 |
parser.add_argument("--split", default="train", help="Dataset split (default: train)")
|
| 330 |
parser.add_argument("--max-samples", type=int, help="Limit number of samples")
|
| 331 |
parser.add_argument("--shuffle", action="store_true", help="Shuffle before sampling")
|
|
|
|
| 344 |
schema=args.schema,
|
| 345 |
image_column=args.image_column,
|
| 346 |
output_column=args.output_column,
|
| 347 |
+
overwrite=args.overwrite,
|
| 348 |
split=args.split,
|
| 349 |
max_samples=args.max_samples,
|
| 350 |
shuffle=args.shuffle,
|
lift-extract.py
CHANGED
|
@@ -111,6 +111,28 @@ def check_cuda_availability() -> None:
|
|
| 111 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 112 |
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
def load_schema_arg(value: str) -> Dict[str, Any]:
|
| 115 |
"""Resolve --schema (inline JSON, a URL, or a file path) into a JSON Schema dict."""
|
| 116 |
text = value.strip()
|
|
@@ -423,6 +445,7 @@ def main(
|
|
| 423 |
image_column: str = "image",
|
| 424 |
pdf_column: Optional[str] = None,
|
| 425 |
output_column: str = "extraction",
|
|
|
|
| 426 |
method: str = "hf",
|
| 427 |
page_range: Optional[str] = None,
|
| 428 |
split: str = "train",
|
|
@@ -478,6 +501,10 @@ def main(
|
|
| 478 |
f"Column '{source_column}' not found. Available: {dataset.column_names}"
|
| 479 |
)
|
| 480 |
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
if shuffle:
|
| 482 |
dataset = dataset.shuffle(seed=seed)
|
| 483 |
if max_samples:
|
|
@@ -704,6 +731,12 @@ Input (one document per row):
|
|
| 704 |
default="extraction",
|
| 705 |
help="Output column (default: extraction)",
|
| 706 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
parser.add_argument(
|
| 708 |
"--method",
|
| 709 |
choices=["hf", "vllm"],
|
|
@@ -792,6 +825,7 @@ Input (one document per row):
|
|
| 792 |
image_column=args.image_column,
|
| 793 |
pdf_column=args.pdf_column,
|
| 794 |
output_column=args.output_column,
|
|
|
|
| 795 |
method=args.method,
|
| 796 |
page_range=args.page_range,
|
| 797 |
split=args.split,
|
|
|
|
| 111 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 112 |
|
| 113 |
|
| 114 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 115 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 116 |
+
|
| 117 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 118 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 119 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 120 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 121 |
+
"""
|
| 122 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 123 |
+
if not clash:
|
| 124 |
+
return dataset
|
| 125 |
+
if overwrite:
|
| 126 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 127 |
+
return dataset.remove_columns(clash)
|
| 128 |
+
logger.error(
|
| 129 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 130 |
+
f"(columns: {dataset.column_names})."
|
| 131 |
+
)
|
| 132 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 133 |
+
sys.exit(1)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
def load_schema_arg(value: str) -> Dict[str, Any]:
|
| 137 |
"""Resolve --schema (inline JSON, a URL, or a file path) into a JSON Schema dict."""
|
| 138 |
text = value.strip()
|
|
|
|
| 445 |
image_column: str = "image",
|
| 446 |
pdf_column: Optional[str] = None,
|
| 447 |
output_column: str = "extraction",
|
| 448 |
+
overwrite: bool = False,
|
| 449 |
method: str = "hf",
|
| 450 |
page_range: Optional[str] = None,
|
| 451 |
split: str = "train",
|
|
|
|
| 501 |
f"Column '{source_column}' not found. Available: {dataset.column_names}"
|
| 502 |
)
|
| 503 |
sys.exit(1)
|
| 504 |
+
|
| 505 |
+
# Fail fast if the output column would collide with an existing input column
|
| 506 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 507 |
+
|
| 508 |
if shuffle:
|
| 509 |
dataset = dataset.shuffle(seed=seed)
|
| 510 |
if max_samples:
|
|
|
|
| 731 |
default="extraction",
|
| 732 |
help="Output column (default: extraction)",
|
| 733 |
)
|
| 734 |
+
parser.add_argument(
|
| 735 |
+
"--overwrite",
|
| 736 |
+
action="store_true",
|
| 737 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 738 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 739 |
+
)
|
| 740 |
parser.add_argument(
|
| 741 |
"--method",
|
| 742 |
choices=["hf", "vllm"],
|
|
|
|
| 825 |
image_column=args.image_column,
|
| 826 |
pdf_column=args.pdf_column,
|
| 827 |
output_column=args.output_column,
|
| 828 |
+
overwrite=args.overwrite,
|
| 829 |
method=args.method,
|
| 830 |
page_range=args.page_range,
|
| 831 |
split=args.split,
|
lighton-ocr.py
CHANGED
|
@@ -85,6 +85,28 @@ def check_cuda_availability():
|
|
| 85 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def resize_image_to_target(image: Image.Image, target_size: int = 1540) -> Image.Image:
|
| 89 |
"""
|
| 90 |
Resize image so longest dimension is target_size while maintaining aspect ratio.
|
|
@@ -295,6 +317,7 @@ def main(
|
|
| 295 |
shuffle: bool = False,
|
| 296 |
seed: int = 42,
|
| 297 |
output_column: str = "markdown",
|
|
|
|
| 298 |
):
|
| 299 |
"""Process images from HF dataset through LightOnOCR model."""
|
| 300 |
|
|
@@ -327,6 +350,9 @@ def main(
|
|
| 327 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 328 |
)
|
| 329 |
|
|
|
|
|
|
|
|
|
|
| 330 |
# Shuffle if requested
|
| 331 |
if shuffle:
|
| 332 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -625,6 +651,12 @@ Examples:
|
|
| 625 |
default="markdown",
|
| 626 |
help="Column name for output text (default: markdown)",
|
| 627 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
|
| 629 |
args = parser.parse_args()
|
| 630 |
|
|
@@ -648,4 +680,5 @@ Examples:
|
|
| 648 |
shuffle=args.shuffle,
|
| 649 |
seed=args.seed,
|
| 650 |
output_column=args.output_column,
|
|
|
|
| 651 |
)
|
|
|
|
| 85 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 86 |
|
| 87 |
|
| 88 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 89 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 90 |
+
|
| 91 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 92 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 93 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 94 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 95 |
+
"""
|
| 96 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 97 |
+
if not clash:
|
| 98 |
+
return dataset
|
| 99 |
+
if overwrite:
|
| 100 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 101 |
+
return dataset.remove_columns(clash)
|
| 102 |
+
logger.error(
|
| 103 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 104 |
+
f"(columns: {dataset.column_names})."
|
| 105 |
+
)
|
| 106 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 107 |
+
sys.exit(1)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
def resize_image_to_target(image: Image.Image, target_size: int = 1540) -> Image.Image:
|
| 111 |
"""
|
| 112 |
Resize image so longest dimension is target_size while maintaining aspect ratio.
|
|
|
|
| 317 |
shuffle: bool = False,
|
| 318 |
seed: int = 42,
|
| 319 |
output_column: str = "markdown",
|
| 320 |
+
overwrite: bool = False,
|
| 321 |
):
|
| 322 |
"""Process images from HF dataset through LightOnOCR model."""
|
| 323 |
|
|
|
|
| 350 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 351 |
)
|
| 352 |
|
| 353 |
+
# Fail fast if the output column would collide with an existing input column
|
| 354 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 355 |
+
|
| 356 |
# Shuffle if requested
|
| 357 |
if shuffle:
|
| 358 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 651 |
default="markdown",
|
| 652 |
help="Column name for output text (default: markdown)",
|
| 653 |
)
|
| 654 |
+
parser.add_argument(
|
| 655 |
+
"--overwrite",
|
| 656 |
+
action="store_true",
|
| 657 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 658 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 659 |
+
)
|
| 660 |
|
| 661 |
args = parser.parse_args()
|
| 662 |
|
|
|
|
| 680 |
shuffle=args.shuffle,
|
| 681 |
seed=args.seed,
|
| 682 |
output_column=args.output_column,
|
| 683 |
+
overwrite=args.overwrite,
|
| 684 |
)
|
nanonets-ocr.py
CHANGED
|
@@ -61,6 +61,28 @@ def check_cuda_availability():
|
|
| 61 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def make_ocr_message(
|
| 65 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 66 |
prompt: str = "Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using ☐ and ☑ for check boxes.",
|
|
@@ -208,7 +230,7 @@ def main(
|
|
| 208 |
image_column: str = "image",
|
| 209 |
batch_size: int = 32,
|
| 210 |
model: str = "nanonets/Nanonets-OCR-s",
|
| 211 |
-
max_model_len: int =
|
| 212 |
max_tokens: int = 15000,
|
| 213 |
gpu_memory_utilization: float = 0.8,
|
| 214 |
hf_token: str = None,
|
|
@@ -217,6 +239,8 @@ def main(
|
|
| 217 |
private: bool = False,
|
| 218 |
shuffle: bool = False,
|
| 219 |
seed: int = 42,
|
|
|
|
|
|
|
| 220 |
verbose: bool = False,
|
| 221 |
):
|
| 222 |
"""Process images from HF dataset through OCR model."""
|
|
@@ -245,6 +269,9 @@ def main(
|
|
| 245 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 246 |
)
|
| 247 |
|
|
|
|
|
|
|
|
|
|
| 248 |
# Shuffle if requested
|
| 249 |
if shuffle:
|
| 250 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -301,9 +328,9 @@ def main(
|
|
| 301 |
# Add error placeholders for failed batch
|
| 302 |
all_markdown.extend(["[OCR FAILED]"] * len(batch_images))
|
| 303 |
|
| 304 |
-
# Add
|
| 305 |
-
logger.info("Adding
|
| 306 |
-
dataset = dataset.add_column(
|
| 307 |
|
| 308 |
# Handle inference_info tracking
|
| 309 |
logger.info("Updating inference_info...")
|
|
@@ -311,7 +338,7 @@ def main(
|
|
| 311 |
inference_entry = {
|
| 312 |
"model_id": model,
|
| 313 |
"model_name": "Nanonets-OCR-s",
|
| 314 |
-
"column_name":
|
| 315 |
"timestamp": datetime.now().isoformat(),
|
| 316 |
"batch_size": batch_size,
|
| 317 |
"max_tokens": max_tokens,
|
|
@@ -466,8 +493,10 @@ Examples:
|
|
| 466 |
parser.add_argument(
|
| 467 |
"--max-model-len",
|
| 468 |
type=int,
|
| 469 |
-
default=
|
| 470 |
-
help="Maximum model context length (default:
|
|
|
|
|
|
|
| 471 |
)
|
| 472 |
parser.add_argument(
|
| 473 |
"--max-tokens",
|
|
@@ -504,6 +533,17 @@ Examples:
|
|
| 504 |
default=42,
|
| 505 |
help="Random seed for shuffling (default: 42)",
|
| 506 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
parser.add_argument(
|
| 508 |
"--verbose",
|
| 509 |
action="store_true",
|
|
@@ -527,5 +567,7 @@ Examples:
|
|
| 527 |
private=args.private,
|
| 528 |
shuffle=args.shuffle,
|
| 529 |
seed=args.seed,
|
|
|
|
|
|
|
| 530 |
verbose=args.verbose,
|
| 531 |
)
|
|
|
|
| 61 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 62 |
|
| 63 |
|
| 64 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 65 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 66 |
+
|
| 67 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 68 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 69 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 70 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 71 |
+
"""
|
| 72 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 73 |
+
if not clash:
|
| 74 |
+
return dataset
|
| 75 |
+
if overwrite:
|
| 76 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 77 |
+
return dataset.remove_columns(clash)
|
| 78 |
+
logger.error(
|
| 79 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 80 |
+
f"(columns: {dataset.column_names})."
|
| 81 |
+
)
|
| 82 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 83 |
+
sys.exit(1)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
def make_ocr_message(
|
| 87 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 88 |
prompt: str = "Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using ☐ and ☑ for check boxes.",
|
|
|
|
| 230 |
image_column: str = "image",
|
| 231 |
batch_size: int = 32,
|
| 232 |
model: str = "nanonets/Nanonets-OCR-s",
|
| 233 |
+
max_model_len: int = 32768,
|
| 234 |
max_tokens: int = 15000,
|
| 235 |
gpu_memory_utilization: float = 0.8,
|
| 236 |
hf_token: str = None,
|
|
|
|
| 239 |
private: bool = False,
|
| 240 |
shuffle: bool = False,
|
| 241 |
seed: int = 42,
|
| 242 |
+
output_column: str = "markdown",
|
| 243 |
+
overwrite: bool = False,
|
| 244 |
verbose: bool = False,
|
| 245 |
):
|
| 246 |
"""Process images from HF dataset through OCR model."""
|
|
|
|
| 269 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 270 |
)
|
| 271 |
|
| 272 |
+
# Fail fast if the output column would collide with an existing input column
|
| 273 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 274 |
+
|
| 275 |
# Shuffle if requested
|
| 276 |
if shuffle:
|
| 277 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 328 |
# Add error placeholders for failed batch
|
| 329 |
all_markdown.extend(["[OCR FAILED]"] * len(batch_images))
|
| 330 |
|
| 331 |
+
# Add output column to dataset
|
| 332 |
+
logger.info(f"Adding '{output_column}' column to dataset")
|
| 333 |
+
dataset = dataset.add_column(output_column, all_markdown)
|
| 334 |
|
| 335 |
# Handle inference_info tracking
|
| 336 |
logger.info("Updating inference_info...")
|
|
|
|
| 338 |
inference_entry = {
|
| 339 |
"model_id": model,
|
| 340 |
"model_name": "Nanonets-OCR-s",
|
| 341 |
+
"column_name": output_column,
|
| 342 |
"timestamp": datetime.now().isoformat(),
|
| 343 |
"batch_size": batch_size,
|
| 344 |
"max_tokens": max_tokens,
|
|
|
|
| 493 |
parser.add_argument(
|
| 494 |
"--max-model-len",
|
| 495 |
type=int,
|
| 496 |
+
default=32768,
|
| 497 |
+
help="Maximum model context length (default: 32768; must exceed --max-tokens "
|
| 498 |
+
"15000 plus the image/prompt tokens — the old 8192 default couldn't hold the "
|
| 499 |
+
"output budget). Model max is 128k, so there's ample room.",
|
| 500 |
)
|
| 501 |
parser.add_argument(
|
| 502 |
"--max-tokens",
|
|
|
|
| 533 |
default=42,
|
| 534 |
help="Random seed for shuffling (default: 42)",
|
| 535 |
)
|
| 536 |
+
parser.add_argument(
|
| 537 |
+
"--output-column",
|
| 538 |
+
default="markdown",
|
| 539 |
+
help="Column name for the OCR output text (default: markdown)",
|
| 540 |
+
)
|
| 541 |
+
parser.add_argument(
|
| 542 |
+
"--overwrite",
|
| 543 |
+
action="store_true",
|
| 544 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 545 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 546 |
+
)
|
| 547 |
parser.add_argument(
|
| 548 |
"--verbose",
|
| 549 |
action="store_true",
|
|
|
|
| 567 |
private=args.private,
|
| 568 |
shuffle=args.shuffle,
|
| 569 |
seed=args.seed,
|
| 570 |
+
output_column=args.output_column,
|
| 571 |
+
overwrite=args.overwrite,
|
| 572 |
verbose=args.verbose,
|
| 573 |
)
|
nuextract3.py
CHANGED
|
@@ -93,6 +93,28 @@ def check_cuda_availability():
|
|
| 93 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def load_template_arg(value: Optional[str]) -> Optional[Dict[str, Any]]:
|
| 97 |
"""Load a NuExtract template/JSON Schema from inline JSON, a URL, or a file path."""
|
| 98 |
if value is None:
|
|
@@ -295,6 +317,7 @@ def main(
|
|
| 295 |
shuffle: bool = False,
|
| 296 |
seed: int = 42,
|
| 297 |
output_column: Optional[str] = None,
|
|
|
|
| 298 |
verbose: bool = False,
|
| 299 |
config: str = None,
|
| 300 |
create_pr: bool = False,
|
|
@@ -332,6 +355,13 @@ def main(
|
|
| 332 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 333 |
)
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
if shuffle:
|
| 336 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 337 |
dataset = dataset.shuffle(seed=seed)
|
|
@@ -713,6 +743,12 @@ Examples:
|
|
| 713 |
default=None,
|
| 714 |
help="Column name for output (default: 'markdown' in OCR mode, 'extraction' in template mode)",
|
| 715 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
parser.add_argument(
|
| 717 |
"--verbose",
|
| 718 |
action="store_true",
|
|
@@ -743,6 +779,7 @@ Examples:
|
|
| 743 |
shuffle=args.shuffle,
|
| 744 |
seed=args.seed,
|
| 745 |
output_column=args.output_column,
|
|
|
|
| 746 |
verbose=args.verbose,
|
| 747 |
config=args.config,
|
| 748 |
create_pr=args.create_pr,
|
|
|
|
| 93 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 94 |
|
| 95 |
|
| 96 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 97 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 98 |
+
|
| 99 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 100 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 101 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 102 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 103 |
+
"""
|
| 104 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 105 |
+
if not clash:
|
| 106 |
+
return dataset
|
| 107 |
+
if overwrite:
|
| 108 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 109 |
+
return dataset.remove_columns(clash)
|
| 110 |
+
logger.error(
|
| 111 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 112 |
+
f"(columns: {dataset.column_names})."
|
| 113 |
+
)
|
| 114 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 115 |
+
sys.exit(1)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
def load_template_arg(value: Optional[str]) -> Optional[Dict[str, Any]]:
|
| 119 |
"""Load a NuExtract template/JSON Schema from inline JSON, a URL, or a file path."""
|
| 120 |
if value is None:
|
|
|
|
| 317 |
shuffle: bool = False,
|
| 318 |
seed: int = 42,
|
| 319 |
output_column: Optional[str] = None,
|
| 320 |
+
overwrite: bool = False,
|
| 321 |
verbose: bool = False,
|
| 322 |
config: str = None,
|
| 323 |
create_pr: bool = False,
|
|
|
|
| 355 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 356 |
)
|
| 357 |
|
| 358 |
+
# Fail fast if an output column would collide with an existing input column.
|
| 359 |
+
# With --enable-thinking the script also writes "{output_column}_reasoning".
|
| 360 |
+
out_cols = [output_column]
|
| 361 |
+
if enable_thinking:
|
| 362 |
+
out_cols.append(f"{output_column}_reasoning")
|
| 363 |
+
dataset = ensure_output_columns_free(dataset, out_cols, overwrite=overwrite)
|
| 364 |
+
|
| 365 |
if shuffle:
|
| 366 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 367 |
dataset = dataset.shuffle(seed=seed)
|
|
|
|
| 743 |
default=None,
|
| 744 |
help="Column name for output (default: 'markdown' in OCR mode, 'extraction' in template mode)",
|
| 745 |
)
|
| 746 |
+
parser.add_argument(
|
| 747 |
+
"--overwrite",
|
| 748 |
+
action="store_true",
|
| 749 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 750 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 751 |
+
)
|
| 752 |
parser.add_argument(
|
| 753 |
"--verbose",
|
| 754 |
action="store_true",
|
|
|
|
| 779 |
shuffle=args.shuffle,
|
| 780 |
seed=args.seed,
|
| 781 |
output_column=args.output_column,
|
| 782 |
+
overwrite=args.overwrite,
|
| 783 |
verbose=args.verbose,
|
| 784 |
config=args.config,
|
| 785 |
create_pr=args.create_pr,
|
numarkdown-ocr.py
CHANGED
|
@@ -75,6 +75,28 @@ def check_gpu_availability() -> int:
|
|
| 75 |
return num_gpus
|
| 76 |
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def validate_and_resize_image(
|
| 79 |
image: Image.Image,
|
| 80 |
min_pixels: int = 100 * 28 * 28,
|
|
@@ -317,6 +339,7 @@ def main(
|
|
| 317 |
temperature: float = 0.0,
|
| 318 |
custom_prompt: Optional[str] = None,
|
| 319 |
output_column: str = "markdown",
|
|
|
|
| 320 |
config: str = None,
|
| 321 |
create_pr: bool = False,
|
| 322 |
verbose: bool = False,
|
|
@@ -361,6 +384,9 @@ def main(
|
|
| 361 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 362 |
)
|
| 363 |
|
|
|
|
|
|
|
|
|
|
| 364 |
# Shuffle if requested
|
| 365 |
if shuffle:
|
| 366 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -700,6 +726,12 @@ Examples:
|
|
| 700 |
default="markdown",
|
| 701 |
help="Column name for output text (default: markdown)",
|
| 702 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
parser.add_argument(
|
| 704 |
"--config",
|
| 705 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
@@ -737,6 +769,7 @@ Examples:
|
|
| 737 |
temperature=args.temperature,
|
| 738 |
custom_prompt=args.custom_prompt,
|
| 739 |
output_column=args.output_column,
|
|
|
|
| 740 |
config=args.config,
|
| 741 |
create_pr=args.create_pr,
|
| 742 |
verbose=args.verbose,
|
|
|
|
| 75 |
return num_gpus
|
| 76 |
|
| 77 |
|
| 78 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 79 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 80 |
+
|
| 81 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 82 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 83 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 84 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 85 |
+
"""
|
| 86 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 87 |
+
if not clash:
|
| 88 |
+
return dataset
|
| 89 |
+
if overwrite:
|
| 90 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 91 |
+
return dataset.remove_columns(clash)
|
| 92 |
+
logger.error(
|
| 93 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 94 |
+
f"(columns: {dataset.column_names})."
|
| 95 |
+
)
|
| 96 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 97 |
+
sys.exit(1)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
def validate_and_resize_image(
|
| 101 |
image: Image.Image,
|
| 102 |
min_pixels: int = 100 * 28 * 28,
|
|
|
|
| 339 |
temperature: float = 0.0,
|
| 340 |
custom_prompt: Optional[str] = None,
|
| 341 |
output_column: str = "markdown",
|
| 342 |
+
overwrite: bool = False,
|
| 343 |
config: str = None,
|
| 344 |
create_pr: bool = False,
|
| 345 |
verbose: bool = False,
|
|
|
|
| 384 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 385 |
)
|
| 386 |
|
| 387 |
+
# Fail fast if the output column would collide with an existing input column
|
| 388 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 389 |
+
|
| 390 |
# Shuffle if requested
|
| 391 |
if shuffle:
|
| 392 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 726 |
default="markdown",
|
| 727 |
help="Column name for output text (default: markdown)",
|
| 728 |
)
|
| 729 |
+
parser.add_argument(
|
| 730 |
+
"--overwrite",
|
| 731 |
+
action="store_true",
|
| 732 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 733 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 734 |
+
)
|
| 735 |
parser.add_argument(
|
| 736 |
"--config",
|
| 737 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
|
|
| 769 |
temperature=args.temperature,
|
| 770 |
custom_prompt=args.custom_prompt,
|
| 771 |
output_column=args.output_column,
|
| 772 |
+
overwrite=args.overwrite,
|
| 773 |
config=args.config,
|
| 774 |
create_pr=args.create_pr,
|
| 775 |
verbose=args.verbose,
|
olmocr2-vllm.py
CHANGED
|
@@ -81,6 +81,28 @@ def check_cuda_availability():
|
|
| 81 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 82 |
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def parse_yaml_frontmatter(text: str) -> tuple[dict, str]:
|
| 85 |
"""
|
| 86 |
Parse YAML front matter from olmOCR output.
|
|
@@ -280,6 +302,7 @@ def main(
|
|
| 280 |
output_dataset: str,
|
| 281 |
image_column: str = "image",
|
| 282 |
output_column: str = "markdown",
|
|
|
|
| 283 |
batch_size: int = 16,
|
| 284 |
model: str = "allenai/olmOCR-2-7B-1025-FP8",
|
| 285 |
max_model_len: int = 16384,
|
|
@@ -333,6 +356,9 @@ def main(
|
|
| 333 |
logger.info(f"Loading dataset: {input_dataset}")
|
| 334 |
ds = load_dataset(input_dataset, split=split)
|
| 335 |
|
|
|
|
|
|
|
|
|
|
| 336 |
# Shuffle if requested
|
| 337 |
if shuffle:
|
| 338 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -558,6 +584,12 @@ Examples:
|
|
| 558 |
default="markdown",
|
| 559 |
help="Column name for markdown output (default: markdown)",
|
| 560 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
parser.add_argument(
|
| 562 |
"--batch-size",
|
| 563 |
type=int,
|
|
@@ -635,6 +667,7 @@ Examples:
|
|
| 635 |
output_dataset=args.output_dataset,
|
| 636 |
image_column=args.image_column,
|
| 637 |
output_column=args.output_column,
|
|
|
|
| 638 |
batch_size=args.batch_size,
|
| 639 |
model=args.model,
|
| 640 |
max_model_len=args.max_model_len,
|
|
|
|
| 81 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 82 |
|
| 83 |
|
| 84 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 85 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 86 |
+
|
| 87 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 88 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 89 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 90 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 91 |
+
"""
|
| 92 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 93 |
+
if not clash:
|
| 94 |
+
return dataset
|
| 95 |
+
if overwrite:
|
| 96 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 97 |
+
return dataset.remove_columns(clash)
|
| 98 |
+
logger.error(
|
| 99 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 100 |
+
f"(columns: {dataset.column_names})."
|
| 101 |
+
)
|
| 102 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 103 |
+
sys.exit(1)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
def parse_yaml_frontmatter(text: str) -> tuple[dict, str]:
|
| 107 |
"""
|
| 108 |
Parse YAML front matter from olmOCR output.
|
|
|
|
| 302 |
output_dataset: str,
|
| 303 |
image_column: str = "image",
|
| 304 |
output_column: str = "markdown",
|
| 305 |
+
overwrite: bool = False,
|
| 306 |
batch_size: int = 16,
|
| 307 |
model: str = "allenai/olmOCR-2-7B-1025-FP8",
|
| 308 |
max_model_len: int = 16384,
|
|
|
|
| 356 |
logger.info(f"Loading dataset: {input_dataset}")
|
| 357 |
ds = load_dataset(input_dataset, split=split)
|
| 358 |
|
| 359 |
+
# Fail fast if the output column would collide with an existing input column
|
| 360 |
+
ds = ensure_output_columns_free(ds, [output_column], overwrite=overwrite)
|
| 361 |
+
|
| 362 |
# Shuffle if requested
|
| 363 |
if shuffle:
|
| 364 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 584 |
default="markdown",
|
| 585 |
help="Column name for markdown output (default: markdown)",
|
| 586 |
)
|
| 587 |
+
parser.add_argument(
|
| 588 |
+
"--overwrite",
|
| 589 |
+
action="store_true",
|
| 590 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 591 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 592 |
+
)
|
| 593 |
parser.add_argument(
|
| 594 |
"--batch-size",
|
| 595 |
type=int,
|
|
|
|
| 667 |
output_dataset=args.output_dataset,
|
| 668 |
image_column=args.image_column,
|
| 669 |
output_column=args.output_column,
|
| 670 |
+
overwrite=args.overwrite,
|
| 671 |
batch_size=args.batch_size,
|
| 672 |
model=args.model,
|
| 673 |
max_model_len=args.max_model_len,
|
paddleocr-vl-1.5.py
CHANGED
|
@@ -101,6 +101,28 @@ def check_cuda_availability():
|
|
| 101 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 102 |
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def prepare_image(
|
| 105 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 106 |
) -> Image.Image:
|
|
@@ -282,6 +304,7 @@ def main(
|
|
| 282 |
shuffle: bool = False,
|
| 283 |
seed: int = 42,
|
| 284 |
output_column: str = "markdown",
|
|
|
|
| 285 |
config: str = None,
|
| 286 |
create_pr: bool = False,
|
| 287 |
verbose: bool = False,
|
|
@@ -318,6 +341,9 @@ def main(
|
|
| 318 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 319 |
)
|
| 320 |
|
|
|
|
|
|
|
|
|
|
| 321 |
# Shuffle if requested
|
| 322 |
if shuffle:
|
| 323 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -641,6 +667,12 @@ Backend: Transformers batch inference (not vLLM)
|
|
| 641 |
default="markdown",
|
| 642 |
help="Column name for output text (default: markdown)",
|
| 643 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
parser.add_argument(
|
| 645 |
"--config",
|
| 646 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
@@ -671,6 +703,7 @@ Backend: Transformers batch inference (not vLLM)
|
|
| 671 |
shuffle=args.shuffle,
|
| 672 |
seed=args.seed,
|
| 673 |
output_column=args.output_column,
|
|
|
|
| 674 |
config=args.config,
|
| 675 |
create_pr=args.create_pr,
|
| 676 |
verbose=args.verbose,
|
|
|
|
| 101 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 102 |
|
| 103 |
|
| 104 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 105 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 106 |
+
|
| 107 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 108 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 109 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 110 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 111 |
+
"""
|
| 112 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 113 |
+
if not clash:
|
| 114 |
+
return dataset
|
| 115 |
+
if overwrite:
|
| 116 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 117 |
+
return dataset.remove_columns(clash)
|
| 118 |
+
logger.error(
|
| 119 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 120 |
+
f"(columns: {dataset.column_names})."
|
| 121 |
+
)
|
| 122 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 123 |
+
sys.exit(1)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
def prepare_image(
|
| 127 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 128 |
) -> Image.Image:
|
|
|
|
| 304 |
shuffle: bool = False,
|
| 305 |
seed: int = 42,
|
| 306 |
output_column: str = "markdown",
|
| 307 |
+
overwrite: bool = False,
|
| 308 |
config: str = None,
|
| 309 |
create_pr: bool = False,
|
| 310 |
verbose: bool = False,
|
|
|
|
| 341 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 342 |
)
|
| 343 |
|
| 344 |
+
# Fail fast if the output column would collide with an existing input column
|
| 345 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 346 |
+
|
| 347 |
# Shuffle if requested
|
| 348 |
if shuffle:
|
| 349 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 667 |
default="markdown",
|
| 668 |
help="Column name for output text (default: markdown)",
|
| 669 |
)
|
| 670 |
+
parser.add_argument(
|
| 671 |
+
"--overwrite",
|
| 672 |
+
action="store_true",
|
| 673 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 674 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 675 |
+
)
|
| 676 |
parser.add_argument(
|
| 677 |
"--config",
|
| 678 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
|
|
| 703 |
shuffle=args.shuffle,
|
| 704 |
seed=args.seed,
|
| 705 |
output_column=args.output_column,
|
| 706 |
+
overwrite=args.overwrite,
|
| 707 |
config=args.config,
|
| 708 |
create_pr=args.create_pr,
|
| 709 |
verbose=args.verbose,
|
paddleocr-vl-1.6.py
CHANGED
|
@@ -104,6 +104,28 @@ def check_cuda_availability():
|
|
| 104 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 105 |
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
def smart_resize(
|
| 108 |
height: int,
|
| 109 |
width: int,
|
|
@@ -362,6 +384,7 @@ def main(
|
|
| 362 |
shuffle: bool = False,
|
| 363 |
seed: int = 42,
|
| 364 |
output_column: str = None,
|
|
|
|
| 365 |
config: str = None,
|
| 366 |
create_pr: bool = False,
|
| 367 |
verbose: bool = False,
|
|
@@ -405,6 +428,9 @@ def main(
|
|
| 405 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 406 |
)
|
| 407 |
|
|
|
|
|
|
|
|
|
|
| 408 |
# Shuffle if requested
|
| 409 |
if shuffle:
|
| 410 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -757,6 +783,12 @@ Examples:
|
|
| 757 |
"--output-column",
|
| 758 |
help="Column name for output (default: markdown)",
|
| 759 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
parser.add_argument(
|
| 761 |
"--config",
|
| 762 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
@@ -792,6 +824,7 @@ Examples:
|
|
| 792 |
shuffle=args.shuffle,
|
| 793 |
seed=args.seed,
|
| 794 |
output_column=args.output_column,
|
|
|
|
| 795 |
config=args.config,
|
| 796 |
create_pr=args.create_pr,
|
| 797 |
verbose=args.verbose,
|
|
|
|
| 104 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 105 |
|
| 106 |
|
| 107 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 108 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 109 |
+
|
| 110 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 111 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 112 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 113 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 114 |
+
"""
|
| 115 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 116 |
+
if not clash:
|
| 117 |
+
return dataset
|
| 118 |
+
if overwrite:
|
| 119 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 120 |
+
return dataset.remove_columns(clash)
|
| 121 |
+
logger.error(
|
| 122 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 123 |
+
f"(columns: {dataset.column_names})."
|
| 124 |
+
)
|
| 125 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 126 |
+
sys.exit(1)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
def smart_resize(
|
| 130 |
height: int,
|
| 131 |
width: int,
|
|
|
|
| 384 |
shuffle: bool = False,
|
| 385 |
seed: int = 42,
|
| 386 |
output_column: str = None,
|
| 387 |
+
overwrite: bool = False,
|
| 388 |
config: str = None,
|
| 389 |
create_pr: bool = False,
|
| 390 |
verbose: bool = False,
|
|
|
|
| 428 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 429 |
)
|
| 430 |
|
| 431 |
+
# Fail fast if the output column would collide with an existing input column
|
| 432 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 433 |
+
|
| 434 |
# Shuffle if requested
|
| 435 |
if shuffle:
|
| 436 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 783 |
"--output-column",
|
| 784 |
help="Column name for output (default: markdown)",
|
| 785 |
)
|
| 786 |
+
parser.add_argument(
|
| 787 |
+
"--overwrite",
|
| 788 |
+
action="store_true",
|
| 789 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 790 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 791 |
+
)
|
| 792 |
parser.add_argument(
|
| 793 |
"--config",
|
| 794 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
|
|
|
| 824 |
shuffle=args.shuffle,
|
| 825 |
seed=args.seed,
|
| 826 |
output_column=args.output_column,
|
| 827 |
+
overwrite=args.overwrite,
|
| 828 |
config=args.config,
|
| 829 |
create_pr=args.create_pr,
|
| 830 |
verbose=args.verbose,
|
paddleocr-vl.py
CHANGED
|
@@ -93,6 +93,28 @@ def check_cuda_availability():
|
|
| 93 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def smart_resize(
|
| 97 |
height: int,
|
| 98 |
width: int,
|
|
@@ -337,6 +359,7 @@ def main(
|
|
| 337 |
shuffle: bool = False,
|
| 338 |
seed: int = 42,
|
| 339 |
output_column: str = None,
|
|
|
|
| 340 |
verbose: bool = False,
|
| 341 |
):
|
| 342 |
"""Process images from HF dataset through PaddleOCR-VL model."""
|
|
@@ -385,6 +408,9 @@ def main(
|
|
| 385 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 386 |
)
|
| 387 |
|
|
|
|
|
|
|
|
|
|
| 388 |
# Shuffle if requested
|
| 389 |
if shuffle:
|
| 390 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -706,6 +732,12 @@ Examples:
|
|
| 706 |
"--output-column",
|
| 707 |
help="Column name for output (default: markdown)",
|
| 708 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
parser.add_argument(
|
| 710 |
"--verbose",
|
| 711 |
action="store_true",
|
|
@@ -732,5 +764,6 @@ Examples:
|
|
| 732 |
shuffle=args.shuffle,
|
| 733 |
seed=args.seed,
|
| 734 |
output_column=args.output_column,
|
|
|
|
| 735 |
verbose=args.verbose,
|
| 736 |
)
|
|
|
|
| 93 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 94 |
|
| 95 |
|
| 96 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 97 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 98 |
+
|
| 99 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 100 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 101 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 102 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 103 |
+
"""
|
| 104 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 105 |
+
if not clash:
|
| 106 |
+
return dataset
|
| 107 |
+
if overwrite:
|
| 108 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 109 |
+
return dataset.remove_columns(clash)
|
| 110 |
+
logger.error(
|
| 111 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 112 |
+
f"(columns: {dataset.column_names})."
|
| 113 |
+
)
|
| 114 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 115 |
+
sys.exit(1)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
def smart_resize(
|
| 119 |
height: int,
|
| 120 |
width: int,
|
|
|
|
| 359 |
shuffle: bool = False,
|
| 360 |
seed: int = 42,
|
| 361 |
output_column: str = None,
|
| 362 |
+
overwrite: bool = False,
|
| 363 |
verbose: bool = False,
|
| 364 |
):
|
| 365 |
"""Process images from HF dataset through PaddleOCR-VL model."""
|
|
|
|
| 408 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 409 |
)
|
| 410 |
|
| 411 |
+
# Fail fast if the output column would collide with an existing input column
|
| 412 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 413 |
+
|
| 414 |
# Shuffle if requested
|
| 415 |
if shuffle:
|
| 416 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 732 |
"--output-column",
|
| 733 |
help="Column name for output (default: markdown)",
|
| 734 |
)
|
| 735 |
+
parser.add_argument(
|
| 736 |
+
"--overwrite",
|
| 737 |
+
action="store_true",
|
| 738 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 739 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 740 |
+
)
|
| 741 |
parser.add_argument(
|
| 742 |
"--verbose",
|
| 743 |
action="store_true",
|
|
|
|
| 764 |
shuffle=args.shuffle,
|
| 765 |
seed=args.seed,
|
| 766 |
output_column=args.output_column,
|
| 767 |
+
overwrite=args.overwrite,
|
| 768 |
verbose=args.verbose,
|
| 769 |
)
|
pp-doclayout.py
CHANGED
|
@@ -433,6 +433,7 @@ class DatasetRepoSink:
|
|
| 433 |
create_pr: bool,
|
| 434 |
source_id: str,
|
| 435 |
original_dataset=None,
|
|
|
|
| 436 |
):
|
| 437 |
self.repo_id = repo_id
|
| 438 |
self.hf_token = hf_token
|
|
@@ -441,6 +442,7 @@ class DatasetRepoSink:
|
|
| 441 |
self.create_pr = create_pr
|
| 442 |
self.source_id = source_id
|
| 443 |
self.original_dataset = original_dataset
|
|
|
|
| 444 |
# Used when original_dataset is None: row-by-row buffer.
|
| 445 |
self._rows: List[Dict[str, Any]] = []
|
| 446 |
# Used when original_dataset is set: ordered layouts aligned with dataset rows.
|
|
@@ -476,7 +478,16 @@ class DatasetRepoSink:
|
|
| 476 |
# Pad to keep add_column happy.
|
| 477 |
while len(self._layouts) < len(self.original_dataset):
|
| 478 |
self._layouts.append("[]")
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
else:
|
| 481 |
if not self._rows:
|
| 482 |
logger.warning("No rows produced; nothing to push.")
|
|
@@ -891,6 +902,17 @@ def main(args: argparse.Namespace) -> None:
|
|
| 891 |
seed=args.seed,
|
| 892 |
max_samples=args.max_samples,
|
| 893 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 894 |
|
| 895 |
# ---------- sink ----------
|
| 896 |
if is_bucket_url(args.output_target):
|
|
@@ -911,6 +933,7 @@ def main(args: argparse.Namespace) -> None:
|
|
| 911 |
create_pr=args.create_pr,
|
| 912 |
source_id=args.input_source,
|
| 913 |
original_dataset=original_dataset,
|
|
|
|
| 914 |
)
|
| 915 |
|
| 916 |
completed = sink.already_done()
|
|
@@ -1163,6 +1186,12 @@ def build_parser() -> argparse.ArgumentParser:
|
|
| 1163 |
action="store_true",
|
| 1164 |
help="Create PR instead of direct push (dataset sink only)",
|
| 1165 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1166 |
# Bucket-sink-specific
|
| 1167 |
p.add_argument(
|
| 1168 |
"--shard-size",
|
|
|
|
| 433 |
create_pr: bool,
|
| 434 |
source_id: str,
|
| 435 |
original_dataset=None,
|
| 436 |
+
overwrite: bool = False,
|
| 437 |
):
|
| 438 |
self.repo_id = repo_id
|
| 439 |
self.hf_token = hf_token
|
|
|
|
| 442 |
self.create_pr = create_pr
|
| 443 |
self.source_id = source_id
|
| 444 |
self.original_dataset = original_dataset
|
| 445 |
+
self.overwrite = overwrite
|
| 446 |
# Used when original_dataset is None: row-by-row buffer.
|
| 447 |
self._rows: List[Dict[str, Any]] = []
|
| 448 |
# Used when original_dataset is set: ordered layouts aligned with dataset rows.
|
|
|
|
| 478 |
# Pad to keep add_column happy.
|
| 479 |
while len(self._layouts) < len(self.original_dataset):
|
| 480 |
self._layouts.append("[]")
|
| 481 |
+
base = self.original_dataset
|
| 482 |
+
if "layout" in base.column_names:
|
| 483 |
+
if not self.overwrite:
|
| 484 |
+
raise ValueError(
|
| 485 |
+
"Output column 'layout' already exists in the input dataset; "
|
| 486 |
+
"pass --overwrite to replace it."
|
| 487 |
+
)
|
| 488 |
+
logger.warning("--overwrite: replacing existing column 'layout'")
|
| 489 |
+
base = base.remove_columns("layout")
|
| 490 |
+
ds = base.add_column("layout", self._layouts)
|
| 491 |
else:
|
| 492 |
if not self._rows:
|
| 493 |
logger.warning("No rows produced; nothing to push.")
|
|
|
|
| 902 |
seed=args.seed,
|
| 903 |
max_samples=args.max_samples,
|
| 904 |
)
|
| 905 |
+
# Fail fast, before minutes of inference, if the 'layout' output column would
|
| 906 |
+
# collide with an existing input column. --overwrite opts in to replacing it.
|
| 907 |
+
if original_dataset is not None and "layout" in original_dataset.column_names:
|
| 908 |
+
if not args.overwrite:
|
| 909 |
+
logger.error(
|
| 910 |
+
"Output column 'layout' already exists in the input dataset "
|
| 911 |
+
f"(columns: {original_dataset.column_names})."
|
| 912 |
+
)
|
| 913 |
+
logger.error("Pass --overwrite to replace it.")
|
| 914 |
+
sys.exit(1)
|
| 915 |
+
logger.warning("--overwrite: will replace existing column 'layout'")
|
| 916 |
|
| 917 |
# ---------- sink ----------
|
| 918 |
if is_bucket_url(args.output_target):
|
|
|
|
| 933 |
create_pr=args.create_pr,
|
| 934 |
source_id=args.input_source,
|
| 935 |
original_dataset=original_dataset,
|
| 936 |
+
overwrite=args.overwrite,
|
| 937 |
)
|
| 938 |
|
| 939 |
completed = sink.already_done()
|
|
|
|
| 1186 |
action="store_true",
|
| 1187 |
help="Create PR instead of direct push (dataset sink only)",
|
| 1188 |
)
|
| 1189 |
+
p.add_argument(
|
| 1190 |
+
"--overwrite",
|
| 1191 |
+
action="store_true",
|
| 1192 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 1193 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 1194 |
+
)
|
| 1195 |
# Bucket-sink-specific
|
| 1196 |
p.add_argument(
|
| 1197 |
"--shard-size",
|
qianfan-ocr.py
CHANGED
|
@@ -77,6 +77,28 @@ def check_cuda_availability():
|
|
| 77 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def extract_content_from_thinking(text: str, include_thinking: bool = False) -> str:
|
| 81 |
"""
|
| 82 |
Extract final content from Qianfan-OCR's Layout-as-Thought output.
|
|
@@ -237,6 +259,7 @@ def main(
|
|
| 237 |
include_thinking: bool = False,
|
| 238 |
custom_prompt: str = None,
|
| 239 |
output_column: str = "markdown",
|
|
|
|
| 240 |
config: str = None,
|
| 241 |
create_pr: bool = False,
|
| 242 |
verbose: bool = False,
|
|
@@ -276,6 +299,9 @@ def main(
|
|
| 276 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 277 |
)
|
| 278 |
|
|
|
|
|
|
|
|
|
|
| 279 |
if shuffle:
|
| 280 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 281 |
dataset = dataset.shuffle(seed=seed)
|
|
@@ -588,6 +614,12 @@ Examples:
|
|
| 588 |
default="markdown",
|
| 589 |
help="Column name for output text (default: markdown)",
|
| 590 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
parser.add_argument(
|
| 592 |
"--config",
|
| 593 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models)",
|
|
@@ -626,6 +658,7 @@ Examples:
|
|
| 626 |
include_thinking=args.include_thinking,
|
| 627 |
custom_prompt=args.custom_prompt,
|
| 628 |
output_column=args.output_column,
|
|
|
|
| 629 |
config=args.config,
|
| 630 |
create_pr=args.create_pr,
|
| 631 |
verbose=args.verbose,
|
|
|
|
| 77 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 78 |
|
| 79 |
|
| 80 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 81 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 82 |
+
|
| 83 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 84 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 85 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 86 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 87 |
+
"""
|
| 88 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 89 |
+
if not clash:
|
| 90 |
+
return dataset
|
| 91 |
+
if overwrite:
|
| 92 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 93 |
+
return dataset.remove_columns(clash)
|
| 94 |
+
logger.error(
|
| 95 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 96 |
+
f"(columns: {dataset.column_names})."
|
| 97 |
+
)
|
| 98 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 99 |
+
sys.exit(1)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
def extract_content_from_thinking(text: str, include_thinking: bool = False) -> str:
|
| 103 |
"""
|
| 104 |
Extract final content from Qianfan-OCR's Layout-as-Thought output.
|
|
|
|
| 259 |
include_thinking: bool = False,
|
| 260 |
custom_prompt: str = None,
|
| 261 |
output_column: str = "markdown",
|
| 262 |
+
overwrite: bool = False,
|
| 263 |
config: str = None,
|
| 264 |
create_pr: bool = False,
|
| 265 |
verbose: bool = False,
|
|
|
|
| 299 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 300 |
)
|
| 301 |
|
| 302 |
+
# Fail fast if the output column would collide with an existing input column
|
| 303 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 304 |
+
|
| 305 |
if shuffle:
|
| 306 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 307 |
dataset = dataset.shuffle(seed=seed)
|
|
|
|
| 614 |
default="markdown",
|
| 615 |
help="Column name for output text (default: markdown)",
|
| 616 |
)
|
| 617 |
+
parser.add_argument(
|
| 618 |
+
"--overwrite",
|
| 619 |
+
action="store_true",
|
| 620 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 621 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 622 |
+
)
|
| 623 |
parser.add_argument(
|
| 624 |
"--config",
|
| 625 |
help="Config/subset name when pushing to Hub (for benchmarking multiple models)",
|
|
|
|
| 658 |
include_thinking=args.include_thinking,
|
| 659 |
custom_prompt=args.custom_prompt,
|
| 660 |
output_column=args.output_column,
|
| 661 |
+
overwrite=args.overwrite,
|
| 662 |
config=args.config,
|
| 663 |
create_pr=args.create_pr,
|
| 664 |
verbose=args.verbose,
|
rolm-ocr.py
CHANGED
|
@@ -61,6 +61,28 @@ def check_cuda_availability():
|
|
| 61 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def make_ocr_message(
|
| 65 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 66 |
prompt: str = "Return the plain text representation of this document as if you were reading it naturally.\n",
|
|
@@ -209,6 +231,7 @@ def main(
|
|
| 209 |
max_samples: int = None,
|
| 210 |
private: bool = False,
|
| 211 |
output_column: str = None,
|
|
|
|
| 212 |
shuffle: bool = False,
|
| 213 |
seed: int = 42,
|
| 214 |
verbose: bool = False,
|
|
@@ -243,6 +266,9 @@ def main(
|
|
| 243 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 244 |
)
|
| 245 |
|
|
|
|
|
|
|
|
|
|
| 246 |
# Shuffle if requested
|
| 247 |
if shuffle:
|
| 248 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
@@ -502,6 +528,12 @@ Examples:
|
|
| 502 |
default=None,
|
| 503 |
help="Name of the output column for extracted text (default: markdown)",
|
| 504 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
parser.add_argument(
|
| 506 |
"--shuffle",
|
| 507 |
action="store_true",
|
|
@@ -535,6 +567,7 @@ Examples:
|
|
| 535 |
max_samples=args.max_samples,
|
| 536 |
private=args.private,
|
| 537 |
output_column=args.output_column,
|
|
|
|
| 538 |
shuffle=args.shuffle,
|
| 539 |
seed=args.seed,
|
| 540 |
verbose=args.verbose,
|
|
|
|
| 61 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 62 |
|
| 63 |
|
| 64 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 65 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 66 |
+
|
| 67 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 68 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 69 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 70 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 71 |
+
"""
|
| 72 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 73 |
+
if not clash:
|
| 74 |
+
return dataset
|
| 75 |
+
if overwrite:
|
| 76 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 77 |
+
return dataset.remove_columns(clash)
|
| 78 |
+
logger.error(
|
| 79 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 80 |
+
f"(columns: {dataset.column_names})."
|
| 81 |
+
)
|
| 82 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 83 |
+
sys.exit(1)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
def make_ocr_message(
|
| 87 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 88 |
prompt: str = "Return the plain text representation of this document as if you were reading it naturally.\n",
|
|
|
|
| 231 |
max_samples: int = None,
|
| 232 |
private: bool = False,
|
| 233 |
output_column: str = None,
|
| 234 |
+
overwrite: bool = False,
|
| 235 |
shuffle: bool = False,
|
| 236 |
seed: int = 42,
|
| 237 |
verbose: bool = False,
|
|
|
|
| 266 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 267 |
)
|
| 268 |
|
| 269 |
+
# Fail fast if the output column would collide with an existing input column
|
| 270 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 271 |
+
|
| 272 |
# Shuffle if requested
|
| 273 |
if shuffle:
|
| 274 |
logger.info(f"Shuffling dataset with seed {seed}")
|
|
|
|
| 528 |
default=None,
|
| 529 |
help="Name of the output column for extracted text (default: markdown)",
|
| 530 |
)
|
| 531 |
+
parser.add_argument(
|
| 532 |
+
"--overwrite",
|
| 533 |
+
action="store_true",
|
| 534 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 535 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 536 |
+
)
|
| 537 |
parser.add_argument(
|
| 538 |
"--shuffle",
|
| 539 |
action="store_true",
|
|
|
|
| 567 |
max_samples=args.max_samples,
|
| 568 |
private=args.private,
|
| 569 |
output_column=args.output_column,
|
| 570 |
+
overwrite=args.overwrite,
|
| 571 |
shuffle=args.shuffle,
|
| 572 |
seed=args.seed,
|
| 573 |
verbose=args.verbose,
|
smoldocling-ocr.py
CHANGED
|
@@ -65,6 +65,28 @@ def check_cuda_availability():
|
|
| 65 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def prepare_llm_input(
|
| 69 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 70 |
prompt_text: str = "Convert page to Docling.",
|
|
@@ -228,6 +250,7 @@ def main(
|
|
| 228 |
max_samples: int = None,
|
| 229 |
private: bool = False,
|
| 230 |
output_column: str = "markdown",
|
|
|
|
| 231 |
output_format: str = "markdown",
|
| 232 |
shuffle: bool = False,
|
| 233 |
seed: int = 42,
|
|
@@ -259,6 +282,9 @@ def main(
|
|
| 259 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 260 |
)
|
| 261 |
|
|
|
|
|
|
|
|
|
|
| 262 |
# Validate output format
|
| 263 |
if output_format not in ["markdown", "doctags"]:
|
| 264 |
raise ValueError(
|
|
@@ -571,6 +597,12 @@ Examples:
|
|
| 571 |
default="markdown",
|
| 572 |
help="Column name for output text (default: markdown)",
|
| 573 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
parser.add_argument(
|
| 575 |
"--output-format",
|
| 576 |
default="markdown",
|
|
@@ -624,6 +656,7 @@ Examples:
|
|
| 624 |
max_samples=args.max_samples,
|
| 625 |
private=args.private,
|
| 626 |
output_column=args.output_column,
|
|
|
|
| 627 |
output_format=args.output_format,
|
| 628 |
shuffle=args.shuffle,
|
| 629 |
seed=args.seed,
|
|
|
|
| 65 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 66 |
|
| 67 |
|
| 68 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 69 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 70 |
+
|
| 71 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 72 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 73 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 74 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 75 |
+
"""
|
| 76 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 77 |
+
if not clash:
|
| 78 |
+
return dataset
|
| 79 |
+
if overwrite:
|
| 80 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 81 |
+
return dataset.remove_columns(clash)
|
| 82 |
+
logger.error(
|
| 83 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 84 |
+
f"(columns: {dataset.column_names})."
|
| 85 |
+
)
|
| 86 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 87 |
+
sys.exit(1)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
def prepare_llm_input(
|
| 91 |
image: Union[Image.Image, Dict[str, Any], str],
|
| 92 |
prompt_text: str = "Convert page to Docling.",
|
|
|
|
| 250 |
max_samples: int = None,
|
| 251 |
private: bool = False,
|
| 252 |
output_column: str = "markdown",
|
| 253 |
+
overwrite: bool = False,
|
| 254 |
output_format: str = "markdown",
|
| 255 |
shuffle: bool = False,
|
| 256 |
seed: int = 42,
|
|
|
|
| 282 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 283 |
)
|
| 284 |
|
| 285 |
+
# Fail fast if the output column would collide with an existing input column
|
| 286 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 287 |
+
|
| 288 |
# Validate output format
|
| 289 |
if output_format not in ["markdown", "doctags"]:
|
| 290 |
raise ValueError(
|
|
|
|
| 597 |
default="markdown",
|
| 598 |
help="Column name for output text (default: markdown)",
|
| 599 |
)
|
| 600 |
+
parser.add_argument(
|
| 601 |
+
"--overwrite",
|
| 602 |
+
action="store_true",
|
| 603 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 604 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 605 |
+
)
|
| 606 |
parser.add_argument(
|
| 607 |
"--output-format",
|
| 608 |
default="markdown",
|
|
|
|
| 656 |
max_samples=args.max_samples,
|
| 657 |
private=args.private,
|
| 658 |
output_column=args.output_column,
|
| 659 |
+
overwrite=args.overwrite,
|
| 660 |
output_format=args.output_format,
|
| 661 |
shuffle=args.shuffle,
|
| 662 |
seed=args.seed,
|
unlimited-ocr-vllm.py
CHANGED
|
@@ -100,6 +100,28 @@ def check_cuda_availability():
|
|
| 100 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 104 |
"""Convert various dataset image cell formats to an RGB PIL image."""
|
| 105 |
if isinstance(image, Image.Image):
|
|
@@ -195,6 +217,7 @@ def main(
|
|
| 195 |
model: str = MODEL,
|
| 196 |
image_column: str = "image",
|
| 197 |
output_column: str = "markdown",
|
|
|
|
| 198 |
grounding_column: Optional[str] = None,
|
| 199 |
batch_size: int = 8,
|
| 200 |
max_model_len: int = 32768,
|
|
@@ -228,6 +251,9 @@ def main(
|
|
| 228 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 229 |
)
|
| 230 |
|
|
|
|
|
|
|
|
|
|
| 231 |
if shuffle:
|
| 232 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 233 |
dataset = dataset.shuffle(seed=seed)
|
|
@@ -456,6 +482,12 @@ Examples:
|
|
| 456 |
default="markdown",
|
| 457 |
help="Output column name (default: markdown)",
|
| 458 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
parser.add_argument(
|
| 460 |
"--strip-grounding",
|
| 461 |
action="store_true",
|
|
@@ -526,6 +558,7 @@ Examples:
|
|
| 526 |
model=args.model,
|
| 527 |
image_column=args.image_column,
|
| 528 |
output_column=args.output_column,
|
|
|
|
| 529 |
grounding_column=args.grounding_column,
|
| 530 |
batch_size=args.batch_size,
|
| 531 |
max_model_len=args.max_model_len,
|
|
|
|
| 100 |
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name(0)}")
|
| 101 |
|
| 102 |
|
| 103 |
+
def ensure_output_columns_free(dataset, columns, overwrite=False):
|
| 104 |
+
"""Fail fast if an output column would collide with an existing input column.
|
| 105 |
+
|
| 106 |
+
Adding a column that already exists silently overwrites it (e.g. a ground-truth
|
| 107 |
+
`text`/`markdown` column) or crashes on push with a duplicate-column error only
|
| 108 |
+
*after* inference has run. Catch it up front. With overwrite=True, drop the clashing
|
| 109 |
+
column(s) here instead (logged) so the later add_column is clean.
|
| 110 |
+
"""
|
| 111 |
+
clash = [c for c in columns if c in dataset.column_names]
|
| 112 |
+
if not clash:
|
| 113 |
+
return dataset
|
| 114 |
+
if overwrite:
|
| 115 |
+
logger.warning(f"--overwrite: replacing existing column(s) {clash}")
|
| 116 |
+
return dataset.remove_columns(clash)
|
| 117 |
+
logger.error(
|
| 118 |
+
f"Output column(s) {clash} already exist in the input dataset "
|
| 119 |
+
f"(columns: {dataset.column_names})."
|
| 120 |
+
)
|
| 121 |
+
logger.error("Choose a different --output-column, or pass --overwrite to replace them.")
|
| 122 |
+
sys.exit(1)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
def to_pil(image: Union[Image.Image, Dict[str, Any], str]) -> Image.Image:
|
| 126 |
"""Convert various dataset image cell formats to an RGB PIL image."""
|
| 127 |
if isinstance(image, Image.Image):
|
|
|
|
| 217 |
model: str = MODEL,
|
| 218 |
image_column: str = "image",
|
| 219 |
output_column: str = "markdown",
|
| 220 |
+
overwrite: bool = False,
|
| 221 |
grounding_column: Optional[str] = None,
|
| 222 |
batch_size: int = 8,
|
| 223 |
max_model_len: int = 32768,
|
|
|
|
| 251 |
f"Column '{image_column}' not found. Available: {dataset.column_names}"
|
| 252 |
)
|
| 253 |
|
| 254 |
+
# Fail fast if the output column would collide with an existing input column
|
| 255 |
+
dataset = ensure_output_columns_free(dataset, [output_column], overwrite=overwrite)
|
| 256 |
+
|
| 257 |
if shuffle:
|
| 258 |
logger.info(f"Shuffling dataset with seed {seed}")
|
| 259 |
dataset = dataset.shuffle(seed=seed)
|
|
|
|
| 482 |
default="markdown",
|
| 483 |
help="Output column name (default: markdown)",
|
| 484 |
)
|
| 485 |
+
parser.add_argument(
|
| 486 |
+
"--overwrite",
|
| 487 |
+
action="store_true",
|
| 488 |
+
help="Replace the output column if it already exists in the input dataset "
|
| 489 |
+
"(default: error out to avoid clobbering an existing column).",
|
| 490 |
+
)
|
| 491 |
parser.add_argument(
|
| 492 |
"--strip-grounding",
|
| 493 |
action="store_true",
|
|
|
|
| 558 |
model=args.model,
|
| 559 |
image_column=args.image_column,
|
| 560 |
output_column=args.output_column,
|
| 561 |
+
overwrite=args.overwrite,
|
| 562 |
grounding_column=args.grounding_column,
|
| 563 |
batch_size=args.batch_size,
|
| 564 |
max_model_len=args.max_model_len,
|