API for serving machine translation models.
It can run two types of models:
- ctranslate2 models
- Helsinki-NLP models provided through huggingface.
Model specifications need to go in config.json
.
For an English to Turkish model, place the following files under model/entr
:
- ctranslator2 model as
model.bin
- (Optional) BPE subword codes file (e.g.
bpe.en-tr.codes
) - (Optional) Sentencepiece model (To be implemented)
Add the following configuration under models
in config.json
:
{
"src": "en",
"tgt": "tr",
"model_type": "ctranslator2",
"model_path": "entr", //model directory name under models
"bpe_file": "bpe.en-tr.codes",
"load": true,
"sentence_split": "nltk",
"pipeline": {
"lowercase": true, //make true if you processed your data in lowercase when training your model
"tokenize": true, //make true if you processed your data with moses tokenizer when training your model
"bpe": true, //make true if you use bpe subwording when training your model
"translate": true,
"recase": true // true if you want to do recasing on output (capitalizes first letter of sentence)
}
}
You can serve Helsinki-NLP models provided in huggingface, as long as they are one-to-one. Make sure they are listed in https://huggingface.co/Helsinki-NLP and place the language codes exactly as they are.
For an French to English model, add the following configuration under models
in config.json
:
{
"src": "fr",
"tgt": "en",
"model_type": "opus",
"load": true,
"sentence_split": "nltk" ,
"pipeline": {
"lowercase": false,
"translate": true,
"recase": true
}
}
Set the environment variables:
MT_API_CONFIG=config.json
MODELS_ROOT=models
MT_API_DEVICE=gpu|cpu
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8001
docker-compose build
docker-compose up
Do the following edits on docker-compose file
- Remove comment
runtime: nvidia
line - Under environment, set
MT_API_DEVICE=gpu
- Rename
Dockerfile
toDockerfile-cpu
- Rename
Dockerfile-gpu
toDockerfile
- Build and run.
Endpoint for translating a single phrase.
curl --location --request POST 'http://127.0.0.1:8001/api/v1/translate' \
--header 'Content-Type: application/json' \
--data-raw '{"src":"fr", "tgt":"en", "text":"c'\''est un test"}'
import httpx
translate_service_url = "http://127.0.0.1:8001/api/v1/translate"
json_data = {'src':'fr', 'tgt':'en', 'text':"c'est un test."}
r = httpx.post(translate_service_url, json=json_data)
response = r.json()
print("Translation:", response['translation'])
Endpoint for translating a list of sentences.
curl --location --request POST 'http://127.0.0.1:8001/api/v1/translate/batch' \
--header 'Content-Type: application/json' \
--data-raw '{"src":"en", "tgt":"fr", "texts":["hello twb", "this is another sentence"]}'
import httpx
translate_service_url = "http://127.0.0.1:8001/api/v1/translate/batch"
json_data = {'src':'fr', 'tgt':'en', 'texts':["hello twb", "this is another sentence"]}
r = httpx.post(translate_service_url, json=json_data)
response = r.json()
print("Translation:", response['translation'])
Retrieves a the list of supported languages and model pairs.
curl 'http://127.0.0.1:8001/api/v1/translate/'
import httpx
translate_service_url = "http://127.0.0.1:8001/api/v1/translate/"
r = httpx.get(translate_url)
response = r.json()
print(response)
{
"languages": {
"ar": "Levantine Arabic",
"en": "English",
"fr": "French",
"swc": "Congolese Swahili",
"ti": "Tigrinya"
}, "models":{"fr":{"en":["fr_en"],"swc":["fr_swc"]},"swc":{"fr":["swc_fr"]},"ti":{"en":["ti_en"]},"en":{"ti":["en_ti"]},"ar":{"en":["ar_en", "ar_en_domainspecific"]}}
}
languages
: All language codes used in the system and their respective language names.models
: Three level dictionary listing the available models with the structure:
<source-language>
↳ <target-language>
↳ List of <model-id>s associated with the language pair
Note: The third level model list is for seeing different versions of the model. There is always a default model in the language pair with id <src>_<tgt>
and there could be alternative models with model id <src>_<tgt>_<alt-tag>
.
For example in the setup above, there are two alternative models in the Arabic-English direction: A default model ar_en
and a domain specific ar_en_domainspecific
model where domainspecific is the alternative model id. For the rest of the language pairs there is only one default model.