Spaces:
Sleeping
Sleeping
Upload Dockerfile
Browse files- Dockerfile +85 -0
Dockerfile
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 2 |
+
# Dockerfile β Children's Learning Router Service
|
| 3 |
+
# Target: Hugging Face Spaces (CPU-only, Docker SDK)
|
| 4 |
+
# Port: 7860 (required by HF Spaces)
|
| 5 |
+
#
|
| 6 |
+
# Model delivery: via `preload_from_hub` in README.md
|
| 7 |
+
# HF Spaces downloads Qwen/Qwen2.5-1.5B-Instruct before container start
|
| 8 |
+
# and places it under /repo-cache (HF_HOME=/repo-cache).
|
| 9 |
+
# No in-build download is needed or possible (build env has no internet).
|
| 10 |
+
#
|
| 11 |
+
# OOM mitigation: packages are installed in small isolated groups so pip's
|
| 12 |
+
# dependency resolver never spikes RAM. --no-cache-dir and --no-compile
|
| 13 |
+
# keep peak memory low throughout the build.
|
| 14 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
|
| 16 |
+
FROM python:3.10-slim
|
| 17 |
+
|
| 18 |
+
# ββ System packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 20 |
+
build-essential \
|
| 21 |
+
git \
|
| 22 |
+
curl \
|
| 23 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 24 |
+
|
| 25 |
+
# ββ Working directory βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
+
WORKDIR /app
|
| 27 |
+
|
| 28 |
+
# ββ Pip hygiene: upgrade pip/wheel first (small, fast) βββββββββββββββββββ
|
| 29 |
+
RUN pip install --no-cache-dir --no-compile --upgrade pip wheel
|
| 30 |
+
|
| 31 |
+
# ββ 1 of 5 Β· CPU-only PyTorch (largest wheel β install alone) ββββββββββββ
|
| 32 |
+
RUN pip install --no-cache-dir --no-compile \
|
| 33 |
+
torch==2.3.1 \
|
| 34 |
+
--index-url https://download.pytorch.org/whl/cpu
|
| 35 |
+
|
| 36 |
+
# ββ 2 of 5 Β· HuggingFace stack (transformers pulls in tokenizers etc.) βββ
|
| 37 |
+
RUN pip install --no-cache-dir --no-compile \
|
| 38 |
+
transformers==4.46.3 \
|
| 39 |
+
accelerate==1.1.1
|
| 40 |
+
|
| 41 |
+
# ββ 3 of 5 Β· Serialisation libs ββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
+
RUN pip install --no-cache-dir --no-compile \
|
| 43 |
+
sentencepiece==0.2.0 \
|
| 44 |
+
protobuf==5.28.3
|
| 45 |
+
|
| 46 |
+
# ββ 4 of 5 Β· Async HTTP client βββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
RUN pip install --no-cache-dir --no-compile \
|
| 48 |
+
httpx==0.27.2
|
| 49 |
+
|
| 50 |
+
# ββ 5 of 5 Β· Web framework + ASGI server βββββββββββββββββββββββββββββββββ
|
| 51 |
+
RUN pip install --no-cache-dir --no-compile \
|
| 52 |
+
fastapi==0.115.0 \
|
| 53 |
+
gunicorn==22.0.0 \
|
| 54 |
+
uvicorn[standard]==0.30.6
|
| 55 |
+
|
| 56 |
+
# ββ Application code ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
COPY app.py .
|
| 58 |
+
|
| 59 |
+
# ββ HuggingFace Spaces: run as non-root user (UID 1000) ββββββββββββββββββ
|
| 60 |
+
# mkdir -p /repo-cache/hub ensures the cache path exists and is writable
|
| 61 |
+
# by hfuser whether HF Spaces pre-populates it or the model downloads fresh.
|
| 62 |
+
RUN useradd -m -u 1000 hfuser \
|
| 63 |
+
&& mkdir -p /repo-cache/hub \
|
| 64 |
+
&& chown -R hfuser:hfuser /app /repo-cache
|
| 65 |
+
USER hfuser
|
| 66 |
+
|
| 67 |
+
# ββ Runtime config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
+
# HF Spaces sets HF_HOME=/repo-cache and places preload_from_hub models
|
| 69 |
+
# there before the container starts. HF_HOME alone is sufficient;
|
| 70 |
+
# TRANSFORMERS_CACHE is deprecated since transformers v4 and removed in v5.
|
| 71 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 72 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 73 |
+
HF_HOME=/repo-cache
|
| 74 |
+
|
| 75 |
+
EXPOSE 7860
|
| 76 |
+
|
| 77 |
+
# ββ Start-up command ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
+
# Gunicorn + uvicorn worker serves the FastAPI app.
|
| 79 |
+
# --preload ensures the model is loaded ONCE before workers fork.
|
| 80 |
+
CMD ["gunicorn", "app:app", \
|
| 81 |
+
"--worker-class", "uvicorn.workers.UvicornWorker", \
|
| 82 |
+
"--workers", "1", \
|
| 83 |
+
"--bind", "0.0.0.0:7860", \
|
| 84 |
+
"--timeout", "300", \
|
| 85 |
+
"--preload"]
|