digifreely commited on
Commit
d7182a3
Β·
verified Β·
1 Parent(s): b2e0eb6

Upload Dockerfile

Browse files
Files changed (1) hide show
  1. 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"]