suppa09 commited on
Commit
408b819
·
verified ·
1 Parent(s): a3d5353

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +81 -6
Dockerfile CHANGED
@@ -1,21 +1,96 @@
1
- # HuggingFace Spaces Docker SDK — runs on port 7860
 
 
 
 
 
 
 
 
 
 
 
2
  FROM python:3.11-slim
3
 
 
4
  ENV PYTHONUNBUFFERED=1 \
5
  PIP_NO_CACHE_DIR=1 \
 
 
6
  HF_HOME=/tmp/hf \
7
  TRANSFORMERS_CACHE=/tmp/hf \
8
  PORT=7860
9
 
10
- WORKDIR /app
11
-
12
  RUN apt-get update && apt-get install -y --no-install-recommends \
13
- build-essential git curl && rm -rf /var/lib/apt/lists/*
 
14
 
 
 
 
15
  COPY requirements.txt .
16
- RUN pip install --upgrade pip && pip install -r requirements.txt
17
 
 
 
 
 
 
 
 
 
18
  COPY app.py .
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  EXPOSE 7860
21
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
1
+ # PolyBloom ML Ensemble Service v15.0 — Dockerfile
2
+ #
3
+ # CRITICAL CHANGES vs v14.x:
4
+ # 1. MOIRAI weights pre-downloaded at build time (RUN snapshot_download).
5
+ # In v14.x the model downloaded on first request → timeout on free tier.
6
+ # 2. TimesFM + MOMENT weights also pre-downloaded at build time.
7
+ # 3. Health probe CMD added (optional — uncomment if your HF Space supports it).
8
+ # 4. CPU-only torch wheel used (torch+cpu) to cut image size ~1.5 GB.
9
+ #
10
+ # HuggingFace Spaces Docker SDK — runs on port 7860.
11
+ # Base image: python:3.11-slim (matches HF free-tier runtime).
12
+
13
  FROM python:3.11-slim
14
 
15
+ # ── Environment ────────────────────────────────────────────────────────────────
16
  ENV PYTHONUNBUFFERED=1 \
17
  PIP_NO_CACHE_DIR=1 \
18
+ # All HuggingFace model weights go here. This dir is INSIDE the image,
19
+ # so weights are baked in and available at cold-start with no network call.
20
  HF_HOME=/tmp/hf \
21
  TRANSFORMERS_CACHE=/tmp/hf \
22
  PORT=7860
23
 
24
+ # ── System deps ────────────────────────────────────────────────────────────────
 
25
  RUN apt-get update && apt-get install -y --no-install-recommends \
26
+ build-essential git curl && \
27
+ rm -rf /var/lib/apt/lists/*
28
 
29
+ WORKDIR /app
30
+
31
+ # ── Python deps ───────────────────────────────────────────────────────────────
32
  COPY requirements.txt .
 
33
 
34
+ # Install CPU-only torch first (saves ~1.5 GB vs full CUDA wheel).
35
+ # Then install the rest normally. If HF Spaces ships CUDA by default you can
36
+ # remove the --index-url line and just do pip install -r requirements.txt.
37
+ RUN pip install --upgrade pip && \
38
+ pip install torch==2.4.1 --index-url https://download.pytorch.org/whl/cpu && \
39
+ pip install -r requirements.txt
40
+
41
+ # ── App code ──────────────────────────────────────────────────────────────────
42
  COPY app.py .
43
 
44
+ # ── Pre-download ML model weights at BUILD TIME ────────────────────────────────
45
+ #
46
+ # This is the core v15.0 fix. Downloading at build time means:
47
+ # - Cold starts are instant (no network call at first /omega request).
48
+ # - MOIRAI_AVAILABLE is always True on HF free tier (was almost always False
49
+ # in v14.x because the 500 MB download timed out at inference time).
50
+ # - If HF is rate-limiting at build time, rebuild — don't silently fall back.
51
+ #
52
+ # Each download uses local_files_only=False here (build time).
53
+ # get_moirai() in app.py uses local_files_only=True (inference time).
54
+
55
+ # 1. Chronos (Amazon T5-small ~300 MB)
56
+ RUN python -c " \
57
+ from huggingface_hub import snapshot_download; \
58
+ snapshot_download('amazon/chronos-t5-small', cache_dir='/tmp/hf')" || \
59
+ echo "WARNING: Chronos download failed — will fall back to Holt-Winters at runtime"
60
+
61
+ # 2. MOIRAI small (~500 MB)
62
+ # MUST succeed — this is the primary v15.0 fix.
63
+ # If this RUN step fails, rebuild or switch to moirai-1.1-R-tiny.
64
+ RUN python -c " \
65
+ from huggingface_hub import snapshot_download; \
66
+ snapshot_download('Salesforce/moirai-1.1-R-small', cache_dir='/tmp/hf')"
67
+
68
+ # 3. TimesFM 200M (~800 MB)
69
+ RUN python -c " \
70
+ from huggingface_hub import snapshot_download; \
71
+ snapshot_download('google/timesfm-1.0-200m', cache_dir='/tmp/hf')" || \
72
+ echo "WARNING: TimesFM download failed — will fall back to Holt-Winters at runtime"
73
+
74
+ # 4. MOMENT-1-large (~1.4 GB)
75
+ # This is the largest model. On HF free-tier builder it may timeout.
76
+ # If it does, switch to 'AutonLab/MOMENT-1-small' (~400 MB) and update
77
+ # MOMENT_MODEL env var accordingly.
78
+ RUN python -c " \
79
+ from huggingface_hub import snapshot_download; \
80
+ snapshot_download('AutonLab/MOMENT-1-large', cache_dir='/tmp/hf')" || \
81
+ echo "WARNING: MOMENT download failed — will fall back to PatchTST-formula at runtime"
82
+
83
+ # ── Optional: LightGBM production model ───────────────────────────────────────
84
+ # If you have a trained lgbm_model.pkl, copy it here.
85
+ # Without it, app.py fits a bootstrap model at startup (see get_lgbm() in app.py).
86
+ # COPY lgbm_model.pkl /app/lgbm_model.pkl
87
+
88
+ # ── Expose + start ─────────────────────────────────────────────────────────────
89
  EXPOSE 7860
90
+
91
+ # Health check (optional — HF Spaces may not support HEALTHCHECK, but it's
92
+ # good practice and works in standard Docker deployments).
93
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
94
+ CMD curl -f http://localhost:7860/ || exit 1
95
+
96
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]