Download app.py from fincierge/churnly: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/spaces/fincierge/churnly/resolve/main/app.py
- Command line
-
hf download hf://spaces/fincierge/churnly/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fincierge/churnly/resolve/main/app.py
2.36 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import pickle | |
| import shap | |
| import matplotlib.pyplot as plt | |
| import xgboost as xgb | |
| # ================== LOAD MODEL ================== | |
| def load_model(): | |
| with open("churnaizer_model.pkl", "rb") as f: | |
| model = pickle.load(f) | |
| return model | |
| model = load_model() | |
| # ================== PAGE CONFIG ================== | |
| st.set_page_config(page_title="Churnaizer - Churn Prediction", layout="wide") | |
| st.title("๐ฎ Churnaizer - AI-Powered Churn Prediction") | |
| st.markdown( | |
| """ | |
| Upload your **customer CSV file** and get: | |
| - Churn probability per customer | |
| - SHAP-based feature explanations | |
| - Benchmark comparison vs. SaaS industry churn rates | |
| """ | |
| ) | |
| # ================== FILE UPLOAD ================== | |
| uploaded_file = st.file_uploader("๐ Upload CSV File", type="csv") | |
| if uploaded_file is not None: | |
| data = pd.read_csv(uploaded_file) | |
| st.subheader("Preview of Uploaded Data") | |
| st.dataframe(data.head()) | |
| # ========== PREDICTIONS ========== | |
| dmatrix = xgb.DMatrix(data) | |
| preds = model.predict(dmatrix) | |
| data["Churn_Probability"] = preds | |
| st.subheader("๐ Predictions") | |
| st.dataframe(data.head(20)) | |
| # Add risk labels | |
| data["Risk_Level"] = np.where(data["Churn_Probability"] > 0.5, "โ ๏ธ High Risk", "โ Low Risk") | |
| # Churn rate | |
| churn_rate = data["Churn_Probability"].mean() | |
| st.metric("๐ Average Predicted Churn Rate", f"{churn_rate:.2%}") | |
| # ========== BENCHMARK COMPARISON ========== | |
| st.subheader("๐ Benchmark Comparison") | |
| benchmarks = { | |
| "B2B SaaS": 0.121, | |
| "B2C SaaS": 0.071, | |
| "Overall SaaS Median": 0.106, | |
| } | |
| for label, rate in benchmarks.items(): | |
| diff = churn_rate - rate | |
| color = "red" if diff > 0 else "green" | |
| st.markdown(f"- **{label}:** {rate:.1%} _(Your rate {diff:+.1%} vs this benchmark)_") | |
| # ========== SHAP EXPLANATION ========== | |
| try: | |
| st.subheader("๐ Feature Importance (SHAP)") | |
| explainer = shap.TreeExplainer(model) | |
| shap_values = explainer.shap_values(data) | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| shap.summary_plot(shap_values, data, show=False) | |
| st.pyplot(fig) | |
| except Exception as e: | |
| st.warning(f"SHAP explanation not available: {e}") | |