File size: 2,791 Bytes
200cb0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Iris on Hugging Face Spaces (Docker): self-contained llama.cpp + Gradio.
# The small MiniCPM GGUF is baked into the image so the model runs entirely
# inside the Space at runtime (no external model API).
#
# Build args let you swap the model without code changes. Defaults target the
# Tiny Titan badge (<=4B params) with reliable upstream llama.cpp support.
ARG GGUF_REPO=openbmb/MiniCPM3-4B-GGUF
ARG GGUF_FILE=minicpm3-4b-q4_k_m.gguf
ARG LLAMA_CPP_TAG=b9616
ARG LLAMA_CPP_ARCHIVE=llama-b9616-bin-ubuntu-x64.tar.gz

# ---------------------------------------------------------------------------
# Stage 1: fetch llama.cpp's prebuilt Ubuntu CPU server binary.
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS llama-bin
ARG LLAMA_CPP_TAG
ARG LLAMA_CPP_ARCHIVE

RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates curl tar \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /opt/llama.cpp \
    && curl -L --fail \
        "https://github.com/ggml-org/llama.cpp/releases/download/${LLAMA_CPP_TAG}/${LLAMA_CPP_ARCHIVE}" \
        | tar -xz --strip-components=1 -C /opt/llama.cpp \
    && test -x /opt/llama.cpp/llama-server

# ---------------------------------------------------------------------------
# Stage 2: download the GGUF once, at build time, into the image.
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS model-fetch
ARG GGUF_REPO
ARG GGUF_FILE

RUN pip install --no-cache-dir "huggingface_hub>=0.23"
RUN python3 -c "from huggingface_hub import hf_hub_download; \
hf_hub_download(repo_id='${GGUF_REPO}', filename='${GGUF_FILE}', local_dir='/models')"

# ---------------------------------------------------------------------------
# Stage 3: runtime image.
# ---------------------------------------------------------------------------
FROM python:3.11-slim
ARG GGUF_FILE

# libcurl4 / libgomp1 are needed by the llama-server binary at runtime.
RUN apt-get update && apt-get install -y --no-install-recommends \
        libcurl4 libgomp1 curl \
    && rm -rf /var/lib/apt/lists/*

# HF Spaces run the container as uid 1000.
RUN useradd -m -u 1000 user
ENV HOME=/home/user \
    PATH=/opt/llama.cpp:/home/user/.local/bin:$PATH \
    LD_LIBRARY_PATH=/opt/llama.cpp \
    PYTHONUNBUFFERED=1 \
    IRIS_MODEL_PATH=/models/${GGUF_FILE}

COPY --from=llama-bin /opt/llama.cpp /opt/llama.cpp
COPY --from=model-fetch /models /models

WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . /app
RUN python scripts/patch_gradio_templates.py \
    && chmod +x /app/scripts/space_entrypoint.sh \
    && chown -R user:user /app /models

USER user
EXPOSE 7860
ENTRYPOINT ["/app/scripts/space_entrypoint.sh"]