Skip to content

Commit

Permalink
update docker
Browse files Browse the repository at this point in the history
  • Loading branch information
nreimers committed Mar 16, 2021
1 parent 674213b commit f55578f
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 15 deletions.
46 changes: 41 additions & 5 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ We provide a [Docker](https://www.docker.com/) based REST-API for EasyNMT: Send

To start the EasyNMT REST API on port `24080`run the following docker command:
```
docker run -p 24080:80 easynmt/api:1.1.0-cpu
docker run -p 24080:80 easynmt/api:1.1-cpu
```

This uses the CPU image. If you have GPU (CUDA), there are various GPU images available. Have a look at our [Docker Hub Page](https://hub.docker.com/repository/docker/easynmt/api/tags?page=1&ordering=last_updated).
Expand All @@ -30,17 +30,53 @@ This should yield the following JSON:
"translation_time": 22.163145542144775
}
```
If you have started it with a different port, replace `24080` with the port you chose.

Note, for the first translation, the respective models are downloaded. This might take some time. Consecutive calls will be faster.

The endpoi
## Programmatic Usage
- **Python:** [python_query_api.py](examples/python_query_api.py)

## Documentation

To get an overview of all REST API endpoints, with all possible parameters and their description, you open the following url:
To get an overview of all REST API endpoints, with all possible parameters and their description, you open the following url: [http://localhost:24080/docs](http://localhost:24080/docs)

### Endpoints
The following endpoints with the GET method are defined (i.e. you can call them like `http://localhost:24080/name?param1=val1&param2=val2`)

```
http://localhost:24080/docs
/translate
Translates the text to the given target language.
:param text: Text that should be translated
:param target_lang: Target language
:param source_lang: Language of text. Optional, if empty: Automatic language detection
:param beam_size: Beam size. Optional
:param perform_sentence_splitting: Split longer documents into individual sentences for translation. Optional
:return: Returns a json with the translated text
/language_detection
Detects the language for the provided text
:param text: A single text for which we want to know the language
:return: The detected language
/get_languages
Returns the languages the model supports
:param source_lang: Optional. Only return languages with this language as source
:param target_lang: Optional. Only return languages with this language as target
:return:
```

If you have started it with a different port, replace `24080` with the port you chose.
You can call the `/translate` and `/language_detection` also with a POST request, giving you the option to pass a list of multiple texts. Then all texts are translated and returned at once.

### Environment Variables
You can control the Docker image using various environment variables:
- *MAX_WORKERS_BACKEND*: Number of worker processes for the translation. Default: 1
- *MAX_WORKERS_FRONTEND*: Number of worker processes for language detection & model info. Default: 2
- *EASYNMT_MODEL*: Which EasyNMT Model to load. Default: opus-mt
- *EASYNMT_MODEL_ARGS*: Json encoded string with parameters when loading EasyNMT: Default: {}
- *EASYNMT_MAX_TEXT_LEN*: Maximal text length for translation. Default: Not set
- *EASYNMT_MAX_BEAM_SIZE*: Maximal beam size for translation. Default: Not set
- *EASYNMT_BATCH_SIZE*: Batch size for translation. Default: 16
- *TIMEOUT*: [Gunicorn timeout](https://docs.gunicorn.org/en/stable/settings.html#timeout). Default: 120

All model files are stored at `/cache/`. You can mount this path to your host machine if you want to re-use previously downloaded models.
4 changes: 2 additions & 2 deletions docker/api/cpu.dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM python:3.8-slim
LABEL maintainer="Nils Reimers <info@nils-reimers>"

RUN apt-get update && apt-get -y install -y procps
RUN pip install --no-cache-dir torch==1.8.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

###################################### Same code for all docker files ###############
Expand Down Expand Up @@ -35,8 +36,7 @@ EXPOSE 80
####

# Create cache folders
RUN mkdir /cache/
VOLUME /cache
RUN mkdir /cache
RUN mkdir /cache/easynmt
RUN mkdir /cache/transformers
RUN mkdir /cache/torch
Expand Down
1 change: 0 additions & 1 deletion docker/api/cuda10.1.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ EXPOSE 80

# Create cache folders
RUN mkdir /cache/
VOLUME /cache
RUN mkdir /cache/easynmt
RUN mkdir /cache/transformers
RUN mkdir /cache/torch
Expand Down
1 change: 0 additions & 1 deletion docker/api/cuda11.0.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ EXPOSE 80

# Create cache folders
RUN mkdir /cache/
VOLUME /cache
RUN mkdir /cache/easynmt
RUN mkdir /cache/transformers
RUN mkdir /cache/torch
Expand Down
1 change: 0 additions & 1 deletion docker/api/cuda11.1.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ EXPOSE 80

# Create cache folders
RUN mkdir /cache/
VOLUME /cache
RUN mkdir /cache/easynmt
RUN mkdir /cache/transformers
RUN mkdir /cache/torch
Expand Down
4 changes: 2 additions & 2 deletions docker/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
http3
easynmt
easynmt
http3
6 changes: 3 additions & 3 deletions docker/api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@




app = FastAPI()
app.add_middleware(
CORSMiddleware,
Expand All @@ -32,7 +31,7 @@


model_name = os.getenv('EASYNMT_MODEL', 'opus-mt')
model_args = json.loads(os.getenv('EASYNMT_MODEL_ARGS', '{}') )
model_args = json.loads(os.getenv('EASYNMT_MODEL_ARGS', '{}'))
print("Load model: "+ model_name)
model = EasyNMT(model_name, load_translator=IS_BACKEND, **model_args)

Expand All @@ -41,7 +40,7 @@
@app.get("/translate")
async def translate(target_lang: str, text: List[str] = Query([]), source_lang: Optional[str] = '', beam_size: Optional[int] = 5, perform_sentence_splitting: Optional[bool] = True):
"""
Translation
Translates the text to the given target language.
:param text: Text that should be translated
:param target_lang: Target language
:param source_lang: Language of text. Optional, if empty: Automatic language detection
Expand Down Expand Up @@ -126,6 +125,7 @@ async def get_languages(source_lang: Optional[str] = None, target_lang: Optional
return model.get_languages(source_lang=source_lang, target_lang=target_lang)



@app.get("/language_detection")
async def language_detection(text: str):
"""
Expand Down
16 changes: 16 additions & 0 deletions docker/build-docker-hub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
#This version build the docker hub containers

VERSION="1.1"

docker build -t easynmt/api:${VERSION}-cpu -f api/cpu.dockerfile api/
docker push easynmt/api:${VERSION}-cpu

docker build -t easynmt/api:${VERSION}-cuda10.1 -f api/cuda10.1.dockerfile api/
docker push easynmt/api:${VERSION}-cuda10.1

docker build -t easynmt/api:${VERSION}-cuda11.0 -f api/cuda11.0.dockerfile api/
docker push easynmt/api:${VERSION}-cuda11.0

docker build -t easynmt/api:${VERSION}-cuda11.1 -f api/cuda11.1.dockerfile api/
docker push easynmt/api:${VERSION}-cuda11.1
File renamed without changes.
Loading

0 comments on commit f55578f

Please sign in to comment.