Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
from sklearn.linear_model import LogisticRegression
|
8 |
+
from sklearn.feature_selection import SelectKBest
|
9 |
+
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
|
10 |
+
from sklearn.impute import SimpleImputer
|
11 |
+
from sklearn.pipeline import Pipeline
|
12 |
+
from sklearn.utils.class_weight import compute_class_weight
|
13 |
+
import gradio as gr
|
14 |
+
import joblib
|
15 |
+
import warnings
|
16 |
+
|
17 |
+
warnings.filterwarnings("ignore")
|
18 |
+
|
19 |
+
model= joblib.load("models/LR.joblib")
|
20 |
+
|
21 |
+
model
|
22 |
+
|
23 |
+
test= pd.read_csv("dataframes/Vodafone_churn.csv")
|
24 |
+
test
|
25 |
+
|
26 |
+
##testing our model
|
27 |
+
model.predict(test)
|
28 |
+
|
29 |
+
##creating a function to return a string depending on the output of the model
|
30 |
+
|
31 |
+
def classify(num):
|
32 |
+
if num == 0:
|
33 |
+
return "Customer will not Churn"
|
34 |
+
else:
|
35 |
+
return "Customer will churn"
|
36 |
+
|
37 |
+
|
38 |
+
"""creating a function for my gradion fn
|
39 |
+
defining my parameters which my fucntion will accept, and are the same as the features I trained my model on"""
|
40 |
+
|
41 |
+
|
42 |
+
def predict_churn(SeniorCitizen, Partner, Dependents, tenure, InternetService,
|
43 |
+
OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
|
44 |
+
StreamingTV, StreamingMovies, Contract, PaperlessBilling,
|
45 |
+
PaymentMethod, MonthlyCharges, TotalCharges):
|
46 |
+
|
47 |
+
|
48 |
+
##in the code below, I am created a list of my input features
|
49 |
+
|
50 |
+
input_data = [
|
51 |
+
SeniorCitizen, Partner, Dependents, tenure, InternetService,
|
52 |
+
OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
|
53 |
+
StreamingTV, StreamingMovies, Contract, PaperlessBilling,
|
54 |
+
PaymentMethod, MonthlyCharges, TotalCharges
|
55 |
+
]
|
56 |
+
##I am changing my features into a dataframe since that is how I trained my model
|
57 |
+
|
58 |
+
input_df = pd.DataFrame([input_data], columns=[
|
59 |
+
"SeniorCitizen", "Partner", "Dependents", "tenure", "InternetService",
|
60 |
+
"OnlineSecurity", "OnlineBackup", "DeviceProtection", "TechSupport",
|
61 |
+
"StreamingTV", "StreamingMovies", "Contract", "PaperlessBilling",
|
62 |
+
"PaymentMethod", "MonthlyCharges", "TotalCharges"
|
63 |
+
])
|
64 |
+
|
65 |
+
|
66 |
+
pred = model.predict(input_df) ##I am making a prediction on the input data.
|
67 |
+
|
68 |
+
output = classify(pred[0]) ## I am passing the first predction through my classify function I created earlier
|
69 |
+
|
70 |
+
if output == "Customer will not Churn":
|
71 |
+
return [(0, output)]
|
72 |
+
else:
|
73 |
+
return [(1, output)] ##setting my function to return the binary classification and the written output
|
74 |
+
|
75 |
+
output = gr.outputs.HighlightedText(color_map={
|
76 |
+
"Customer will not Churn": "green",
|
77 |
+
"Customer will churn": "red"
|
78 |
+
}) ##assigning colors to the respective output
|
79 |
+
|
80 |
+
##building my interface and wrapping my model in the function
|
81 |
+
|
82 |
+
##using gradio blocks to beautify my output
|
83 |
+
|
84 |
+
block= gr.Blocks(theme= "freddyaboulton/dracula_revamped") ##instatiating my blocks class
|
85 |
+
|
86 |
+
with block:
|
87 |
+
gr.Markdown(""" # Welcome to My Customer Churn Prediction App""")
|
88 |
+
|
89 |
+
input=[gr.inputs.Slider(minimum=0, maximum= 1, step=1, label="SeniorCitizen: Select 1 for Yes and 0 for No"),
|
90 |
+
gr.inputs.Radio(["Yes", "No"], label="Partner: Do You Have a Partner?"),
|
91 |
+
gr.inputs.Radio(["Yes", "No"], label="Dependents: Do You Have a Dependent?"),
|
92 |
+
gr.inputs.Number(label="tenure: How Long Have You Been with Vodafone in Months?"),
|
93 |
+
gr.inputs.Radio(["DSL", "Fiber optic", "No"], label="What Internet Service Do You Use?"),
|
94 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Have Online Security?"),
|
95 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Have Any Online Backup Service?"),
|
96 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Use Any Device Protection?"),
|
97 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Use TechSupport?"),
|
98 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Stream TV?"),
|
99 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="Do You Stream Movies?"),
|
100 |
+
gr.inputs.Radio(["Month-to-month", "One year", "Two year"], label="What Is Your Contract Type?"),
|
101 |
+
gr.inputs.Radio(["Yes", "No"], label=" Do You Use Paperless Billing?"),
|
102 |
+
gr.inputs.Radio([
|
103 |
+
"Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"
|
104 |
+
], label="What Payment Method Do You Use?"),
|
105 |
+
gr.inputs.Number(label="What is you Monthly Charges?"),
|
106 |
+
gr.inputs.Number(label="How Much Is Your Total Charges?")]
|
107 |
+
|
108 |
+
output= gr.outputs.HighlightedText(color_map={
|
109 |
+
"Customer will not Churn": "green",
|
110 |
+
"Customer will churn": "red"}, label= "Your Output")
|
111 |
+
predict_btn= gr.Button("Predict")
|
112 |
+
|
113 |
+
predict_btn.click(fn= predict_churn, inputs= input, outputs=output)
|
114 |
+
|
115 |
+
block.launch()
|
116 |
+
|
117 |
+
|