Spaces:
Running
Running
Commit
·
0fd155c
1
Parent(s):
7d5387e
Changed Dockerfile to multi-stage build to further reduce size
Browse files- Dockerfile +40 -52
Dockerfile
CHANGED
|
@@ -1,80 +1,68 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
FROM python:3.11.9-slim-bookworm
|
| 4 |
|
| 5 |
# Install Lambda web adapter in case you want to run with with an AWS Lamba function URL (not essential if not using Lambda)
|
| 6 |
#COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.3 /lambda-adapter /opt/extensions/lambda-adapter
|
| 7 |
|
| 8 |
-
# Install
|
| 9 |
-
RUN apt-get update && apt-get install -y \
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#wget \
|
| 13 |
-
#curl \
|
| 14 |
|
| 15 |
-
# Create
|
| 16 |
-
RUN mkdir
|
| 17 |
|
| 18 |
WORKDIR /src
|
| 19 |
|
|
|
|
| 20 |
COPY requirements_aws.txt .
|
| 21 |
-
|
| 22 |
RUN pip install --no-cache-dir -r requirements_aws.txt
|
| 23 |
|
| 24 |
-
# Gradio
|
| 25 |
RUN pip install --no-cache-dir gradio==4.41.0
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
RUN
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
&&
|
| 39 |
-
&& mkdir -p /home/user/app/cache && chown -R user:user /home/user/app/cache
|
| 40 |
|
| 41 |
# Download the quantised phi model directly with curl. Changed at it is so big - not loaded
|
| 42 |
#RUN curl -L -o /home/user/app/model/rep/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf https://huggingface.co/bartowski/Phi-3.1-mini-128k-instruct-GGUF/tree/main/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#RUN git lfs install
|
| 48 |
-
#RUN git clone https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1 /home/user/app/model/embed
|
| 49 |
-
#RUN rm -rf /home/user/app/model/embed/.git
|
| 50 |
-
|
| 51 |
-
# Download the embedding model - Create a directory for the model and download specific files using huggingface_hub
|
| 52 |
-
COPY download_model.py /src/download_model.py
|
| 53 |
-
RUN python /src/download_model.py
|
| 54 |
|
| 55 |
-
# Switch to the
|
| 56 |
USER user
|
| 57 |
|
| 58 |
-
# Set
|
| 59 |
ENV HOME=/home/user \
|
| 60 |
-
|
| 61 |
PYTHONPATH=/home/user/app \
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# Set
|
| 75 |
WORKDIR $HOME/app
|
| 76 |
-
|
| 77 |
-
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 78 |
COPY --chown=user . $HOME/app
|
| 79 |
|
|
|
|
| 80 |
CMD ["python", "app.py"]
|
|
|
|
| 1 |
+
# Stage 1: Build dependencies and download models
|
| 2 |
+
FROM python:3.11.9-slim-bookworm AS builder
|
|
|
|
| 3 |
|
| 4 |
# Install Lambda web adapter in case you want to run with with an AWS Lamba function URL (not essential if not using Lambda)
|
| 5 |
#COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.3 /lambda-adapter /opt/extensions/lambda-adapter
|
| 6 |
|
| 7 |
+
# Install system dependencies
|
| 8 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Create directories (if needed for model download script)
|
| 12 |
+
RUN mkdir -p /model/rep /model/embed
|
| 13 |
|
| 14 |
WORKDIR /src
|
| 15 |
|
| 16 |
+
# Copy requirements file and install dependencies
|
| 17 |
COPY requirements_aws.txt .
|
|
|
|
| 18 |
RUN pip install --no-cache-dir -r requirements_aws.txt
|
| 19 |
|
| 20 |
+
# Install Gradio
|
| 21 |
RUN pip install --no-cache-dir gradio==4.41.0
|
| 22 |
|
| 23 |
+
# Download models (using your download_model.py script)
|
| 24 |
+
COPY download_model.py /src/download_model.py
|
| 25 |
+
RUN python /src/download_model.py
|
| 26 |
|
| 27 |
+
# Stage 2: Final runtime image
|
| 28 |
+
FROM python:3.11.9-slim-bookworm
|
| 29 |
|
| 30 |
+
# Create a non-root user
|
| 31 |
+
RUN useradd -m -u 1000 user
|
| 32 |
+
|
| 33 |
+
# Create necessary directories and set ownership
|
| 34 |
+
RUN mkdir -p /home/user/app/output /home/user/.cache/huggingface/hub /home/user/.cache/matplotlib /home/user/app/cache \
|
| 35 |
+
&& chown -R user:user /home/user
|
|
|
|
| 36 |
|
| 37 |
# Download the quantised phi model directly with curl. Changed at it is so big - not loaded
|
| 38 |
#RUN curl -L -o /home/user/app/model/rep/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf https://huggingface.co/bartowski/Phi-3.1-mini-128k-instruct-GGUF/tree/main/Phi-3.1-mini-128k-instruct-Q4_K_M.gguf
|
| 39 |
|
| 40 |
+
# Copy models from the builder stage
|
| 41 |
+
COPY --from=builder /model/rep /home/user/app/model/rep
|
| 42 |
+
COPY --from=builder /model/embed /home/user/app/model/embed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Switch to the non-root user
|
| 45 |
USER user
|
| 46 |
|
| 47 |
+
# Set environment variables
|
| 48 |
ENV HOME=/home/user \
|
| 49 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 50 |
PYTHONPATH=/home/user/app \
|
| 51 |
+
PYTHONUNBUFFERED=1 \
|
| 52 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 53 |
+
GRADIO_ALLOW_FLAGGING=never \
|
| 54 |
+
GRADIO_NUM_PORTS=1 \
|
| 55 |
+
GRADIO_SERVER_NAME=0.0.0.0 \
|
| 56 |
+
GRADIO_SERVER_PORT=7860 \
|
| 57 |
+
GRADIO_THEME=huggingface \
|
| 58 |
+
AWS_STS_REGIONAL_ENDPOINT=regional \
|
| 59 |
+
GRADIO_OUTPUT_FOLDER='output/' \
|
| 60 |
+
NUMBA_CACHE_DIR=/home/user/app/cache \
|
| 61 |
+
SYSTEM=spaces
|
| 62 |
+
|
| 63 |
+
# Set working directory and copy application code
|
| 64 |
WORKDIR $HOME/app
|
|
|
|
|
|
|
| 65 |
COPY --chown=user . $HOME/app
|
| 66 |
|
| 67 |
+
# Command to run your application
|
| 68 |
CMD ["python", "app.py"]
|