minhthien commited on
Commit
24d38de
·
1 Parent(s): e508595

fix: pre-download GGUF and embedding models at build time to eliminate cold-start delay

Browse files
Files changed (2) hide show
  1. Dockerfile +14 -12
  2. src/utils.py +8 -3
Dockerfile CHANGED
@@ -8,26 +8,28 @@ RUN apt-get update && apt-get install -y \
8
 
9
  WORKDIR /app
10
 
11
- # Set at build time so pip uses them when compiling llama-cpp-python
12
  ENV CMAKE_ARGS="-DGGML_BLAS=OFF -DGGML_METAL=OFF -DGGML_OPENMP=OFF -DGGML_NATIVE=OFF -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_SERVER=OFF"
13
  ENV CMAKE_BUILD_PARALLEL_LEVEL=4
 
14
 
15
  COPY requirements.txt .
16
  RUN pip install --no-cache-dir -r requirements.txt
17
 
18
  COPY . .
19
 
20
- # Pre-download the GGUF model at build time so cold starts don't fetch ~2.5 GB at runtime
21
- RUN python - <<'EOF'
22
- from huggingface_hub import hf_hub_download
23
- hf_hub_download(
24
- repo_id="Jackrong/Qwen3.5-4B-Neo-GGUF",
25
- filename="Qwen3.5-4B.Q4_K_S.gguf",
26
- cache_dir="./model",
27
- )
28
- EOF
29
-
30
- RUN useradd -m -u 1000 user
 
 
31
  USER user
32
  ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
33
 
 
8
 
9
  WORKDIR /app
10
 
 
11
  ENV CMAKE_ARGS="-DGGML_BLAS=OFF -DGGML_METAL=OFF -DGGML_OPENMP=OFF -DGGML_NATIVE=OFF -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_SERVER=OFF"
12
  ENV CMAKE_BUILD_PARALLEL_LEVEL=4
13
+ ENV HF_HUB_CACHE=/app/model
14
 
15
  COPY requirements.txt .
16
  RUN pip install --no-cache-dir -r requirements.txt
17
 
18
  COPY . .
19
 
20
+ # Pre-download GGUF model (~2.5 GB) at build time
21
+ RUN python -c "\
22
+ from huggingface_hub import hf_hub_download; \
23
+ hf_hub_download(repo_id='Jackrong/Qwen3.5-4B-Neo-GGUF', filename='Qwen3.5-4B.Q4_K_S.gguf', cache_dir='/app/model')"
24
+
25
+ # Pre-download embedding model at build time
26
+ RUN python -c "\
27
+ from huggingface_hub import snapshot_download; \
28
+ snapshot_download('BAAI/bge-small-en-v1.5')"
29
+
30
+ RUN useradd -m -u 1000 user \
31
+ && chown -R 1000:1000 /app/model
32
+
33
  USER user
34
  ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
35
 
src/utils.py CHANGED
@@ -17,12 +17,17 @@ class UTF8LocalFileSystem(LocalFileSystem):
17
 
18
  def resolve_gguf_model_path(status_callback=None) -> str:
19
  """Return local path to the GGUF model file, downloading from HF Hub if needed."""
20
- direct = Path("model") / MODEL_FILENAME
 
 
 
 
 
21
  if direct.exists():
22
  return str(direct)
23
 
24
  snapshots_root = (
25
- Path("model")
26
  / f"models--{'--'.join(MODEL_REPO_ID.split('/'))}"
27
  / "snapshots"
28
  )
@@ -35,5 +40,5 @@ def resolve_gguf_model_path(status_callback=None) -> str:
35
  return hf_hub_download(
36
  repo_id=MODEL_REPO_ID,
37
  filename=MODEL_FILENAME,
38
- cache_dir="./model",
39
  )
 
17
 
18
  def resolve_gguf_model_path(status_callback=None) -> str:
19
  """Return local path to the GGUF model file, downloading from HF Hub if needed."""
20
+ import os
21
+
22
+ # Respect HF_HUB_CACHE env var (set in Dockerfile to /app/model)
23
+ cache_dir = Path(os.environ.get("HF_HUB_CACHE", "./model"))
24
+
25
+ direct = cache_dir / MODEL_FILENAME
26
  if direct.exists():
27
  return str(direct)
28
 
29
  snapshots_root = (
30
+ cache_dir
31
  / f"models--{'--'.join(MODEL_REPO_ID.split('/'))}"
32
  / "snapshots"
33
  )
 
40
  return hf_hub_download(
41
  repo_id=MODEL_REPO_ID,
42
  filename=MODEL_FILENAME,
43
+ cache_dir=str(cache_dir),
44
  )