|
82 | 82 | import os |
83 | 83 | import pickle |
84 | 84 | import re |
| 85 | +import shlex |
85 | 86 | import shutil |
86 | 87 | import subprocess |
87 | 88 | import sys |
88 | 89 | import threading |
89 | 90 | import types |
90 | 91 | import zlib |
91 | 92 |
|
| 93 | +SCRIPT_URL = "https://huggingface.co/datasets/uv-scripts/ocr/raw/main/unlimited-ocr2-saturate.py" |
| 94 | + |
92 | 95 | # Per-value provenance: |
93 | 96 | # - image lmsysorg/sglang:v0.5.20-cu130: the SGLang release whose pins match |
94 | 97 | # Baidu's wheel exactly (torch 2.13.0, transformers 5.12.1, flashinfer 0.6.18 |
@@ -375,6 +378,35 @@ def find_server_python(candidates) -> str: |
375 | 378 | ) |
376 | 379 |
|
377 | 380 |
|
| 381 | +def write_card(args): |
| 382 | + """Push a minimal card: the uv-script / hf_jobs tags and the command that made this dataset.""" |
| 383 | + from huggingface_hub import DatasetCard, DatasetCardData |
| 384 | + |
| 385 | + on_jobs = os.environ.get("HF_JOB_ID") is not None |
| 386 | + flavor = os.environ.get("HF_JOB_FLAVOR") |
| 387 | + origin = ( |
| 388 | + "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)" |
| 389 | + + (f" (`{flavor}`)" if flavor else "") |
| 390 | + ) if on_jobs else "Generated" |
| 391 | + argv = sys.argv[1:] |
| 392 | + argv[argv.index(args.output, argv.index(args.input) + 1)] = "<output-dataset>" # the output positional only |
| 393 | + command = re.sub(r"hf_[A-Za-z0-9]{20,}", "hf_***", shlex.join(argv)) # never publish a token |
| 394 | + data = DatasetCardData( |
| 395 | + tags=["ocr", "document-processing", "unlimited-ocr-2", "uv-script", "generated"] + (["hf_jobs"] if on_jobs else []), |
| 396 | + task_categories=["image-to-text"], |
| 397 | + ) |
| 398 | + card = DatasetCard( |
| 399 | + f"---\n{data.to_yaml()}\n---\n\n" |
| 400 | + f"# {args.output}\n\n" |
| 401 | + f"{origin} with the [`unlimited-ocr2-saturate.py`]({SCRIPT_URL}) recipe " |
| 402 | + f"from [uv-scripts](https://huggingface.co/uv-scripts). Run it yourself:\n\n" |
| 403 | + + (f"The input `{args.input}` was a local or mounted path: add the `-v` mount that provides it.\n\n" |
| 404 | + if not args.input.startswith("hf://") else "") |
| 405 | + + f"```bash\nhf jobs uv run{' --flavor ' + flavor if flavor else ''} {SCRIPT_URL} {command}\n```\n" |
| 406 | + ) |
| 407 | + card.push_to_hub(args.output) |
| 408 | + |
| 409 | + |
378 | 410 | def main(): |
379 | 411 | ap = argparse.ArgumentParser(description="Unlimited-OCR-2 document OCR via saturate") |
380 | 412 | ap.add_argument("input", help="Folder/glob of page images or PDFs: hf://buckets/..., a mounted dir, ...") |
@@ -532,18 +564,21 @@ def parse_repair(row, body): |
532 | 564 | def run_passes(endpoint, headers=None): |
533 | 565 | common = {"retry_errors": args.retry_errors, "headers": headers, |
534 | 566 | "prepare_workers": args.prepare_workers, "read_timeout": 3600} |
| 567 | + stats = rstats = None |
535 | 568 | if not args.repair_only: |
536 | 569 | stats = pump(rows, to_request, parse, endpoint, output, window=window, flush_every=4, **common) |
537 | 570 | print(f"{output} ({stats.rows_processed} ok, {stats.rows_failed} error rows)", file=sys.stderr) |
538 | 571 | print("UNLIMITED_OCR2_SATURATE " + stats.to_json(), flush=True) |
539 | | - if args.no_repair: |
540 | | - return |
541 | | - targets = list(repair_rows(rows, output)) |
542 | | - print(f"repair pass: {len(targets)} pages to re-OCR alone (gundam) -> {repairs_out}", file=sys.stderr) |
543 | | - if targets: |
544 | | - rstats = pump(targets, to_request, parse_repair, endpoint, repairs_out, |
545 | | - window=Auto(initial=32, max_limit=64), flush_every=16, **common) |
546 | | - print("UNLIMITED_OCR2_REPAIR " + rstats.to_json(), flush=True) |
| 572 | + if not args.no_repair: |
| 573 | + targets = list(repair_rows(rows, output)) |
| 574 | + print(f"repair pass: {len(targets)} pages to re-OCR alone (gundam) -> {repairs_out}", file=sys.stderr) |
| 575 | + if targets: |
| 576 | + rstats = pump(targets, to_request, parse_repair, endpoint, repairs_out, |
| 577 | + window=Auto(initial=32, max_limit=64), flush_every=16, **common) |
| 578 | + print("UNLIMITED_OCR2_REPAIR " + rstats.to_json(), flush=True) |
| 579 | + # A card only for a dataset repo, and not from --repair-only (the main run wrote it). |
| 580 | + if is_repo_id and stats is not None: |
| 581 | + write_card(args) |
547 | 582 |
|
548 | 583 | if args.endpoint: |
549 | 584 | run_passes(args.endpoint, {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"} |
|
0 commit comments