-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.py
139 lines (120 loc) · 5.79 KB
/
Home.py
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
import streamlit as st
import pandas as pd
import numpy as np
#from PIL import Image
#load the model from disk
import joblib
model = joblib.load(r"churn_predictor.sav")
#Import python scripts
from preprocessing import preprocess
import base64
@st.cache(allow_output_mutation=True)
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
def set_png_as_page_bg(png_file):
bin_str = get_base64_of_bin_file(png_file)
page_bg_img = '''
<style>
.stApp {
background-image: url("data:image/png;base64,%s");
min-height: 200px;
background-attachment: scroll;
background-position: 300px 1800px;
background-size: cover;
}
</style>
''' % bin_str
st.markdown(page_bg_img, unsafe_allow_html=True)
return
def main():
#Setting Application title
st.title('WillTheyStay.ai')
#Setting Application description
st.markdown("""
:dart: WillTheyStay.ai is an open source streamlit app made to predict customer churn in a ficitional telecommunication use case.
The application is functional for both online prediction and batch data prediction.
""")
st.markdown("<h3></h3>", unsafe_allow_html=True)
set_png_as_page_bg('joakim-honkasalo-ssvjJLB6wIw-unsplash.jpg')
#Setting Application sidebar default
#image = Image.open('App.jpg')
add_selectbox = st.sidebar.selectbox(
"How would you like to predict?", ("Online", "Batch"))
st.sidebar.info('This app is created to predict Customer Churn')
#st.sidebar.image(image)
if add_selectbox == "Online":
st.info("Input data below")
#Based on our optimal features selection
st.subheader("Demographic data")
seniorcitizen = st.selectbox('Senior Citizen:', ('Yes', 'No'))
dependents = st.selectbox('Dependent:', ('Yes', 'No'))
st.subheader("Payment data")
tenure = st.slider('Number of months the customer has stayed with the company', min_value=0, max_value=72, value=0)
contract = st.selectbox('Contract', ('Month-to-month', 'One year', 'Two year'))
paperlessbilling = st.selectbox('Paperless Billing', ('Yes', 'No'))
PaymentMethod = st.selectbox('PaymentMethod',('Electronic check', 'Mailed check', 'Bank transfer (automatic)','Credit card (automatic)'))
monthlycharges = st.number_input('The amount charged to the customer monthly', min_value=0, max_value=150, value=0)
totalcharges = st.number_input('The total amount charged to the customer',min_value=0, max_value=10000, value=0)
st.subheader("Services signed up for")
mutliplelines = st.selectbox("Does the customer have multiple lines",('Yes','No','No phone service'))
phoneservice = st.selectbox('Phone Service:', ('Yes', 'No'))
internetservice = st.selectbox("Does the customer have internet service", ('DSL', 'Fiber optic', 'No'))
onlinesecurity = st.selectbox("Does the customer have online security",('Yes','No','No internet service'))
onlinebackup = st.selectbox("Does the customer have online backup",('Yes','No','No internet service'))
techsupport = st.selectbox("Does the customer have technology support", ('Yes','No','No internet service'))
streamingtv = st.selectbox("Does the customer stream TV", ('Yes','No','No internet service'))
streamingmovies = st.selectbox("Does the customer stream movies", ('Yes','No','No internet service'))
data = {
'SeniorCitizen': seniorcitizen,
'Dependents': dependents,
'tenure':tenure,
'PhoneService': phoneservice,
'MultipleLines': mutliplelines,
'InternetService': internetservice,
'OnlineSecurity': onlinesecurity,
'OnlineBackup': onlinebackup,
'TechSupport': techsupport,
'StreamingTV': streamingtv,
'StreamingMovies': streamingmovies,
'Contract': contract,
'PaperlessBilling': paperlessbilling,
'PaymentMethod':PaymentMethod,
'MonthlyCharges': monthlycharges,
'TotalCharges': totalcharges
}
features_df = pd.DataFrame.from_dict([data])
st.markdown("<h3></h3>", unsafe_allow_html=True)
st.write('Overview of input is shown below')
st.markdown("<h3></h3>", unsafe_allow_html=True)
st.dataframe(features_df)
#Preprocess inputs
preprocess_df = preprocess(features_df, 'Online')
prediction = model.predict(preprocess_df)
if st.button('Predict'):
if prediction == 1:
st.warning('Yes, the customer will terminate the service.')
else:
st.success('No, the customer is happy with Telco Services.')
else:
st.subheader("Dataset upload")
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
data = pd.read_csv(uploaded_file)
#Get overview of data
st.write(data.head())
st.markdown("<h3></h3>", unsafe_allow_html=True)
#Preprocess inputs
preprocess_df = preprocess(data, "Batch")
if st.button('Predict'):
#Get batch prediction
prediction = model.predict(preprocess_df)
prediction_df = pd.DataFrame(prediction, columns=["Predictions"])
prediction_df = prediction_df.replace({1:'Yes, the customer will terminate the service.',
0:'No, the customer is happy with Telco Services.'})
st.markdown("<h3></h3>", unsafe_allow_html=True)
st.subheader('Prediction')
st.write(prediction_df)
if __name__ == '__main__':
main()