Commit ·
fa7a872
1
Parent(s): c0663cf
Simplify nanonets-ocr2.py to 3B-only and fix dataset card
Browse files- Remove 1.5B model support (vLLM compatibility issues)
- Focus on working Nanonets-OCR2-3B model (3.75B params)
- Remove viewer:false from dataset card (allow dataset viewer)
- Set batch size default to 16 (appropriate for 3B model)
- Clean up documentation and help text
🤖 Generated with Claude Code
12 unmodified lines131415161617181920212223181920212283 unmodified lines1061071081131091101161111121136 unmodified lines12012112212912312412512612712813512913013113210 unmodified lines14314414515214614714814958 unmodified lines2082092102172112122132145 unmodified lines220221222229230231232233234235236237238223224225226153 unmodified lines3803813823983833843854014024034043863873883893 unmodified lines3933943954143963974164174184193983994004014024244034044264054064074294084094104114 unmodified lines41641741844041942042144344444544642244842342442545145245342642742815 unmodified lines44444544647547644744844945045145248148245345445545612 unmodified lines# ///
"""Convert document images to markdown using Nanonets-OCR2 models with vLLM.Convert document images to markdown using Nanonets-OCR2-3B with vLLM.
This script processes images through Nanonets-OCR2 models (1.5B or 3B) to extracttext and structure as markdown, ideal for document understanding tasks.
Models:- Nanonets-OCR2-3B (default): 3.75B params, best quality- Nanonets-OCR2-1.5B-exp: 1.65B params, faster processingThis script processes images through the Nanonets-OCR2-3B model (3.75B params)to extract text and structure as markdown, ideal for document understanding tasks.
Features:- LaTeX equation recognition83 unmodified lines) -> str: """Create a dataset card documenting the OCR process.""" model_name = model.split("/")[-1] model_size = "3B" if "3B" in model else "1.5B"
return f"""---viewer: falsetags:- ocr- document-processing6 unmodified lines
# Document OCR using {model_name}
This dataset contains markdown-formatted OCR results from images in [{source_dataset}](https://huggingface.co/datasets/{source_dataset}) using Nanonets-OCR2-{model_size}.This dataset contains markdown-formatted OCR results from images in [{source_dataset}](https://huggingface.co/datasets/{source_dataset}) using Nanonets-OCR2-3B.
## Processing Details
- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})- **Model**: [{model}](https://huggingface.co/{model})- **Model Size**: {model_size} parameters- **Model Size**: 3.75B parameters- **Number of Samples**: {num_samples:,}- **Processing Time**: {processing_time}- **Processing Date**: {datetime.now().strftime("%Y-%m-%d %H:%M UTC")}10 unmodified lines
## Model Information
Nanonets-OCR2-{model_size} is a state-of-the-art document OCR model that excels at:Nanonets-OCR2-3B is a state-of-the-art document OCR model that excels at:- 📐 **LaTeX equations** - Mathematical formulas preserved in LaTeX format- 📊 **Tables** - Extracted and formatted as HTML- 📝 **Document structure** - Headers, lists, and formatting maintained58 unmodified lines input_dataset: str, output_dataset: str, image_column: str = "image", batch_size: int = None, batch_size: int = 16, model: str = "nanonets/Nanonets-OCR2-3B", max_model_len: int = 8192, max_tokens: int = 4096,5 unmodified lines shuffle: bool = False, seed: int = 42,): """Process images from HF dataset through Nanonets-OCR2 model."""
# Auto-set batch size based on model if not specified if batch_size is None: if "1.5B" in model: batch_size = 32 logger.info("Auto-set batch size to 32 for 1.5B model") else: # 3B model batch_size = 16 logger.info("Auto-set batch size to 16 for 3B model") """Process images from HF dataset through Nanonets-OCR2-3B model."""
# Check CUDA availability first check_cuda_availability()153 unmodified lines # Show example usage if no arguments if len(sys.argv) == 1: print("=" * 80) print("Nanonets OCR2 to Markdown Converter") print("Nanonets OCR2-3B to Markdown Converter") print("=" * 80) print("\nThis script converts document images to structured markdown using") print("Nanonets-OCR2 models (1.5B or 3B) with vLLM acceleration.") print("\nModel Options:") print("- Nanonets-OCR2-3B (default): 3.75B params, best quality") print("- Nanonets-OCR2-1.5B-exp: 1.65B params, faster processing") print("the Nanonets-OCR2-3B model (3.75B params) with vLLM acceleration.") print("\nFeatures:") print("- LaTeX equation recognition") print("- Table extraction and formatting (HTML)")3 unmodified lines print("- Checkbox recognition (☐/☑)") print("- Multilingual support") print("\nExample usage:") print("\n1. Basic OCR conversion (3B model, best quality):") print("\n1. Basic OCR conversion:") print(" uv run nanonets-ocr2.py document-images markdown-docs") print("\n2. Fast processing with 1.5B model:") print(" uv run nanonets-ocr2.py documents output \\") print(" --model nanonets/Nanonets-OCR2-1.5B-exp") print("\n3. With custom settings:") print("\n2. With custom settings:") print(" uv run nanonets-ocr2.py scanned-pdfs extracted-text \\") print(" --image-column page \\") print(" --batch-size 32 \\") print(" --gpu-memory-utilization 0.8") print("\n4. Process a subset for testing:") print("\n3. Process a subset for testing:") print(" uv run nanonets-ocr2.py large-dataset test-output --max-samples 10") print("\n5. Random sample from ordered dataset:") print("\n4. Random sample from ordered dataset:") print(" uv run nanonets-ocr2.py ordered-dataset random-test \\") print(" --max-samples 50 --shuffle") print("\n6. Running on HF Jobs:") 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/nanonets-ocr2.py \\")4 unmodified lines sys.exit(0)
parser = argparse.ArgumentParser( description="OCR images to markdown using Nanonets-OCR2 models", description="OCR images to markdown using Nanonets-OCR2-3B", formatter_class=argparse.RawDescriptionHelpFormatter, epilog="""Models: nanonets/Nanonets-OCR2-3B (default) - 3.75B params, best quality nanonets/Nanonets-OCR2-1.5B-exp - 1.65B params, faster
Examples: # Basic usage (3B model) # Basic usage uv run nanonets-ocr2.py my-images-dataset ocr-results
# Fast processing with 1.5B model uv run nanonets-ocr2.py documents output --model nanonets/Nanonets-OCR2-1.5B-exp
# With specific image column uv run nanonets-ocr2.py documents extracted-text --image-column scan
15 unmodified lines parser.add_argument( "--batch-size", type=int, default=None, help="Batch size for processing (default: auto - 16 for 3B, 32 for 1.5B)", default=16, help="Batch size for processing (default: 16)", ) parser.add_argument( "--model", default="nanonets/Nanonets-OCR2-3B", choices=["nanonets/Nanonets-OCR2-3B", "nanonets/Nanonets-OCR2-1.5B-exp"], help="Model to use (default: Nanonets-OCR2-3B for best quality)", help="Model to use (default: nanonets/Nanonets-OCR2-3B)", ) parser.add_argument( "--max-model-len",nanonets-ocr2.py+20-49