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 lines131415161718192021222324252683 unmodified lines1101111121131141151161171181196 unmodified lines12612712812913013113213313413513613713810 unmodified lines14915015115215315415558 unmodified lines2142152162172182192205 unmodified lines226227228229230231232233234235236237238239240241153 unmodified lines3953963973983994004014024034044054064073 unmodified lines4114124134144154164174184194204214224234244254264274284294304314324 unmodified lines43743843944044144244344444544644744844945045145245345445545615 unmodified lines47247347447547647747847948048148248348448512 unmodified lines# ///"""Convert document images to markdown using Nanonets-OCR2 models 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 processingFeatures:- 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}.## Processing Details- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})- **Model**: [{model}](https://huggingface.co/{model})- **Model Size**: {model_size} 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 InformationNanonets-OCR2-{model_size} 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 linesinput_dataset: str,output_dataset: str,image_column: str = "image",batch_size: int = None,model: str = "nanonets/Nanonets-OCR2-3B",max_model_len: int = 8192,max_tokens: int = 4096,5 unmodified linesshuffle: bool = False,seed: int = 42,):"""Process images from HF dataset through Nanonets-OCR2 model."""# Auto-set batch size based on model if not specifiedif batch_size is None:if "1.5B" in model:batch_size = 32logger.info("Auto-set batch size to 32 for 1.5B model")else: # 3B modelbatch_size = 16logger.info("Auto-set batch size to 16 for 3B model")# Check CUDA availability firstcheck_cuda_availability()153 unmodified lines# Show example usage if no argumentsif len(sys.argv) == 1:print("=" * 80)print("Nanonets OCR2 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("\nFeatures:")print("- LaTeX equation recognition")print("- Table extraction and formatting (HTML)")3 unmodified linesprint("- Checkbox recognition (☐/☑)")print("- Multilingual support")print("\nExample usage:")print("\n1. Basic OCR conversion (3B model, best quality):")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(" 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(" uv run nanonets-ocr2.py large-dataset test-output --max-samples 10")print("\n5. 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(" 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 linessys.exit(0)parser = argparse.ArgumentParser(description="OCR images to markdown using Nanonets-OCR2 models",formatter_class=argparse.RawDescriptionHelpFormatter,epilog="""Models:nanonets/Nanonets-OCR2-3B (default) - 3.75B params, best qualitynanonets/Nanonets-OCR2-1.5B-exp - 1.65B params, fasterExamples:# Basic usage (3B model)uv run nanonets-ocr2.py my-images-dataset ocr-results# Fast processing with 1.5B modeluv run nanonets-ocr2.py documents output --model nanonets/Nanonets-OCR2-1.5B-exp# With specific image columnuv run nanonets-ocr2.py documents extracted-text --image-column scan15 unmodified linesparser.add_argument("--batch-size",type=int,default=None,help="Batch size for processing (default: auto - 16 for 3B, 32 for 1.5B)",)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)",)parser.add_argument("--max-model-len",12 unmodified lines1314151617181920212283 unmodified lines1061071081091101111121136 unmodified lines12012112212312412512612712812913013113210 unmodified lines14314414514614714814958 unmodified lines2082092102112122132145 unmodified lines220221222223224225226153 unmodified lines3803813823833843853863873883893 unmodified lines3933943953963973983994004014024034044054064074084094104114 unmodified lines41641741841942042142242342442542642742815 unmodified lines44444544644744844945045145245345445545612 unmodified lines# ///"""Convert document images to markdown using Nanonets-OCR2-3B with vLLM.This 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]return f"""---tags:- 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-3B.## Processing Details- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})- **Model**: [{model}](https://huggingface.co/{model})- **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 InformationNanonets-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 linesinput_dataset: str,output_dataset: str,image_column: str = "image",batch_size: int = 16,model: str = "nanonets/Nanonets-OCR2-3B",max_model_len: int = 8192,max_tokens: int = 4096,5 unmodified linesshuffle: bool = False,seed: int = 42,):"""Process images from HF dataset through Nanonets-OCR2-3B model."""# Check CUDA availability firstcheck_cuda_availability()153 unmodified lines# Show example usage if no argumentsif len(sys.argv) == 1:print("=" * 80)print("Nanonets OCR2-3B to Markdown Converter")print("=" * 80)print("\nThis script converts document images to structured markdown using")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 linesprint("- Checkbox recognition (☐/☑)")print("- Multilingual support")print("\nExample usage:")print("\n1. Basic OCR conversion:")print(" uv run nanonets-ocr2.py document-images markdown-docs")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("\n3. Process a subset for testing:")print(" uv run nanonets-ocr2.py large-dataset test-output --max-samples 10")print("\n4. Random sample from ordered dataset:")print(" uv run nanonets-ocr2.py ordered-dataset random-test \\")print(" --max-samples 50 --shuffle")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 linessys.exit(0)parser = argparse.ArgumentParser(description="OCR images to markdown using Nanonets-OCR2-3B",formatter_class=argparse.RawDescriptionHelpFormatter,epilog="""Examples:# Basic usageuv run nanonets-ocr2.py my-images-dataset ocr-results# With specific image columnuv run nanonets-ocr2.py documents extracted-text --image-column scan15 unmodified linesparser.add_argument("--batch-size",type=int,default=16,help="Batch size for processing (default: 16)",)parser.add_argument("--model",default="nanonets/Nanonets-OCR2-3B",help="Model to use (default: nanonets/Nanonets-OCR2-3B)",)parser.add_argument("--max-model-len",
nanonets-ocr2.py+20-49