File size: 1,053 Bytes
3ca686a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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