import pandas as pd from tensorflow.keras.models import load_model import pickle import streamlit as st # ---------------- BACKGROUND STYLE ---------------- # def add_bg(): st.markdown( """ """, 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("""

ChurnGuard AI

Predict Customer Exit & Protect Your Revenue

""", 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 """)