Repository: deepset-ai/haystack
Pull request: Add models to demo docker image
Introduces a small utility to download and cache the demo's model and NLTK data, then modifies Dockerimage and Dockerimage-GPU to call this function at build time.

This will provide users with a demo that does not need to download any model at its first run, making the demo installation process easier and a bit faster too.

Closes #1652 


Problem statement:
Add model to demo docker image
When running our "quick start" via docker-compose, the API container directly triggers a download of the roberta reader model. Depending on your internet connection this might take a while. How about we add a docker image with the tag `demo_latest` that includes the roberta model and therefore doesn't trigger a download when starting the container

Hints from issue discussion:

Commit message: Add utility to cache models and nltk data & modify Dockerfiles to use it

Patch to review:
diff --git a/Dockerfile b/Dockerfile
index d4fab69233..79cca32c11 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -20,9 +20,7 @@ COPY haystack /home/user/haystack
 COPY setup.py requirements.txt README.md /home/user/
 RUN pip install -r requirements.txt
 RUN pip install -e .
-
-# download punkt tokenizer to be included in image
-RUN python3 -c "import nltk;nltk.download('punkt', download_dir='/usr/nltk_data')"
+RUN python3 -c "from haystack.utils.docker import cache_models;cache_models()"
 
 # create folder for /file-upload API endpoint with write permissions, this might be adjusted depending on FILE_UPLOAD_PATH
 RUN mkdir -p /home/user/file-upload
diff --git a/Dockerfile-GPU b/Dockerfile-GPU
index 8a0edbf4ad..4d4926d847 100644
--- a/Dockerfile-GPU
+++ b/Dockerfile-GPU
@@ -43,9 +43,6 @@ RUN echo "Install required packages" && \
     # Install from requirements.txt		
     pip3 install -r requirements.txt
 
-# download punkt tokenizer to be included in image
-RUN python3 -c "import nltk;nltk.download('punkt', download_dir='/usr/nltk_data')"
-
 # copy saved models
 COPY README.md models* /home/user/models/
 
@@ -58,6 +55,9 @@ COPY haystack /home/user/haystack
 # Install package
 RUN pip3 install -e .
 
+# Cache Roberta and NLTK data
+RUN python3 -c "from haystack.utils.docker import cache_models;cache_models()"
+
 # optional : copy sqlite db if needed for testing
 #COPY qa.db /home/user/
 
diff --git a/haystack/utils/docker.py b/haystack/utils/docker.py
new file mode 100644
index 0000000000..6b2e779e94
--- /dev/null
+++ b/haystack/utils/docker.py
@@ -0,0 +1,15 @@
+
+def cache_models():
+    """
+    Small function that caches models and other data.
+    Used only in the Dockerfile to include these caches in the images.
+    """
+    # download punkt tokenizer
+    import nltk
+    nltk.download('punkt', download_dir='/usr/nltk_data')
+    
+    # Cache roberta-base-squad2 model
+    import transformers
+    model_to_cache='deepset/roberta-base-squad2'
+    transformers.AutoTokenizer.from_pretrained(model_to_cache)
+    transformers.AutoModel.from_pretrained(model_to_cache)
\ No newline at end of file


Code review comments:
diff_hunk: @@ -0,0 +1,15 @@
+
+def cache_models():
+    """
+    Small function that caches models and other data.
+    Used only in the Dockerfile to include these caches in the images.
+    """
+    # download punkt tokenizer
+    import nltk
+    nltk.download('punkt', download_dir='/usr/nltk_data')
path: haystack/utils/docker.py
text: @user1:
I am seeing some surprising messages in my log file when running a container (both with your PR or on master branch). It tries to download nltk tokenizers punkt although it should not:
```
[nltk_data] Downloading package punkt to /root/nltk_data...

[nltk_data]     Unzipping tokenizers/punkt.zip.
```
I understand that your code did not really change that behavior compared to master but maybe you could still quickly check whether the code tries to download nltk despite we want it to be cached? I could imagine it's because of the user and the different path names `'/usr/nltk_data'` and `'/root/nltk_data'`