Skip to content

Commit

Permalink
add .envdefault and docker-compose.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Guokan SHANG committed Dec 2, 2021
1 parent 85ea60f commit 9a78670
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .envdefault
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_LANG=fr en
ASSETS_PATH_ON_HOST=./assets
ASSETS_PATH_IN_CONTAINER=/app/assets
WORKER_NUMBER=1
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
assets
assets
.env
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ LABEL maintainer="[email protected]"

WORKDIR /app

VOLUME /app/assets
ENV ASSETS_PATH=/app/assets

COPY ./requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

COPY .envdefault /app/
COPY ./scripts /app/scripts
COPY ./components /app/components

HEALTHCHECK --interval=15s CMD curl -fs http://0.0.0.0/health || exit 1

ENTRYPOINT ["/home/user/miniconda/bin/uvicorn", "scripts.main:app", "--host", "0.0.0.0", "--port", "80"]
ENTRYPOINT ["/home/user/miniconda/bin/gunicorn", "scripts.main:app", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:80", "--access-logfile", "-", "--error-logfile", "-"]
CMD ["--workers", "1"]
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,51 @@ With our proposed stack [https://github.com/linto-ai/linto-platform-stack](https
# Develop

## Build and run
1 Create a named volume for storaging models.
```bash
sudo docker volume create linto-platform-nlp-assets
```

2 Download models into `assets/` on the host machine, make sure that `git-lfs`: [Git Large File Storage](https://git-lfs.github.com/) is installed and availble at `/usr/local/bin/git-lfs`.
1 Download models into `./assets` on the host machine (can be stored in other places), make sure that `git-lfs`: [Git Large File Storage](https://git-lfs.github.com/) is installed and availble at `/usr/local/bin/git-lfs`.
```bash
cd linto-platform-nlp-keyphrase-extraction/
bash scripts/download_models.sh
```

3 Copy downloaded models into created volume `linto-platform-nlp-assets`
2 configure running environment variables
```bash
sudo docker container create --name cp_helper -v linto-platform-nlp-assets:/root hello-world
sudo docker cp assets/* cp_helper:/root
sudo docker rm cp_helper
mv .envdefault .env
# cat .envdefault
# APP_LANG=fr en | Running language of application, "fr en", "fr", etc.
# ASSETS_PATH_ON_HOST=./assets | Storage path of models on host. (only applicable when docker-compose is used)
# ASSETS_PATH_IN_CONTAINER=/app/assets | Volume mount point of models in container. (only applicable when docker-compose is used)
# WORKER_NUMBER=3 | Number of processing workers. (only applicable when docker-compose is used)
```

4 Build image
```bash
sudo docker build --tag lintoai/linto-platform-keyphrase-extraction:latest .
```
or
```bash
sudo docker-compose build
```

5 Run container (with GPU), make sure that [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian) and GPU driver are installed.
5 Run container with GPU support, make sure that [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian) and GPU driver are installed.
```bash
sudo docker run --gpus all \
--rm -d -p 80:80 \
-v linto-platform-nlp-assets:/app/assets:ro \
--env APP_LANG="fr en" \
lintoai/linto-platform-keyphrase-extraction:latest
--rm -p 80:80 \
-v $PWD/assets:/app/assets:ro \
--env-file .env \
lintoai/linto-platform-keyphrase-extraction:latest \
--workers 1
```
or
```bash
sudo docker-compose up
```
<details>
<summary>Check running with CPU only setting</summary>

```bash
sudo docker run \
--rm -d -p 80:80 \
-v linto-platform-nlp-assets:/app/assets:ro \
--env APP_LANG="fr en" \
lintoai/linto-platform-keyphrase-extraction:latest
```
- remove `--gpus all` from the first command.
- remove `runtime: nvidia` from the `docker-compose.yml` file.
</details>
To specify running language of the container, modify APP_LANG="fr en", APP_LANG="fr", etc.

To lanche with multiple workers, add `--workers INTEGER` in the end of the above command.

6 Navigate to `http://localhost/docs` or `http://localhost/redoc` in your browser, to explore the REST API interactively. See the examples for how to query the API.

Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.9"
services:
kpe:
build: .
image: lintoai/linto-platform-keyphrase-extraction:latest
ports:
- "80:80"
env_file:
- .env
volumes:
- $ASSETS_PATH_ON_HOST:$ASSETS_PATH_IN_CONTAINER:ro
command: ["--workers", $WORKER_NUMBER]
runtime: nvidia
7 changes: 6 additions & 1 deletion scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi_health import health
from sentence_transformers import SentenceTransformer
from dotenv import load_dotenv

# To force the GPU usage: spacy.require_gpu()
spacy.prefer_gpu()

# Parse environment variables.
# variable and its value in the .envdefault file will be set, only if the variable is missing or empty in the current enviroment.
load_dotenv(".envdefault", override=False)

# Supported languages and corresponding model names
LM_MAP = {
"fr": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
"en": "sentence-transformers/all-MiniLM-L6-v2"
}

# Load models
MODELS = {LM_MAP[lang]: SentenceTransformer(os.environ.get("ASSETS_PATH") + '/' + LM_MAP[lang]) for lang in os.environ.get("APP_LANG").split(" ")}
MODELS = {LM_MAP[lang]: SentenceTransformer(os.environ.get("ASSETS_PATH_IN_CONTAINER") + '/' + LM_MAP[lang]) for lang in os.environ.get("APP_LANG").split(" ")}
print(f"Loaded {len(MODELS)} models: {MODELS.keys()}")

@spacy.registry.misc("get_model")
Expand Down

0 comments on commit 9a78670

Please sign in to comment.