Spaces:
Sleeping
Sleeping
Files for my app
Browse files- LICENSE +21 -0
- README.md +36 -13
- UI_Design&Screenshots/Default_UI.png +0 -0
- UI_Design&Screenshots/UI_with_output.png +0 -0
- requirements.txt +5 -0
- src/Churn_App.py +115 -0
- src/pipe.pkl +0 -0
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Jauloma
|
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.
|
README.md
CHANGED
@@ -1,13 +1,36 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Customer Churn Prediction App with Gradio
|
2 |
+
## Introduction
|
3 |
+
A Gradio-based churn prediction tool is based on a machine learning algorithm. This project is more of a quest to figure out how to integrate it into a web app with a user-friendly interface, in this case, Gradio. The goal is to create an interface that allows consumers to engage with an ML model independent of their level of expertise in machine learning.
|
4 |
+
|
5 |
+
## The Process
|
6 |
+
The procedure begins with exporting the essential items from the notebook, followed by correctly designing an interface, importing the necessary objects for modeling, and then writing the code to process inputs. The procedure can be summarized as follows:
|
7 |
+
- Import machine learning components into the app script.
|
8 |
+
- Create an interface,
|
9 |
+
- Create a function to handle inputs.
|
10 |
+
- Values are passed through the interface.
|
11 |
+
- Restore these values in the backend,
|
12 |
+
- Apply the required processing,
|
13 |
+
- To produce predictions, submit the processed values to the ML model.
|
14 |
+
- Process the acquired predictions and present them on the interface.
|
15 |
+
|
16 |
+
# Installation
|
17 |
+
To setup and run this project you need to have Python3 installed on your system. Then you can clone this repo. At the repo's root, use the code from below which applies:
|
18 |
+
|
19 |
+
- Windows:
|
20 |
+
|
21 |
+
python -m venv venv; venv\Scripts\activate; python -m pip install -q --upgrade pip; python -m pip install -qr requirements.txt
|
22 |
+
|
23 |
+
- How to run the application:
|
24 |
+
In the Terminal type: python Churn_App.py
|
25 |
+
|
26 |
+
# Screenshots
|
27 |
+
| Churn_App default interface | Churn_App with output |
|
28 |
+
|-----------------------------------|---------------------------------|
|
29 |
+
| | |
|
30 |
+
|
31 |
+
|
32 |
+
# Author
|
33 |
+
- Felix Kiprotich
|
34 |
+
- https://www.linkedin.com/in/felix-kiprotich-a2ba1a1a4/
|
35 |
+
- https://huggingface.co/spaces/UholoDala/Churn_Prediction
|
36 |
+
|
UI_Design&Screenshots/Default_UI.png
ADDED
![]() |
UI_Design&Screenshots/UI_with_output.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy==1.23.3
|
3 |
+
pandas==1.4.4
|
4 |
+
regex
|
5 |
+
scikit-learn==1.0.2
|
src/Churn_App.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
from sklearn.pipeline import Pipeline
|
5 |
+
from sklearn.impute import SimpleImputer
|
6 |
+
from sklearn.compose import ColumnTransformer
|
7 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
|
10 |
+
# Load the saved full pipeline from the file
|
11 |
+
full_pipeline = joblib.load('pipe.pkl')
|
12 |
+
|
13 |
+
# Define the predict function
|
14 |
+
def predict(gender, SeniorCitizen, Partner, Dependents, Contract, tenure, MonthlyCharges,
|
15 |
+
TotalCharges, PaymentMethod, PhoneService, MultipleLines, InternetService,
|
16 |
+
OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV,
|
17 |
+
StreamingMovies, PaperlessBilling):
|
18 |
+
# Create a DataFrame from the input data
|
19 |
+
input_data = pd.DataFrame({
|
20 |
+
'gender': [gender] if gender else ['Male'], # Replace None with default value
|
21 |
+
'SeniorCitizen': [SeniorCitizen] if SeniorCitizen is not None else [0], # Replace None with default value
|
22 |
+
'Partner': [Partner] if Partner else ['No'], # Replace None with default value
|
23 |
+
'Dependents': [Dependents] if Dependents else ['No'], # Replace None with default value
|
24 |
+
'tenure': [tenure] if tenure else [1], # Replace None with default value
|
25 |
+
'PhoneService': [PhoneService] if PhoneService else ['Yes'], # Replace None with default value
|
26 |
+
'MultipleLines': [MultipleLines] if MultipleLines else ['No'], # Replace None with default value
|
27 |
+
'InternetService': [InternetService] if InternetService else ['DSL'], # Replace None with default value
|
28 |
+
'OnlineSecurity': [OnlineSecurity] if OnlineSecurity else ['No'], # Replace None with default value
|
29 |
+
'OnlineBackup': [OnlineBackup] if OnlineBackup else ['No'], # Replace None with default value
|
30 |
+
'DeviceProtection': [DeviceProtection] if DeviceProtection else ['No'], # Replace None with default value
|
31 |
+
'TechSupport': [TechSupport] if TechSupport else ['No'], # Replace None with default value
|
32 |
+
'StreamingTV': [StreamingTV] if StreamingTV else ['No'], # Replace None with default value
|
33 |
+
'StreamingMovies': [StreamingMovies] if StreamingMovies else ['No'], # Replace None with default value
|
34 |
+
'Contract': [Contract] if Contract else ['Month-to-month'], # Replace None with default value
|
35 |
+
'PaperlessBilling': [PaperlessBilling] if PaperlessBilling else ['No'], # Replace None with default value
|
36 |
+
'PaymentMethod': [PaymentMethod] if PaymentMethod else ['Electronic check'], # Replace None with default value
|
37 |
+
'MonthlyCharges': [MonthlyCharges] if MonthlyCharges else [0.0], # Replace None with default value
|
38 |
+
'TotalCharges': [TotalCharges] if TotalCharges else [0.0] # Replace None with default value
|
39 |
+
})
|
40 |
+
|
41 |
+
|
42 |
+
# Make predictions using the loaded logistic regression model
|
43 |
+
predictions = full_pipeline.predict(input_data)
|
44 |
+
|
45 |
+
#return predictions[0]
|
46 |
+
if predictions[0] == "Yes":
|
47 |
+
return "Churn"
|
48 |
+
else:
|
49 |
+
return "Not Churn"
|
50 |
+
|
51 |
+
# Setting Gradio App Interface
|
52 |
+
with gr.Blocks(css=".gradio-container {background-color: grey}") as demo:
|
53 |
+
gr.Markdown("# Teleco Customer Churn Prediction #\n*This App allows the user to predict whether a customer will churn or not by entering values in the given fields. Any field left blank takes the default value.*")
|
54 |
+
|
55 |
+
# Receiving ALL Input Data here
|
56 |
+
gr.Markdown("**Demographic Data**")
|
57 |
+
with gr.Row():
|
58 |
+
gender = gr.Dropdown(label="Gender", choices=["Male", "Female"])
|
59 |
+
SeniorCitizen = gr.Radio(label="Senior Citizen", choices=[1, 0])
|
60 |
+
Partner = gr.Radio(label="Partner", choices=["Yes", "No"])
|
61 |
+
Dependents = gr.Radio(label="Dependents", choices=["Yes", "No"])
|
62 |
+
|
63 |
+
gr.Markdown("**Service Length and Charges (USD)**")
|
64 |
+
with gr.Row():
|
65 |
+
Contract = gr.Dropdown(label="Contract", choices=["Month-to-month", "One year", "Two year"])
|
66 |
+
tenure = gr.Slider(label="Tenure (months)", minimum=1, step=1, interactive=True)
|
67 |
+
MonthlyCharges = gr.Slider(label="Monthly Charges", step=0.05)
|
68 |
+
TotalCharges = gr.Slider(label="Total Charges", step=0.05)
|
69 |
+
|
70 |
+
# Phone Service Usage part
|
71 |
+
gr.Markdown("**Phone Service Usage**")
|
72 |
+
with gr.Row():
|
73 |
+
PhoneService = gr.Radio(label="Phone Service", choices=["Yes", "No"])
|
74 |
+
MultipleLines = gr.Dropdown(label="Multiple Lines", choices=[
|
75 |
+
"Yes", "No", "No phone service"])
|
76 |
+
|
77 |
+
# Internet Service Usage part
|
78 |
+
gr.Markdown("**Internet Service Usage**")
|
79 |
+
with gr.Row():
|
80 |
+
InternetService = gr.Dropdown(label="Internet Service", choices=["DSL", "Fiber optic", "No"])
|
81 |
+
OnlineSecurity = gr.Dropdown(label="Online Security", choices=["Yes", "No", "No internet service"])
|
82 |
+
OnlineBackup = gr.Dropdown(label="Online Backup", choices=["Yes", "No", "No internet service"])
|
83 |
+
DeviceProtection = gr.Dropdown(label="Device Protection", choices=["Yes", "No", "No internet service"])
|
84 |
+
TechSupport = gr.Dropdown(label="Tech Support", choices=["Yes", "No", "No internet service"])
|
85 |
+
StreamingTV = gr.Dropdown(label="TV Streaming", choices=["Yes", "No", "No internet service"])
|
86 |
+
StreamingMovies = gr.Dropdown(label="Movie Streaming", choices=["Yes", "No", "No internet service"])
|
87 |
+
|
88 |
+
# Billing and Payment part
|
89 |
+
gr.Markdown("**Billing and Payment**")
|
90 |
+
with gr.Row():
|
91 |
+
PaperlessBilling = gr.Radio(
|
92 |
+
label="Paperless Billing", choices=["Yes", "No"])
|
93 |
+
PaymentMethod = gr.Dropdown(label="Payment Method", choices=["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"])
|
94 |
+
|
95 |
+
# Output Prediction
|
96 |
+
output = gr.Text(label="Outcome")
|
97 |
+
submit_button = gr.Button("Predict")
|
98 |
+
|
99 |
+
submit_button.click(fn= predict,
|
100 |
+
outputs= output,
|
101 |
+
inputs=[gender, SeniorCitizen, Partner, Dependents, Contract, tenure, MonthlyCharges, TotalCharges, PaymentMethod, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV, StreamingMovies, PaperlessBilling],
|
102 |
+
|
103 |
+
),
|
104 |
+
|
105 |
+
# Add the reset and flag buttons
|
106 |
+
|
107 |
+
def clear():
|
108 |
+
output.value = ""
|
109 |
+
return None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
110 |
+
|
111 |
+
clear_btn = gr.Button("Reset", variant="primary")
|
112 |
+
clear_btn.click(fn=clear, inputs=None, outputs=output)
|
113 |
+
|
114 |
+
|
115 |
+
demo.launch(inbrowser = True)
|
src/pipe.pkl
ADDED
Binary file (9.62 kB). View file
|
|