forked from AISaturdaysLagos/Cohort8-Team-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer_churn_web_app.py
More file actions
357 lines (299 loc) · 16.4 KB
/
Copy pathCustomer_churn_web_app.py
File metadata and controls
357 lines (299 loc) · 16.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import streamlit as st
import requests
import io
import pickle
import pandas as pd
import numpy as np
# GitHub raw content URL
raw_model_url = "https://raw.githubusercontent.com/AISaturdaysLagos/Cohort8-Maathai/main/model_and_transformers2.sav"
# Download the model file
response = requests.get(raw_model_url)
model_content = response.content
# Load the model and transformers
loaded_model, scaler, normalizer = pickle.loads(model_content)
# GitHub raw content URL
raw_model_url = "https://raw.githubusercontent.com/AISaturdaysLagos/Cohort8-Maathai/main/model2_and_transformers2.sav"
# Download the model file
response = requests.get(raw_model_url)
model_content = response.content
# Load the model and transformers
loaded_model2, scaler2, normalizer2 = pickle.loads(model_content)
# Define global variables with default values
total_relationship_count = 1
total_revolving_bal = 0
total_amt_chng_q4_q1 = 0.275
total_trans_amt = 510
total_trans_ct = 10
total_ct_chng_q4_q1 = 0.206
Dependent_count = 0
Months_Inactive_12_mon = 0
Contacts_Count_12_mon = 0
# Prediction function
def hybrid_prediction(trans_ct, dep_count, inactive_months, Contacts_Count, revol_bal, rel_count, trans_amt, amt_chng_q4_q1, ct_chng_q4_q1):
# Prediction for the first model
data = {
'Total_Relationship_Count': [rel_count],
'Total_Revolving_Bal': [revol_bal],
'Total_Amt_Chng_Q4_Q1': [amt_chng_q4_q1],
'Total_Trans_Amt': [trans_amt],
'Total_Trans_Ct': [trans_ct],
'Total_Ct_Chng_Q4_Q1': [ct_chng_q4_q1]
}
df = pd.DataFrame(data)
df2array = np.asarray(df)
reshape_array = df2array.reshape(1, -1)
def transformation(reshape_array):
scaler_reshape = scaler.transform(reshape_array)
normalizer_reshape = normalizer.transform(scaler_reshape)
return normalizer_reshape
# Transform data
transformed_data = transformation(reshape_array)
# Make prediction1
prediction1 = loaded_model.predict(transformed_data)
data2 = {
'Dependent_count': [dep_count],
'Total_Relationship_Count': [rel_count],
'Months_Inactive_12_mon': [inactive_months],
'Contacts_Count_12_mon': [Contacts_Count],
'Total_Trans_Ct': [trans_ct],
'Total_Ct_Chng_Q4_Q1': [ct_chng_q4_q1]
}
dfx = pd.DataFrame(data2)
dfxarray = np.asarray(dfx)
reshapex_array = dfxarray.reshape(1, -1)
def transformation2(reshapex_array):
scaler_reshapex = scaler2.transform(reshapex_array)
normalizer_reshapex = normalizer2.transform(scaler_reshapex)
return normalizer_reshapex
# Transform data
transformed_data2 = transformation2(reshapex_array)
# Make prediction2
prediction2 = loaded_model2.predict(transformed_data2)
final_prediction = (prediction1 + prediction2) / 2
if prediction1 != prediction2:
return prediction1[0]
else:
return final_prediction[0]
def predict_single_individual():
st.title('🚀 Single Individual Churn Prediction')
st.markdown(
'<p style="font-size: 24px; color: #1F4D7A; animation: pulse 1s infinite;">Predict Customer Churn for a Single Individual</p>',
unsafe_allow_html=True
)
st.sidebar.markdown(
'<style>div.Widget.row-widget.stRadio div[role="radiogroup"] > label {border-radius: 10px;}</style>',
unsafe_allow_html=True
)
st.sidebar.subheader("Legend")
st.sidebar.markdown('- **Total Relationship Count**: Enter the total relationship count.')
st.sidebar.markdown('- **Total Revolving Balance**: Enter the total revolving balance.')
st.sidebar.markdown('- **Total Amount Change Q4-Q1**: Enter the total amount change from Q4 to Q1.')
st.sidebar.markdown('- **Total Transaction Amount**: Enter the total transaction amount.')
st.sidebar.markdown('- **Total Transaction Count**: Enter the total transaction count.')
st.sidebar.markdown('- **Total Count Change Q4-Q1**: Enter the total count change from Q4 to Q1.')
st.sidebar.markdown('- **Dependent count**: Enter the Dependent count.')
st.sidebar.markdown("- **Inactive Months in the Past 12 Months**: Please input the number of months you've been inactive within the last 12 months.")
st.sidebar.markdown("- **Contacts Count in the Past 12 Months**: Enter the total number of contacts you've had in the last 12 months.")
col1, col2 = st.columns(2)
with col1:
# Use the global variables within this block
total_relationship_count = st.number_input('Total Relationship Count',
min_value=1,
max_value=6,
value=1)
total_revolving_bal = st.number_input('Total Revolving Balance',
min_value=0,
max_value=2517,
value=0)
total_amt_chng_q4_q1 = st.slider('Total Amount Change Q4-Q1',
min_value=0.275,
max_value=1.212,
value=0.275,
step=0.001)
Dependent_count = st.number_input('Dependent_count',
min_value=0,
max_value=5,
value=0)
Months_Inactive_12_mon = st.number_input('Months Inactive 12 months',
min_value=0,
max_value=6,
value=0)
with col2:
# Use the global variables within this block
total_trans_amt = st.number_input('Total Transaction Amount',
min_value=510,
max_value=8618,
value=510)
total_trans_ct = st.number_input('Total Transaction Count',
min_value=10,
max_value=113,
value=10)
total_ct_chng_q4_q1 = st.slider('Total Count Change Q4-Q1',
min_value=0.206,
max_value=1.182,
value=0.206,
step=0.001)
Contacts_Count_12_mon = st.number_input('Contacts Count 12 months',
min_value= 0,
max_value= 6,
value= 0)
if st.button('Predict Customer Churn', key='prediction_button', help="Click to predict customer churn"):
with st.spinner('Predicting ⏳...'):
# Prediction logic
attrition = hybrid_prediction(total_trans_ct, Dependent_count, Months_Inactive_12_mon, Contacts_Count_12_mon, total_revolving_bal, total_relationship_count,
total_trans_amt, total_amt_chng_q4_q1, total_ct_chng_q4_q1)
# Display prediction result with custom styling and icon
result_placeholder = st.empty()
# Check if attrition is not empty and handle the result
if attrition is not None:
if attrition == 1:
result_placeholder.error(' ❗ The customer is on the verge of churning. 🚨')
else:
result_placeholder.success('🎉 The customer is not on the verge of churning. 🌟')
else:
# Handle the case where attrition is empty or None
st.warning('No prediction result. Please check your input values and try again.')
def predict_many_individuals():
st.title('🚀 Many Individuals Churn Prediction')
st.markdown(
'<p style="font-size: 24px; color: #1F4D7A; animation: pulse 1s infinite;">Predict Customer Churn for Many Individuals</p>',
unsafe_allow_html=True
)
st.sidebar.markdown(
'<style>div.Widget.row-widget.stRadio div[role="radiogroup"] > label {border-radius: 10px;}</style>',
unsafe_allow_html=True
)
st.sidebar.subheader("Legend")
st.sidebar.markdown('- **Total Relationship Count**: Enter the total relationship count.')
st.sidebar.markdown('- **Total Revolving Balance**: Enter the total revolving balance.')
st.sidebar.markdown('- **Total Amount Change Q4-Q1**: Enter the total amount change from Q4 to Q1.')
st.sidebar.markdown('- **Total Transaction Amount**: Enter the total transaction amount.')
st.sidebar.markdown('- **Total Transaction Count**: Enter the total transaction count.')
st.sidebar.markdown('- **Total Count Change Q4-Q1**: Enter the total count change from Q4 to Q1.')
st.sidebar.markdown('- **Dependent count**: Enter the Dependent count.')
st.sidebar.markdown("- **Inactive Months in the Past 12 Months**: Please input the number of months you've been inactive within the last 12 months.")
st.sidebar.markdown("- **Contacts Count in the Past 12 Months**: Enter the total number of contacts you've had in the last 12 months.")
# Option to upload a file with a file icon
uploaded_file = st.file_uploader("Upload a CSV file with customer data", type=["csv"])
if uploaded_file is not None:
# Read the uploaded file
uploaded_df = pd.read_csv(uploaded_file)
# Make predictions for the uploaded data
uploaded_df['predicted_result'] = uploaded_df.apply(lambda row: hybrid_prediction(row['Total_Trans_Ct'], row['Dependent_count'],
row['Months_Inactive_12_mon'], row['Contacts_Count_12_mon'],
row['Total_Revolving_Bal'], row['Total_Relationship_Count'],
row['Total_Trans_Amt'], row['Total_Amt_Chng_Q4_Q1'],
row['Total_Ct_Chng_Q4_Q1']), axis=1)
uploaded_df['predicted_result'] = uploaded_df['predicted_result'].map(lambda x : 'Attrited Customer' if x == 1 else 'Existing Customer')
# Download the CSV file with a download icon
csv_data = uploaded_df.to_csv(index=False)
st.download_button(
label="Download Predicted Results",
data=io.StringIO(csv_data).read(),
file_name="predicted_results.csv",
key='download_button',
help="Click to download the predicted results"
)
# Real-time updates with placeholder and loading spinner
result_placeholder = st.empty()
result_placeholder.text("Waiting for predictions...")
def main():
st.title('🚀 Customer Churn Prediction Web App')
st.markdown(
'<p style="font-size: 24px; color: #1F4D7A; animation: pulse 1s infinite;">Predict Customer Churn</p>',
unsafe_allow_html=True
)
# Header image with centered alignment
st.image(r"C:\Users\Administrator\Documents\AIsat\Group_Project\Customer-Churn.png",
caption="Predict Customer Churn",
use_column_width=True,
)
# Sidebar layout with rounded corners
st.sidebar.markdown(
'<style>div.Widget.row-widget.stRadio div[role="radiogroup"] > label {border-radius: 10px;}</style>',
unsafe_allow_html=True
)
st.sidebar.subheader("Legend")
st.sidebar.markdown('- **Total Relationship Count**: Enter the total relationship count.')
st.sidebar.markdown('- **Total Revolving Balance**: Enter the total revolving balance.')
st.sidebar.markdown('- **Total Amount Change Q4-Q1**: Enter the total amount change from Q4 to Q1.')
st.sidebar.markdown('- **Total Transaction Amount**: Enter the total transaction amount.')
st.sidebar.markdown('- **Total Transaction Count**: Enter the total transaction count.')
st.sidebar.markdown('- **Total Count Change Q4-Q1**: Enter the total count change from Q4 to Q1.')
st.sidebar.markdown('- **Dependent count**: Enter the Dependent count.')
st.sidebar.markdown("- **Inactive Months in the Past 12 Months**: Please input the number of months you've been inactive within the last 12 months.")
st.sidebar.markdown("- **Contacts Count in the Past 12 Months**: Enter the total number of contacts you've had in the last 12 months.")
# Main content layout with rounded corners
st.markdown(
'<style>div.Widget.stButton button{border-radius: 10px;}</style>',
unsafe_allow_html=True
)
col1, col2 = st.columns(2)
with col1:
# Use the global variables within this block
total_relationship_count = st.number_input('Total Relationship Count',
min_value=1,
max_value=6,
value=1)
total_revolving_bal = st.number_input('Total Revolving Balance',
min_value=0,
max_value=2517,
value=0)
total_amt_chng_q4_q1 = st.slider('Total Amount Change Q4-Q1',
min_value=0.275,
max_value=1.212,
value=0.275,
step=0.001)
Dependent_count = st.number_input('Dependent_count',
min_value=0,
max_value=5,
value=0)
Months_Inactive_12_mon = st.number_input('Months Inactive 12 months',
min_value=0,
max_value=6,
value=0)
with col2:
# Use the global variables within this block
total_trans_amt = st.number_input('Total Transaction Amount',
min_value=510,
max_value=8618,
value=510)
total_trans_ct = st.number_input('Total Transaction Count',
min_value=10,
max_value=113,
value=10)
total_ct_chng_q4_q1 = st.slider('Total Count Change Q4-Q1',
min_value=0.206,
max_value=1.182,
value=0.206,
step=0.001)
Contacts_Count_12_mon = st.number_input('Contacts Count 12 months',
min_value= 0,
max_value= 6,
value= 0)
# Animated button for prediction with a success icon
if st.button('Predict Customer Churn', key='prediction_button', help="Click to predict customer churn"):
with st.spinner('Predicting ⏳...'):
# Prediction logic
attrition = attrition = hybrid_prediction(total_trans_ct, Dependent_count, Months_Inactive_12_mon, Contacts_Count_12_mon,
total_revolving_bal, total_relationship_count, total_trans_amt, total_amt_chng_q4_q1, total_ct_chng_q4_q1)
# Display prediction result with custom styling and icon
result_placeholder = st.empty()
# Check if attrition is not empty and handle the result
if attrition is not None:
if attrition == 1:
result_placeholder.error(' ❗ The customer is on the verge of churning. 🚨')
else:
result_placeholder.success('🎉 The customer is not on the verge of churning. 🌟')
else:
# Handle the case where attrition is empty or None
st.warning('No prediction result. Please check your input values and try again.')
if __name__ == '__main__':
st.sidebar.title('Select Prediction Type')
prediction_type = st.sidebar.radio("Choose prediction type", ["Single Individual", "Many Individuals"])
if prediction_type == "Single Individual":
predict_single_individual()
elif prediction_type == "Many Individuals":
predict_many_individuals()
else:
main()