KwameOO commited on
Commit
5d047f0
·
1 Parent(s): 2fb65ed

Upload 7 files

Browse files
Files changed (7) hide show
  1. .gitignore +4 -0
  2. LICENSE +21 -0
  3. app.py +123 -0
  4. requirements.txt +13 -0
  5. src/Gradio_App_toolkit +0 -0
  6. src/app_thumbnail.png +0 -0
  7. src/xgb_model.json +0 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__
2
+ .vs
3
+ .vscode
4
+ venv
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Kwame
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ----- Load base libraries and packages
2
+ import gradio as gr
3
+
4
+ import numpy as np
5
+ import pandas as pd
6
+
7
+ import pickle
8
+
9
+ import xgboost as xgb
10
+ from xgboost import XGBClassifier
11
+
12
+ # ----- Useful lists
13
+ expected_inputs = ["tenure", "montant", "frequence_rech", "arpu_segment", "frequence", "data_volume", "regularity", "freq_top_pack"]
14
+ columns_to_scale = ["montant", "frequence_rech", "arpu_segment", "frequence", "data_volume", "regularity", "freq_top_pack"]
15
+ categoricals = ["tenure"]
16
+
17
+
18
+ # ----- Helper Functions
19
+ # Function to load ML toolkit
20
+ def load_ml_toolkit(file_path=r"src\Gradio_App_toolkit"):
21
+ """
22
+ This function loads the ML items into this app by taking the path to the ML items.
23
+
24
+ Args:
25
+ file_path (regexp, optional): It receives the file path to the ML items, but defaults to the "src" folder in the repository.
26
+
27
+ Returns:
28
+ file: It returns the pickle file (which in this case contains the Machine Learning items.)
29
+ """
30
+
31
+ with open(file_path, "rb") as file:
32
+ loaded_toolkit = pickle.load(file)
33
+ return loaded_toolkit
34
+
35
+
36
+ # Importing the toolkit
37
+ loaded_toolkit = load_ml_toolkit(r"src\Gradio_App_toolkit")
38
+ encoder = loaded_toolkit["encoder"]
39
+ scaler = loaded_toolkit["scaler"]
40
+
41
+ # Import the model
42
+ model = XGBClassifier()
43
+ model.load_model(r"src\xgb_model.json")
44
+
45
+
46
+ # Function to process inputs and return prediction
47
+ def process_and_predict(*args, encoder=encoder, scaler=scaler, model=model):
48
+ """
49
+ This function processes the inputs and returns the predicted churn status of the customer.
50
+ It receives the user inputs, the encoder, scaler and model. The inputs are then put through the same process as was done during modelling, including but not limited to encoding of categorical columns and scaling of numeric columns.
51
+
52
+ Args:
53
+ encoder (LabelEncoder, optional): It is the encoder used to encode the categorical features before training the model, and should be loaded either as part of the ML items or as a standalone item. Defaults to encoder, which comes with the ML Items dictionary.
54
+ scaler (MinMaxScaler, optional): It is the scaler (MinMaxScaler) used to scale the numeric features before training the model, and should be loaded either as part of the ML Items or as a standalone item. Defaults to scaler, which comes with the ML Items dictionary.
55
+ model (XGBoost, optional): This is the model that was trained and is to be used for the prediction. It defaults to "model", as loaded from the ML toolkit.
56
+
57
+ Returns:
58
+ Prediction (label): Returns the label of the predicted class, i.e. one of whether the given customer will churn or not.
59
+ """
60
+
61
+ # Convert inputs into a DataFrame
62
+ input_data = pd.DataFrame([args], columns=expected_inputs)
63
+
64
+ # Encode the categorical column
65
+ input_data["tenure"] = encoder.transform(input_data["tenure"])
66
+
67
+ # Scale the numeric columns
68
+ input_data[columns_to_scale] = scaler.transform(input_data[columns_to_scale])
69
+
70
+ # Making the prediction
71
+ model_output = model.predict(input_data)
72
+ return {"Prediction: CHURN": float(model_output[0]), "Prediction: STAY": 1-float(model_output[0])}
73
+
74
+
75
+ # ----- App Interface
76
+ with gr.Blocks() as turn_on_the_gradio:
77
+ gr.Markdown("# Expresso Customer Churn Prediction")
78
+ gr.Markdown("""This app uses a machine learning model to predict whether or not a customer will churn based on inputs made by you, the user. The (XGBoost) model was trained and built based on Zindi's Expresso Dataset. You may refer to the expander at the bottom for more information on the inputs.""")
79
+
80
+ # Phase 1: Receiving Inputs
81
+ # Usage
82
+ gr.Markdown("**Customer Usage Data**")
83
+ with gr.Row():
84
+ montant = gr.Slider(label="Top-up amount", minimum=20, step=1, interactive=True, value=1, maximum= 500000)
85
+ data_volume = gr.Slider(label="Number of connections", minimum=0, step=1, interactive=True, value=1, maximum= 2000000)
86
+
87
+ # Activity
88
+ gr.Markdown("**Activity Levels**")
89
+ with gr.Row():
90
+ frequence_rech = gr.Slider(label="Recharge Frequency", minimum=1, step=1, interactive=True, value=1, maximum=220)
91
+ freq_top_pack = gr.Slider(label="Top Package Activation Frequency", minimum=1, step=1, interactive=True, value=1, maximum=1050)
92
+ regularity = gr.Slider(label="Days of Activity (out of 90 days)", minimum=1, step=1, interactive=True, value=1, maximum=90)
93
+ tenure = gr.Dropdown(label="Tenure (time on the network)", choices=["D 3-6 month", "E 6-9 month", "F 9-12 month", "G 12-15 month", "H 15-18 month", "I 18-21 month", "J 21-24 month", "K > 24 month"], value="K > 24 month")
94
+
95
+ # Income
96
+ gr.Markdown("**Income from the Customer**")
97
+ with gr.Row():
98
+ arpu_segment = gr.Slider(label="Income over the last 90 days", step=1, maximum=287000, interactive=True)
99
+ frequence = gr.Slider(label="Number of times the customer has made an income", step=1, minimum=1, maximum=91, interactive=True)
100
+
101
+ # Output Prediction
102
+ output = gr.Label("Awaiting Submission...")
103
+ submit_button = gr.Button("Submit")
104
+
105
+ # Expander for more info on columns
106
+ with gr.Accordion("Open for information on inputs"):
107
+ gr.Markdown("""This app receives the following as inputs and processes them to return the prediction on whether a customer, given the inputs, will churn or not.
108
+ - arpu_segment: income over 90 days / 3
109
+ - churn: variable to predict - target
110
+ - data_volume: number of connections
111
+ - frequence: number of times the client has made an income
112
+ - frequence_rech: number of times the client recharged
113
+ - freq_top_pack: number of times the client has activated the top pack packages
114
+ - montant: top-up amount
115
+ - regularity: number of times the client is active for 90 days
116
+ - tenure: duration in the network
117
+ """)
118
+
119
+ submit_button.click(fn = process_and_predict,
120
+ outputs = output,
121
+ inputs=[tenure, montant, frequence_rech, arpu_segment, frequence, data_volume, regularity, freq_top_pack])
122
+
123
+ turn_on_the_gradio.launch(favicon_path= r"src\app_thumbnail.png", inbrowser= True, share= True)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ docker
2
+ gradio
3
+ lightgbm==2.2.3
4
+ numpy==1.23.3
5
+ pandas==1.4.4
6
+ plotly
7
+ re==2.2.1
8
+ regex
9
+ requests==2.25.1
10
+ scikit-learn==1.0.2
11
+ seaborn==0.11.2
12
+ wheel
13
+ xgboost==1.7.4
src/Gradio_App_toolkit ADDED
Binary file (370 kB). View file
 
src/app_thumbnail.png ADDED
src/xgb_model.json ADDED
The diff for this file is too large to render. See raw diff