Skip to content

Commit 9767220

Browse files
authored
Fix entrypoint.sh and kenlm installation (#108)
* Fix `entrypoint.sh` At the moment if a custom handler is provided via a `HF_MODEL_ID` then the `requirements.txt` are not downloaded nor installed, meaning that the current cannot use custom handlers via Hugging Face Hub model IDs, meaning that this would only work on Inference Endpoints as the model is downloaded in advance, but for other partners that's not the case; meaning that the `entrypoint.sh` should handle these scenarios. * Fix `kenlm` installation issue (more details in comment) * Move `kenlm` patched installation to `setup.py` instead * Bump `VERSION` to 0.5.5 * Fix `entrypoint.sh`
1 parent 828a927 commit 9767220

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

dockerfiles/pytorch/Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ RUN apt-get update && \
1616
apt-get install -y \
1717
build-essential \
1818
bzip2 \
19+
cmake \
1920
curl \
2021
git \
2122
git-lfs \
2223
tar \
2324
gcc \
2425
g++ \
25-
cmake \
2626
libprotobuf-dev \
2727
protobuf-compiler \
2828
python3.11 \
@@ -44,14 +44,17 @@ RUN curl -O https://bootstrap.pypa.io/get-pip.py && \
4444
python get-pip.py && \
4545
rm get-pip.py
4646

47-
# install wheel and setuptools
47+
# Upgrade pip
48+
RUN pip install --no-cache-dir --upgrade pip
49+
50+
# Install wheel and setuptools
4851
RUN pip install --no-cache-dir --upgrade pip ".[torch,st,diffusers]"
4952

50-
# copy application
53+
# Copy application
5154
COPY src/huggingface_inference_toolkit huggingface_inference_toolkit
5255
COPY src/huggingface_inference_toolkit/webservice_starlette.py webservice_starlette.py
5356

54-
# copy entrypoint and change permissions
57+
# Copy entrypoint and change permissions
5558
COPY --chmod=0755 scripts/entrypoint.sh entrypoint.sh
5659

5760
ENTRYPOINT ["bash", "-c", "./entrypoint.sh"]

scripts/entrypoint.sh

100644100755
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
#!/bin/bash
22

3-
# Define the default port
3+
# Set the default port
44
PORT=5000
55

66
# Check if AIP_MODE is set and adjust the port for Vertex AI
77
if [[ ! -z "${AIP_MODE}" ]]; then
88
PORT=${AIP_HTTP_PORT}
99
fi
1010

11-
# Check if HF_MODEL_DIR is set and if not skip installing custom dependencies
11+
# Check that only one of HF_MODEL_ID or HF_MODEL_DIR is provided
12+
if [[ ! -z "${HF_MODEL_ID}" && ! -z "${HF_MODEL_DIR}" ]]; then
13+
echo "Error: Both HF_MODEL_ID and HF_MODEL_DIR are set. Please provide only one."
14+
exit 1
15+
elif [[ -z "${HF_MODEL_ID}" && -z "${HF_MODEL_DIR}" ]]; then
16+
echo "Error: Neither HF_MODEL_ID nor HF_MODEL_DIR is set. Please provide one of them."
17+
exit 1
18+
fi
19+
20+
# If HF_MODEL_ID is provided, download handler.py and requirements.txt if available
21+
if [[ ! -z "${HF_MODEL_ID}" ]]; then
22+
filename=${HF_DEFAULT_PIPELINE_NAME:-handler.py}
23+
revision=${HF_REVISION:-main}
24+
25+
echo "Downloading $filename for model ${HF_MODEL_ID}"
26+
huggingface-cli download ${HF_MODEL_ID} "$filename" --revision "$revision" --local-dir /tmp
27+
28+
# Check if handler.py was downloaded successfully
29+
if [ -f "/tmp/$filename" ]; then
30+
echo "$filename downloaded successfully, checking if there's a requirements.txt file..."
31+
rm /tmp/$filename
32+
33+
# Attempt to download requirements.txt
34+
echo "Downloading requirements.txt for model ${HF_MODEL_ID}"
35+
huggingface-cli download "${HF_MODEL_ID}" requirements.txt --revision "$revision" --local-dir /tmp
36+
37+
# Check if requirements.txt was downloaded successfully
38+
if [ -f "/tmp/requirements.txt" ]; then
39+
echo "requirements.txt downloaded successfully, now installing the dependencies..."
40+
41+
# Install dependencies
42+
pip install -r /tmp/requirements.txt --no-cache-dir
43+
rm /tmp/requirements.txt
44+
else
45+
echo "${HF_MODEL_ID} with revision $revision contains a custom handler at $filename but doesn't contain a requirements.txt file, so skipping downloading and installing extra requirements from it."
46+
fi
47+
else
48+
echo "${HF_MODEL_ID} with revision $revision doesn't contain a $filename file, so skipping download."
49+
fi
50+
fi
51+
52+
# If HF_MODEL_DIR is provided, check for requirements.txt and install dependencies if available
1253
if [[ ! -z "${HF_MODEL_DIR}" ]]; then
1354
# Check if requirements.txt exists and if so install dependencies
1455
if [ -f "${HF_MODEL_DIR}/requirements.txt" ]; then

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
# We don't declare our dependency on transformers here because we build with
66
# different packages for different variants
77

8-
VERSION = "0.5.4"
8+
VERSION = "0.5.5"
99

1010
# Ubuntu packages
1111
# libsndfile1-dev: torchaudio requires the development version of the libsndfile package which can be installed via a system package manager. On Ubuntu it can be installed as follows: apt install libsndfile1-dev
1212
# ffmpeg: ffmpeg is required for audio processing. On Ubuntu it can be installed as follows: apt install ffmpeg
1313
# libavcodec-extra : libavcodec-extra includes additional codecs for ffmpeg
1414

1515
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",
1619
"transformers[sklearn,sentencepiece,audio,vision]==4.48.0",
1720
"huggingface_hub[hf_transfer]==0.27.1",
1821
# vision

0 commit comments

Comments
 (0)