Update app/model_loader.py
Browse files- app/model_loader.py +5 -17
app/model_loader.py
CHANGED
|
@@ -1,34 +1,22 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tensorflow as tf
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
|
| 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 |
-
|
|
|
|
| 24 |
repo_id=REPO_ID,
|
| 25 |
filename=FILENAME,
|
| 26 |
-
local_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)
|
|
|
|
| 1 |
+
# app/model_loader.py
|
| 2 |
import os
|
| 3 |
import tensorflow as tf
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
+
MODEL_PATH = "app/model/efficientnetv2s.h5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
REPO_ID = "Miguel764/efficientnetv2s-skin-cancer-classifier"
|
| 8 |
FILENAME = "efficientnetv2s.h5"
|
| 9 |
|
| 10 |
def load_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if not os.path.exists(MODEL_PATH):
|
| 12 |
print("Model not found locally. Downloading from Hugging Face...")
|
| 13 |
+
os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True)
|
| 14 |
+
hf_hub_download(
|
| 15 |
repo_id=REPO_ID,
|
| 16 |
filename=FILENAME,
|
| 17 |
+
local_dir="app/model"
|
|
|
|
| 18 |
)
|
|
|
|
| 19 |
else:
|
| 20 |
print("Model already exists locally.")
|
| 21 |
|
|
|
|
| 22 |
return tf.keras.models.load_model(MODEL_PATH)
|