Spaces:
Sleeping
Sleeping
fix: optimize backend Dockerfile with multi-stage build and pre-built wheels
Browse files- Dockerfile +35 -20
- requirements.txt +1 -1
Dockerfile
CHANGED
|
@@ -1,35 +1,50 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
# Set the working directory to /app
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
COPY requirements.txt .
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
|
| 19 |
-
# Copy the
|
| 20 |
COPY . .
|
| 21 |
|
| 22 |
-
# Create a non-root user
|
| 23 |
RUN useradd -m -u 1000 user
|
| 24 |
USER user
|
| 25 |
-
ENV HOME=/home/user
|
| 26 |
-
PATH=/home/user/.local/bin:$PATH
|
| 27 |
|
| 28 |
-
# Expose
|
| 29 |
EXPOSE 7860
|
| 30 |
|
| 31 |
-
#
|
| 32 |
ENV MODEL_CTX_SIZE=8192
|
| 33 |
|
| 34 |
-
# Run
|
| 35 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
# Stage 1: Builder
|
| 2 |
+
# We use a standard Python image and install uv, which is safer and still incredibly fast.
|
| 3 |
+
FROM python:3.10-slim-bookworm AS builder
|
|
|
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
+
# Install uv (The extremely fast Python package installer)
|
| 7 |
+
RUN pip install uv
|
| 8 |
+
|
| 9 |
+
# Configure uv
|
| 10 |
+
ENV UV_COMPILE_BYTECODE=1
|
| 11 |
+
ENV UV_LINK_MODE=copy
|
| 12 |
+
|
| 13 |
+
# Copy requirements
|
| 14 |
COPY requirements.txt .
|
| 15 |
|
| 16 |
+
# Create a virtual environment and install dependencies using uv
|
| 17 |
+
# We point to the extra index URL for the pre-built llama-cpp-python wheels
|
| 18 |
+
RUN uv venv /app/.venv && \
|
| 19 |
+
uv pip install \
|
| 20 |
+
--no-cache \
|
| 21 |
+
-r requirements.txt \
|
| 22 |
+
--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu \
|
| 23 |
+
--python /app/.venv
|
| 24 |
+
|
| 25 |
+
# Stage 2: Final Runtime Image
|
| 26 |
+
FROM python:3.10-slim-bookworm
|
| 27 |
+
WORKDIR /app
|
| 28 |
+
|
| 29 |
+
# Copy the virtual environment from the builder stage
|
| 30 |
+
COPY --from=builder /app/.venv /app/.venv
|
| 31 |
|
| 32 |
+
# Set environment variables to use the virtual environment automatically
|
| 33 |
+
ENV PATH="/app/.venv/bin:$PATH"
|
| 34 |
|
| 35 |
+
# Copy the application code
|
| 36 |
COPY . .
|
| 37 |
|
| 38 |
+
# Create a non-root user for security (Hugging Face Spaces requirement)
|
| 39 |
RUN useradd -m -u 1000 user
|
| 40 |
USER user
|
| 41 |
+
ENV HOME=/home/user
|
|
|
|
| 42 |
|
| 43 |
+
# Expose the application port
|
| 44 |
EXPOSE 7860
|
| 45 |
|
| 46 |
+
# Runtime configuration
|
| 47 |
ENV MODEL_CTX_SIZE=8192
|
| 48 |
|
| 49 |
+
# Run the application
|
| 50 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
llama-cpp-python
|
| 4 |
huggingface-hub
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
llama-cpp-python==0.3.2
|
| 4 |
huggingface-hub
|