datalab-to/chandra
53270k
Chandra
Chandra is an OCR model that outputs markdown, HTML, and JSON. It is highly accurate at extracting text from images and PDFs, while preserving layout information.
You can try Chandra in the free playground here, or at a hosted API here.
Features
- Convert documents to markdown, html, or json with detailed layout information
- Good handwriting support
- Reconstructs forms accurately, including checkboxes
- Good support for tables, math, and complex layouts
- Extracts images and diagrams, with captions and structured data
- Support for 40+ languages
Quickstart
The easiest way to start is with the CLI tools:
pip install chandra-ocr
# With VLLM
chandra_vllm
chandra input.pdf ./output
# With HuggingFace
chandra input.pdf ./output --method hf
# Interactive streamlit app
chandra_appBenchmarks
We used the olmocr benchmark, which seems to be the most reliable current OCR benchmark in our testing.
<img src="bench.png" width="600px"/>
Examples
<img src="handwritten_form.png" width="600px"/>
Usage
Installation
pip install chandra-ocrFrom code
from chandra.model import InferenceManager
from chandra.model.schema import BatchInputItem
# Run chandra_vllm to start a vLLM server first if you pass vllm, else pass hf
# you can also start your own vllm server with the datalab-to/chandra model
manager = InferenceManager(method="vllm")
batch = [
BatchInputItem(
image=PIL_IMAGE,
prompt_type="ocr_layout"
)
]
result = manager.generate(batch)[0]
print(result.markdown)With transformers
from transformers import AutoModel, AutoProcessor
from chandra.model.hf import generate_hf
from chandra.model.schema import BatchInputItem
from chandra.output import parse_markdown
model = AutoModel.from_pretrained("datalab-to/chandra").cuda()
model.processor = AutoProcessor.from_pretrained("datalab-to/chandra")
batch = [
BatchInputItem(
image=PIL_IMAGE,
prompt_type="ocr_layout"
)
]
result = generate_hf(batch, model)[0]
markdown = parse_markdown(result.raw)Credits
Thank you to the following open source projects:
