Add --config/--create-pr support to glm-ocr.py
Browse files- glm-ocr.py +41 -2
glm-ocr.py
CHANGED
|
@@ -51,6 +51,7 @@ import json
|
|
| 51 |
import logging
|
| 52 |
import os
|
| 53 |
import sys
|
|
|
|
| 54 |
from datetime import datetime
|
| 55 |
from typing import Any, Dict, List, Union
|
| 56 |
|
|
@@ -234,6 +235,8 @@ def main(
|
|
| 234 |
seed: int = 42,
|
| 235 |
output_column: str = "markdown",
|
| 236 |
verbose: bool = False,
|
|
|
|
|
|
|
| 237 |
):
|
| 238 |
"""Process images from HF dataset through GLM-OCR model."""
|
| 239 |
|
|
@@ -367,9 +370,34 @@ def main(
|
|
| 367 |
inference_list = [json.dumps([inference_entry])] * len(dataset)
|
| 368 |
dataset = dataset.add_column("inference_info", inference_list)
|
| 369 |
|
| 370 |
-
# Push to hub
|
| 371 |
logger.info(f"Pushing to {output_dataset}")
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
# Create and push dataset card
|
| 375 |
logger.info("Creating dataset card")
|
|
@@ -525,6 +553,15 @@ Examples:
|
|
| 525 |
parser.add_argument(
|
| 526 |
"--private", action="store_true", help="Make output dataset private"
|
| 527 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
parser.add_argument(
|
| 529 |
"--shuffle", action="store_true", help="Shuffle dataset before processing"
|
| 530 |
)
|
|
@@ -567,4 +604,6 @@ Examples:
|
|
| 567 |
seed=args.seed,
|
| 568 |
output_column=args.output_column,
|
| 569 |
verbose=args.verbose,
|
|
|
|
|
|
|
| 570 |
)
|
|
|
|
| 51 |
import logging
|
| 52 |
import os
|
| 53 |
import sys
|
| 54 |
+
import time
|
| 55 |
from datetime import datetime
|
| 56 |
from typing import Any, Dict, List, Union
|
| 57 |
|
|
|
|
| 235 |
seed: int = 42,
|
| 236 |
output_column: str = "markdown",
|
| 237 |
verbose: bool = False,
|
| 238 |
+
config: str = None,
|
| 239 |
+
create_pr: bool = False,
|
| 240 |
):
|
| 241 |
"""Process images from HF dataset through GLM-OCR model."""
|
| 242 |
|
|
|
|
| 370 |
inference_list = [json.dumps([inference_entry])] * len(dataset)
|
| 371 |
dataset = dataset.add_column("inference_info", inference_list)
|
| 372 |
|
| 373 |
+
# Push to hub with retry and XET fallback
|
| 374 |
logger.info(f"Pushing to {output_dataset}")
|
| 375 |
+
max_retries = 3
|
| 376 |
+
for attempt in range(1, max_retries + 1):
|
| 377 |
+
try:
|
| 378 |
+
if attempt > 1:
|
| 379 |
+
logger.warning("Disabling XET (fallback to HTTP upload)")
|
| 380 |
+
os.environ["HF_HUB_DISABLE_XET"] = "1"
|
| 381 |
+
dataset.push_to_hub(
|
| 382 |
+
output_dataset,
|
| 383 |
+
private=private,
|
| 384 |
+
token=HF_TOKEN,
|
| 385 |
+
max_shard_size="500MB",
|
| 386 |
+
config_name=config,
|
| 387 |
+
create_pr=create_pr,
|
| 388 |
+
commit_message=f"Add {MODEL} OCR results ({len(dataset)} samples)"
|
| 389 |
+
+ (f" [{config}]" if config else ""),
|
| 390 |
+
)
|
| 391 |
+
break
|
| 392 |
+
except Exception as e:
|
| 393 |
+
logger.error(f"Upload attempt {attempt}/{max_retries} failed: {e}")
|
| 394 |
+
if attempt < max_retries:
|
| 395 |
+
delay = 30 * (2 ** (attempt - 1))
|
| 396 |
+
logger.info(f"Retrying in {delay}s...")
|
| 397 |
+
time.sleep(delay)
|
| 398 |
+
else:
|
| 399 |
+
logger.error("All upload attempts failed. OCR results are lost.")
|
| 400 |
+
sys.exit(1)
|
| 401 |
|
| 402 |
# Create and push dataset card
|
| 403 |
logger.info("Creating dataset card")
|
|
|
|
| 553 |
parser.add_argument(
|
| 554 |
"--private", action="store_true", help="Make output dataset private"
|
| 555 |
)
|
| 556 |
+
parser.add_argument(
|
| 557 |
+
"--config",
|
| 558 |
+
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
| 559 |
+
)
|
| 560 |
+
parser.add_argument(
|
| 561 |
+
"--create-pr",
|
| 562 |
+
action="store_true",
|
| 563 |
+
help="Create a pull request instead of pushing directly (for parallel benchmarking)",
|
| 564 |
+
)
|
| 565 |
parser.add_argument(
|
| 566 |
"--shuffle", action="store_true", help="Shuffle dataset before processing"
|
| 567 |
)
|
|
|
|
| 604 |
seed=args.seed,
|
| 605 |
output_column=args.output_column,
|
| 606 |
verbose=args.verbose,
|
| 607 |
+
config=args.config,
|
| 608 |
+
create_pr=args.create_pr,
|
| 609 |
)
|