Spanish to english translator service
This repo contains an app to translate text from english to spanish. Its an api rest intended for personal use, translating the my blog's posts. The final text may not be perfect, but is a good starting point.
It uses 🤗 Transfomers with Helsinki-NLP/opus-mt-en-es model, and Ray Serve for model serving.
The app is intented to be run from a docker container, take into account the dependencies make it BIG (3GB plus).
It builds from the base ray docker image.
Build and run the app:
docker compose up --build
And stop the app:
docker compose down
Or directly from docker, build:
docker build -t spanglish .
Run:
docker run -p 8000:8000 -it --env-file=model-name.env spanglish
We can also start the app without docker. Its tested with python 3.10.7 (ray currently fails with python 3.11).
Install the requirements (inside a virtual environment):
pip install -r requirements.txt
And start the service:
serve run app:translator
Once the service is ready listening (you should see the following message: Deployed Serve app successfully), we can start sending requests.
Once the container is running we are ready to test it.
There are two endpoints, one for a single text:
>>> import json
>>> import requests
>>> payload = "hello world"
>>> requests.get("http://localhost:8000/single", params={"text": payload}).json()
'Hola mundo'
And one for a batch of texts (see the ray batching docs for more info):
>>> import json
>>> import requests
>>> payload = json.dumps(["hello", "world", "one", "two"])
>>> requests.get("http://localhost:8000/batched", params={"texts": payload}).json()
'["hola", "mundo", "uno", "dos"]'
Even though the app has been created for translating texts from english to spanish, the 🤗 Transformers API makes it easily interchangeable. For this reason, the model name can be modified via environment variable, updating the model-name.env
file, take a look at the Helsinki NLP models for alternatives!