Spaces:
Running
Running
fix: fixed crash in the server
Browse files
IMG_models/.gitattributes
DELETED
|
File without changes
|
features/image_classifier/model_loader.py
CHANGED
|
@@ -1,32 +1,43 @@
|
|
| 1 |
import tensorflow as tf
|
| 2 |
from tensorflow.keras.models import load_model as keras_load_model
|
| 3 |
import os
|
| 4 |
-
from huggingface_hub import snapshot_download
|
| 5 |
import shutil
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
REPO_ID = "can-org/AI-VS-HUMAN-IMAGE-classifier"
|
| 9 |
MODEL_DIR = "./IMG_models"
|
| 10 |
-
|
| 11 |
MODEL_PATH = os.path.join(MODEL_DIR, 'latest-my_cnn_model.h5') # adjust path as needed
|
|
|
|
|
|
|
|
|
|
| 12 |
def warmup():
|
| 13 |
global _model_img
|
| 14 |
if not os.path.exists(MODEL_DIR):
|
| 15 |
download_model_Repo()
|
| 16 |
_model_img = load_model()
|
|
|
|
| 17 |
def download_model_Repo():
|
| 18 |
-
# fix typo: os.path.exists (not os.path.exist)
|
| 19 |
if os.path.exists(MODEL_DIR):
|
| 20 |
return
|
| 21 |
-
# download the repo snapshot from HF hub
|
| 22 |
snapshot_path = snapshot_download(repo_id=REPO_ID)
|
| 23 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 24 |
-
# copy contents from snapshot_path to MODEL_DIR, allow existing dirs
|
| 25 |
shutil.copytree(snapshot_path, MODEL_DIR, dirs_exist_ok=True)
|
| 26 |
|
| 27 |
def load_model():
|
| 28 |
if not os.path.exists(MODEL_DIR):
|
| 29 |
download_model_Repo()
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return model
|
| 32 |
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
from tensorflow.keras.models import load_model as keras_load_model
|
| 3 |
import os
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
import shutil
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
REPO_ID = "can-org/AI-VS-HUMAN-IMAGE-classifier"
|
| 9 |
MODEL_DIR = "./IMG_models"
|
|
|
|
| 10 |
MODEL_PATH = os.path.join(MODEL_DIR, 'latest-my_cnn_model.h5') # adjust path as needed
|
| 11 |
+
|
| 12 |
+
_model_img = None # global model variable
|
| 13 |
+
|
| 14 |
def warmup():
|
| 15 |
global _model_img
|
| 16 |
if not os.path.exists(MODEL_DIR):
|
| 17 |
download_model_Repo()
|
| 18 |
_model_img = load_model()
|
| 19 |
+
|
| 20 |
def download_model_Repo():
|
|
|
|
| 21 |
if os.path.exists(MODEL_DIR):
|
| 22 |
return
|
|
|
|
| 23 |
snapshot_path = snapshot_download(repo_id=REPO_ID)
|
| 24 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
|
|
|
| 25 |
shutil.copytree(snapshot_path, MODEL_DIR, dirs_exist_ok=True)
|
| 26 |
|
| 27 |
def load_model():
|
| 28 |
if not os.path.exists(MODEL_DIR):
|
| 29 |
download_model_Repo()
|
| 30 |
+
|
| 31 |
+
# Check for GPU availability
|
| 32 |
+
gpus = tf.config.list_physical_devices('GPU')
|
| 33 |
+
if gpus:
|
| 34 |
+
# GPU is available, load model normally
|
| 35 |
+
print("GPU detected, loading model on GPU.")
|
| 36 |
+
model = keras_load_model(MODEL_PATH)
|
| 37 |
+
else:
|
| 38 |
+
# No GPU, force CPU usage
|
| 39 |
+
print("No GPU detected, forcing model loading on CPU.")
|
| 40 |
+
with tf.device('/CPU:0'):
|
| 41 |
+
model = keras_load_model(MODEL_PATH)
|
| 42 |
return model
|
| 43 |
|