File size: 5,112 Bytes
66423d5 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import pandas as pd
from tensorflow.keras.models import load_model
import pickle
import streamlit as st
# ---------------- BACKGROUND STYLE ---------------- #
def add_bg():
st.markdown(
"""
<style>
.stApp {
background-image: url("https://images.unsplash.com/photo-1563013544-824ae1b704d3");
background-size: cover;
background-position: center;
background-attachment: fixed;
}
/* Light overlay */
.stApp::before {
content: "";
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.85);
z-index: -1;
}
/* Main container card */
.block-container {
background: white;
padding: 2rem;
border-radius: 15px;
box-shadow: 0px 6px 30px rgba(0,0,0,0.1);
}
/* Text */
html, body, [class*="css"] {
color: #1f2937;
font-family: 'Segoe UI', sans-serif;
}
/* Button */
div.stButton > button {
background: linear-gradient(to right, #4facfe, #00f2fe);
color: white;
font-size: 18px;
border-radius: 10px;
height: 3em;
width: 100%;
border: none;
}
/* Inputs */
input, select {
border-radius: 8px !important;
}
</style>
""",
unsafe_allow_html=True
)
add_bg()
# ---------------- LOAD FILES ---------------- #
with open('preprocessor.pkl', 'rb') as f:
preprocessor = pickle.load(f)
ann_model = load_model('ann_model.keras')
df = pd.read_csv('customer_churn_1M.csv')
# ---------------- TITLE ---------------- #
st.markdown("""
<h1 style='text-align: center;'>
ChurnGuard AI
</h1>
<h4 style='text-align: center; color: #4b5563;'>
Predict Customer Exit & Protect Your Revenue
</h4>
""", unsafe_allow_html=True)
# ---------------- BANNER ---------------- #
st.image(
"Customer_Churn_Prediction_-_AI__Analytics___Retention_Tips-1764682187258.webp",
width='stretch'
)
# ---------------- INPUT FORM ---------------- #
col1, col2 = st.columns(2)
with col1:
age = st.number_input("Age", min_value=18, max_value=100)
gender = st.selectbox("Gender", df['gender'].unique())
annual_income = st.number_input("Annual Income", min_value=0.0)
education = st.selectbox("Education", df['education'].unique())
marital_status = st.selectbox("Marital Status", df['marital_status'].unique())
with col2:
dependents = st.number_input("Dependents", min_value=0, max_value=10)
tenure = st.number_input("Tenure", min_value=0, max_value=50)
contract = st.selectbox("Contract Type", df['contract'].unique())
payment_method = st.selectbox("Payment Method", df['payment_method'].unique())
credit_score = st.number_input("Credit Score", min_value=300, max_value=900)
# ---------------- MODEL FUNCTION ---------------- #
def model():
input_data = pd.DataFrame([{
"age": age,
"gender": gender,
"annual_income": annual_income,
"education": education,
"marital_status": marital_status,
"dependents": dependents,
"tenure": tenure,
"contract": contract,
"payment_method": payment_method,
"credit_score": credit_score,
# Default values
"avg_monthly_gb": 0,
"has_streaming_tv": "No",
"customer_age_days": 0,
"senior_citizen": 0,
"late_payments": 0,
"totalcharges": 0,
"paperless_billing": "No",
"has_tech_support": "No",
"has_online_security": "No",
"monthlycharges": 0,
"has_device_protection": "No",
"has_phone_service": "Yes",
"customer_satisfaction": 3,
"has_internet_service": "Yes",
"has_streaming_movies": "No",
"days_since_last_interaction": 0,
"num_complaints": 0,
"has_online_backup": "No",
"num_service_calls": 0
}])
data = preprocessor.transform(input_data)
pred = ann_model.predict(data)
return pred[0][0]
# ---------------- PREDICTION ---------------- #
if st.button(' Predict Churn'):
result = model()
st.markdown("### Prediction Result")
# Progress bar
st.progress(float(result))
if result > 0.5:
st.error(f" High Churn Risk: {result:.2%}")
st.markdown("""
**Recommended Actions:**
- Offer personalized discounts
- Improve engagement
- Provide loyalty rewards
""")
else:
st.success(f" Low Churn Risk: {result:.2%}")
st.markdown("""
**Strategy:**
- Maintain good service
- Upsell premium features
- Keep customer engaged
""") |