Spaces:
Sleeping
Sleeping
added app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Importing the libraries
|
2 |
+
import gradio as gr
|
3 |
+
import pickle
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
import joblib
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
#using joblib to load the model:
|
10 |
+
num_imputer = joblib.load('assets\\num_imputer.joblib') # loading the imputer
|
11 |
+
cat_imputer = joblib.load('assets\\cat_imputer.joblib') # loading the imputer
|
12 |
+
encoder = joblib.load('assets\\encoder.joblib') # loading the encoder
|
13 |
+
scaler = joblib.load('assets\\scaler.joblib') # loading the scaler
|
14 |
+
model = joblib.load('assets\\ml.joblib') # loading the model
|
15 |
+
|
16 |
+
|
17 |
+
# Create a function that applies the ML pipeline and makes predictions
|
18 |
+
def predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
|
19 |
+
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
|
20 |
+
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges):
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
# Create a dataframe with the input data
|
25 |
+
input_df = pd.DataFrame({
|
26 |
+
'gender': [gender],
|
27 |
+
'SeniorCitizen': [SeniorCitizen],
|
28 |
+
'Partner': [Partner],
|
29 |
+
'Dependents': [Dependents],
|
30 |
+
'tenure': [tenure],
|
31 |
+
'PhoneService': [PhoneService],
|
32 |
+
'MultipleLines': [MultipleLines],
|
33 |
+
'InternetService': [InternetService],
|
34 |
+
'OnlineSecurity': [OnlineSecurity],
|
35 |
+
'OnlineBackup': [OnlineBackup],
|
36 |
+
'DeviceProtection': [DeviceProtection],
|
37 |
+
'TechSupport': [TechSupport],
|
38 |
+
'StreamingTV': [StreamingTV],
|
39 |
+
'StreamingMovies': [StreamingMovies],
|
40 |
+
'Contract': [Contract],
|
41 |
+
'PaperlessBilling': [PaperlessBilling],
|
42 |
+
'PaymentMethod': [PaymentMethod],
|
43 |
+
'MonthlyCharges': [MonthlyCharges],
|
44 |
+
'TotalCharges': [TotalCharges]
|
45 |
+
|
46 |
+
})
|
47 |
+
|
48 |
+
# Create a list with the categorical and numerical columns
|
49 |
+
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
50 |
+
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
51 |
+
|
52 |
+
# Impute the missing values
|
53 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
54 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
55 |
+
|
56 |
+
# Encode the categorical columns
|
57 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
58 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
59 |
+
|
60 |
+
# Scale the numerical columns
|
61 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
62 |
+
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
63 |
+
|
64 |
+
|
65 |
+
#joining the cat encoded and num scaled
|
66 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
67 |
+
|
68 |
+
final_df = final_df.reindex(columns=['SeniorCitizen','tenure','MonthlyCharges','TotalCharges',
|
69 |
+
'gender_Female','gender_Male','Partner_No','Partner_Yes','Dependents_No','Dependents_Yes','PhoneService_No',
|
70 |
+
'PhoneService_Yes','MultipleLines_No','MultipleLines_Yes','InternetService_DSL','InternetService_Fiber optic',
|
71 |
+
'InternetService_No','OnlineSecurity_No','OnlineSecurity_Yes','OnlineBackup_No','OnlineBackup_Yes','DeviceProtection_No',
|
72 |
+
'DeviceProtection_Yes','TechSupport_No','TechSupport_Yes','StreamingTV_No','StreamingTV_Yes','StreamingMovies_No',
|
73 |
+
'StreamingMovies_Yes','Contract_Month-to-month','Contract_One year','Contract_Two year','PaperlessBilling_No',
|
74 |
+
'PaperlessBilling_Yes','PaymentMethod_Bank transfer (automatic)','PaymentMethod_Credit card (automatic)','PaymentMethod_Electronic check',
|
75 |
+
'PaymentMethod_Mailed check'])
|
76 |
+
|
77 |
+
# Make predictions using the model
|
78 |
+
predict = model.predict(final_df)
|
79 |
+
|
80 |
+
|
81 |
+
prediction_label = "THIS CUSTOMER WILL CHURN" if predict.item() == "Yes" else "THIS CUSTOMER WILL NOT CHURN"
|
82 |
+
|
83 |
+
|
84 |
+
return prediction_label
|
85 |
+
|
86 |
+
#return predictions
|
87 |
+
|
88 |
+
#define the input interface
|
89 |
+
|
90 |
+
|
91 |
+
input_interface = []
|
92 |
+
|
93 |
+
with gr.Blocks(css=".gradio-container {background-color:silver}") as app:
|
94 |
+
title = gr.Label('VODAFONE CUSTOMER CHURN PREDICTION')
|
95 |
+
img = gr.Image("assets\\VODA.png").style(height= 210 , width= 1250)
|
96 |
+
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
gr.Markdown("This application provides predictions on whether a customer will churn or remain with the Company. Please enter the customer's information below and click PREDICT to view the prediction outcome.")
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column(scale=3.5, min_width=500):
|
103 |
+
input_interface = [
|
104 |
+
gr.components.Radio(['male', 'female'], label='What is your Gender?'),
|
105 |
+
gr.components.Number(label="Are you a Seniorcitizen? (No=0 and Yes=1), 55years and above"),
|
106 |
+
gr.components.Radio(['Yes', 'No'], label='Do you have a Partner?'),
|
107 |
+
gr.components.Dropdown(['No', 'Yes'], label='Do you have any Dependents?'),
|
108 |
+
gr.components.Number(label='Length of Tenure (No. of months with Vodafone)'),
|
109 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use Phone Service?'),
|
110 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use Multiple Lines?'),
|
111 |
+
gr.components.Radio(['DSL', 'Fiber optic', 'No'], label='Do you use Internet Service?'),
|
112 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use Online Security?'),
|
113 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use Online Backup?'),
|
114 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use Device Protection?'),
|
115 |
+
gr.components.Radio(['No', 'Yes'], label='Do you use the Tech Support?'),
|
116 |
+
gr.components.Radio(['No', 'Yes'], label='Do you Streaming TV?'),
|
117 |
+
gr.components.Radio(['No', 'Yes'], label='Do you Streaming Movies?'),
|
118 |
+
gr.components.Dropdown(['Month-to-month', 'One year', 'Two year'], label='Please what Contract Type do you Subscribe to?'),
|
119 |
+
gr.components.Radio(['Yes', 'No'], label='Do you use Paperless Billing?'),
|
120 |
+
gr.components.Dropdown(['Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
121 |
+
'Credit card (automatic)'], label='What type of Payment Method do you use please?'),
|
122 |
+
gr.components.Number(label="How much is you Monthly Charges?"),
|
123 |
+
gr.components.Number(label="How much is your Total Charges?")
|
124 |
+
]
|
125 |
+
|
126 |
+
with gr.Row():
|
127 |
+
predict_btn = gr.Button('Predict')
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
# Define the output interfaces
|
132 |
+
output_interface = gr.Label(label="churn", type="label", style="font-weight: bold; font-size: larger; color: red")
|
133 |
+
|
134 |
+
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
135 |
+
|
136 |
+
|
137 |
+
app.launch(share=False)
|