Skip to content

Commit 67491ac

Browse files
committed
Dockerfile refacto: split requirements and source code layers
To optimize build time and enhance layer reuse Signed-off-by: Raphael Glon <[email protected]>
1 parent 0818705 commit 67491ac

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

dockerfiles/pytorch/Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ RUN apt-get update && \
3232
&& apt-get clean autoremove --yes \
3333
&& rm -rf /var/lib/{apt,dpkg,cache,log}
3434

35-
# Copying only necessary files as filtered by .dockerignore
36-
COPY . .
35+
RUN mkdir -p /var/lib/dpkg && touch /var/lib/dpkg/status
3736

3837
# Set Python 3.11 as the default python version
3938
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \
@@ -47,13 +46,20 @@ RUN curl -O https://bootstrap.pypa.io/get-pip.py && \
4746
# Upgrade pip
4847
RUN pip install --no-cache-dir --upgrade pip
4948

49+
COPY requirements.txt .
50+
RUN pip install -r requirements.txt
51+
52+
# Copying only necessary files as filtered by .dockerignore
53+
COPY . .
5054
# Install wheel and setuptools
5155
RUN pip install --no-cache-dir --upgrade pip ".[torch,st,diffusers]"
5256

5357
# Copy application
5458
COPY src/huggingface_inference_toolkit huggingface_inference_toolkit
5559
COPY src/huggingface_inference_toolkit/webservice_starlette.py webservice_starlette.py
5660

61+
RUN rm -rf src/
62+
5763
# Copy entrypoint and change permissions
5864
COPY --chmod=0755 scripts/entrypoint.sh entrypoint.sh
5965

setup.py

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
from __future__ import absolute_import
2-
2+
import os
33
from setuptools import find_packages, setup
44

5+
lib_folder = os.path.dirname(os.path.realpath(__file__))
6+
requirements_path = f"{lib_folder}/requirements.txt"
7+
install_requires = [] # Here we'll add: ["gunicorn", "docutils>=0.3", "lxml==0.5a7"]
8+
if os.path.isfile(requirements_path):
9+
with open(requirements_path) as f:
10+
install_requires = f.read().splitlines()
11+
12+
test_requirements_path = f"{lib_folder}/test-requirements.txt"
13+
if os.path.isfile(test_requirements_path):
14+
with open(test_requirements_path) as f:
15+
test_requirements = f.read().splitlines()
16+
517
# We don't declare our dependency on transformers here because we build with
618
# different packages for different variants
719

@@ -12,49 +24,14 @@
1224
# ffmpeg: ffmpeg is required for audio processing. On Ubuntu it can be installed as follows: apt install ffmpeg
1325
# libavcodec-extra : libavcodec-extra includes additional codecs for ffmpeg
1426

15-
install_requires = [
16-
# Due to an error affecting kenlm and cmake (see https://github.com/kpu/kenlm/pull/464)
17-
# Also see the transformers patch for it https://github.com/huggingface/transformers/pull/37091
18-
"kenlm@git+https://github.com/kpu/kenlm@ba83eafdce6553addd885ed3da461bb0d60f8df7",
19-
"transformers[sklearn,sentencepiece,audio,vision]==4.51.3",
20-
"huggingface_hub[hf_transfer,hf_xet]==0.31.1",
21-
# vision
22-
"Pillow",
23-
"librosa",
24-
# speech + torchaudio
25-
"pyctcdecode>=0.3.0",
26-
"phonemizer",
27-
"ffmpeg",
28-
# web api
29-
"starlette",
30-
"uvicorn",
31-
"gunicorn",
32-
"pandas",
33-
"orjson",
34-
"einops",
35-
"timm",
36-
]
37-
3827
extras = {}
39-
4028
extras["st"] = ["sentence_transformers==4.0.2"]
4129
extras["diffusers"] = ["diffusers==0.33.1", "accelerate==1.6.0"]
4230
# Includes `peft` as PEFT requires `torch` so having `peft` as a core dependency
4331
# means that `torch` will be installed even if the `torch` extra is not specified.
4432
extras["torch"] = ["torch==2.5.1", "torchvision", "torchaudio", "peft==0.15.1"]
45-
extras["test"] = [
46-
"pytest==7.2.1",
47-
"pytest-xdist",
48-
"parameterized",
49-
"psutil",
50-
"datasets",
51-
"pytest-sugar",
52-
"mock==2.0.0",
53-
"docker",
54-
"requests",
55-
"tenacity",
56-
]
5733
extras["quality"] = ["isort", "ruff"]
34+
extras["test"] = test_requirements
5835
extras["inf2"] = ["optimum-neuron"]
5936
extras["google"] = ["google-cloud-storage", "crcmod==1.7"]
6037

test-requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
isort
2+
ruff
3+
datasets
4+
docker
5+
mock==2.0.0
6+
parameterized
7+
psutil
8+
pytest-sugar
9+
pytest-xdist
10+
pytest==7.2.1
11+
requests
12+
tenacity

0 commit comments

Comments
 (0)