Gyimah3 commited on
Commit
8118ad0
·
1 Parent(s): 0b41d90

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +227 -0
app.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing required Library
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import numpy as np
5
+ import os, pickle
6
+ from sklearn import preprocessing
7
+ from PIL import Image
8
+
9
+
10
+ # Setting up page configuration and directory path
11
+ st.set_page_config(page_title="Customer churn prediction App", page_icon="🛳️", layout="centered")
12
+ DIRPATH = os.path.dirname(os.path.realpath(__file__))
13
+
14
+
15
+ # Setting background image
16
+ page_bg_img = '''
17
+ <style>
18
+ [data-testid="stAppViewContainer"] {
19
+ background-color:black;
20
+ background-image:
21
+ radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
22
+ radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
23
+ radial-gradient(white, rgba(255,255,255,.1) 2px, transparent 40px),
24
+ radial-gradient(rgba(255,255,255,.4), rgba(255,255,255,.1) 2px, transparent 30px);
25
+ background-size: 550px 550px, 350px 350px, 250px 250px, 150px 150px;
26
+ background-position: 0 0, 40px 60px, 130px 270px, 70px 100px;
27
+
28
+ }
29
+
30
+ </style>
31
+ '''
32
+ st.markdown(page_bg_img,unsafe_allow_html=True)
33
+
34
+
35
+
36
+ # Setting up logo
37
+ st.image('https://miro.medium.com/v2/resize:fit:786/format:webp/1*xT7y7u-DDssc3P_nT_qp2Q.png', width=700,caption=None, use_column_width=None, clamp=100, channels="RGB", output_format='JPEG')
38
+
39
+ # Setting up Sidebar
40
+ social_acc = ['Data Field Description', 'EDA', 'About App']
41
+ social_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', social_acc)
42
+
43
+ if social_acc_nav == 'Data Field Description':
44
+ st.sidebar.markdown("""
45
+ The table below gives a description on the variables required to make predictions.
46
+ | Variable | Definition: |
47
+ | :------------ |:--------------- |
48
+ | FREQUENCE | number of times the client has made an income |
49
+ | TENURE | duration in the network |
50
+ | FREQUENCE_RECH| number of times the customer refilled |
51
+ | MONTANT | top-up amount |
52
+ | DATA_VOLUME | number of connections|
53
+ | ORANGE | call to orange |
54
+ | TIGO | call to Tigo |
55
+ | ZONE1 | call to zones 1 |
56
+ | ZONE2 | call to zones 2 |
57
+ | ARPU_SEGMENT | income over 90 days / 3 |
58
+ | ON_NET | inter expresso call |
59
+ | REGULARITY | number of times the client is active for 90 days |
60
+ | FREQ_TOP_PACK | number of times client has activated the top pack packages|
61
+ | REVENUE | monthly income of each client |
62
+ """)
63
+
64
+ elif social_acc_nav == 'EDA':
65
+ st.sidebar.markdown("<h2 style='text-align: center;'> Exploratory Data Analysis </h2> ", unsafe_allow_html=True)
66
+ st.sidebar.markdown('''---''')
67
+ st.sidebar.markdown('''The exploratory data analysis of this project can be find in a Jupyter notebook from the linl below''')
68
+ st.sidebar.markdown("[Open Notebook](https://github.com/Gyimah3/Store-Sales----Time-Series-Forecasting-Regression-project-/blob/main/Store%20Sales%20--%20Time%20Series%20Forecasting(Regression%20project).ipynb)")
69
+
70
+ elif social_acc_nav == 'About App':
71
+ st.sidebar.markdown("<h2 style='text-align: center;'> Customer Churn prediction App </h2> ", unsafe_allow_html=True)
72
+ st.sidebar.markdown('''---''')
73
+ st.sidebar.markdown("""
74
+ | Brief Introduction|
75
+ | :------------ |
76
+ This projet is based on a Zindi challenge for an African telecommunications company (Expresso)
77
+ that provides customers with airtime and mobile data bundles. The objective of this challenge
78
+ is to develop a machine learning model to predict the likelihood of each customer “churning,”
79
+ i.e. becoming inactive and not making any transactions for 90 days. This solution will help
80
+ this telecom company to better serve their customers by understanding which customers are at risk of leaving""")
81
+ st.sidebar.markdown("")
82
+ st.sidebar.markdown("[ Visit Github Repository for more information](https://github.com/Gyimah3/Store-Sales----Time-Series-Forecasting-Regression-project-)")
83
+ st.sidebar.markdown("Dedicated to: mom❄️ and Sis Evelyn❄️.")
84
+ st.sidebar.markdown("")
85
+
86
+ # Config & Setup
87
+ @st.cache(allow_output_mutation=True)
88
+ def Load_ml_items(relative_path):
89
+ "Load ML items to reuse them"
90
+ with open(relative_path, 'rb') as file:
91
+ loaded_object = pickle.load(file)
92
+ return loaded_object
93
+
94
+
95
+ loaded_object = Load_ml_items(r'ml_com.pkl')
96
+
97
+
98
+
99
+ pipeline_of_my_model = loaded_object["pipeline"]
100
+ num_cols = loaded_object['numeric_columns']
101
+ cat_cols = loaded_object['categorical_columns']
102
+
103
+
104
+
105
+ # Setting up variables for input data
106
+ @st.cache()
107
+ def setup(tmp_df_file):
108
+ "Setup the required elements like files, models, global variables, etc"
109
+ pd.DataFrame(
110
+ dict(
111
+ TENURE=[],
112
+ MONTANT=[],
113
+ FREQUENCE_RECH=[],
114
+ REVENUE=[],
115
+ ARPU_SEGMENT=[],
116
+ FREQUENCE=[],
117
+ DATA_VOLUME=[],
118
+ ON_NET=[],
119
+ ORANGE=[],
120
+ TIGO=[],
121
+ ZONE1=[],
122
+ ZONE2=[],
123
+ REGULARITY=[],
124
+ FREQ_TOP_PACK=[]
125
+
126
+ )
127
+ ).to_csv(tmp_df_file, index=False)
128
+
129
+ # Setting up a file to save our input data
130
+ tmp_df_file = os.path.join(DIRPATH, "tmp", "data.csv")
131
+ setup(tmp_df_file)
132
+
133
+ # setting Title for forms
134
+ st.markdown("<h2 style='text-align: center;'> Customer Churn Prediction </h2> ", unsafe_allow_html=True)
135
+ st.markdown("<h7 style='text-align: center;'> Fill in the details below and click on SUBMIT button to make a prediction </h7> ", unsafe_allow_html=True)
136
+
137
+
138
+ # Creating columns for for input data(forms)
139
+ left_col,right_col = st.columns(2)#[#20,20],gap="small")
140
+
141
+ # Developing forms to collect input data
142
+ with st.form(key="information", clear_on_submit=True):
143
+ # Setting up input data for 1st column
144
+ left_col.markdown("**FIRST COLUMN DATA**")
145
+ TENURE= left_col.selectbox("duration in the network:",['K > 24 month','J 21-24 month','I 18-21 month','H 15-18 month','G 12-15 month','F 9-12 month', 'E 6-9 month', 'D 3-6 month'])
146
+ MONTANT = left_col.number_input("top-up amount:", min_value=0, max_value= 1000000000)
147
+ FREQUENCE_RECH = left_col.number_input("number of times the customer refilled:", min_value=0, max_value=1000)
148
+ REVENUE = left_col.number_input("monthly income of each client:", min_value=0, max_value=1000000000)
149
+ ARPU_SEGMENT=left_col.number_input("income over 90 days / 3:",min_value=0, max_value=30000)
150
+ FREQUENCE = left_col.number_input("number of times the client has made an income:", min_value=0, max_value=100)
151
+ DATA_VOLUME = left_col.number_input("number of connections:",min_value=0, max_value=10000)
152
+
153
+
154
+
155
+ # Setting up input data for 2nd column
156
+ right_col.markdown("**SECOND COLUMN DATA**")
157
+ ON_NET = right_col.number_input("inter expresso call:", min_value=0, max_value=10000)
158
+ ORANGE = right_col.number_input("call to orange:", min_value=0, max_value=10000)
159
+ TIGO = right_col.number_input("call to Tigo:", min_value=0, max_value=10000)
160
+ ZONE1 = right_col.number_input("call to Zones 1:", min_value=0, max_value=10000)
161
+ ZONE2 = right_col.number_input("call to Zones 2:", min_value=0, max_value=10000)
162
+ REGULARITY = right_col.number_input("number of times the client is active for 90 day:", min_value=0, max_value=100)
163
+ FREQ_TOP_PACK=right_col.number_input("number of times the client has activated the top pack packages:", min_value=0, max_value=100)
164
+
165
+ submitted = st.form_submit_button(label="Submit")
166
+
167
+ if submitted:
168
+ # Saving input data as csv after submission
169
+ pd.read_csv(tmp_df_file).append(
170
+ dict(
171
+
172
+
173
+
174
+ TENURE=TENURE,
175
+ MONTANT=MONTANT,
176
+ FREQUENCE_RECH=FREQUENCE_RECH,
177
+ REVENUE=REVENUE,
178
+ ARPU_SEGMENT=ARPU_SEGMENT,
179
+ FREQUENCE=FREQUENCE,
180
+ DATA_VOLUME=DATA_VOLUME,
181
+ ON_NET=ON_NET,
182
+ ORANGE=ORANGE,
183
+ TIGO=TIGO,
184
+ ZONE1=ZONE1,
185
+ ZONE2=ZONE2,
186
+ REGULARITY=REGULARITY,
187
+ FREQ_TOP_PACK=FREQ_TOP_PACK
188
+
189
+ ),
190
+ ignore_index=True,
191
+ ).to_csv(tmp_df_file, index=False)
192
+ st.balloons()
193
+
194
+
195
+ df = pd.read_csv(tmp_df_file)
196
+ df= df.copy()
197
+
198
+
199
+ # Making Predictions
200
+ # Passing data to pipeline to make prediction
201
+ pred_output = pipeline_of_my_model.predict(df)
202
+ prob_output = np.max(pipeline_of_my_model.predict_proba(df))
203
+
204
+ # Interpleting prediction output for display
205
+ X= pred_output[-1]
206
+ if X == 1:
207
+ explanation = 'Person will Churn'
208
+ else:
209
+ explanation = "Person won't churn"
210
+ output = explanation
211
+
212
+
213
+ # Displaying prediction results
214
+ st.markdown('''---''')
215
+ st.markdown("<h4 style='text-align: center;'> Prediction Results </h4> ", unsafe_allow_html=True)
216
+ st.success(f"Predicted Survival: {output}")
217
+ st.success(f"Confidence Probability: {prob_output}")
218
+ st.markdown('''---''')
219
+
220
+ # Making expander to view all records
221
+ expander = st.expander("See all records")
222
+ with expander:
223
+ df = pd.read_csv(tmp_df_file)
224
+ df['CHURN']= pred_output
225
+ st.dataframe(df)
226
+
227
+