Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
7 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
|
10 |
+
# Load the saved full pipeline from the file
|
11 |
+
full_pipeline = joblib.load('pipe.pkl')
|
12 |
+
|
13 |
+
# Define the predict function
|
14 |
+
def predict(gender, SeniorCitizen, Partner, Dependents, Contract, tenure, MonthlyCharges,
|
15 |
+
TotalCharges, PaymentMethod, PhoneService, MultipleLines, InternetService,
|
16 |
+
OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV,
|
17 |
+
StreamingMovies, PaperlessBilling):
|
18 |
+
# Create a DataFrame from the input data
|
19 |
+
input_data = pd.DataFrame({
|
20 |
+
'gender': [gender] if gender else ['Male'], # Replace None with default value
|
21 |
+
'SeniorCitizen': [SeniorCitizen] if SeniorCitizen is not None else [0], # Replace None with default value
|
22 |
+
'Partner': [Partner] if Partner else ['No'], # Replace None with default value
|
23 |
+
'Dependents': [Dependents] if Dependents else ['No'], # Replace None with default value
|
24 |
+
'tenure': [tenure] if tenure else [1], # Replace None with default value
|
25 |
+
'PhoneService': [PhoneService] if PhoneService else ['Yes'], # Replace None with default value
|
26 |
+
'MultipleLines': [MultipleLines] if MultipleLines else ['No'], # Replace None with default value
|
27 |
+
'InternetService': [InternetService] if InternetService else ['DSL'], # Replace None with default value
|
28 |
+
'OnlineSecurity': [OnlineSecurity] if OnlineSecurity else ['No'], # Replace None with default value
|
29 |
+
'OnlineBackup': [OnlineBackup] if OnlineBackup else ['No'], # Replace None with default value
|
30 |
+
'DeviceProtection': [DeviceProtection] if DeviceProtection else ['No'], # Replace None with default value
|
31 |
+
'TechSupport': [TechSupport] if TechSupport else ['No'], # Replace None with default value
|
32 |
+
'StreamingTV': [StreamingTV] if StreamingTV else ['No'], # Replace None with default value
|
33 |
+
'StreamingMovies': [StreamingMovies] if StreamingMovies else ['No'], # Replace None with default value
|
34 |
+
'Contract': [Contract] if Contract else ['Month-to-month'], # Replace None with default value
|
35 |
+
'PaperlessBilling': [PaperlessBilling] if PaperlessBilling else ['No'], # Replace None with default value
|
36 |
+
'PaymentMethod': [PaymentMethod] if PaymentMethod else ['Electronic check'], # Replace None with default value
|
37 |
+
'MonthlyCharges': [MonthlyCharges] if MonthlyCharges else [0.0], # Replace None with default value
|
38 |
+
'TotalCharges': [TotalCharges] if TotalCharges else [0.0] # Replace None with default value
|
39 |
+
})
|
40 |
+
|
41 |
+
|
42 |
+
# Make predictions using the loaded logistic regression model
|
43 |
+
predictions = full_pipeline.predict(input_data)
|
44 |
+
|
45 |
+
#return predictions[0]
|
46 |
+
if predictions[0] == "Yes":
|
47 |
+
return "Churn"
|
48 |
+
else:
|
49 |
+
return "Not Churn"
|
50 |
+
|
51 |
+
# Setting Gradio App Interface
|
52 |
+
with gr.Blocks(css=".gradio-container {background-color: grey}") as demo:
|
53 |
+
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.*")
|
54 |
+
|
55 |
+
# Receiving ALL Input Data here
|
56 |
+
gr.Markdown("**Demographic Data**")
|
57 |
+
with gr.Row():
|
58 |
+
gender = gr.Dropdown(label="Gender", choices=["Male", "Female"])
|
59 |
+
SeniorCitizen = gr.Radio(label="Senior Citizen", choices=[1, 0])
|
60 |
+
Partner = gr.Radio(label="Partner", choices=["Yes", "No"])
|
61 |
+
Dependents = gr.Radio(label="Dependents", choices=["Yes", "No"])
|
62 |
+
|
63 |
+
gr.Markdown("**Service Length and Charges (USD)**")
|
64 |
+
with gr.Row():
|
65 |
+
Contract = gr.Dropdown(label="Contract", choices=["Month-to-month", "One year", "Two year"])
|
66 |
+
tenure = gr.Slider(label="Tenure (months)", minimum=1, step=1, interactive=True)
|
67 |
+
MonthlyCharges = gr.Slider(label="Monthly Charges", step=0.05)
|
68 |
+
TotalCharges = gr.Slider(label="Total Charges", step=0.05)
|
69 |
+
|
70 |
+
# Phone Service Usage part
|
71 |
+
gr.Markdown("**Phone Service Usage**")
|
72 |
+
with gr.Row():
|
73 |
+
PhoneService = gr.Radio(label="Phone Service", choices=["Yes", "No"])
|
74 |
+
MultipleLines = gr.Dropdown(label="Multiple Lines", choices=[
|
75 |
+
"Yes", "No", "No phone service"])
|
76 |
+
|
77 |
+
# Internet Service Usage part
|
78 |
+
gr.Markdown("**Internet Service Usage**")
|
79 |
+
with gr.Row():
|
80 |
+
InternetService = gr.Dropdown(label="Internet Service", choices=["DSL", "Fiber optic", "No"])
|
81 |
+
OnlineSecurity = gr.Dropdown(label="Online Security", choices=["Yes", "No", "No internet service"])
|
82 |
+
OnlineBackup = gr.Dropdown(label="Online Backup", choices=["Yes", "No", "No internet service"])
|
83 |
+
DeviceProtection = gr.Dropdown(label="Device Protection", choices=["Yes", "No", "No internet service"])
|
84 |
+
TechSupport = gr.Dropdown(label="Tech Support", choices=["Yes", "No", "No internet service"])
|
85 |
+
StreamingTV = gr.Dropdown(label="TV Streaming", choices=["Yes", "No", "No internet service"])
|
86 |
+
StreamingMovies = gr.Dropdown(label="Movie Streaming", choices=["Yes", "No", "No internet service"])
|
87 |
+
|
88 |
+
# Billing and Payment part
|
89 |
+
gr.Markdown("**Billing and Payment**")
|
90 |
+
with gr.Row():
|
91 |
+
PaperlessBilling = gr.Radio(
|
92 |
+
label="Paperless Billing", choices=["Yes", "No"])
|
93 |
+
PaymentMethod = gr.Dropdown(label="Payment Method", choices=["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"])
|
94 |
+
|
95 |
+
# Output Prediction
|
96 |
+
output = gr.Text(label="Outcome")
|
97 |
+
submit_button = gr.Button("Predict")
|
98 |
+
|
99 |
+
submit_button.click(fn= predict,
|
100 |
+
outputs= output,
|
101 |
+
inputs=[gender, SeniorCitizen, Partner, Dependents, Contract, tenure, MonthlyCharges, TotalCharges, PaymentMethod, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, StreamingTV, StreamingMovies, PaperlessBilling],
|
102 |
+
|
103 |
+
),
|
104 |
+
|
105 |
+
# Add the reset and flag buttons
|
106 |
+
|
107 |
+
def clear():
|
108 |
+
output.value = ""
|
109 |
+
return None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
|
110 |
+
|
111 |
+
clear_btn = gr.Button("Reset", variant="primary")
|
112 |
+
clear_btn.click(fn=clear, inputs=None, outputs=output)
|
113 |
+
|
114 |
+
|
115 |
+
demo.launch(inbrowser = True)
|