A high-performance REST API for generating sentence embeddings for Indian regional languages using IndicBERT v2. This service provides semantic text representations that can be used for similarity search, clustering, and other NLP tasks.
| Code | Language | Code | Language |
|---|---|---|---|
| kn | Kannada | mr | Marathi |
| hi | Hindi | bn | Bengali |
| ta | Tamil | gu | Gujarati |
| te | Telugu | pa | Punjabi |
| ml | Malayalam | or | Odia |
| ur | Urdu | en | English |
GET /health
Returns system status, model readiness, and resource usage.
Response:
{
"status": "ok",
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM",
"threads_used": 5,
"total_cpu_cores": 11,
"cpu_cap": "50%",
"ram_total_gb": 16.0,
"ram_available_gb": 12.5,
"ram_used_pct": 21.8
}
POST /embed
Generate embedding for a single text.
Request:
{
"text": "ಭಾರತ ವಿಶ್ವಕಪ್ ಗೆದ್ದಿತು",
"language": "kn"
}
Response:
{
"embedding": [0.123, 0.456, ...],
"dimensions": 768,
"language": "kn",
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM"
}
POST /embed/batch
Generate embeddings for multiple texts.
Request:
{
"items": [
{"text": "Text 1", "language": "kn"},
{"text": "Text 2", "language": "hi"}
]
}
Response:
{
"embeddings": [[0.123, ...], [0.456, ...]],
"count": 2,
"dimensions": 768,
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM"
}
GET /languages
Returns list of supported languages.
Response:
{
"supported": [
{"code": "kn", "name": "Kannada"},
{"code": "hi", "name": "Hindi"},
...
]
}
Clone the repository:
git clone <repository-url>
cd indian-languages-text-embedding
Build the Docker image:
docker build -t indicbert-api .
This performs a multi-stage build that optimizes image size by:
__pycache__)Run the container:
docker run -d -p 8000:8000 --name indicbert indicbert-api
Test the API:
curl http://localhost:8000/health
Install Python dependencies:
pip install fastapi uvicorn transformers torch numpy psutil psycopg2-binary
Download the model (optional - speeds up startup):
from transformers import AutoTokenizer, AutoModel
AutoTokenizer.from_pretrained('ai4bharat/IndicBERTv2-MLM-Sam-TLM')
AutoModel.from_pretrained('ai4bharat/IndicBERTv2-MLM-Sam-TLM')
Run the server:
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 2
| Variable | Default | Description |
|---|---|---|
MODEL_NAME | ai4bharat/IndicBERTv2-MLM-Sam-TLM | HuggingFace model name |
MAX_LENGTH | 512 | Maximum token length for input texts |
MAX_BATCH_SIZE | 64 | Maximum number of texts in batch requests |
TEXT_PREVIEW | 500 | Character limit before tokenization (performance optimization) |
CPU_USAGE_CAP | 0.5 | Fraction of CPU cores to use (0.0-1.0) |
import requests
# Single embedding
response = requests.post('http://localhost:8000/embed', json={
'text': 'भारत विश्व कप जीता',
'language': 'hi'
})
embedding = response.json()['embedding']
# Batch embeddings
response = requests.post('http://localhost:8000/embed/batch', json={
'items': [
{'text': 'ಕನ್ನಡದಲ್ಲಿ ಬರೆಯಲಾಗಿದೆ', 'language': 'kn'},
{'text': 'Written in Hindi', 'language': 'hi'}
]
})
embeddings = response.json()['embeddings']
# Health check
curl http://localhost:8000/health
# Single embedding
curl -X POST http://localhost:8000/embed \
-H "Content-Type: application/json" \
-d '{"text": "ಭಾರತ ವಿಶ್ವಕಪ್ ಗೆದ್ದಿತು", "language": "kn"}'
# Batch embedding
curl -X POST http://localhost:8000/embed/batch \
-H "Content-Type: application/json" \
-d '{
"items": [
{"text": "Text 1", "language": "kn"},
{"text": "Text 2", "language": "hi"}
]
}'
__pycache__ files cleaned up to reduce bloatThe API provides health endpoints for monitoring:
├── app.py # Main FastAPI application
├── Dockerfile # Multi-stage Docker build
│ # - Stage 1 (builder): Creates venv, installs deps, downloads model
│ # - Stage 2 (runtime): Copies only venv and model files
├── requirements.txt # Python dependencies (if not using Docker)
└── README.md # This file
The Dockerfile uses a two-stage build process:
Stage 1 - Builder:
/opt/venv__pycache__) to save space/app/model to avoid symlink overheadStage 2 - Runtime:
python:3.12-slim base imageThis approach significantly reduces image size by excluding build dependencies from the final image.
The model supports additional Indian languages. To add support:
SUPPORTED_LANGUAGES dictionary in app.pyModel Loading Timeout
Memory Issues
MAX_BATCH_SIZECPU_USAGE_CAP if more cores availablePort Already in Use
docker run -p 8001:8000 ...View container logs:
docker logs indicbert
This project is licensed under the MIT License - see the LICENSE file for details.
A high-performance REST API for generating sentence embeddings for Indian regional languages using IndicBERT v2. This service provides semantic text representations that can be used for similarity search, clustering, and other NLP tasks.
| Code | Language | Code | Language |
|---|---|---|---|
| kn | Kannada | mr | Marathi |
| hi | Hindi | bn | Bengali |
| ta | Tamil | gu | Gujarati |
| te | Telugu | pa | Punjabi |
| ml | Malayalam | or | Odia |
| ur | Urdu | en | English |
GET /health
Returns system status, model readiness, and resource usage.
Response:
{
"status": "ok",
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM",
"threads_used": 5,
"total_cpu_cores": 11,
"cpu_cap": "50%",
"ram_total_gb": 16.0,
"ram_available_gb": 12.5,
"ram_used_pct": 21.8
}
POST /embed
Generate embedding for a single text.
Request:
{
"text": "ಭಾರತ ವಿಶ್ವಕಪ್ ಗೆದ್ದಿತು",
"language": "kn"
}
Response:
{
"embedding": [0.123, 0.456, ...],
"dimensions": 768,
"language": "kn",
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM"
}
POST /embed/batch
Generate embeddings for multiple texts.
Request:
{
"items": [
{"text": "Text 1", "language": "kn"},
{"text": "Text 2", "language": "hi"}
]
}
Response:
{
"embeddings": [[0.123, ...], [0.456, ...]],
"count": 2,
"dimensions": 768,
"model": "ai4bharat/IndicBERTv2-MLM-Sam-TLM"
}
GET /languages
Returns list of supported languages.
Response:
{
"supported": [
{"code": "kn", "name": "Kannada"},
{"code": "hi", "name": "Hindi"},
...
]
}
Clone the repository:
git clone <repository-url>
cd indian-languages-text-embedding
Build the Docker image:
docker build -t indicbert-api .
This performs a multi-stage build that optimizes image size by:
__pycache__)Run the container:
docker run -d -p 8000:8000 --name indicbert indicbert-api
Test the API:
curl http://localhost:8000/health
Install Python dependencies:
pip install fastapi uvicorn transformers torch numpy psutil psycopg2-binary
Download the model (optional - speeds up startup):
from transformers import AutoTokenizer, AutoModel
AutoTokenizer.from_pretrained('ai4bharat/IndicBERTv2-MLM-Sam-TLM')
AutoModel.from_pretrained('ai4bharat/IndicBERTv2-MLM-Sam-TLM')
Run the server:
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 2
| Variable | Default | Description |
|---|---|---|
MODEL_NAME | ai4bharat/IndicBERTv2-MLM-Sam-TLM | HuggingFace model name |
MAX_LENGTH | 512 | Maximum token length for input texts |
MAX_BATCH_SIZE | 64 | Maximum number of texts in batch requests |
TEXT_PREVIEW | 500 | Character limit before tokenization (performance optimization) |
CPU_USAGE_CAP | 0.5 | Fraction of CPU cores to use (0.0-1.0) |
import requests
# Single embedding
response = requests.post('http://localhost:8000/embed', json={
'text': 'भारत विश्व कप जीता',
'language': 'hi'
})
embedding = response.json()['embedding']
# Batch embeddings
response = requests.post('http://localhost:8000/embed/batch', json={
'items': [
{'text': 'ಕನ್ನಡದಲ್ಲಿ ಬರೆಯಲಾಗಿದೆ', 'language': 'kn'},
{'text': 'Written in Hindi', 'language': 'hi'}
]
})
embeddings = response.json()['embeddings']
# Health check
curl http://localhost:8000/health
# Single embedding
curl -X POST http://localhost:8000/embed \
-H "Content-Type: application/json" \
-d '{"text": "ಭಾರತ ವಿಶ್ವಕಪ್ ಗೆದ್ದಿತು", "language": "kn"}'
# Batch embedding
curl -X POST http://localhost:8000/embed/batch \
-H "Content-Type: application/json" \
-d '{
"items": [
{"text": "Text 1", "language": "kn"},
{"text": "Text 2", "language": "hi"}
]
}'
__pycache__ files cleaned up to reduce bloatThe API provides health endpoints for monitoring:
├── app.py # Main FastAPI application
├── Dockerfile # Multi-stage Docker build
│ # - Stage 1 (builder): Creates venv, installs deps, downloads model
│ # - Stage 2 (runtime): Copies only venv and model files
├── requirements.txt # Python dependencies (if not using Docker)
└── README.md # This file
The Dockerfile uses a two-stage build process:
Stage 1 - Builder:
/opt/venv__pycache__) to save space/app/model to avoid symlink overheadStage 2 - Runtime:
python:3.12-slim base imageThis approach significantly reduces image size by excluding build dependencies from the final image.
The model supports additional Indian languages. To add support:
SUPPORTED_LANGUAGES dictionary in app.pyModel Loading Timeout
Memory Issues
MAX_BATCH_SIZECPU_USAGE_CAP if more cores availablePort Already in Use
docker run -p 8001:8000 ...View container logs:
docker logs indicbert
This project is licensed under the MIT License - see the LICENSE file for details.