Gaston895 commited on
Commit
49df561
·
1 Parent(s): 8d3a0aa

fix: remove build-time model download — HF secrets unavailable at build time

Browse files
Files changed (1) hide show
  1. Dockerfile +6 -25
Dockerfile CHANGED
@@ -1,7 +1,5 @@
1
  FROM python:3.11-slim
2
 
3
- # HuggingFace Spaces runs as a non-root user
4
- RUN useradd -m -u 1000 user
5
  WORKDIR /app
6
 
7
  RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -9,42 +7,25 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  # Python deps
12
- COPY --chown=user requirements.txt ./
13
  RUN pip install --no-cache-dir -r requirements.txt
14
 
15
  # App source
16
- COPY --chown=user . .
17
 
18
  # ---------------------------------------------------------------------------
19
- # Pre-download model into the image at build time so startup is instant.
20
- # Pass --build-arg HF_TOKEN=<your_token> to authenticate during build.
21
- # On HuggingFace Spaces, set HF_TOKEN as a Space secret.
22
  # ---------------------------------------------------------------------------
23
- ARG HF_TOKEN=""
24
  ENV MODEL_ID=gsstec/LFM2-700M
25
  ENV HF_HOME=/app/model_cache
26
  ENV TRANSFORMERS_CACHE=/app/model_cache
27
  ENV HF_HUB_DISABLE_PROGRESS_BARS=1
28
 
29
- RUN HF_TOKEN="${HF_TOKEN}" \
30
- HUGGING_FACE_HUB_TOKEN="${HF_TOKEN}" \
31
- python - <<'EOF'
32
- from transformers import AutoModelForCausalLM, AutoTokenizer
33
- import os
34
- model_id = os.environ["MODEL_ID"]
35
- token = os.environ.get("HF_TOKEN") or None
36
- print(f"Pre-downloading {model_id} (authenticated={bool(token)}) ...")
37
- AutoTokenizer.from_pretrained(model_id, token=token, clean_up_tokenization_spaces=False)
38
- AutoModelForCausalLM.from_pretrained(model_id, token=token, device_map="cpu")
39
- print("Done.")
40
- EOF
41
-
42
- RUN chown -R user:user /app/model_cache
43
-
44
  USER user
45
 
46
  EXPOSE 7860
47
 
48
- # python app.py — model loads at module level, then uvicorn starts in the same process
49
- # No forking, no workers — model is guaranteed loaded before first request
50
  CMD ["python", "app.py"]
 
1
  FROM python:3.11-slim
2
 
 
 
3
  WORKDIR /app
4
 
5
  RUN apt-get update && apt-get install -y --no-install-recommends \
 
7
  && rm -rf /var/lib/apt/lists/*
8
 
9
  # Python deps
10
+ COPY requirements.txt ./
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
13
  # App source
14
+ COPY . .
15
 
16
  # ---------------------------------------------------------------------------
17
+ # Runtime environment — model is downloaded on first startup using HF_TOKEN
18
+ # secret set in Space settings (Spaces secrets are NOT available at build time)
 
19
  # ---------------------------------------------------------------------------
 
20
  ENV MODEL_ID=gsstec/LFM2-700M
21
  ENV HF_HOME=/app/model_cache
22
  ENV TRANSFORMERS_CACHE=/app/model_cache
23
  ENV HF_HUB_DISABLE_PROGRESS_BARS=1
24
 
25
+ # HuggingFace Spaces runs as uid 1000
26
+ RUN useradd -m -u 1000 user && chown -R user /app
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  USER user
28
 
29
  EXPOSE 7860
30
 
 
 
31
  CMD ["python", "app.py"]