LiamKhoaLe commited on
Commit
67b29cd
·
1 Parent(s): 5813c39

Add download model to a separate file

Browse files
Files changed (3) hide show
  1. Dockerfile +6 -14
  2. app.py +4 -4
  3. download_model.py +21 -0
Dockerfile CHANGED
@@ -12,7 +12,7 @@ WORKDIR /app
12
  COPY . .
13
 
14
  # Install dependencies
15
- RUN pip install --no-cache-dir -r requirements.txt
16
 
17
  # Set Hugging Face cache directory to persist model downloads
18
  ENV HF_HOME="/home/user/.cache/huggingface"
@@ -22,22 +22,14 @@ 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 the model properly (USE snapshot_download)
26
- RUN python -c "import os, shutil; \
27
- from huggingface_hub import snapshot_download; \
28
- model_path = snapshot_download(repo_id='sentence-transformers/all-MiniLM-L6-v2', cache_dir='/app/model_cache'); \
29
- snapshot_dir = os.path.join(model_path, 'snapshots', os.listdir(os.path.join(model_path, 'snapshots'))[0]); \
30
- for filename in os.listdir(snapshot_dir): \
31
- shutil.move(os.path.join(snapshot_dir, filename), '/app/model_cache/')"
32
-
33
- # Ensure the model files are available at runtime (list out)
34
- RUN ls -l /app/model_cache && cat /app/model_cache/config.json
35
 
36
  # Ensure ownership and permissions remain intact
37
- RUN ls -l /app/model_cache
38
 
39
- # Expose application port
40
  EXPOSE 7860
41
 
42
- # Run server
43
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
12
  COPY . .
13
 
14
  # Install dependencies
15
+ RUN pip install --no-cache-dir -r requirements.txt
16
 
17
  # Set Hugging Face cache directory to persist model downloads
18
  ENV HF_HOME="/home/user/.cache/huggingface"
 
22
  RUN mkdir -p $SENTENCE_TRANSFORMERS_HOME && \
23
  chown -R user:user /home/user/.cache/huggingface
24
 
25
+ # Run the model download script
26
+ RUN python /app/download_model.py
 
 
 
 
 
 
 
 
27
 
28
  # Ensure ownership and permissions remain intact
29
+ RUN chown -R user:user /app/model_cache
30
 
31
+ # Expose port
32
  EXPOSE 7860
33
 
34
+ # Run the application
35
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -69,11 +69,11 @@ os.environ["SENTENCE_TRANSFORMERS_HOME"] = hf_cache_dir
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,7 +82,7 @@ else:
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...**")
 
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
+ # a) First, try loading from our copied cache
73
+ if os.path.exists(model_cache_dir) and os.listdir(model_cache_dir): # Check if model folder exists and is not empty
74
  print(f"✅ Found cached model at {model_cache_dir}")
75
  model_loc = model_cache_dir
76
+ # b) 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
+ # 4. Load the model to application
86
  from sentence_transformers import SentenceTransformer
87
  print("📥 **Loading Embedding Model...**")
88
  # st.write("📥 **Loading Embedding Model...**")
download_model.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ from huggingface_hub import snapshot_download
4
+
5
+ # Define the target cache directory
6
+ MODEL_CACHE_DIR = "/app/model_cache"
7
+
8
+ # Download model
9
+ print("⏳ Downloading the SentenceTransformer model...")
10
+ model_path = snapshot_download(repo_id="sentence-transformers/all-MiniLM-L6-v2", cache_dir=MODEL_CACHE_DIR)
11
+
12
+ # Find the snapshot folder
13
+ snapshots_dir = os.path.join(model_path, "snapshots")
14
+ if os.path.exists(snapshots_dir):
15
+ snapshot_subdirs = os.listdir(snapshots_dir)
16
+ if snapshot_subdirs:
17
+ snapshot_dir = os.path.join(snapshots_dir, snapshot_subdirs[0])
18
+ # Move all files to the main model cache directory
19
+ for filename in os.listdir(snapshot_dir):
20
+ shutil.move(os.path.join(snapshot_dir, filename), MODEL_CACHE_DIR)
21
+ print(f"✅ Model downloaded and stored in {MODEL_CACHE_DIR}")