Image-Text-to-Text
Transformers
Safetensors
Portuguese
qwen2_5_vl
ocr
document-understanding
structured-extraction
specialized-small-language-model
brazilian-portuguese
conversational
text-generation-inference
compressed-tensors
Instructions to use Dharma-AI/Dharma-OCR-LITE with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Dharma-AI/Dharma-OCR-LITE with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Dharma-AI/Dharma-OCR-LITE") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Dharma-AI/Dharma-OCR-LITE") model = AutoModelForMultimodalLM.from_pretrained("Dharma-AI/Dharma-OCR-LITE", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Dharma-AI/Dharma-OCR-LITE with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Dharma-AI/Dharma-OCR-LITE" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Dharma-AI/Dharma-OCR-LITE", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/Dharma-AI/Dharma-OCR-LITE
- SGLang
How to use Dharma-AI/Dharma-OCR-LITE with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Dharma-AI/Dharma-OCR-LITE" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Dharma-AI/Dharma-OCR-LITE", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Dharma-AI/Dharma-OCR-LITE" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Dharma-AI/Dharma-OCR-LITE", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use Dharma-AI/Dharma-OCR-LITE with Docker Model Runner:
docker model run hf.co/Dharma-AI/Dharma-OCR-LITE
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,18 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
license:
|
| 3 |
language:
|
| 4 |
- pt
|
| 5 |
- en
|
| 6 |
library_name: transformers
|
| 7 |
-
base_model: Nanonets/Nanonets-OCR2-3B
|
| 8 |
pipeline_tag: image-text-to-text
|
| 9 |
tags:
|
| 10 |
- ocr
|
| 11 |
- document-understanding
|
| 12 |
- structured-extraction
|
| 13 |
-
- dpo
|
| 14 |
-
- sft
|
| 15 |
-
- awq
|
| 16 |
- specialized-small-language-model
|
| 17 |
- brazilian-portuguese
|
| 18 |
datasets:
|
|
@@ -27,68 +23,57 @@ datasets:
|
|
| 27 |
|
| 28 |
## Introduction
|
| 29 |
|
| 30 |
-
**DharmaOCR Lite** is a 3B-parameter Specialized Small Language Model (SSLM) for structured OCR, developed by [Dharma-AI](https://dharma-ai.com). It
|
| 31 |
|
| 32 |
DharmaOCR Lite achieves **state-of-the-art performance** on [DharmaOCR-Benchmark](https://huggingface.co/datasets/dharma-ai/DharmaOCR-Benchmark), outperforming all evaluated open-source and commercial baselines β including GPT-4o, GPT-5.4, Claude Opus 4.6, Gemini 3.1 Pro, Google Document AI, Amazon Textract, and olmOCR-2-7B β while being significantly cheaper and faster to run.
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
- **Text degeneration rate: 0.20%** β the lowest across all models tested
|
| 38 |
-
- **~34% relative cost** compared to olmOCR-2-7B vanilla (reference baseline)
|
| 39 |
-
- **1.464s average time per page** on an NVIDIA L40S GPU
|
| 40 |
-
- Trained with a novel **SFT + DPO** pipeline, where DPO is applied to OCR for the first time to explicitly reduce text degeneration
|
| 41 |
-
- **AWQ-quantized** (FP8) for efficient deployment with negligible quality loss
|
| 42 |
|
| 43 |
-
##
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|---|---|
|
| 47 |
-
| **Base model** | [Nanonets-OCR2-3B](https://huggingface.co/Nanonets/Nanonets-OCR2-3B) |
|
| 48 |
-
| **Architecture** | Qwen2.5-VL-3B-Instruct (decoder-only VLM) |
|
| 49 |
-
| **Parameters** | ~3B |
|
| 50 |
-
| **Training pipeline** | SFT β DPO β AWQ Quantization (FP8) |
|
| 51 |
-
| **Output format** | Structured JSON (`header`, `text`, `footer`, `margin`) |
|
| 52 |
-
| **Primary language** | Brazilian Portuguese |
|
| 53 |
-
| **Context length** | 8,192 tokens (generation limit) |
|
| 54 |
-
| **Precision** | FP8 (AWQ) |
|
| 55 |
-
| **License** | Apache 2.0 |
|
| 56 |
-
|
| 57 |
-
## Performance
|
| 58 |
-
|
| 59 |
-
### DharmaOCR-Benchmark Results
|
| 60 |
|
| 61 |
| Model | Score | Degeneration Rate (%) | Time/Page (s) |
|
| 62 |
|---|---|---|---|
|
| 63 |
-
| **DharmaOCR
|
| 64 |
-
| DharmaOCR
|
| 65 |
-
| olmOCR-2-7B (vanilla) | 0.823 | 1.41 | 4.306 |
|
| 66 |
-
| Nanonets-OCR2-3B (vanilla) | 0.791 | 2.62 | 1.911 |
|
| 67 |
| Claude Opus 4.6 | 0.833 | β | β |
|
|
|
|
|
|
|
| 68 |
| Gemini 3.1 Pro | 0.820 | β | β |
|
|
|
|
| 69 |
| GPT-5.4 | 0.750 | β | β |
|
| 70 |
| GPT-4o | 0.635 | β | β |
|
| 71 |
| Google Document AI | 0.640 | β | β |
|
| 72 |
| Amazon Textract | 0.618 | β | β |
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
-
###
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
## Quickstart
|
| 87 |
|
| 88 |
```python
|
| 89 |
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 90 |
from qwen_vl_utils import process_vision_info
|
| 91 |
-
from PIL import Image
|
| 92 |
|
| 93 |
model_name = "dharma-ai/DharmaOCR-Lite"
|
| 94 |
|
|
@@ -99,7 +84,6 @@ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
| 99 |
)
|
| 100 |
processor = AutoProcessor.from_pretrained(model_name)
|
| 101 |
|
| 102 |
-
# Load your document image
|
| 103 |
image_path = "document.png"
|
| 104 |
|
| 105 |
messages = [
|
|
@@ -133,7 +117,7 @@ output_text = processor.batch_decode(
|
|
| 133 |
print(output_text)
|
| 134 |
```
|
| 135 |
|
| 136 |
-
###
|
| 137 |
|
| 138 |
```json
|
| 139 |
{
|
|
@@ -144,7 +128,7 @@ print(output_text)
|
|
| 144 |
}
|
| 145 |
```
|
| 146 |
|
| 147 |
-
Fields
|
| 148 |
|
| 149 |
## Serving with vLLM
|
| 150 |
|
|
@@ -155,43 +139,11 @@ vllm serve dharma-ai/DharmaOCR-Lite \
|
|
| 155 |
--max-num-batched-tokens 32000
|
| 156 |
```
|
| 157 |
|
| 158 |
-
## Training Details
|
| 159 |
-
|
| 160 |
-
### Pipeline
|
| 161 |
-
|
| 162 |
-
1. **Supervised Fine-Tuning (SFT):** Full fine-tuning on ~39,680 pages of diverse document types (printed, handwritten, tables, forms, mixed layouts) predominantly in Brazilian Portuguese. Labels were generated by large LLMs (Claude Sonnet 4, Llama 4 Maverick, Gemini 2.5 Pro) and validated by human reviewers.
|
| 163 |
-
|
| 164 |
-
2. **Direct Preference Optimization (DPO):** A novel application of DPO to OCR β degenerate model outputs were explicitly used as *rejected* examples to penalize looping/repetitive behavior. Preference pairs were constructed from 5 candidate responses per document, scored by Qwen3-VL-235B as judge, and filtered through a multi-stage policy combining Selective-DPO principles and reward-margin analysis. Final dataset: 49,170 preference pairs.
|
| 165 |
-
|
| 166 |
-
3. **AWQ Quantization (FP8):** Activation-aware weight quantization preserving visual encoder and output layer in full precision. Reduces cost by ~12% with minimal quality degradation (score: 0.921 β 0.911).
|
| 167 |
-
|
| 168 |
-
### Infrastructure
|
| 169 |
-
|
| 170 |
-
- **SFT:** 1Γ NVIDIA H200, 24 CPU cores, 256 GB RAM
|
| 171 |
-
- **DPO:** 4Γ NVIDIA H200, 96 CPU cores, 1024 GB RAM
|
| 172 |
-
- **Hyperparameters:** Effective batch size 32, AdamW optimizer, cosine LR schedule
|
| 173 |
-
|
| 174 |
-
## Why DPO for OCR?
|
| 175 |
-
|
| 176 |
-
Text degeneration β where models get stuck in repetitive loops β is a critical but underreported problem in OCR systems. It's not just a quality issue: degenerate requests consume disproportionate GPU time, reduce throughput for *all* concurrent requests, and inflate costs.
|
| 177 |
-
|
| 178 |
-
DharmaOCR Lite applies DPO to reduce degeneration by **87.6%** relative to SFT-only training (from 1.61% β 0.20%), the largest reduction observed across all model families tested.
|
| 179 |
-
|
| 180 |
-
## DharmaOCR-Benchmark
|
| 181 |
-
|
| 182 |
-
We release [DharmaOCR-Benchmark](https://huggingface.co/datasets/dharma-ai/DharmaOCR-Benchmark), a 496-instance evaluation suite for OCR in Brazilian Portuguese covering:
|
| 183 |
-
|
| 184 |
-
- **ESTER-Pt** (363 samples): Printed text recognition
|
| 185 |
-
- **Legal** (83 samples): Legal and administrative documents
|
| 186 |
-
- **BRESSAY** (50 samples): Handwritten text recognition
|
| 187 |
-
|
| 188 |
-
The benchmark evaluates transcription quality, text degeneration rate, and unit inference cost under a unified protocol.
|
| 189 |
-
|
| 190 |
## Limitations
|
| 191 |
|
| 192 |
- **Language focus:** Primarily optimized for Brazilian Portuguese documents. Performance on other languages may vary.
|
| 193 |
-
- **Field repetition:** The model may occasionally repeat header/footer content within the `text` field. A post-processing step checking exact matches resolves >80% of occurrences.
|
| 194 |
-
- **Domain scope:** Best results on printed, handwritten, and legal/administrative documents.
|
| 195 |
|
| 196 |
## Citation
|
| 197 |
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
language:
|
| 4 |
- pt
|
| 5 |
- en
|
| 6 |
library_name: transformers
|
|
|
|
| 7 |
pipeline_tag: image-text-to-text
|
| 8 |
tags:
|
| 9 |
- ocr
|
| 10 |
- document-understanding
|
| 11 |
- structured-extraction
|
|
|
|
|
|
|
|
|
|
| 12 |
- specialized-small-language-model
|
| 13 |
- brazilian-portuguese
|
| 14 |
datasets:
|
|
|
|
| 23 |
|
| 24 |
## Introduction
|
| 25 |
|
| 26 |
+
**DharmaOCR Lite** is a 3B-parameter Specialized Small Language Model (SSLM) for structured OCR, developed by [Dharma-AI](https://dharma-ai.com). It extracts text from document images into a structured JSON format with explicit `header`, `text`, `footer`, and `margin` fields.
|
| 27 |
|
| 28 |
DharmaOCR Lite achieves **state-of-the-art performance** on [DharmaOCR-Benchmark](https://huggingface.co/datasets/dharma-ai/DharmaOCR-Benchmark), outperforming all evaluated open-source and commercial baselines β including GPT-4o, GPT-5.4, Claude Opus 4.6, Gemini 3.1 Pro, Google Document AI, Amazon Textract, and olmOCR-2-7B β while being significantly cheaper and faster to run.
|
| 29 |
|
| 30 |
+
For the full methodology, training details, and ablation studies, see our paper: **[DharmaOCR: Specialized Small Language Models for Structured OCR that Outperform Open-Source and Commercial Baselines](link_to_paper)**.
|
| 31 |
|
| 32 |
+
## Why DharmaOCR Lite?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
### Best quality among all models evaluated
|
| 35 |
|
| 36 |
+
DharmaOCR Lite (3B) **outperforms models with more than twice as many parameters**, including olmOCR-2-7B, and surpasses every commercial API tested.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
| Model | Score | Degeneration Rate (%) | Time/Page (s) |
|
| 39 |
|---|---|---|---|
|
| 40 |
+
| **DharmaOCR Full** (7B, ours) | **0.925** | 0.40 | 2.132 |
|
| 41 |
+
| **DharmaOCR Lite** (3B, ours) | **0.911** | **0.20** | **1.464** |
|
|
|
|
|
|
|
| 42 |
| Claude Opus 4.6 | 0.833 | β | β |
|
| 43 |
+
| Qwen3-VL-8B | 0.829 | 5.65 | 7.250 |
|
| 44 |
+
| olmOCR-2-7B | 0.823 | 1.41 | 4.306 |
|
| 45 |
| Gemini 3.1 Pro | 0.820 | β | β |
|
| 46 |
+
| Nanonets-OCR2-3B | 0.791 | 2.62 | 1.911 |
|
| 47 |
| GPT-5.4 | 0.750 | β | β |
|
| 48 |
| GPT-4o | 0.635 | β | β |
|
| 49 |
| Google Document AI | 0.640 | β | β |
|
| 50 |
| Amazon Textract | 0.618 | β | β |
|
| 51 |
+
| Mistral OCR 3 | 0.574 | β | β |
|
| 52 |
|
| 53 |
+
Score = (LevenshteinRatio + BLEU) / 2, evaluated on [DharmaOCR-Benchmark](https://huggingface.co/datasets/dharma-ai/DharmaOCR-Benchmark) (496 instances covering printed, handwritten, and legal/administrative documents in Brazilian Portuguese). Time/page measured on NVIDIA L40S.
|
| 54 |
|
| 55 |
+
### Lowest text degeneration rate
|
| 56 |
|
| 57 |
+
Text degeneration β where models get stuck in repetitive loops β is a critical but underreported problem in OCR. It's not just a quality issue: **a single degenerate request can nearly double GPU time and cost** for a batch of concurrent requests, reducing throughput for the entire system.
|
| 58 |
|
| 59 |
+
DharmaOCR Lite achieves a **0.20% degeneration rate**, the lowest across all 22+ models evaluated (open-source and commercial).
|
| 60 |
+
|
| 61 |
+
### Competitive cost
|
| 62 |
|
| 63 |
+
| Model | Score | Relative Cost |
|
| 64 |
+
|---|---|---|
|
| 65 |
+
| **DharmaOCR Lite** (3B) | 0.911 | **34%** |
|
| 66 |
+
| olmOCR-2-7B | 0.823 | 100% (reference) |
|
| 67 |
+
| Qwen3-VL-8B | 0.829 | 168% |
|
| 68 |
+
| Nanonets-OCR2-3B | 0.791 | 44% |
|
| 69 |
+
|
| 70 |
+
DharmaOCR Lite is **~3Γ cheaper** than olmOCR-2-7B while scoring ~10% higher. On an H200 GPU with optimized inference settings, cost drops even further.
|
| 71 |
|
| 72 |
## Quickstart
|
| 73 |
|
| 74 |
```python
|
| 75 |
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 76 |
from qwen_vl_utils import process_vision_info
|
|
|
|
| 77 |
|
| 78 |
model_name = "dharma-ai/DharmaOCR-Lite"
|
| 79 |
|
|
|
|
| 84 |
)
|
| 85 |
processor = AutoProcessor.from_pretrained(model_name)
|
| 86 |
|
|
|
|
| 87 |
image_path = "document.png"
|
| 88 |
|
| 89 |
messages = [
|
|
|
|
| 117 |
print(output_text)
|
| 118 |
```
|
| 119 |
|
| 120 |
+
### Output Format
|
| 121 |
|
| 122 |
```json
|
| 123 |
{
|
|
|
|
| 128 |
}
|
| 129 |
```
|
| 130 |
|
| 131 |
+
Fields not present in the original document are returned as `null`. This lets you decide whether to use auxiliary fields (header, footer, margins) depending on your application.
|
| 132 |
|
| 133 |
## Serving with vLLM
|
| 134 |
|
|
|
|
| 139 |
--max-num-batched-tokens 32000
|
| 140 |
```
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
## Limitations
|
| 143 |
|
| 144 |
- **Language focus:** Primarily optimized for Brazilian Portuguese documents. Performance on other languages may vary.
|
| 145 |
+
- **Field repetition:** The model may occasionally repeat header/footer content within the `text` field. A post-processing step checking exact matches between fields resolves >80% of occurrences.
|
| 146 |
+
- **Domain scope:** Best results on printed, handwritten, and legal/administrative documents.
|
| 147 |
|
| 148 |
## Citation
|
| 149 |
|