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

3 layers for model loading

Browse files
Files changed (2) hide show
  1. Dockerfile +7 -0
  2. app.py +22 -15
Dockerfile CHANGED
@@ -18,10 +18,17 @@ RUN pip install --no-cache-dir -r requirements.txt
18
  ENV HF_HOME="/home/user/.cache/huggingface"
19
  ENV SENTENCE_TRANSFORMERS_HOME="/home/user/.cache/huggingface/sentence-transformers"
20
 
 
 
 
 
21
  # Download and persist SentenceTransformer model during build stage
22
  RUN python -c "from sentence_transformers import SentenceTransformer; \
23
  SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2', cache_folder='/home/user/.cache/huggingface/sentence-transformers')"
24
 
 
 
 
25
  # Ensure ownership and permissions remain intact
26
  RUN chown -R user:user /home/user/.cache/huggingface
27
 
 
18
  ENV HF_HOME="/home/user/.cache/huggingface"
19
  ENV SENTENCE_TRANSFORMERS_HOME="/home/user/.cache/huggingface/sentence-transformers"
20
 
21
+ # Create cache directories and ensure permissions
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
 
app.py CHANGED
@@ -62,25 +62,32 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
62
  hf_cache_dir = "/home/user/.cache/huggingface"
63
  os.environ["HF_HOME"] = hf_cache_dir
64
  os.environ["SENTENCE_TRANSFORMERS_HOME"] = hf_cache_dir
 
 
65
  # 3. Download (or load from cache) the SentenceTransformer model
66
  from huggingface_hub import snapshot_download
67
  print("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
68
  # st.write("⏳ Checking or downloading the all-MiniLM-L6-v2 model from huggingface_hub...")
69
- try:
70
- model_loc = snapshot_download(
71
- repo_id="sentence-transformers/all-MiniLM-L6-v2",
72
- cache_dir=hf_cache_dir, # Set directly also fine `os.environ["HF_HOME"]`
73
- local_files_only=True # 🚨 Avoids re-downloading / fetch from internet (set False for local dev)
74
- )
75
- except Exception as e:
76
- print(f"❌ Error loading model from cache: {e}")
77
- print("⚠️ Retrying with online download enabled...")
78
- model_loc = snapshot_download(
79
- repo_id="sentence-transformers/all-MiniLM-L6-v2",
80
- cache_dir=hf_cache_dir,
81
- local_files_only=False # ⬇️ Fallback
82
- )
83
- print(f"✅ Model directory: {model_loc}")
 
 
 
 
 
84
  # st.write(f"✅ Model directory: {model_loc}")
85
 
86
  from sentence_transformers import SentenceTransformer
 
62
  hf_cache_dir = "/home/user/.cache/huggingface"
63
  os.environ["HF_HOME"] = hf_cache_dir
64
  os.environ["SENTENCE_TRANSFORMERS_HOME"] = hf_cache_dir
65
+ # Model storage location
66
+ model_cache_dir = "/app/model_cache"
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
+ 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:
83
+ print(f"❌ Error loading model from cache: {e}")
84
+ print("⚠️ Retrying with online download enabled...")
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