Pujan-Dev's picture
feat: added files of img classifier and documented
b4f755d
raw
history blame
1.05 kB
import tensorflow as tf
from tensorflow.keras.models import load_model as keras_load_model
import os
from huggingface_hub import snapshot_download # fix import syntax here
import shutil
# Constants
REPO_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
def warmup():
global _model_img
if 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 hub
snapshot_path = snapshot_download(repo_id=REPO_ID)
os.makedirs(MODEL_DIR, exist_ok=True)
# copy contents from snapshot_path to MODEL_DIR, allow existing dirs
shutil.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 model