dtyago commited on
Commit
1a8333a
1 Parent(s): 5659717

Fix for MODEL_PATH export

Browse files
Files changed (3) hide show
  1. app/main.py +2 -1
  2. app/utils/download_model.py +9 -7
  3. entrypoint.sh +14 -21
app/main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import chromadb
2
 
3
  from fastapi import FastAPI, Request, Form, File, UploadFile, Depends
@@ -38,7 +39,7 @@ async def startup_event():
38
  db_path = CHROMADB_LOC
39
  chromadb_face_helper = ChromaDBFaceHelper(db_path) # Used by APIs
40
  # Perform any other startup tasks here
41
-
42
 
43
  # Mount static files
44
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
1
+ import os
2
  import chromadb
3
 
4
  from fastapi import FastAPI, Request, Form, File, UploadFile, Depends
 
39
  db_path = CHROMADB_LOC
40
  chromadb_face_helper = ChromaDBFaceHelper(db_path) # Used by APIs
41
  # Perform any other startup tasks here
42
+ print(f"MODEL_PATH in main.py = {os.getenv('MODEL_PATH')} ")
43
 
44
  # Mount static files
45
  app.mount("/static", StaticFiles(directory="static"), name="static")
app/utils/download_model.py CHANGED
@@ -2,13 +2,15 @@ import os
2
  import requests
3
  from transformers import AutoModel
4
 
5
- '''
6
- DO NOT PRINT ANYTHING OTHER THAN MODEL PATH IN THIS SCRIPT, THIS IS BEING EXPORTED BY DOCKER ENTRY POINT!
7
- '''
8
  def model_file_exists_and_valid(model_file_path):
9
  # Check if the model file exists and has a size greater than 0
10
  return os.path.exists(model_file_path) and os.path.getsize(model_file_path) > 0
11
 
 
 
 
 
 
12
  def download_hf_model():
13
  '''
14
  Model File Path for HF Models: The download_hf_model function now includes a default model file path (pytorch_model.bin) check.
@@ -20,7 +22,7 @@ def download_hf_model():
20
 
21
  if model_file_exists_and_valid(model_file_path):
22
  print(f"Model {model_name} already downloaded.")
23
- print(model_file_path)
24
  return
25
 
26
  # Authenticate with Hugging Face using the token, if available
@@ -33,7 +35,7 @@ def download_hf_model():
33
  model = AutoModel.from_pretrained(model_name)
34
  model.save_pretrained(model_dir)
35
  print(f"Model {model_name} downloaded and saved to {model_dir}")
36
- print(model_file_path)
37
 
38
  def download_gguf_model():
39
  model_name = os.getenv("HF_MODEL_NAME")
@@ -45,7 +47,7 @@ def download_gguf_model():
45
 
46
  if model_file_exists_and_valid(model_file_path):
47
  print(f"Model {model_name} already downloaded.")
48
- print(model_file_path)
49
  return
50
 
51
  print(f"Downloading model from {model_url}...")
@@ -56,7 +58,7 @@ def download_gguf_model():
56
  print(f"Model downloaded and saved to {model_file_path}")
57
  else:
58
  print(f"Failed to download the model. Status code: {response.status_code}")
59
- print(model_file_path)
60
 
61
  def download_model():
62
  model_class = os.getenv("MODEL_CLASS")
 
2
  import requests
3
  from transformers import AutoModel
4
 
 
 
 
5
  def model_file_exists_and_valid(model_file_path):
6
  # Check if the model file exists and has a size greater than 0
7
  return os.path.exists(model_file_path) and os.path.getsize(model_file_path) > 0
8
 
9
+ def write_model_path_to_txt_file(model_file_path):
10
+ # Write the model path to model_path.txt
11
+ with open('/home/user/data/models/model_path.txt', 'w') as f:
12
+ f.write(model_file_path)
13
+
14
  def download_hf_model():
15
  '''
16
  Model File Path for HF Models: The download_hf_model function now includes a default model file path (pytorch_model.bin) check.
 
22
 
23
  if model_file_exists_and_valid(model_file_path):
24
  print(f"Model {model_name} already downloaded.")
25
+ write_model_path_to_txt_file(model_file_path)
26
  return
27
 
28
  # Authenticate with Hugging Face using the token, if available
 
35
  model = AutoModel.from_pretrained(model_name)
36
  model.save_pretrained(model_dir)
37
  print(f"Model {model_name} downloaded and saved to {model_dir}")
38
+ write_model_path_to_txt_file(model_file_path)
39
 
40
  def download_gguf_model():
41
  model_name = os.getenv("HF_MODEL_NAME")
 
47
 
48
  if model_file_exists_and_valid(model_file_path):
49
  print(f"Model {model_name} already downloaded.")
50
+ write_model_path_to_txt_file(model_file_path)
51
  return
52
 
53
  print(f"Downloading model from {model_url}...")
 
58
  print(f"Model downloaded and saved to {model_file_path}")
59
  else:
60
  print(f"Failed to download the model. Status code: {response.status_code}")
61
+ write_model_path_to_txt_file(model_file_path)
62
 
63
  def download_model():
64
  model_class = os.getenv("MODEL_CLASS")
entrypoint.sh CHANGED
@@ -6,27 +6,20 @@ export HF_HOME=/home/user/data/hf_cache
6
  # Assuming HF_TOKEN is already exported to the environment
7
  echo "Using Hugging Face API token for authentication"
8
 
9
- # Use the environment variable for the model name
10
- MODEL_DIR="/home/user/data/models/${HF_MODEL_NAME}"
11
- MODEL_URL=${HF_MODEL_URL} # Ensure consistent variable naming
12
-
13
- # Download the model if it does not exist
14
- if [ ! -d "$MODEL_DIR" ]; then
15
- echo "Model not found. Downloading ${HF_MODEL_NAME} from ${MODEL_URL}..."
16
- # Navigate to the directory where download_model.py is located
17
- cd /home/user/app/app/utils
18
-
19
- # Execute the download_model script
20
- echo "Downloading the model..."
21
- MODEL_PATH=$(python download_model.py) || { echo "Model download failed"; exit 1; }
22
- export MODEL_PATH
23
-
24
-
25
- # Navigate back to the app directory
26
- cd /home/user/app
27
- else
28
- echo "Model ${HF_MODEL_NAME} already present."
29
- fi
30
 
31
  # Execute the main command of the container
32
  exec "$@"
 
6
  # Assuming HF_TOKEN is already exported to the environment
7
  echo "Using Hugging Face API token for authentication"
8
 
9
+ # Navigate to the directory where download_model.py is located
10
+ echo "Determining model path..."
11
+ cd /home/user/app/app/utils
12
+
13
+ # Execute the download_model script
14
+ python download_model.py || { echo "Model download failed"; exit 1; }
15
+ # export MODEL_PATH
16
+ # Read the model path from model_path.txt and export it
17
+ MODEL_PATH=$(cat /home/user/data/models/model_path.txt)
18
+ export MODEL_PATH
19
+ echo "@ Entrypoint - MODEL_PATH exported=${MODEL_PATH}"
20
+
21
+ # Navigate back to the app directory
22
+ cd /home/user/app
 
 
 
 
 
 
 
23
 
24
  # Execute the main command of the container
25
  exec "$@"