Spaces:
Runtime error
Runtime error
upload app.py
Browse files
app.py.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import joblib
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
|
9 |
+
num_imputer = joblib.load('numerical_imputer.joblib')
|
10 |
+
cat_imputer = joblib.load('categorical_imputer.joblib')
|
11 |
+
encoder = joblib.load('encoder.joblib')
|
12 |
+
scaler = joblib.load('scaler.joblib')
|
13 |
+
model = joblib.load('Final_model.joblib')
|
14 |
+
|
15 |
+
|
16 |
+
# Create a function that applies the ML pipeline and makes predictions
|
17 |
+
def predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
|
18 |
+
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
|
19 |
+
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges):
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
# Create a dataframe with the input data
|
24 |
+
input_df = pd.DataFrame({
|
25 |
+
'gender': [gender],
|
26 |
+
'SeniorCitizen': [SeniorCitizen],
|
27 |
+
'Partner': [Partner],
|
28 |
+
'Dependents': [Dependents],
|
29 |
+
'tenure': [tenure],
|
30 |
+
'PhoneService': [PhoneService],
|
31 |
+
'MultipleLines': [MultipleLines],
|
32 |
+
'InternetService': [InternetService],
|
33 |
+
'OnlineSecurity': [OnlineSecurity],
|
34 |
+
'OnlineBackup': [OnlineBackup],
|
35 |
+
'DeviceProtection': [DeviceProtection],
|
36 |
+
'TechSupport': [TechSupport],
|
37 |
+
'StreamingTV': [StreamingTV],
|
38 |
+
'StreamingMovies': [StreamingMovies],
|
39 |
+
'Contract': [Contract],
|
40 |
+
'PaperlessBilling': [PaperlessBilling],
|
41 |
+
'PaymentMethod': [PaymentMethod],
|
42 |
+
'MonthlyCharges': [MonthlyCharges],
|
43 |
+
'TotalCharges': [TotalCharges]
|
44 |
+
|
45 |
+
})
|
46 |
+
|
47 |
+
# Selecting categorical and numerical columns separately
|
48 |
+
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
49 |
+
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
50 |
+
|
51 |
+
# Apply the imputers on the input data
|
52 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
53 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
54 |
+
|
55 |
+
# Encode the categorical columns
|
56 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
57 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
58 |
+
|
59 |
+
# Scale the numerical columns
|
60 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
61 |
+
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
62 |
+
|
63 |
+
|
64 |
+
#joining the cat encoded and num scaled
|
65 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
66 |
+
|
67 |
+
final_df = final_df.reindex(columns=['SeniorCitizen','tenure','MonthlyCharges','TotalCharges',
|
68 |
+
'gender_Female','gender_Male','Partner_No','Partner_Yes','Dependents_No','Dependents_Yes','PhoneService_No',
|
69 |
+
'PhoneService_Yes','MultipleLines_No','MultipleLines_Yes','InternetService_DSL','InternetService_Fiber optic',
|
70 |
+
'InternetService_No','OnlineSecurity_No','OnlineSecurity_Yes','OnlineBackup_No','OnlineBackup_Yes','DeviceProtection_No',
|
71 |
+
'DeviceProtection_Yes','TechSupport_No','TechSupport_Yes','StreamingTV_No','StreamingTV_Yes','StreamingMovies_No',
|
72 |
+
'StreamingMovies_Yes','Contract_Month-to-month','Contract_One year','Contract_Two year','PaperlessBilling_No',
|
73 |
+
'PaperlessBilling_Yes','PaymentMethod_Bank transfer (automatic)','PaymentMethod_Credit card (automatic)','PaymentMethod_Electronic check',
|
74 |
+
'PaymentMethod_Mailed check'])
|
75 |
+
|
76 |
+
# Make predictions using the model
|
77 |
+
predictions = model.predict(final_df)
|
78 |
+
|
79 |
+
# Make predictions using the model
|
80 |
+
#predictions = model.predict(final_df)
|
81 |
+
|
82 |
+
# Convert the numpy array to an integer
|
83 |
+
#prediction_label = int(predictions.item())
|
84 |
+
|
85 |
+
prediction_label = "Beware!!! This customer is likely to Churn" if predictions.item() == "Yes" else "This customer is Not likely churn"
|
86 |
+
|
87 |
+
|
88 |
+
return prediction_label
|
89 |
+
|
90 |
+
#return predictions
|
91 |
+
|
92 |
+
input_interface=[]
|
93 |
+
with gr.Blocks(css=".gradio-container {background-color: powderblue}") as app:
|
94 |
+
img = gr.Image("C:/Users/user/Documents/AZUBI PROGRAM/CAREER ACELERATOR/LP4-buiding an app/Gradio/lp4_part2-1/telecom churn.png").style(height='13')
|
95 |
+
|
96 |
+
Title=gr.Label('CUSTOMER CHURN PREDICTION APP')
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
Title
|
100 |
+
with gr.Row():
|
101 |
+
img
|
102 |
+
|
103 |
+
#with gr.Blocks() as app:
|
104 |
+
# with gr.Blocks(css=".gradio-interface-container {background-color: powderblue}"):
|
105 |
+
#with gr.Row():
|
106 |
+
# gr.Label('Customer Churn Prediction Model')
|
107 |
+
with gr.Row():
|
108 |
+
gr.Markdown("This app predicts whether a customer will leave your company or not. Enter the details of the customer below to see the result")
|
109 |
+
|
110 |
+
#with gr.Row():
|
111 |
+
#gr.Label('This app predicts whether a customer will leave your company or not. Enter the details of the customer below to see the result')
|
112 |
+
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column(scale=3, min_width=600):
|
116 |
+
|
117 |
+
input_interface = [
|
118 |
+
gr.components.Radio(['male', 'female'], label='Select your gender'),
|
119 |
+
gr.components.Number(label="Are you a Seniorcitizen; No=0 and Yes=1"),
|
120 |
+
gr.components.Radio(['Yes', 'No'], label='Do you have Partner'),
|
121 |
+
gr.components.Dropdown(['No', 'Yes'], label='Do you have any Dependents? '),
|
122 |
+
gr.components.Number(label='Lenght of tenure (no. of months with Telco)'),
|
123 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have PhoneService? '),
|
124 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have MultipleLines'),
|
125 |
+
gr.components.Radio(['DSL', 'Fiber optic', 'No'], label='Do you have InternetService'),
|
126 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have OnlineSecurity?'),
|
127 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have OnlineBackup?'),
|
128 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have DeviceProtection?'),
|
129 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have TechSupport?'),
|
130 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have StreamingTV?'),
|
131 |
+
gr.components.Radio(['No', 'Yes'], label='Do you have StreamingMovies?'),
|
132 |
+
gr.components.Dropdown(['Month-to-month', 'One year', 'Two year'], label='which Contract do you use?'),
|
133 |
+
gr.components.Radio(['Yes', 'No'], label='Do you prefer PaperlessBilling?'),
|
134 |
+
gr.components.Dropdown(['Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
135 |
+
'Credit card (automatic)'], label='Which PaymentMethod do you prefer?'),
|
136 |
+
gr.components.Number(label="Enter monthly charges"),
|
137 |
+
gr.components.Number(label="Enter total charges")
|
138 |
+
]
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
submit_btn = gr.Button('Submit')
|
142 |
+
|
143 |
+
predict_btn = gr.Button('Predict')
|
144 |
+
|
145 |
+
# Define the output interfaces
|
146 |
+
output_interface = gr.Label(label="churn")
|
147 |
+
|
148 |
+
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
149 |
+
|
150 |
+
|
151 |
+
|
152 |
+
|
153 |
+
app.launch(share=True)
|