This project sets up an Indic translation server, allowing translation between various languages including English, Kannada, Hindi, and others. It utilizes models from AI4Bharat to perform translations.
We suggest to use non-distilled models for better translation.
We have hosted an Translation service for Indian languages.
Create a virtual environment:
python -m venv venv
Activate the virtual environment:
source venv/bin/activate
venv\Scripts\activate
pip install -r requirements.txt
Below is a table summarizing the available models for different translation tasks:
| Task | Variant | Model Name | VRAM Size | Download Command |
|---|---|---|---|---|
| Indic to English | 200M (distilled) | indictrans2-indic-en-dist-200M | 950 MB | huggingface-cli download ai4bharat/indictrans2-indic-en-dist-200M |
| 1B (base) | indictrans2-indic-en-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-indic-en-1B | |
| English to Indic | 200M (distilled) | indictrans2-en-indic-dist-200M | 950 MB | huggingface-cli download ai4bharat/indictrans2-en-indic-dist-200M |
| 1B (base) | indictrans2-en-indic-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-en-indic-1B | |
| Indic to Indic | 320M (distilled) | indictrans2-indic-indic-dist-320M | 950 MB | huggingface-cli download ai4bharat/indictrans2-indic-indic-dist-320M |
| 1B (base) | indictrans2-indic-indic-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-indic-indic-1B |
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from IndicTransToolkit import IndicProcessor
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
src_lang, tgt_lang = "hin_Deva", "eng_Latn"
model_name = "ai4bharat/indictrans2-indic-en-dist-200M"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForSeq2SeqLM.from_pretrained(
model_name,
trust_remote_code=True,
torch_dtype=torch.float16, # performance might slightly vary for bfloat16
attn_implementation="flash_attention_2"
).to(DEVICE)
ip = IndicProcessor(inference=True)
input_sentences = [
"जब मैं छोटा था, मैं हर रोज़ पार्क जाता था।",
"हमने पिछले सप्ताह एक नई फिल्म देखी जो कि बहुत प्रेरणादायक थी।",
]
batch = ip.preprocess_batch(
input_sentences,
src_lang=src_lang,
tgt_lang=tgt_lang,
)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Tokenize the sentences and generate input encodings
inputs = tokenizer(
batch,
truncation=True,
padding="longest",
return_tensors="pt",
return_attention_mask=True,
).to(DEVICE)
# Generate translations using the model
with torch.no_grad():
generated_tokens = model.generate(
**inputs,
use_cache=True,
min_length=0,
max_length=256,
num_beams=5,
num_return_sequences=1,
)
# Decode the generated tokens into text
with tokenizer.as_target_tokenizer():
generated_tokens = tokenizer.batch_decode(
generated_tokens.detach().cpu().tolist(),
skip_special_tokens=True,
clean_up_tokenization_spaces=True,
)
# Postprocess the translations, including entity replacement
translations = ip.postprocess_batch(generated_tokens, lang=tgt_lang)
for input_sentence, translation in zip(input_sentences, translations):
print(f"{src_lang}: {input_sentence}")
print(f"{tgt_lang}: {translation}")
python translate_code.py
Install dependencies:
pip install -r server-requirements.txt
You can run the server using FastAPI:
python src/server/translate_api.py --port 10802 --host 0.0.0.0 --device cuda --use_distilled
python src/server/translate_api.py --port 10802 --host 0.0.0.0 --device cuda
You can evaluate the translation results using curl commands. Here are some examples:
curl -X 'POST' \
'http://localhost:10802/translate?tgt_lang=kan_Knda&src_lang=eng_Latn&device_type=cuda' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"Hello, how are you?", "Good morning!"
],
"src_lang": "eng_Latn",
"tgt_lang": "kan_Knda"
}'
Response:
{
"translations": [
"ಹಲೋ, ಹೇಗಿದ್ದೀರಿ? ",
"ಶುಭೋದಯ! "
]
}
curl -X 'POST' \
'http://localhost:10802/translate?src_lang=kan_Knda&tgt_lang=eng_Latn&device_type=cuda' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
],
"src_lang": "kan_Knda",
"tgt_lang": "eng_Latn"
}'
Response:
{
"translations": ["Hello, how are you?", "Good morning!"]
}
curl -X 'POST' \
'http://localhost:10802/translate?src_lang=kan_Knda&tgt_lang=hin_Deva' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
],
"src_lang": "kan_Knda",
"tgt_lang": "hin_Deva"
}'
{
"translations": [
"हैलो, कैसा लग रहा है? ",
"गुड मॉर्निंग! "
]
}
Here is the list of languages supported by the IndicTrans2 models:
| Assamese (asm_Beng) | Kashmiri (Arabic) (kas_Arab) | Punjabi (pan_Guru) |
| Bengali (ben_Beng) | Kashmiri (Devanagari) (kas_Deva) | Sanskrit (san_Deva) |
| Bodo (brx_Deva) | Maithili (mai_Deva) | Santali (sat_Olck) |
| Dogri (doi_Deva) | Malayalam (mal_Mlym) | Sindhi (Arabic) (snd_Arab) |
| English (eng_Latn) | Marathi (mar_Deva) | Sindhi (Devanagari) (snd_Deva) |
| Konkani (gom_Deva) | Manipuri (Bengali) (mni_Beng) | Tamil (tam_Taml) |
| Gujarati (guj_Gujr) | Manipuri (Meitei) (mni_Mtei) | Telugu (tel_Telu) |
| Hindi (hin_Deva) | Nepali (npi_Deva) | Urdu (urd_Arab) |
| Kannada (kan_Knda) | Odia (ory_Orya) |
We welcome contributions! Please read the CONTRIBUTING.md file for guidelines on how to contribute to this project.
Also you can join the discord group to collaborate
This project is licensed under the MIT License - see the LICENSE file for details.
This README provides a comprehensive guide to setting up and running the Indic Translate Server. For more details, refer to the linked resources.
@article{gala2023indictrans,
title={IndicTrans2: Towards High-Quality and Accessible Machine Translation Models for all 22 Scheduled Indian Languages},
author={Jay Gala and Pranjal A Chitale and A K Raghavan and Varun Gumma and Sumanth Doddapaneni and Aswanth Kumar M and Janki Atul Nawale and Anupama Sujatha and Ratish Puduppully and Vivek Raghavan and Pratyush Kumar and Mitesh M Khapra and Raj Dabre and Anoop Kunchukuttan},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2023},
url={https://openreview.net/forum?id=vfT4YuzAYA},
note={}
}
This project sets up an Indic translation server, allowing translation between various languages including English, Kannada, Hindi, and others. It utilizes models from AI4Bharat to perform translations.
We suggest to use non-distilled models for better translation.
We have hosted an Translation service for Indian languages.
Create a virtual environment:
python -m venv venv
Activate the virtual environment:
source venv/bin/activate
venv\Scripts\activate
pip install -r requirements.txt
Below is a table summarizing the available models for different translation tasks:
| Task | Variant | Model Name | VRAM Size | Download Command |
|---|---|---|---|---|
| Indic to English | 200M (distilled) | indictrans2-indic-en-dist-200M | 950 MB | huggingface-cli download ai4bharat/indictrans2-indic-en-dist-200M |
| 1B (base) | indictrans2-indic-en-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-indic-en-1B | |
| English to Indic | 200M (distilled) | indictrans2-en-indic-dist-200M | 950 MB | huggingface-cli download ai4bharat/indictrans2-en-indic-dist-200M |
| 1B (base) | indictrans2-en-indic-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-en-indic-1B | |
| Indic to Indic | 320M (distilled) | indictrans2-indic-indic-dist-320M | 950 MB | huggingface-cli download ai4bharat/indictrans2-indic-indic-dist-320M |
| 1B (base) | indictrans2-indic-indic-1B | 4.5 GB | huggingface-cli download ai4bharat/indictrans2-indic-indic-1B |
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from IndicTransToolkit import IndicProcessor
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
src_lang, tgt_lang = "hin_Deva", "eng_Latn"
model_name = "ai4bharat/indictrans2-indic-en-dist-200M"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForSeq2SeqLM.from_pretrained(
model_name,
trust_remote_code=True,
torch_dtype=torch.float16, # performance might slightly vary for bfloat16
attn_implementation="flash_attention_2"
).to(DEVICE)
ip = IndicProcessor(inference=True)
input_sentences = [
"जब मैं छोटा था, मैं हर रोज़ पार्क जाता था।",
"हमने पिछले सप्ताह एक नई फिल्म देखी जो कि बहुत प्रेरणादायक थी।",
]
batch = ip.preprocess_batch(
input_sentences,
src_lang=src_lang,
tgt_lang=tgt_lang,
)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Tokenize the sentences and generate input encodings
inputs = tokenizer(
batch,
truncation=True,
padding="longest",
return_tensors="pt",
return_attention_mask=True,
).to(DEVICE)
# Generate translations using the model
with torch.no_grad():
generated_tokens = model.generate(
**inputs,
use_cache=True,
min_length=0,
max_length=256,
num_beams=5,
num_return_sequences=1,
)
# Decode the generated tokens into text
with tokenizer.as_target_tokenizer():
generated_tokens = tokenizer.batch_decode(
generated_tokens.detach().cpu().tolist(),
skip_special_tokens=True,
clean_up_tokenization_spaces=True,
)
# Postprocess the translations, including entity replacement
translations = ip.postprocess_batch(generated_tokens, lang=tgt_lang)
for input_sentence, translation in zip(input_sentences, translations):
print(f"{src_lang}: {input_sentence}")
print(f"{tgt_lang}: {translation}")
python translate_code.py
Install dependencies:
pip install -r server-requirements.txt
You can run the server using FastAPI:
python src/server/translate_api.py --port 10802 --host 0.0.0.0 --device cuda --use_distilled
python src/server/translate_api.py --port 10802 --host 0.0.0.0 --device cuda
You can evaluate the translation results using curl commands. Here are some examples:
curl -X 'POST' \
'http://localhost:10802/translate?tgt_lang=kan_Knda&src_lang=eng_Latn&device_type=cuda' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"Hello, how are you?", "Good morning!"
],
"src_lang": "eng_Latn",
"tgt_lang": "kan_Knda"
}'
Response:
{
"translations": [
"ಹಲೋ, ಹೇಗಿದ್ದೀರಿ? ",
"ಶುಭೋದಯ! "
]
}
curl -X 'POST' \
'http://localhost:10802/translate?src_lang=kan_Knda&tgt_lang=eng_Latn&device_type=cuda' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
],
"src_lang": "kan_Knda",
"tgt_lang": "eng_Latn"
}'
Response:
{
"translations": ["Hello, how are you?", "Good morning!"]
}
curl -X 'POST' \
'http://localhost:10802/translate?src_lang=kan_Knda&tgt_lang=hin_Deva' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"sentences": [
"ನಮಸ್ಕಾರ, ಹೇಗಿದ್ದೀರಾ?", "ಶುಭೋದಯ!"
],
"src_lang": "kan_Knda",
"tgt_lang": "hin_Deva"
}'
{
"translations": [
"हैलो, कैसा लग रहा है? ",
"गुड मॉर्निंग! "
]
}
Here is the list of languages supported by the IndicTrans2 models:
| Assamese (asm_Beng) | Kashmiri (Arabic) (kas_Arab) | Punjabi (pan_Guru) |
| Bengali (ben_Beng) | Kashmiri (Devanagari) (kas_Deva) | Sanskrit (san_Deva) |
| Bodo (brx_Deva) | Maithili (mai_Deva) | Santali (sat_Olck) |
| Dogri (doi_Deva) | Malayalam (mal_Mlym) | Sindhi (Arabic) (snd_Arab) |
| English (eng_Latn) | Marathi (mar_Deva) | Sindhi (Devanagari) (snd_Deva) |
| Konkani (gom_Deva) | Manipuri (Bengali) (mni_Beng) | Tamil (tam_Taml) |
| Gujarati (guj_Gujr) | Manipuri (Meitei) (mni_Mtei) | Telugu (tel_Telu) |
| Hindi (hin_Deva) | Nepali (npi_Deva) | Urdu (urd_Arab) |
| Kannada (kan_Knda) | Odia (ory_Orya) |
We welcome contributions! Please read the CONTRIBUTING.md file for guidelines on how to contribute to this project.
Also you can join the discord group to collaborate
This project is licensed under the MIT License - see the LICENSE file for details.
This README provides a comprehensive guide to setting up and running the Indic Translate Server. For more details, refer to the linked resources.
@article{gala2023indictrans,
title={IndicTrans2: Towards High-Quality and Accessible Machine Translation Models for all 22 Scheduled Indian Languages},
author={Jay Gala and Pranjal A Chitale and A K Raghavan and Varun Gumma and Sumanth Doddapaneni and Aswanth Kumar M and Janki Atul Nawale and Anupama Sujatha and Ratish Puduppully and Vivek Raghavan and Pratyush Kumar and Mitesh M Khapra and Raj Dabre and Anoop Kunchukuttan},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2023},
url={https://openreview.net/forum?id=vfT4YuzAYA},
note={}
}