= commited on
Commit Β·
66c6f17
1
Parent(s): cf61583
fix: set HF_HUB_OFFLINE=1 at runtime to prevent model network calls on startup
Browse files- Dockerfile +8 -1
- src/embeddings.py +6 -1
Dockerfile
CHANGED
|
@@ -23,9 +23,9 @@ COPY . .
|
|
| 23 |
|
| 24 |
# Pin HuggingFace cache inside /app so the sentence-transformer model is
|
| 25 |
# downloaded once during `docker build` and baked into the image layer.
|
| 26 |
-
# This eliminates cold-start latency on HuggingFace Spaces restarts.
|
| 27 |
ENV HF_HOME=/app/.hf_cache
|
| 28 |
ENV TRANSFORMERS_CACHE=/app/.hf_cache/transformers
|
|
|
|
| 29 |
|
| 30 |
# Set ownership (includes .hf_cache written by the build step below)
|
| 31 |
RUN chown -R appuser:appuser /app
|
|
@@ -35,6 +35,13 @@ USER appuser
|
|
| 35 |
# Pre-build FAISS index + download the embedding model into /app/.hf_cache
|
| 36 |
RUN python src/build_faiss.py
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Environment defaults (override via HF Space secrets)
|
| 39 |
ENV GROQ_API_KEY_1=""
|
| 40 |
ENV GROQ_API_KEY_2=""
|
|
|
|
| 23 |
|
| 24 |
# Pin HuggingFace cache inside /app so the sentence-transformer model is
|
| 25 |
# downloaded once during `docker build` and baked into the image layer.
|
|
|
|
| 26 |
ENV HF_HOME=/app/.hf_cache
|
| 27 |
ENV TRANSFORMERS_CACHE=/app/.hf_cache/transformers
|
| 28 |
+
ENV SENTENCE_TRANSFORMERS_HOME=/app/.hf_cache/sentence_transformers
|
| 29 |
|
| 30 |
# Set ownership (includes .hf_cache written by the build step below)
|
| 31 |
RUN chown -R appuser:appuser /app
|
|
|
|
| 35 |
# Pre-build FAISS index + download the embedding model into /app/.hf_cache
|
| 36 |
RUN python src/build_faiss.py
|
| 37 |
|
| 38 |
+
# ββ Offline mode ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
# Model is now cached in the image. Tell all HF libraries to NEVER call the
|
| 40 |
+
# network at runtime β prevents "Could not resolve host: huggingface.co" errors.
|
| 41 |
+
ENV TRANSFORMERS_OFFLINE=1
|
| 42 |
+
ENV HF_DATASETS_OFFLINE=1
|
| 43 |
+
ENV HF_HUB_OFFLINE=1
|
| 44 |
+
|
| 45 |
# Environment defaults (override via HF Space secrets)
|
| 46 |
ENV GROQ_API_KEY_1=""
|
| 47 |
ENV GROQ_API_KEY_2=""
|
src/embeddings.py
CHANGED
|
@@ -6,8 +6,13 @@ from numpy.linalg import norm
|
|
| 6 |
class EmbeddingsManager:
|
| 7 |
# Khα»i tαΊ‘o model embedding tα»« cofig ngay khi gα»i class
|
| 8 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
self.embeddings = HuggingFaceEmbeddings(
|
| 10 |
-
model_name=Config.EMBEDDING_MODEL
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
def get_embeddings(self):
|
|
|
|
| 6 |
class EmbeddingsManager:
|
| 7 |
# Khα»i tαΊ‘o model embedding tα»« cofig ngay khi gα»i class
|
| 8 |
def __init__(self):
|
| 9 |
+
import os
|
| 10 |
+
# Use cached model; never call the network (model is baked into Docker image)
|
| 11 |
+
local_only = os.getenv("TRANSFORMERS_OFFLINE", "0") == "1" or \
|
| 12 |
+
os.getenv("HF_HUB_OFFLINE", "0") == "1"
|
| 13 |
self.embeddings = HuggingFaceEmbeddings(
|
| 14 |
+
model_name=Config.EMBEDDING_MODEL,
|
| 15 |
+
model_kwargs={"local_files_only": local_only},
|
| 16 |
)
|
| 17 |
|
| 18 |
def get_embeddings(self):
|