Spaces:
Sleeping
Sleeping
Commit
·
f9eef45
1
Parent(s):
c7408dc
Update docker dir structure
Browse files- Dockerfile +5 -2
- app.py +6 -5
Dockerfile
CHANGED
@@ -24,8 +24,11 @@ RUN mkdir -p $SENTENCE_TRANSFORMERS_HOME && \
|
|
24 |
|
25 |
# Download and persist the model properly (USE snapshot_download)
|
26 |
RUN python -c "from huggingface_hub import snapshot_download; \
|
27 |
-
snapshot_download(repo_id='sentence-transformers/all-MiniLM-L6-v2', cache_dir='/app/model_cache')
|
28 |
-
|
|
|
|
|
|
|
29 |
# Ensure the model files are available at runtime (list out)
|
30 |
RUN ls -l /app/model_cache && cat /app/model_cache/config.json
|
31 |
|
|
|
24 |
|
25 |
# Download and persist the model properly (USE snapshot_download)
|
26 |
RUN python -c "from huggingface_hub import snapshot_download; \
|
27 |
+
model_path = snapshot_download(repo_id='sentence-transformers/all-MiniLM-L6-v2', cache_dir='/app/model_cache'); \
|
28 |
+
import shutil, os; \
|
29 |
+
latest_model_dir = os.path.join(model_path, 'snapshots', os.listdir(os.path.join(model_path, 'snapshots'))[0]); \
|
30 |
+
shutil.move(latest_model_dir, '/app/model_cache/model')"
|
31 |
+
|
32 |
# Ensure the model files are available at runtime (list out)
|
33 |
RUN ls -l /app/model_cache && cat /app/model_cache/config.json
|
34 |
|
app.py
CHANGED
@@ -61,18 +61,19 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
61 |
# 2b) Setup Hugging Face Cloud project model cache
|
62 |
hf_cache_dir = "/home/user/.cache/huggingface"
|
63 |
# Model storage location
|
64 |
-
|
|
|
65 |
os.environ["HF_HOME"] = hf_cache_dir
|
66 |
os.environ["SENTENCE_TRANSFORMERS_HOME"] = hf_cache_dir
|
67 |
# 3. Download (or load from cache) the SentenceTransformer model
|
68 |
from huggingface_hub import snapshot_download
|
69 |
print("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
|
70 |
# st.write("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
|
71 |
-
# First, try loading from our copied cache
|
72 |
-
if os.path.exists(
|
73 |
print(f"✅ Found cached model at {model_cache_dir}")
|
74 |
model_loc = model_cache_dir
|
75 |
-
# Else, try loading backup from snapshot_download
|
76 |
else:
|
77 |
print(f"❌ Model not found in {model_cache_dir}. This should not happen!")
|
78 |
print("⚠️ Retrying with snapshot_download...")
|
@@ -81,7 +82,7 @@ else:
|
|
81 |
cache_dir=hf_cache_dir,
|
82 |
local_files_only=True # Change to `False` for fallback to online download
|
83 |
)
|
84 |
-
|
85 |
from sentence_transformers import SentenceTransformer
|
86 |
print("📥 **Loading Embedding Model...**")
|
87 |
# st.write("📥 **Loading Embedding Model...**")
|
|
|
61 |
# 2b) Setup Hugging Face Cloud project model cache
|
62 |
hf_cache_dir = "/home/user/.cache/huggingface"
|
63 |
# Model storage location
|
64 |
+
hf_cache_dir = "/home/user/.cache/huggingface"
|
65 |
+
model_cache_dir = "/app/model_cache/model"
|
66 |
os.environ["HF_HOME"] = hf_cache_dir
|
67 |
os.environ["SENTENCE_TRANSFORMERS_HOME"] = hf_cache_dir
|
68 |
# 3. Download (or load from cache) the SentenceTransformer model
|
69 |
from huggingface_hub import snapshot_download
|
70 |
print("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
|
71 |
# st.write("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
|
72 |
+
# 4a) First, try loading from our copied cache
|
73 |
+
if os.path.exists(os.path.join(model_cache_dir, "config.json")):
|
74 |
print(f"✅ Found cached model at {model_cache_dir}")
|
75 |
model_loc = model_cache_dir
|
76 |
+
# 4b) Else, try loading backup from snapshot_download
|
77 |
else:
|
78 |
print(f"❌ Model not found in {model_cache_dir}. This should not happen!")
|
79 |
print("⚠️ Retrying with snapshot_download...")
|
|
|
82 |
cache_dir=hf_cache_dir,
|
83 |
local_files_only=True # Change to `False` for fallback to online download
|
84 |
)
|
85 |
+
# 5. Load the model to application
|
86 |
from sentence_transformers import SentenceTransformer
|
87 |
print("📥 **Loading Embedding Model...**")
|
88 |
# st.write("📥 **Loading Embedding Model...**")
|