Add --config/--create-pr support to dots-ocr.py
Browse files1 unmodified line2345567849 unmodified lines5859606161626311 unmodified lines75767779787980166 unmodified lines2472482492502512522532543 unmodified lines25825926026126226326126226395 unmodified lines3593603613653623633643653663673683699 unmodified lines37938038138138238338438538638738838939039139239315 unmodified lines40941041140341241341441541641714 unmodified lines43243343442443543643743842643944044144244344444543143243344644744844945045145245345492 unmodified lines54754854955055155255355455555655755855956056115 unmodified lines5775785795805815821 unmodified line# requires-python = ">=3.11"# dependencies = [# "datasets",# "huggingface-hub[hf_transfer]",# "huggingface-hub",# "pillow",# "vllm>=0.9.1",# "tqdm",49 unmodified lines
PROMPT_TEMPLATES = { "ocr": "Extract the text content from this image.",
"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.
1. Bbox format: [x1, y1, x2, y2]11 unmodified lines - All layout elements must be sorted according to human reading order.
5. Final Output: The entire output must be a single JSON object.""",
"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.""",}
166 unmodified lines prompt_mode: str = "ocr", custom_prompt: str = None, output_column: str = "markdown", config: str = None, create_pr: bool = False,): """Process images from HF dataset through DoTS.ocr model."""
3 unmodified lines # Track processing start time start_time = datetime.now()
# Enable HF_TRANSFER for faster downloads os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
# Login to HF if token provided HF_TOKEN = hf_token or os.environ.get("HF_TOKEN") if HF_TOKEN:95 unmodified lines
def update_inference_info(example): try: existing_info = json.loads(example["inference_info"]) if example["inference_info"] else [] existing_info = ( json.loads(example["inference_info"]) if example["inference_info"] else [] ) except (json.JSONDecodeError, TypeError): existing_info = []
9 unmodified lines
# Push to hub logger.info(f"Pushing to {output_dataset}") dataset.push_to_hub(output_dataset, private=private, token=HF_TOKEN) dataset.push_to_hub( output_dataset, private=private, token=HF_TOKEN, config_name=config, create_pr=create_pr, commit_message=f"Add {model} OCR results ({len(dataset)} samples)" + (f" [{config}]" if config else ""), )
# Create and push dataset card logger.info("Creating dataset card")15 unmodified lines card.push_to_hub(output_dataset, token=HF_TOKEN)
logger.info("✅ DoTS.ocr processing complete!") logger.info(f"Dataset available at: https://huggingface.co/datasets/{output_dataset}") logger.info( f"Dataset available at: https://huggingface.co/datasets/{output_dataset}" ) logger.info(f"Processing time: {processing_time_str}")
14 unmodified lines print("\n1. Basic OCR:") print(" uv run dots-ocr.py input-dataset output-dataset") print("\n2. With custom settings:") print(" uv run dots-ocr.py docs analyzed-docs --batch-size 20 --max-samples 100") print( " uv run dots-ocr.py docs analyzed-docs --batch-size 20 --max-samples 100" ) print("\n3. Layout analysis with structure:") print(" uv run dots-ocr.py papers analyzed-structure --prompt-mode layout-all") print( " uv run dots-ocr.py papers analyzed-structure --prompt-mode layout-all" ) print("\n4. Layout detection only (no text):") print(" uv run dots-ocr.py docs layout-info --prompt-mode layout-only") print("\n5. Running on HF Jobs:") print(" hf jobs uv run --flavor l4x1 \\") print(" -e HF_TOKEN=$(python3 -c \"from huggingface_hub import get_token; print(get_token())\") \\") print(" -e HF_HUB_ENABLE_HF_TRANSFER=1 \\") print(" https://huggingface.co/datasets/uv-scripts/ocr/raw/main/dots-ocr.py \\") print( ' -e HF_TOKEN=$(python3 -c "from huggingface_hub import get_token; print(get_token())") \\' ) print( " https://huggingface.co/datasets/uv-scripts/ocr/raw/main/dots-ocr.py \\" ) print(" input-dataset output-dataset") print("\n" + "=" * 80) print("\nFor full help, run: uv run dots-ocr.py --help")92 unmodified lines default="markdown", help="Column name for output text (default: markdown)", ) parser.add_argument( "--config", help="Config/subset name when pushing to Hub (for benchmarking multiple models in one repo)", ) parser.add_argument( "--create-pr", action="store_true", help="Create a pull request instead of pushing directly (for parallel benchmarking)", )
args = parser.parse_args()
15 unmodified lines prompt_mode=args.prompt_mode, custom_prompt=args.custom_prompt, output_column=args.output_column, config=args.config, create_pr=args.create_pr, )dots-ocr.py+43-14