-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_utils.py
49 lines (36 loc) · 1.68 KB
/
model_utils.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from joblib import load
from pathlib import Path
import os
import sys
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder
from project2.model_builders import fit_dump_cf, fit_dump_nf, fit_dump_le
from project2.model_builders import CF_MODEL_NAME, NF_MODEL_NAME
from project2.model_builders import LE_ENCODER_NAME
__MODELS_FOLDER = Path(os.path.join(os.getcwd(), "models")).resolve()
__ASSETS_FOLDER = Path(os.path.join(os.getcwd(), "assets")).resolve()
DATA_FILE = Path(os.path.join(__ASSETS_FOLDER, "yummly.json"))
def load_models() -> tuple[Pipeline, Pipeline, LabelEncoder]:
sys.stderr.write("Loading models...\n")
if not os.path.exists(__ASSETS_FOLDER) or not os.path.exists(DATA_FILE):
raise Exception(f"Yummly.json not present in {__ASSETS_FOLDER}")
if not os.path.exists(__MODELS_FOLDER):
sys.stderr.write(
f"Models folder not found! Creating models folder..\n")
__MODELS_FOLDER.mkdir(parents=True)
cf_path = os.path.join(
__MODELS_FOLDER,
CF_MODEL_NAME) # Cusine Finder Model
nf_path = os.path.join(__MODELS_FOLDER,
NF_MODEL_NAME) # Neighbors Finder Model
le_path = os.path.join(__MODELS_FOLDER,
LE_ENCODER_NAME) # Label Encoder Model
if not os.path.exists(le_path):
fit_dump_le(__MODELS_FOLDER, DATA_FILE)
le = load(le_path)
if not os.path.exists(cf_path):
fit_dump_cf(__MODELS_FOLDER, DATA_FILE, le)
if not os.path.exists(nf_path):
fit_dump_nf(__MODELS_FOLDER, DATA_FILE, le)
sys.stderr.write(f"\nload_models:\tModels loaded\n\n")
return load(cf_path), load(nf_path), load(le_path)