LiamKhoaLe commited on
Commit
c93d8f0
·
1 Parent(s): e52a14a

Update app caching in Dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +6 -9
  2. app.py +8 -7
Dockerfile CHANGED
@@ -22,18 +22,15 @@ ENV SENTENCE_TRANSFORMERS_HOME="/home/user/.cache/huggingface/sentence-transform
22
  RUN mkdir -p $SENTENCE_TRANSFORMERS_HOME && \
23
  chown -R user:user /home/user/.cache/huggingface
24
 
25
- # Download and persist SentenceTransformer model during build stage
26
- RUN python -c "from sentence_transformers import SentenceTransformer; \
27
- SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', cache_folder='/home/user/.cache/huggingface/sentence-transformers')"
28
-
29
- # Copy model to ensure availability at runtime
30
- RUN cp -r /home/user/.cache/huggingface/sentence-transformers /app/model_cache
31
 
32
  # Ensure ownership and permissions remain intact
33
- RUN chown -R user:user /home/user/.cache/huggingface
34
 
35
- # EXPOSE 7860 (HF automatically maps it)
36
  EXPOSE 7860
37
 
38
- # Run server with uvicorn
39
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
22
  RUN mkdir -p $SENTENCE_TRANSFORMERS_HOME && \
23
  chown -R user:user /home/user/.cache/huggingface
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 ownership and permissions remain intact
30
+ RUN chown -R user:user /app/model_cache
31
 
32
+ # Expose application port
33
  EXPOSE 7860
34
 
35
+ # Run server
36
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -68,15 +68,17 @@ model_cache_dir = "/app/model_cache"
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
- if os.path.exists(model_cache_dir):
 
72
  print(f"✅ Found cached model at {model_cache_dir}")
73
  model_loc = model_cache_dir
74
- else:
 
75
  try:
76
  model_loc = snapshot_download(
77
  repo_id="sentence-transformers/all-MiniLM-L6-v2",
78
- cache_dir=hf_cache_dir, # Set directly also fine `os.environ["HF_HOME"]`
79
- local_files_only=True # 🚨 Avoids re-downloading / fetch from internet (set False for local dev)
80
  )
81
  print(f"✅ Model loaded from local cache: {model_loc}")
82
  except Exception as e:
@@ -85,10 +87,9 @@ else:
85
  model_loc = snapshot_download(
86
  repo_id="sentence-transformers/all-MiniLM-L6-v2",
87
  cache_dir=hf_cache_dir,
88
- local_files_only=False # ⬇️ Fallback
89
  )
90
- print(f"✅ Model directory reloaded: {model_loc}")
91
- # st.write(f"✅ Model directory: {model_loc}")
92
 
93
  from sentence_transformers import SentenceTransformer
94
  print("📥 **Loading Embedding 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(model_cache_dir) and os.path.exists(os.path.join(model_cache_dir, "config.json")):
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
  try:
78
  model_loc = snapshot_download(
79
  repo_id="sentence-transformers/all-MiniLM-L6-v2",
80
+ cache_dir=hf_cache_dir,
81
+ local_files_only=True # Ensure it's loaded from cache
82
  )
83
  print(f"✅ Model loaded from local cache: {model_loc}")
84
  except Exception as e:
 
87
  model_loc = snapshot_download(
88
  repo_id="sentence-transformers/all-MiniLM-L6-v2",
89
  cache_dir=hf_cache_dir,
90
+ local_files_only=False # Fallback to online download
91
  )
92
+ print(f"✅ Model directory after retry: {model_loc}")
 
93
 
94
  from sentence_transformers import SentenceTransformer
95
  print("📥 **Loading Embedding Model...**")