File size: 1,999 Bytes
c1b96f8
 
 
 
9b22a02
c1b96f8
 
9b22a02
c1b96f8
9b22a02
c1b96f8
 
9b22a02
 
c1b96f8
 
 
 
9b22a02
c1b96f8
 
9b22a02
c1b96f8
 
 
 
9b22a02
c1b96f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b22a02
c1b96f8
 
 
 
 
 
 
 
 
 
 
 
 
 
9b22a02
c1b96f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ============================================================================
# Stage 1: Build llama-cpp-python with CPU optimizations
# ============================================================================
FROM python:3.11-slim AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    cmake \
    git \
    libopenblas-dev \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Build llama-cpp-python with OpenBLAS + AVX2
# This is the key advantage over the Gradio wheel — compiled for this CPU
ENV CMAKE_ARGS="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS -DGGML_AVX2=ON -DGGML_FMA=ON"
ENV FORCE_CMAKE=1

RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir llama-cpp-python==0.3.4

# ============================================================================
# Stage 2: Runtime image (lean)
# ============================================================================
FROM python:3.11-slim AS runtime

# Only the runtime lib needed for OpenBLAS
RUN apt-get update && apt-get install -y --no-install-recommends \
    libopenblas0 \
    && rm -rf /var/lib/apt/lists/*

# Copy the compiled llama-cpp-python from builder
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Install remaining Python deps (no llama-cpp here — already copied)
RUN pip install --no-cache-dir \
    fastapi==0.115.0 \
    uvicorn[standard]==0.30.0 \
    huggingface_hub==0.24.0 \
    pydantic==2.8.0

# Create non-root user (HF Spaces requirement)
RUN useradd -m -u 1000 user
USER user

WORKDIR /app

# Copy app code
COPY --chown=user:user app.py .

# HF Spaces persistent storage mounts at /data
# We just make sure the path exists as a fallback if not mounted
RUN mkdir -p /home/user/data/models

EXPOSE 7860

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]