user-churn / utils /models.py
VasithaTilakumara
change local llm to huggingface inference API
ba6d1f3
# 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