Spaces:
Sleeping
Sleeping
File size: 2,397 Bytes
53b92fc 5a01829 53b92fc 679fcb5 ba6d1f3 4d0ee7a 53b92fc |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# utils/models.py
import os
import joblib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from huggingface_hub import hf_hub_download
import joblib
# ------------------------------------------------------------
# 🧠 Universal Model Loader
# ------------------------------------------------------------
def load_model(model_path: str = "app_best.joblib"):
"""
Loads a trained scikit-learn model (.pkl or .joblib) from disk.
Automatically searches in the /models folder if a relative path is provided.
"""
# Download model dynamically from Hugging Face model repo
if os.getenv("SPACE_ID"): # Running inside a Hugging Face Space
model_path_hf = hf_hub_download(
repo_id="VasTk/user-churn-models",
filename=model_path
)
model = joblib.load(model_path_hf)
return model
else:
# Try direct path first
if os.path.exists(model_path):
return joblib.load(model_path)
# Try inside models/ folder
candidate_path = os.path.join("models", model_path)
if os.path.exists(candidate_path):
return joblib.load(candidate_path)
raise FileNotFoundError(
f"❌ Model file not found. Tried: {model_path_hf} and {model_path_hf}"
)
# ------------------------------------------------------------
# 📊 Example placeholder metrics and visuals
# ------------------------------------------------------------
metrics = pd.DataFrame({
"Model": ["Random Forest (App)", "Logistic Regression (App)"],
"Accuracy": [0.82, 0.75],
"AUC": [0.88, 0.80],
"F1": [0.79, 0.72]
})
feature_importance = pd.DataFrame({
"Feature": ["Recency", "Session Count"],
"Importance": [0.7, 0.3]
})
fairness = pd.DataFrame({
"Group": ["Male", "Female"],
"Accuracy": [0.81, 0.83],
"Precision": [0.78, 0.76],
"Recall": [0.80, 0.85]
})
def show_metrics_table():
"""Returns model comparison metrics as a table."""
return metrics
def plot_feature_importance():
"""Returns a Matplotlib bar plot of feature importance."""
fig, ax = plt.subplots()
sns.barplot(data=feature_importance, x="Importance", y="Feature", ax=ax)
ax.set_title("Feature Importance")
return fig
def show_fairness_table():
"""Returns fairness comparison metrics."""
return fairness
|