An OpenAI-compatible API for document layout analysis using the Chandra OCR 2 model.
Chandra OCR 2 is a state-of-the-art document understanding model that performs layout analysis on document images. This API provides an OpenAI-compatible interface for processing document images and extracting layout information including bounding boxes, categories, and text content.
- Layout analysis for document images
- Support for multiple layout categories (Caption, Footnote, Formula, List-item, Page-footer, Page-header, Picture, Section-header, Table, Text, Title)
- OpenAI-compatible API endpoints
- FastAPI-based server with async processing
- GPU acceleration support via PyTorch
- Python 3.11 or higher
- PyTorch with CUDA support (optional, for GPU acceleration)
- Clone the repository:
git clone <repository-url>
cd chandr- Install dependencies:
uv sync- Download the model:
# The model will be automatically downloaded on first run
# Model: datalab-to/chandra-ocr-2Run the following command to start the API server:
uv run mainThe server will start on http://0.0.0.0:8000.
GET /health
Returns the health status of the API.
GET /v1/models
Returns a list of available models.
POST /v1/chat/completions
Processes a document image and returns layout analysis.
Request Body:
{
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,..."
}
}
]
}
],
"max_tokens": 24000
}Response:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "chandra-ocr-2",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
}
]
}
],
"max_tokens": 24000
}'- Model Name: datalab-to/chandra-ocr-2
- Type: Image-to-Text Model
- License: Apache 2.0
The API returns layout analysis results in markdown format, including:
- Bbox: Coordinates in format
[x1, y1, x2, y2] - Categories: Caption, Footnote, Formula, List-item, Page-footer, Page-header, Picture, Section-header, Table, Text, Title
- Text Content: Extracted text formatted according to category:
- Formula: LaTeX format
- Table: HTML format
- Other categories: Markdown format
This project is licensed under the MIT License.