Spaces:
Sleeping
Sleeping
update
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("C:/Users/Gregory Arthur/Desktop/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()
|
85 |
+
|
86 |
+
with block:
|
87 |
+
input=[gr.inputs.Slider(minimum=0, maximum= 1, step=1, label="SeniorCitizen: Select 1 for Yes and 0 for No"),
|
88 |
+
gr.inputs.Radio(["Yes", "No"], label="Partner: Do You Have a Partner?"),
|
89 |
+
gr.inputs.Radio(["Yes", "No"], label="Dependents: Do You Have a Dependent?"),
|
90 |
+
gr.inputs.Number(label="tenure: How Long Have You Been with Vodafone in Months?"),
|
91 |
+
gr.inputs.Radio(["DSL", "Fiber optic", "No"], label="InternetService"),
|
92 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="OnlineSecurity"),
|
93 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="OnlineBackup"),
|
94 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="DeviceProtection"),
|
95 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="TechSupport"),
|
96 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="StreamingTV"),
|
97 |
+
gr.inputs.Radio(["Yes", "No", "No internet service"], label="StreamingMovies"),
|
98 |
+
gr.inputs.Radio(["Month-to-month", "One year", "Two year"], label="Contract"),
|
99 |
+
gr.inputs.Radio(["Yes", "No"], label="PaperlessBilling"),
|
100 |
+
gr.inputs.Radio([
|
101 |
+
"Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"
|
102 |
+
], label="PaymentMethod"),
|
103 |
+
gr.inputs.Number(label="MonthlyCharges"),
|
104 |
+
gr.inputs.Number(label="TotalCharges")]
|
105 |
+
|
106 |
+
output= gr.outputs.HighlightedText(color_map={
|
107 |
+
"Customer will not Churn": "green",
|
108 |
+
"Customer will churn": "red"}, label= "Your Output", show_legend=True)
|
109 |
+
gr.themes="freddyaboulton/dracula_revamped"
|
110 |
+
|
111 |
+
predict_btn= gr.Button("Predict")
|
112 |
+
|
113 |
+
predict_btn.click(fn= predict_churn, inputs= input, outputs=output)
|
114 |
+
|
115 |
+
block.launch()
|
116 |
+
|
117 |
+
|