chkp-talexm commited on
Commit Β·
e3af011
1
Parent(s): 2d3f7d4
update
Browse files
app.py
CHANGED
|
@@ -1,51 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
import joblib
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
-
|
| 5 |
import streamlit as st
|
| 6 |
import pandas as pd
|
| 7 |
-
import os
|
| 8 |
-
import joblib
|
| 9 |
-
from huggingface_hub import hf_hub_download
|
| 10 |
-
|
| 11 |
-
# Hugging Face Model Repo
|
| 12 |
-
MODEL_REPO = "chagu13/is_click_predictor"
|
| 13 |
-
MODEL_DIR = "models"
|
| 14 |
-
os.makedirs(MODEL_DIR, exist_ok=True) # Ensure directory exists
|
| 15 |
|
| 16 |
# Hugging Face Model Repo
|
| 17 |
MODEL_REPO = "chagu13/is_click_predictor"
|
| 18 |
MODEL_DIR = "models"
|
| 19 |
os.makedirs(MODEL_DIR, exist_ok=True) # Ensure directory exists
|
| 20 |
|
| 21 |
-
# Model Filenames
|
| 22 |
CATBOOST_MODEL_FILENAME = "models/catboost_model.pkl"
|
| 23 |
XGB_MODEL_FILENAME = "models/xgb_model.pkl"
|
| 24 |
RF_MODEL_FILENAME = "models/rf_model.pkl"
|
| 25 |
|
| 26 |
-
#
|
| 27 |
CATBOOST_MODEL_PATH = os.path.join(MODEL_DIR, "catboost_model.pkl")
|
| 28 |
XGB_MODEL_PATH = os.path.join(MODEL_DIR, "xgb_model.pkl")
|
| 29 |
RF_MODEL_PATH = os.path.join(MODEL_DIR, "rf_model.pkl")
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def load_models():
|
| 33 |
-
"""Download models from Hugging Face
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
| 35 |
if not os.path.exists(CATBOOST_MODEL_PATH):
|
| 36 |
-
print("
|
| 37 |
-
|
|
|
|
| 38 |
if not os.path.exists(XGB_MODEL_PATH):
|
| 39 |
-
print("
|
| 40 |
-
|
| 41 |
-
if not os.path.exists(RF_MODEL_PATH):
|
| 42 |
-
print("π Downloading RandomForest model...")
|
| 43 |
-
hf_hub_download(repo_id=MODEL_REPO, filename=RF_MODEL_FILENAME, local_dir=MODEL_DIR)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
assert os.path.exists(RF_MODEL_PATH), f"β Missing file: {RF_MODEL_PATH}"
|
| 49 |
|
| 50 |
# β
Load models
|
| 51 |
print("π¦ Loading models...")
|
|
|
|
| 1 |
import os
|
| 2 |
import joblib
|
| 3 |
+
import shutil
|
| 4 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Hugging Face Model Repo
|
| 9 |
MODEL_REPO = "chagu13/is_click_predictor"
|
| 10 |
MODEL_DIR = "models"
|
| 11 |
os.makedirs(MODEL_DIR, exist_ok=True) # Ensure directory exists
|
| 12 |
|
| 13 |
+
# Model Filenames (on Hugging Face)
|
| 14 |
CATBOOST_MODEL_FILENAME = "models/catboost_model.pkl"
|
| 15 |
XGB_MODEL_FILENAME = "models/xgb_model.pkl"
|
| 16 |
RF_MODEL_FILENAME = "models/rf_model.pkl"
|
| 17 |
|
| 18 |
+
# Expected Local Paths
|
| 19 |
CATBOOST_MODEL_PATH = os.path.join(MODEL_DIR, "catboost_model.pkl")
|
| 20 |
XGB_MODEL_PATH = os.path.join(MODEL_DIR, "xgb_model.pkl")
|
| 21 |
RF_MODEL_PATH = os.path.join(MODEL_DIR, "rf_model.pkl")
|
| 22 |
|
| 23 |
|
| 24 |
+
def download_model(filename, local_path):
|
| 25 |
+
"""Download model from Hugging Face and move it to the correct location."""
|
| 26 |
+
temp_path = hf_hub_download(repo_id=MODEL_REPO, filename=filename, local_dir=MODEL_DIR)
|
| 27 |
+
|
| 28 |
+
# Ensure correct file placement
|
| 29 |
+
if temp_path != local_path:
|
| 30 |
+
shutil.move(temp_path, local_path)
|
| 31 |
+
|
| 32 |
+
return local_path
|
| 33 |
+
|
| 34 |
+
|
| 35 |
def load_models():
|
| 36 |
+
"""Download and load models from Hugging Face."""
|
| 37 |
try:
|
| 38 |
+
print("π Checking and downloading models...")
|
| 39 |
+
|
| 40 |
+
# Ensure models are downloaded and placed correctly
|
| 41 |
if not os.path.exists(CATBOOST_MODEL_PATH):
|
| 42 |
+
print("π Downloading CatBoost model...")
|
| 43 |
+
download_model(CATBOOST_MODEL_FILENAME, CATBOOST_MODEL_PATH)
|
| 44 |
+
|
| 45 |
if not os.path.exists(XGB_MODEL_PATH):
|
| 46 |
+
print("π Downloading XGBoost model...")
|
| 47 |
+
download_model(XGB_MODEL_FILENAME, XGB_MODEL_PATH)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if not os.path.exists(RF_MODEL_PATH):
|
| 50 |
+
print("π Downloading RandomForest model...")
|
| 51 |
+
download_model(RF_MODEL_FILENAME, RF_MODEL_PATH)
|
|
|
|
| 52 |
|
| 53 |
# β
Load models
|
| 54 |
print("π¦ Loading models...")
|