Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import joblib
|
|
|
4 |
from sklearn.pipeline import Pipeline
|
5 |
from sklearn.impute import SimpleImputer
|
6 |
from sklearn.compose import ColumnTransformer
|
@@ -39,6 +40,88 @@ def predict(gender, SeniorCitizen, Partner, Dependents, Contract, tenure, Monthl
|
|
39 |
})
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Make predictions using the loaded logistic regression model
|
43 |
predictions = full_pipeline.predict(input_data)
|
44 |
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import joblib
|
4 |
+
import numpy as np
|
5 |
from sklearn.pipeline import Pipeline
|
6 |
from sklearn.impute import SimpleImputer
|
7 |
from sklearn.compose import ColumnTransformer
|
|
|
40 |
})
|
41 |
|
42 |
|
43 |
+
# Make predictions using the loaded logistic regression model
|
44 |
+
#predict probabilities
|
45 |
+
predictions = full_pipeline.predict_proba(input_data)
|
46 |
+
#take the index of the maximum probability
|
47 |
+
index=np.argmax(predictions)
|
48 |
+
higher_pred_prob=round((predictions[0][index])*100)
|
49 |
+
|
50 |
+
|
51 |
+
#return predictions[0]
|
52 |
+
print(f'[Info] Predicted probabilities{predictions},{full_pipeline.classes_}')
|
53 |
+
if full_pipeline.classes_[index] == "Yes":
|
54 |
+
return f"This Customer is likely to Churn\nWe are {higher_pred_prob}% confident about this prediction"
|
55 |
+
else:
|
56 |
+
return f"This Customer is Not likely to Churn \nWe are {higher_pred_prob}% confident about this prediction"
|
57 |
+
|
58 |
+
# Setting Gradio App Interface
|
59 |
+
with gr.Blocks(css=".gradio-container {background-color: grey}",theme=gr.themes.Base(primary_hue='blue'),title='Uriel') as demo:
|
60 |
+
gr.Markdown("# Teleco Customer Churn Prediction #\n*This App allows the user to predict whether a customer will churn or not by entering values in the given fields. Any field left blank takes the default value.*")
|
61 |
+
|
62 |
+
# Receiving ALL Input Data here
|
63 |
+
gr.Markdown("**Demographic Data**")
|
64 |
+
with gr.Row():
|
65 |
+
gender = gr.Dropdown(label="Gender", choices=["Male", "Female"])
|
66 |
+
SeniorCitizen = gr.Radio(label="Senior Citizen", choices=[1, 0])
|
67 |
+
Partner = gr.Radio(label="Partner", choices=["Yes", "No"])
|
68 |
+
Dependents = gr.Radio(label="Dependents", choices=["Yes", "No"])
|
69 |
+
|
70 |
+
gr.Markdown("**Service Length and Charges (USD)**")
|
71 |
+
with gr.Row():
|
72 |
+
Contract = gr.Dropdown(label="Contract", choices=["Month-to-month", "One year", "Two year"])
|
73 |
+
tenure = gr.Slider(label="Tenure (months)", minimum=1, step=1, interactive=True)
|
74 |
+
MonthlyCharges = gr.Slider(label="Monthly Charges", step=0.05)
|
75 |
+
TotalCharges = gr.Slider(label="Total Charges", step=0.05)
|
76 |
+
|
77 |
+
# Phone Service Usage part
|
78 |
+
gr.Markdown("**Phone Service Usage**")
|
79 |
+
with gr.Row():
|
80 |
+
PhoneService = gr.Radio(label="Phone Service", choices=["Yes", "No"])
|
81 |
+
MultipleLines = gr.Dropdown(label="Multiple Lines", choices=[
|
82 |
+
"Yes", "No", "No phone service"])
|
83 |
+
|
84 |
+
# Internet Service Usage part
|
85 |
+
gr.Markdown("**Internet Service Usage**")
|
86 |
+
with gr.Row():
|
87 |
+
InternetService = gr.Dropdown(label="Internet Service", choices=["DSL", "Fiber optic", "No"])
|
88 |
+
OnlineSecurity = gr.Dropdown(label="Online Security", choices=["Yes", "No", "No internet service"])
|
89 |
+
OnlineBackup = gr.Dropdown(label="Online Backup", choices=["Yes", "No", "No internet service"])
|
90 |
+
DeviceProtection = gr.Dropdown(label="Device Protection", choices=["Yes", "No", "No internet service"])
|
91 |
+
TechSupport = gr.Dropdown(label="Tech Support", choices=["Yes", "No", "No internet service"])
|
92 |
+
StreamingTV = gr.Dropdown(label="TV Streaming", choices=["Yes", "No", "No internet service"])
|
93 |
+
StreamingMovies = gr.Dropdown(label="Movie Streaming", choices=["Yes", "No", "No internet service"])
|
94 |
+
|
95 |
+
# Billing and Payment part
|
96 |
+
gr.Markdown("**Billing and Payment**")
|
97 |
+
with gr.Row():
|
98 |
+
PaperlessBilling = gr.Radio(
|
99 |
+
label="Paperless Billing", choices=["Yes", "No"])
|
100 |
+
PaymentMethod = gr.Dropdown(label="Payment Method", choices=["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"])
|
101 |
+
|
102 |
+
# Output Prediction
|
103 |
+
output = gr.Text(label="Outcome")
|
104 |
+
submit_button = gr.Button("Predict")
|
105 |
+
|
106 |
+
submit_button.click(fn= predict,
|
107 |
+
outputs= output,
|
108 |
+
inputs=[gender, SeniorCitizen, Partner, Dependents, Contract, tenure, MonthlyCharges, TotalCharges, PaymentMethod, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV, StreamingMovies, PaperlessBilling],
|
109 |
+
|
110 |
+
),
|
111 |
+
|
112 |
+
# Add the reset and flag buttons
|
113 |
+
|
114 |
+
def clear():
|
115 |
+
output.value = ""
|
116 |
+
return 'Predicted values have been reset'
|
117 |
+
|
118 |
+
clear_btn = gr.Button("Reset", variant="primary")
|
119 |
+
clear_btn.click(fn=clear, inputs=None, outputs=output)
|
120 |
+
|
121 |
+
|
122 |
+
demo.launch(inbrowser = True)
|
123 |
+
|
124 |
+
|
125 |
# Make predictions using the loaded logistic regression model
|
126 |
predictions = full_pipeline.predict(input_data)
|
127 |
|