File size: 4,583 Bytes
40f4c3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import joblib 
import pandas as pd
import numpy as np
import gradio as gr
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import SelectKBest
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.utils.class_weight import compute_class_weight
import gradio as gr
import joblib
import warnings

warnings.filterwarnings("ignore")

model= joblib.load("C:/Users/Gregory Arthur/Desktop/models/LR.joblib")

model

test= pd.read_csv("dataframes/Vodafone_churn.csv")
test

##testing our model
model.predict(test)

##creating a function to return a string depending on the output of the model

def classify(num):
    if num == 0:
        return "Customer will not Churn"
    else:
        return "Customer will churn"


"""creating a function for my gradion fn
defining my parameters which my fucntion will accept, and are the same as the features I trained my model on"""


def predict_churn(SeniorCitizen, Partner, Dependents, tenure, InternetService,
                  OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
                  StreamingTV, StreamingMovies, Contract, PaperlessBilling,
                  PaymentMethod, MonthlyCharges, TotalCharges): 

     
     ##in the code below, I am created a list of my input features

    input_data = [
        SeniorCitizen, Partner, Dependents, tenure, InternetService,
        OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
        StreamingTV, StreamingMovies, Contract, PaperlessBilling,
        PaymentMethod, MonthlyCharges, TotalCharges
    ]    
##I am changing my features into a dataframe since that is how I trained my model

    input_df = pd.DataFrame([input_data], columns=[
        "SeniorCitizen", "Partner", "Dependents", "tenure", "InternetService",
        "OnlineSecurity", "OnlineBackup", "DeviceProtection", "TechSupport",
        "StreamingTV", "StreamingMovies", "Contract", "PaperlessBilling",
        "PaymentMethod", "MonthlyCharges", "TotalCharges"
    ])


    pred = model.predict(input_df) ##I am making a prediction on the input data.

    output = classify(pred[0]) ## I am passing the first predction through my classify function I created earlier

    if output == "Customer will not Churn":
        return [(0, output)]
    else:
        return [(1, output)]   ##setting my function to return the binary classification and the written output

output = gr.outputs.HighlightedText(color_map={
    "Customer will not Churn": "green",
    "Customer will churn": "red"
}) ##assigning colors to the respective output 

##building my interface and wrapping my model in the function

##using gradio blocks to beautify my output

block= gr.Blocks()

with block:
    input=[gr.inputs.Slider(minimum=0, maximum= 1, step=1, label="SeniorCitizen: Select 1 for Yes and 0 for No"),
        gr.inputs.Radio(["Yes", "No"], label="Partner: Do You Have a Partner?"),
        gr.inputs.Radio(["Yes", "No"], label="Dependents: Do You Have a Dependent?"),
        gr.inputs.Number(label="tenure: How Long Have You Been with Vodafone in Months?"),
        gr.inputs.Radio(["DSL", "Fiber optic", "No"], label="InternetService"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="OnlineSecurity"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="OnlineBackup"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="DeviceProtection"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="TechSupport"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="StreamingTV"),
        gr.inputs.Radio(["Yes", "No", "No internet service"], label="StreamingMovies"),
        gr.inputs.Radio(["Month-to-month", "One year", "Two year"], label="Contract"),
        gr.inputs.Radio(["Yes", "No"], label="PaperlessBilling"),
        gr.inputs.Radio([
            "Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"
        ], label="PaymentMethod"),
        gr.inputs.Number(label="MonthlyCharges"),
        gr.inputs.Number(label="TotalCharges")]
     
    output= gr.outputs.HighlightedText(color_map={
     "Customer will not Churn": "green",
     "Customer will churn": "red"}, label= "Your Output", show_legend=True)
    gr.themes="freddyaboulton/dracula_revamped"
     
    predict_btn= gr.Button("Predict")
     
    predict_btn.click(fn= predict_churn, inputs= input, outputs=output)

block.launch()