Add --config/--create-pr support to dots-ocr.py
Browse files1 unmodified line234567849 unmodified lines5859606162636411 unmodified lines76777879808182166 unmodified lines2492502512522532543 unmodified lines25825926026126226326426526695 unmodified lines3623633643653663673689 unmodified lines37837938038138238338415 unmodified lines40040140240340440540614 unmodified lines42142242342442542642742842943043143243343443543692 unmodified lines52953053153253353415 unmodified lines5505515525531 unmodified line# requires-python = ">=3.11"# dependencies = [# "datasets",# "huggingface-hub[hf_transfer]",# "pillow",# "vllm>=0.9.1",# "tqdm",49 unmodified linesPROMPT_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 linesprompt_mode: str = "ocr",custom_prompt: str = None,output_column: str = "markdown",):"""Process images from HF dataset through DoTS.ocr model."""3 unmodified lines# Track processing start timestart_time = datetime.now()# Enable HF_TRANSFER for faster downloadsos.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"# Login to HF if token providedHF_TOKEN = hf_token or os.environ.get("HF_TOKEN")if HF_TOKEN:95 unmodified linesdef update_inference_info(example):try:existing_info = json.loads(example["inference_info"]) if example["inference_info"] else []except (json.JSONDecodeError, TypeError):existing_info = []9 unmodified lines# Push to hublogger.info(f"Pushing to {output_dataset}")dataset.push_to_hub(output_dataset, private=private, token=HF_TOKEN)# Create and push dataset cardlogger.info("Creating dataset card")15 unmodified linescard.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"Processing time: {processing_time_str}")14 unmodified linesprint("\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("\n3. Layout analysis with structure:")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(" input-dataset output-dataset")print("\n" + "=" * 80)print("\nFor full help, run: uv run dots-ocr.py --help")92 unmodified linesdefault="markdown",help="Column name for output text (default: markdown)",)args = parser.parse_args()15 unmodified linesprompt_mode=args.prompt_mode,custom_prompt=args.custom_prompt,output_column=args.output_column,)1 unmodified line234567849 unmodified lines58596061626311 unmodified lines757677787980166 unmodified lines2472482492502512522532543 unmodified lines25825926026126226395 unmodified lines3593603613623633643653663673683699 unmodified lines37938038138238338438538638738838939039139239315 unmodified lines40941041141241341441541641714 unmodified lines43243343443543643743843944044144244344444544644744844945045145245345492 unmodified lines54754854955055155255355455555655755855956056115 unmodified lines5775785795805815821 unmodified line# requires-python = ">=3.11"# dependencies = [# "datasets",# "huggingface-hub",# "pillow",# "vllm>=0.9.1",# "tqdm",49 unmodified linesPROMPT_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 linesprompt_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 timestart_time = datetime.now()# Login to HF if token providedHF_TOKEN = hf_token or os.environ.get("HF_TOKEN")if HF_TOKEN:95 unmodified linesdef update_inference_info(example):try:existing_info = (json.loads(example["inference_info"])if example["inference_info"]else [])except (json.JSONDecodeError, TypeError):existing_info = []9 unmodified lines# Push to hublogger.info(f"Pushing to {output_dataset}")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 cardlogger.info("Creating dataset card")15 unmodified linescard.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"Processing time: {processing_time_str}")14 unmodified linesprint("\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("\n3. Layout analysis with structure:")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(" 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 linesdefault="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 linesprompt_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