Meenakshi9182's picture
Upload 8 files
66423d5 verified
Raw History Blame Contribute Delete
5.11 kB
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
""")