ornith / hf-space-rag /Dockerfile
devarshia5's picture
Upload 12 files
478d756 verified
Raw
History Blame Contribute Delete
2.34 kB
# CPU + FREE-tier RAG Space (Docker SDK).
# Embeddings : BAAI/bge-small-en-v1.5 (fastembed / ONNX, no torch)
# Vector DB : FAISS (in-memory)
# LLM : Qwen2.5-1.5B-Instruct (llama.cpp GGUF, fast on CPU)
# API : OpenAI-compatible -> /v1/chat/completions (+ web UI at /)
#
# Everything is CPU-only. No GPU, no paid hardware required.
# Models are baked into the image so the Space cold-starts instantly.
FROM python:3.11-slim
# build-essential + cmake so llama-cpp-python can compile if no prebuilt wheel
# matches (python:slim has no compiler otherwise -> CMake "gcc not found").
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential cmake git curl ca-certificates && rm -rf /var/lib/apt/lists/*
# Hugging Face Spaces run the container as non-root UID 1000.
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
WORKDIR /home/user/app
# --- Python deps ---
# GGML_NATIVE=OFF -> portable CPU binary (no -march=native), so it can't crash
# with "illegal instruction" if the build CPU != the runtime CPU.
COPY --chown=user requirements.txt .
ENV CMAKE_ARGS="-DGGML_NATIVE=OFF" FORCE_CMAKE=1
RUN pip install --no-cache-dir --user -r requirements.txt
# --- Model config ------------------------------------------------------------
ENV LLM_REPO=Qwen/Qwen2.5-1.5B-Instruct-GGUF \
LLM_FILE=qwen2.5-1.5b-instruct-q4_k_m.gguf \
MODEL_DIR=/home/user/models \
EMBED_MODEL=BAAI/bge-small-en-v1.5 \
FASTEMBED_CACHE=/home/user/.cache/fastembed \
HF_HOME=/home/user/.cache/huggingface
# Bake the LLM (~1 GB) into the image -> instant cold start.
RUN python -c "from huggingface_hub import hf_hub_download; \
hf_hub_download(repo_id='${LLM_REPO}', filename='${LLM_FILE}', local_dir='${MODEL_DIR}')"
# Bake the embedding model (ONNX ~130 MB) into the image too.
RUN python -c "import os; from fastembed import TextEmbedding; \
TextEmbedding(os.environ['EMBED_MODEL'], cache_dir=os.environ['FASTEMBED_CACHE'])"
# --- App ---------------------------------------------------------------------
COPY --chown=user . .
# Runtime knobs (free tier = 2 vCPU).
ENV N_CTX=8192 \
N_THREADS=2 \
TOP_K=4 \
DOCS_DIR=documents
EXPOSE 7860
CMD ["python", "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]