Spaces:
Sleeping
Sleeping
fix: fixed crash in the server
Browse filesFiles changed (2)
1234567891011121314151617181920212223242526272829303132import tensorflow as tffrom tensorflow.keras.models import load_model as keras_load_modelimport osfrom huggingface_hub import snapshot_download # fix import syntax hereimport shutil# ConstantsREPO_ID = "can-org/AI-VS-HUMAN-IMAGE-classifier"MODEL_DIR = "./IMG_models"MODEL_PATH = os.path.join(MODEL_DIR, 'latest-my_cnn_model.h5') # adjust path as neededdef warmup():global _model_imgif not os.path.exists(MODEL_DIR):download_model_Repo()_model_img = load_model()def download_model_Repo():# fix typo: os.path.exists (not os.path.exist)if os.path.exists(MODEL_DIR):return# download the repo snapshot from HF hubsnapshot_path = snapshot_download(repo_id=REPO_ID)os.makedirs(MODEL_DIR, exist_ok=True)# copy contents from snapshot_path to MODEL_DIR, allow existing dirsshutil.copytree(snapshot_path, MODEL_DIR, dirs_exist_ok=True)def load_model():if not os.path.exists(MODEL_DIR):download_model_Repo()model = keras_load_model(MODEL_PATH)return model12345678910111213141516171819202122232425262728293031323334353637383940414243import tensorflow as tffrom tensorflow.keras.models import load_model as keras_load_modelimport osfrom huggingface_hub import snapshot_downloadimport shutil# ConstantsREPO_ID = "can-org/AI-VS-HUMAN-IMAGE-classifier"MODEL_DIR = "./IMG_models"MODEL_PATH = os.path.join(MODEL_DIR, 'latest-my_cnn_model.h5') # adjust path as needed_model_img = None # global model variabledef warmup():global _model_imgif not os.path.exists(MODEL_DIR):download_model_Repo()_model_img = load_model()def download_model_Repo():if os.path.exists(MODEL_DIR):returnsnapshot_path = snapshot_download(repo_id=REPO_ID)os.makedirs(MODEL_DIR, exist_ok=True)shutil.copytree(snapshot_path, MODEL_DIR, dirs_exist_ok=True)def load_model():if not os.path.exists(MODEL_DIR):download_model_Repo()# Check for GPU availabilitygpus = tf.config.list_physical_devices('GPU')if gpus:# GPU is available, load model normallyprint("GPU detected, loading model on GPU.")model = keras_load_model(MODEL_PATH)else:# No GPU, force CPU usageprint("No GPU detected, forcing model loading on CPU.")with tf.device('/CPU:0'):model = keras_load_model(MODEL_PATH)return model
IMG_models/.gitattributesDeleted
features/image_classifier/model_loader.py+17-6