Add --config/--create-pr support to dots-ocr.py
Browse files- dots-ocr.py +43 -14
dots-ocr.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
# requires-python = ">=3.11"
|
| 3 |
# dependencies = [
|
| 4 |
# "datasets",
|
| 5 |
-
# "huggingface-hub
|
| 6 |
# "pillow",
|
| 7 |
# "vllm>=0.9.1",
|
| 8 |
# "tqdm",
|
|
@@ -58,7 +58,6 @@ logger = logging.getLogger(__name__)
|
|
| 58 |
|
| 59 |
PROMPT_TEMPLATES = {
|
| 60 |
"ocr": "Extract the text content from this image.",
|
| 61 |
-
|
| 62 |
"layout-all": """Please output the layout information from the PDF image, including each layout element's bbox, its category, and the corresponding text content within the bbox.
|
| 63 |
|
| 64 |
1. Bbox format: [x1, y1, x2, y2]
|
|
@@ -76,7 +75,6 @@ PROMPT_TEMPLATES = {
|
|
| 76 |
- All layout elements must be sorted according to human reading order.
|
| 77 |
|
| 78 |
5. Final Output: The entire output must be a single JSON object.""",
|
| 79 |
-
|
| 80 |
"layout-only": """Please output the layout information from this PDF image, including each layout's bbox and its category. The bbox should be in the format [x1, y1, x2, y2]. The layout categories for the PDF document include ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title']. Do not output the corresponding text. The layout result should be in JSON format.""",
|
| 81 |
}
|
| 82 |
|
|
@@ -249,6 +247,8 @@ def main(
|
|
| 249 |
prompt_mode: str = "ocr",
|
| 250 |
custom_prompt: str = None,
|
| 251 |
output_column: str = "markdown",
|
|
|
|
|
|
|
| 252 |
):
|
| 253 |
"""Process images from HF dataset through DoTS.ocr model."""
|
| 254 |
|
|
@@ -258,9 +258,6 @@ def main(
|
|
| 258 |
# Track processing start time
|
| 259 |
start_time = datetime.now()
|
| 260 |
|
| 261 |
-
# Enable HF_TRANSFER for faster downloads
|
| 262 |
-
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 263 |
-
|
| 264 |
# Login to HF if token provided
|
| 265 |
HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
|
| 266 |
if HF_TOKEN:
|
|
@@ -362,7 +359,11 @@ def main(
|
|
| 362 |
|
| 363 |
def update_inference_info(example):
|
| 364 |
try:
|
| 365 |
-
existing_info =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
except (json.JSONDecodeError, TypeError):
|
| 367 |
existing_info = []
|
| 368 |
|
|
@@ -378,7 +379,15 @@ def main(
|
|
| 378 |
|
| 379 |
# Push to hub
|
| 380 |
logger.info(f"Pushing to {output_dataset}")
|
| 381 |
-
dataset.push_to_hub(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
# Create and push dataset card
|
| 384 |
logger.info("Creating dataset card")
|
|
@@ -400,7 +409,9 @@ def main(
|
|
| 400 |
card.push_to_hub(output_dataset, token=HF_TOKEN)
|
| 401 |
|
| 402 |
logger.info("✅ DoTS.ocr processing complete!")
|
| 403 |
-
logger.info(
|
|
|
|
|
|
|
| 404 |
logger.info(f"Processing time: {processing_time_str}")
|
| 405 |
|
| 406 |
|
|
@@ -421,16 +432,23 @@ if __name__ == "__main__":
|
|
| 421 |
print("\n1. Basic OCR:")
|
| 422 |
print(" uv run dots-ocr.py input-dataset output-dataset")
|
| 423 |
print("\n2. With custom settings:")
|
| 424 |
-
print(
|
|
|
|
|
|
|
| 425 |
print("\n3. Layout analysis with structure:")
|
| 426 |
-
print(
|
|
|
|
|
|
|
| 427 |
print("\n4. Layout detection only (no text):")
|
| 428 |
print(" uv run dots-ocr.py docs layout-info --prompt-mode layout-only")
|
| 429 |
print("\n5. Running on HF Jobs:")
|
| 430 |
print(" hf jobs uv run --flavor l4x1 \\")
|
| 431 |
-
print(
|
| 432 |
-
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
| 434 |
print(" input-dataset output-dataset")
|
| 435 |
print("\n" + "=" * 80)
|
| 436 |
print("\nFor full help, run: uv run dots-ocr.py --help")
|
|
@@ -529,6 +547,15 @@ Examples:
|
|
| 529 |
default="markdown",
|
| 530 |
help="Column name for output text (default: markdown)",
|
| 531 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
args = parser.parse_args()
|
| 534 |
|
|
@@ -550,4 +577,6 @@ Examples:
|
|
| 550 |
prompt_mode=args.prompt_mode,
|
| 551 |
custom_prompt=args.custom_prompt,
|
| 552 |
output_column=args.output_column,
|
|
|
|
|
|
|
| 553 |
)
|
|
|
|
| 2 |
# requires-python = ">=3.11"
|
| 3 |
# dependencies = [
|
| 4 |
# "datasets",
|
| 5 |
+
# "huggingface-hub",
|
| 6 |
# "pillow",
|
| 7 |
# "vllm>=0.9.1",
|
| 8 |
# "tqdm",
|
|
|
|
| 58 |
|
| 59 |
PROMPT_TEMPLATES = {
|
| 60 |
"ocr": "Extract the text content from this image.",
|
|
|
|
| 61 |
"layout-all": """Please output the layout information from the PDF image, including each layout element's bbox, its category, and the corresponding text content within the bbox.
|
| 62 |
|
| 63 |
1. Bbox format: [x1, y1, x2, y2]
|
|
|
|
| 75 |
- All layout elements must be sorted according to human reading order.
|
| 76 |
|
| 77 |
5. Final Output: The entire output must be a single JSON object.""",
|
|
|
|
| 78 |
"layout-only": """Please output the layout information from this PDF image, including each layout's bbox and its category. The bbox should be in the format [x1, y1, x2, y2]. The layout categories for the PDF document include ['Caption', 'Footnote', 'Formula', 'List-item', 'Page-footer', 'Page-header', 'Picture', 'Section-header', 'Table', 'Text', 'Title']. Do not output the corresponding text. The layout result should be in JSON format.""",
|
| 79 |
}
|
| 80 |
|
|
|
|
| 247 |
prompt_mode: str = "ocr",
|
| 248 |
custom_prompt: str = None,
|
| 249 |
output_column: str = "markdown",
|
| 250 |
+
config: str = None,
|
| 251 |
+
create_pr: bool = False,
|
| 252 |
):
|
| 253 |
"""Process images from HF dataset through DoTS.ocr model."""
|
| 254 |
|
|
|
|
| 258 |
# Track processing start time
|
| 259 |
start_time = datetime.now()
|
| 260 |
|
|
|
|
|
|
|
|
|
|
| 261 |
# Login to HF if token provided
|
| 262 |
HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
|
| 263 |
if HF_TOKEN:
|
|
|
|
| 359 |
|
| 360 |
def update_inference_info(example):
|
| 361 |
try:
|
| 362 |
+
existing_info = (
|
| 363 |
+
json.loads(example["inference_info"])
|
| 364 |
+
if example["inference_info"]
|
| 365 |
+
else []
|
| 366 |
+
)
|
| 367 |
except (json.JSONDecodeError, TypeError):
|
| 368 |
existing_info = []
|
| 369 |
|
|
|
|
| 379 |
|
| 380 |
# Push to hub
|
| 381 |
logger.info(f"Pushing to {output_dataset}")
|
| 382 |
+
dataset.push_to_hub(
|
| 383 |
+
output_dataset,
|
| 384 |
+
private=private,
|
| 385 |
+
token=HF_TOKEN,
|
| 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 |
|
| 392 |
# Create and push dataset card
|
| 393 |
logger.info("Creating dataset card")
|
|
|
|
| 409 |
card.push_to_hub(output_dataset, token=HF_TOKEN)
|
| 410 |
|
| 411 |
logger.info("✅ DoTS.ocr processing complete!")
|
| 412 |
+
logger.info(
|
| 413 |
+
f"Dataset available at: https://huggingface.co/datasets/{output_dataset}"
|
| 414 |
+
)
|
| 415 |
logger.info(f"Processing time: {processing_time_str}")
|
| 416 |
|
| 417 |
|
|
|
|
| 432 |
print("\n1. Basic OCR:")
|
| 433 |
print(" uv run dots-ocr.py input-dataset output-dataset")
|
| 434 |
print("\n2. With custom settings:")
|
| 435 |
+
print(
|
| 436 |
+
" uv run dots-ocr.py docs analyzed-docs --batch-size 20 --max-samples 100"
|
| 437 |
+
)
|
| 438 |
print("\n3. Layout analysis with structure:")
|
| 439 |
+
print(
|
| 440 |
+
" uv run dots-ocr.py papers analyzed-structure --prompt-mode layout-all"
|
| 441 |
+
)
|
| 442 |
print("\n4. Layout detection only (no text):")
|
| 443 |
print(" uv run dots-ocr.py docs layout-info --prompt-mode layout-only")
|
| 444 |
print("\n5. Running on HF Jobs:")
|
| 445 |
print(" hf jobs uv run --flavor l4x1 \\")
|
| 446 |
+
print(
|
| 447 |
+
' -e HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \\'
|
| 448 |
+
)
|
| 449 |
+
print(
|
| 450 |
+
" https://huggingface.co/datasets/uv-scripts/ocr/raw/main/dots-ocr.py \\"
|
| 451 |
+
)
|
| 452 |
print(" input-dataset output-dataset")
|
| 453 |
print("\n" + "=" * 80)
|
| 454 |
print("\nFor full help, run: uv run dots-ocr.py --help")
|
|
|
|
| 547 |
default="markdown",
|
| 548 |
help="Column name for output text (default: markdown)",
|
| 549 |
)
|
| 550 |
+
parser.add_argument(
|
| 551 |
+
"--config",
|
| 552 |
+
help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)",
|
| 553 |
+
)
|
| 554 |
+
parser.add_argument(
|
| 555 |
+
"--create-pr",
|
| 556 |
+
action="store_true",
|
| 557 |
+
help="Create a pull request instead of pushing directly (for parallel benchmarking)",
|
| 558 |
+
)
|
| 559 |
|
| 560 |
args = parser.parse_args()
|
| 561 |
|
|
|
|
| 577 |
prompt_mode=args.prompt_mode,
|
| 578 |
custom_prompt=args.custom_prompt,
|
| 579 |
output_column=args.output_column,
|
| 580 |
+
config=args.config,
|
| 581 |
+
create_pr=args.create_pr,
|
| 582 |
)
|