App2.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importing required Libraries
|
| 2 |
+
from IPython.utils.py3compat import encode
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import pickle
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Loading Machine Learning Objects
|
| 10 |
+
def load_saved_objets(filepath='ML_items'):
|
| 11 |
+
"Function to load saved objects"
|
| 12 |
+
|
| 13 |
+
with open(filepath, 'rb') as file:
|
| 14 |
+
loaded_object = pickle.load(file)
|
| 15 |
+
|
| 16 |
+
return loaded_object
|
| 17 |
+
|
| 18 |
+
# Instantiating ML_items
|
| 19 |
+
loaded_object = load_saved_objets()
|
| 20 |
+
pipeline_of_my_app = loaded_object["pipeline"]
|
| 21 |
+
num_cols = loaded_object['numeric_columns']
|
| 22 |
+
cat_cols = loaded_object['categorical_columns']
|
| 23 |
+
encoder_categories = loaded_object["encoder_categories"]
|
| 24 |
+
|
| 25 |
+
# Main function to collect the inputs process them and outpuT the predicition
|
| 26 |
+
def predict_churn(
|
| 27 |
+
TotalCharges,
|
| 28 |
+
MonthlyCharges,
|
| 29 |
+
tenure,
|
| 30 |
+
StreamingTV,
|
| 31 |
+
PaperlessBilling,
|
| 32 |
+
DeviceProtection,
|
| 33 |
+
TechSupport,
|
| 34 |
+
InternetService,
|
| 35 |
+
OnlineSecurity,
|
| 36 |
+
StreamingMovies,
|
| 37 |
+
PaymentMethod,
|
| 38 |
+
Dependents,
|
| 39 |
+
Partner,
|
| 40 |
+
OnlineBackup,
|
| 41 |
+
gender,
|
| 42 |
+
SeniorCitizen,
|
| 43 |
+
MultipleLines,
|
| 44 |
+
Contract,
|
| 45 |
+
PhoneService,
|
| 46 |
+
):
|
| 47 |
+
|
| 48 |
+
df = pd.DataFrame(
|
| 49 |
+
[
|
| 50 |
+
[
|
| 51 |
+
TotalCharges,
|
| 52 |
+
MonthlyCharges,
|
| 53 |
+
tenure,
|
| 54 |
+
StreamingTV,
|
| 55 |
+
PaperlessBilling,
|
| 56 |
+
DeviceProtection,
|
| 57 |
+
TechSupport,
|
| 58 |
+
InternetService,
|
| 59 |
+
OnlineSecurity,
|
| 60 |
+
StreamingMovies,
|
| 61 |
+
PaymentMethod,
|
| 62 |
+
Dependents,
|
| 63 |
+
Partner
|
| 64 |
+
OnlineBackup,
|
| 65 |
+
gender,
|
| 66 |
+
SeniorCitizen,
|
| 67 |
+
MultipleLines,
|
| 68 |
+
Contract,
|
| 69 |
+
PhoneService,
|
| 70 |
+
]
|
| 71 |
+
],
|
| 72 |
+
columns= num_cols + cat_cols,
|
| 73 |
+
).replace("", np.nan)
|
| 74 |
+
|
| 75 |
+
df[cat_cols] = df[cat_cols].astype("object")
|
| 76 |
+
|
| 77 |
+
# Passing data to pipeline to make prediction
|
| 78 |
+
output = pipeline_of_my_app.predict(df)
|
| 79 |
+
|
| 80 |
+
# Labelling Model output
|
| 81 |
+
if output == 0:
|
| 82 |
+
model_output = "No"
|
| 83 |
+
else:
|
| 84 |
+
model_output = "Yes"
|
| 85 |
+
|
| 86 |
+
return model_output
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# Setting up app interface and data inputs
|
| 90 |
+
inputs = []
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
|
| 94 |
+
# Setting Titles for App
|
| 95 |
+
gr.Markdown("<h2 style='text-align: center;'> Customer Churn Prediction App </h2> ", unsafe_allow_html=True)
|
| 96 |
+
gr.Markdown("<h6 style='text-align: center;'> (Fill in the details below and click on PREDICT button to make a prediction for Customer Churn) </h6> ", unsafe_allow_html=True)
|
| 97 |
+
|
| 98 |
+
with gr.Column(): #main frame
|
| 99 |
+
|
| 100 |
+
with gr.Row(): #col 1 : for num features
|
| 101 |
+
|
| 102 |
+
for i in num_cols:
|
| 103 |
+
inputs.append(gr.Number(label=f"Input {i} "))
|
| 104 |
+
|
| 105 |
+
with gr.Row(): #col 2 : for cat features
|
| 106 |
+
|
| 107 |
+
for (lab, choices) in zip(cat_cols, encoder_categories):
|
| 108 |
+
inputs.append(gr.inputs.Dropdown(
|
| 109 |
+
choices=choices.tolist(),
|
| 110 |
+
type="value",
|
| 111 |
+
label=f"Select {lab}",
|
| 112 |
+
default=choices.tolist()[0],))
|
| 113 |
+
# Setting up preediction Button
|
| 114 |
+
with gr.Row():
|
| 115 |
+
make_prediction = gr.Button("Predict")
|
| 116 |
+
|
| 117 |
+
# Setting up prediction output row
|
| 118 |
+
with gr.Row():
|
| 119 |
+
output_prediction = gr.Text(label="Will Customer Churn?")
|
| 120 |
+
make_prediction.click(predict_churn, inputs, output_prediction)
|
| 121 |
+
|
| 122 |
+
# Launching app
|
| 123 |
+
demo.launch(
|
| 124 |
+
share=True,
|
| 125 |
+
# debug=True
|
| 126 |
+
)
|