Uploaded required files
Browse files- app.py +108 -0
- full_pipeline.pkl +3 -0
- logistic_reg_class_model.pkl +3 -0
- requirements.txt +11 -0
- theme.py +3 -0
- utils.py +28 -0
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
from gradio.themes.base import Base
|
4 |
+
# import time
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
from utils import create_new_columns, create_processed_dataframe
|
8 |
+
def tenure_values():
|
9 |
+
cols = ['0-2', '3-5', '6-8', '9-11', '12-14', '15-17', '18-20', '21-23', '24-26', '27-29', '30-32', '33-35', '36-38', '39-41', '42-44', '45-47', '48-50', '51-53', '54-56', '57-59', '60-62', '63-65', '66-68', '69-71', '72-74']
|
10 |
+
return cols
|
11 |
+
|
12 |
+
def predict_churn(gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines, InternetService,
|
13 |
+
OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies,
|
14 |
+
Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges):
|
15 |
+
|
16 |
+
data = [gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines, InternetService,
|
17 |
+
OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies,
|
18 |
+
Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges]
|
19 |
+
|
20 |
+
x = np.array([data])
|
21 |
+
dataframe = pd.DataFrame(x, columns=train_features)
|
22 |
+
dataframe = dataframe.astype({'MonthlyCharges': 'float', 'TotalCharges': 'float', 'tenure': 'float'})
|
23 |
+
create_new_columns(dataframe)
|
24 |
+
processed_data = pipeline.transform(dataframe)
|
25 |
+
processed_dataframe = create_processed_dataframe(processed_data, dataframe)
|
26 |
+
predictions = model.predict_proba(processed_dataframe)
|
27 |
+
return round(predictions[0][0], 3), round(predictions[0][1], 3)
|
28 |
+
|
29 |
+
|
30 |
+
theme = gr.themes.Soft(
|
31 |
+
primary_hue="orange")
|
32 |
+
|
33 |
+
|
34 |
+
def load_pickle(filename):
|
35 |
+
with open(filename, 'rb') as file:
|
36 |
+
data = pickle.load(file)
|
37 |
+
return data
|
38 |
+
|
39 |
+
pipeline = load_pickle('full_pipeline.pkl')
|
40 |
+
model = load_pickle('logistic_reg_class_model.pkl')
|
41 |
+
|
42 |
+
train_features = ['gender', 'SeniorCitizen', 'Partner', 'Dependents','tenure', 'PhoneService', 'MultipleLines', 'InternetService',
|
43 |
+
'OnlineSecurity', 'OnlineBackup', 'DeviceProtection','TechSupport','StreamingTV', 'StreamingMovies',
|
44 |
+
'Contract', 'PaperlessBilling', 'PaymentMethod', 'MonthlyCharges', 'TotalCharges']
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
# theme = gr.themes.Base()
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown(
|
51 |
+
"""
|
52 |
+
# Welcome Cherished User 👋 !
|
53 |
+
## Customer Churn Classification App
|
54 |
+
Start predicting customer churn.
|
55 |
+
""")
|
56 |
+
with gr.Row():
|
57 |
+
gender = gr.Dropdown(label='Gender', choices=['Female', 'Male'])
|
58 |
+
Contract = gr.Dropdown(label='Contract', choices=['Month-to-month', 'One year', 'Two year'])
|
59 |
+
InternetService = gr.Dropdown(label='Internet Service', choices=['DSL', 'Fiber optic', 'No'])
|
60 |
+
|
61 |
+
with gr.Accordion('Yes or no'):
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
OnlineSecurity = gr.Radio(label="Online Security", choices=["Yes", "No", "No internet service"])
|
65 |
+
OnlineBackup = gr.Radio(label="Online Backup", choices=["Yes", "No", "No internet service"])
|
66 |
+
DeviceProtection = gr.Radio(label="Device Protection", choices=["Yes", "No", "No internet service"])
|
67 |
+
TechSupport = gr.Radio(label="Tech Support", choices=["Yes", "No", "No internet service"])
|
68 |
+
StreamingTV = gr.Radio(label="TV Streaming", choices=["Yes", "No", "No internet service"])
|
69 |
+
StreamingMovies = gr.Radio(label="Movie Streaming", choices=["Yes", "No", "No internet service"])
|
70 |
+
with gr.Row():
|
71 |
+
SeniorCitizen = gr.Radio(label="Senior Citizen", choices=["Yes", "No"])
|
72 |
+
Partner = gr.Radio(label="Partner", choices=["Yes", "No"])
|
73 |
+
Dependents = gr.Radio(label="Dependents", choices=["Yes", "No"])
|
74 |
+
PaperlessBilling = gr.Radio(label="Paperless Billing", choices=["Yes", "No"])
|
75 |
+
PhoneService = gr.Radio(label="Phone Service", choices=["Yes", "No"])
|
76 |
+
MultipleLines = gr.Radio(label="Multiple Lines", choices=["No phone service", "Yes", "No"])
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
MonthlyCharges = gr.Number(label="Monthly Charges")
|
80 |
+
TotalCharges = gr.Number(label="Total Charges")
|
81 |
+
Tenure = gr.Number(label='Months of Tenure')
|
82 |
+
PaymentMethod = gr.Dropdown(label="Payment Method", choices=["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"])
|
83 |
+
|
84 |
+
submit_button = gr.Button('Prediction')
|
85 |
+
print(type([[122, 456]]))
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
with gr.Accordion('Churn Prediction'):
|
89 |
+
output1 = gr.Slider(maximum=1,
|
90 |
+
minimum=0,
|
91 |
+
value=0.0,
|
92 |
+
label='Yes')
|
93 |
+
output2 = gr.Slider(maximum=1,
|
94 |
+
minimum=0,
|
95 |
+
value=0.0,
|
96 |
+
label='No')
|
97 |
+
|
98 |
+
submit_button.click(fn=predict_churn, inputs=[gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines,
|
99 |
+
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges], outputs=[output1, output2])
|
100 |
+
|
101 |
+
# if submit_button:
|
102 |
+
# print(predict_churn(gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines, InternetService,
|
103 |
+
# OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies,
|
104 |
+
# Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges))
|
105 |
+
|
106 |
+
#demo = gr.Interface(fn=predict_churn, inputs=[gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines,
|
107 |
+
# InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges], outputs=['slider', 'slider'], theme=theme)
|
108 |
+
demo.launch()
|
full_pipeline.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8258008cf92ff5e5446c62b85547661f5139b8034dd3408cb61224b44fdf7c1b
|
3 |
+
size 3517
|
logistic_reg_class_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:909c3db0e63bc22cacd72f7bd76e53e978f8667b32123fb543ff66831b0e9d1a
|
3 |
+
size 1301
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
matplotlib==3.3.4
|
2 |
+
numpy==1.20.1
|
3 |
+
pandas==1.2.4
|
4 |
+
plotly==5.4.0
|
5 |
+
seaborn==0.11.1
|
6 |
+
scikit-learn==0.24.1
|
7 |
+
IPython==7.22.0
|
8 |
+
jupyter_client==6.1.12
|
9 |
+
jupyter_core==4.7.1
|
10 |
+
jupyterlab==3.0.14
|
11 |
+
notebook==6.3.0
|
theme.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
gr.themes.builder()
|
utils.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import pickle
|
4 |
+
|
5 |
+
|
6 |
+
def load_pickle(filename):
|
7 |
+
with open(filename, 'rb') as file:
|
8 |
+
data = pickle.load(file)
|
9 |
+
return data
|
10 |
+
|
11 |
+
preprocessor = load_pickle('full_pipeline.pkl')
|
12 |
+
|
13 |
+
|
14 |
+
def create_new_columns(train_data):
|
15 |
+
train_data['Monthly Variations'] = (train_data.loc[:, 'TotalCharges']) -((train_data.loc[:, 'tenure'] * train_data.loc[:, 'MonthlyCharges']))
|
16 |
+
labels =['{0}-{1}'.format(i, i+2) for i in range(0, 73, 3)]
|
17 |
+
train_data['tenure_group'] = pd.cut(train_data['tenure'], bins=(range(0, 78, 3)), right=False, labels=labels)
|
18 |
+
train_data.drop(columns=['tenure'], inplace=True)
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
def create_processed_dataframe(processed_data, train_data):
|
24 |
+
train_num_cols=train_data.select_dtypes(exclude=['object', 'category']).columns
|
25 |
+
cat_features = preprocessor.named_transformers_['categorical']['cat_encoder'].get_feature_names()
|
26 |
+
labels = np.concatenate([train_num_cols, cat_features])
|
27 |
+
processed_dataframe = pd.DataFrame(processed_data.toarray(), columns=labels)
|
28 |
+
return processed_dataframe
|