File size: 5,671 Bytes
425b6ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from xgboost import XGBClassifier
import joblib
import time

# Load the trained XGBClassifier
xgb_model = joblib.load('xgb_model.joblib')
random_forest = joblib.load('randomforest1.joblib')

# Function to get user input
def get_user_input():
    st.subheader("Enter Customer Information:")
    CreditScore = st.number_input("Credit Score", min_value=0, step=1)
    Age = st.number_input("Age", min_value=0, step=1)
    Tenure = st.number_input("Tenure", min_value=0, step=1)
    Balance = st.number_input("Balance", min_value=0.0, step=1.0)
    NumOfProducts = st.number_input("NumOfProducts", min_value=0, step=1)
    HasCrCard = st.number_input("Has CrCard", min_value=0, step=1)
    IsActiveMember = st.number_input("IsActiveMember", min_value=0, step=1)
    Complain = st.number_input('Complain', min_value=0, step=1)
    Satisfaction_Score = st.number_input('Satisfaction Score', min_value=0, step=1)
    Point_Earned = st.number_input('Point Earned', min_value=0, step=1)

    features_dict = {
        'CreditScore': CreditScore,
        'Age': Age,
        'Tenure': Tenure,
        'Balance': Balance,
        'NumOfProducts': NumOfProducts,
        'HasCrCard': HasCrCard,
        'IsActiveMember': IsActiveMember,
        'Complain': Complain,
        'Satisfaction Score': Satisfaction_Score,
        'Point Earned': Point_Earned
    }

    return pd.DataFrame([features_dict])

# Function to visualize Churn Risk Progress Bar
def churn_risk_progress_bar(churn_prob):
    st.subheader("Churn Risk Progress Bar")
    # Use a progress bar to visualize churn risk
    st.progress(float(churn_prob))  # Convert to float

    # Display churn probability as a percentage
    st.text(f"Churn Probability: {churn_prob * 100:.2f}%")

# Function for automated model questions without data selection
def automate_model_questions():
    st.subheader("Automate Model Questions")

    # Button to start automation
    if st.button("Click for Automate"):
        sample_data = pd.DataFrame({
            'CreditScore': [700, 650, 600, 720, 680],
            'Age': [35, 40, 25, 30, 45],
            'Tenure': [5, 8, 2, 7, 4],
            'Balance': [5000, 8000, 2000, 7000, 4000],
            'NumOfProducts': [2, 3, 1, 2, 1],
            'HasCrCard': [1, 1, 0, 1, 0],
            'IsActiveMember': [1, 0, 1, 1, 0],
            'Complain': [0, 1, 0, 0, 1],
            'SatisfactionScore': [4, 3, 5, 4, 2],
            'PointEarned': [20, 15, 25, 18, 12],
        })

        for _, customer in sample_data.iterrows():
            st.text(f"Credit Score: {customer['CreditScore']}")
            st.text(f"Age: {customer['Age']}")
            st.text(f"Tenure: {customer['Tenure']}")
            st.text(f"Balance: {customer['Balance']}")
            st.text(f"NumOfProducts: {customer['NumOfProducts']}")
            st.text(f"HasCrCard: {customer['HasCrCard']}")
            st.text(f"IsActiveMember: {customer['IsActiveMember']}")
            st.text(f"Complain: {customer['Complain']}")
            st.text(f"Satisfaction Score: {customer['SatisfactionScore']}")
            st.text(f"Point Earned: {customer['PointEarned']}")

            # Prepare data for prediction
            features_df = pd.DataFrame(
                {
                    'CreditScore': [customer['CreditScore']],
                    'Age': [customer['Age']],
                    'Tenure': [customer['Tenure']],
                    'Balance': [customer['Balance']],
                    'NumOfProducts': [customer['NumOfProducts']],
                    'HasCrCard': [customer['HasCrCard']],
                    'IsActiveMember': [customer['IsActiveMember']],
                    'Complain': [customer['Complain']],
                    'Satisfaction Score': [customer['SatisfactionScore']],
                    'Point Earned': [customer['PointEarned']],
                }
            )

            # Make prediction
            churn_prob = xgb_model.predict_proba(features_df)[:, 1][0]

            # Display Prediction Result
            if churn_prob > 0.5:
                st.warning("Churn Risk Detected!")
            else:
                st.success("No Churn Risk Detected.")

            # Pause for 2 seconds before moving to the next customer
            time.sleep(2)

# Streamlit App
def main():
    st.title("Customer Churn Prediction App")

    # Sidebar Navigation
    page_selection = st.sidebar.selectbox("Select Page", ["Normal Prediction", "Automated Prediction"])

    # Display selected page
    if page_selection == "Normal Prediction":
        # Model selection
        model_selection = st.sidebar.selectbox("Select Model", ["XGBoost","Random forest"])
        
        if model_selection == "XGBoost":
            model = xgb_model
        elif model_selection == "Random forest":
            model = random_forest
        else:
            st.error("Model not available. Please select a different model.")

        # Get user input
        features_df = get_user_input()

        # Check if input is valid
        if features_df is not None:
            # Make prediction
            if st.button("Predict"):
                churn_prob = model.predict_proba(features_df)[:, 1][0]

                # Display Churn Risk Progress Bar
                churn_risk_progress_bar(churn_prob)

                # Display Prediction Result
                if churn_prob > 0.5:
                    st.warning("Churn Risk Detected!")
                else:
                    st.success("No Churn Risk Detected.")

    elif page_selection == "Automated Prediction":
        automate_model_questions()

if __name__ == "__main__":
    main()