Tantawi commited on
Commit
1391d90
·
verified ·
1 Parent(s): 48c4be3

Update app/model_loader.py

Browse files
Files changed (1) hide show
  1. app/model_loader.py +31 -8
app/model_loader.py CHANGED
@@ -1,11 +1,34 @@
1
- from huggingface_hub import hf_hub_download
2
  import tensorflow as tf
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def load_model():
5
- # Download the model file from the public repo
6
- model_path = hf_hub_download(
7
- repo_id="Miguel764/efficientnetv2s-skin-cancer-classifier", # public repo
8
- filename="efficientnetv2s.h5" # file name inside the repo
9
- )
10
- # Load the downloaded model
11
- return tf.keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import tensorflow as tf
3
+ from huggingface_hub import hf_hub_download
4
+
5
+ # ✅ Force Hugging Face cache to a writable directory
6
+ os.environ["HF_HUB_CACHE"] = "/tmp/huggingface"
7
+
8
+ # ✅ Model directory and path
9
+ MODEL_DIR = "/tmp/model"
10
+ MODEL_PATH = os.path.join(MODEL_DIR, "efficientnetv2s.h5")
11
+
12
+ REPO_ID = "Miguel764/efficientnetv2s-skin-cancer-classifier"
13
+ FILENAME = "efficientnetv2s.h5"
14
 
15
  def load_model():
16
+ # Create directories if not exist
17
+ os.makedirs(MODEL_DIR, exist_ok=True)
18
+ os.makedirs(os.environ["HF_HUB_CACHE"], exist_ok=True)
19
+
20
+ # ✅ Only download if the model is missing
21
+ if not os.path.exists(MODEL_PATH):
22
+ print("Model not found locally. Downloading from Hugging Face...")
23
+ downloaded_path = hf_hub_download(
24
+ repo_id=REPO_ID,
25
+ filename=FILENAME,
26
+ local_dir=MODEL_DIR,
27
+ local_dir_use_symlinks=False
28
+ )
29
+ print(f"Model downloaded to: {downloaded_path}")
30
+ else:
31
+ print("Model already exists locally.")
32
+
33
+ # ✅ Load the model safely
34
+ return tf.keras.models.load_model(MODEL_PATH)