chkp-talexm commited on
Commit
e3af011
Β·
1 Parent(s): 2d3f7d4
Files changed (1) hide show
  1. app.py +26 -23
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
- # Full Local Paths
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 and load them."""
34
  try:
 
 
 
35
  if not os.path.exists(CATBOOST_MODEL_PATH):
36
- print("πŸ”„ Downloading CatBoost model...")
37
- hf_hub_download(repo_id=MODEL_REPO, filename=CATBOOST_MODEL_FILENAME, local_dir=MODEL_DIR)
 
38
  if not os.path.exists(XGB_MODEL_PATH):
39
- print("πŸ”„ Downloading XGBoost model...")
40
- hf_hub_download(repo_id=MODEL_REPO, filename=XGB_MODEL_FILENAME, local_dir=MODEL_DIR)
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
- # βœ… Debug: Check if models exist
46
- assert os.path.exists(CATBOOST_MODEL_PATH), f"❌ Missing file: {CATBOOST_MODEL_PATH}"
47
- assert os.path.exists(XGB_MODEL_PATH), f"❌ Missing file: {XGB_MODEL_PATH}"
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...")