Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import pickle
|
6 |
+
|
7 |
+
# Load the pre-trained model and its key components
|
8 |
+
with open('rf_key_components.pkl', 'rb') as file:
|
9 |
+
key_components = pickle.load(file)
|
10 |
+
|
11 |
+
categorical_imputer = key_components['categorical_imputer']
|
12 |
+
numerical_imputer = key_components['numerical_imputer']
|
13 |
+
encoder = key_components['encoder']
|
14 |
+
scaler = key_components['scaler']
|
15 |
+
best_model = key_components['best_model']
|
16 |
+
|
17 |
+
# Preprocessing function
|
18 |
+
def churn_prediction(gender, SeniorCitizen, Partner, Dependents, tenure, PhoneService, MultipleLines,
|
19 |
+
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
|
20 |
+
StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod,
|
21 |
+
MonthlyCharges, TotalCharges, MonthlyCharges_TotalCharges_Ratio, AverageMonthlyCharges):
|
22 |
+
|
23 |
+
# Create a DataFrame from user inputs
|
24 |
+
user_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 |
+
'MonthlyCharges_TotalCharges_Ratio': [MonthlyCharges_TotalCharges_Ratio],
|
45 |
+
'AverageMonthlyCharges': [AverageMonthlyCharges]
|
46 |
+
})
|
47 |
+
|
48 |
+
# Preprocessing for categorical data
|
49 |
+
pred_cat_data = user_input_df.select_dtypes(include='object')
|
50 |
+
encoded_pred_data = encoder.transform(categorical_imputer.transform(pred_cat_data))
|
51 |
+
|
52 |
+
# Convert the encoded data to a DataFrame
|
53 |
+
encoded_pred_data_df = pd.DataFrame.sparse.from_spmatrix(encoded_pred_data,
|
54 |
+
columns=encoder.get_feature_names_out(pred_cat_data.columns),
|
55 |
+
index=pred_cat_data.index)
|
56 |
+
|
57 |
+
# Preprocessing for numerical data
|
58 |
+
pred_num_data = user_input_df.select_dtypes(include=['int', 'float'])
|
59 |
+
scaled_pred_data = scaler.transform(numerical_imputer.transform(pred_num_data))
|
60 |
+
|
61 |
+
# Convert the scaled numerical data to a DataFrame
|
62 |
+
scaled_pred_data_df = pd.DataFrame(scaled_pred_data, columns=pred_num_data.columns, index=pred_num_data.index)
|
63 |
+
|
64 |
+
# Concatenate the encoded categorical data and scaled numerical data
|
65 |
+
final_df = pd.concat([encoded_pred_data_df, scaled_pred_data_df], axis=1)
|
66 |
+
|
67 |
+
# Make predictions using the loaded model
|
68 |
+
predictions = best_model.predict(final_df)
|
69 |
+
|
70 |
+
# Map the predictions to 'Yes' or 'No'
|
71 |
+
input_prediction = 'Churn' if predictions[0] == 1 else 'Not Churn'
|
72 |
+
|
73 |
+
|
74 |
+
return input_prediction
|
75 |
+
|
76 |
+
|
77 |
+
# Define input components
|
78 |
+
input_components = [
|
79 |
+
gr.Radio(label='Customer Gender', choices=['Female', 'Male']),
|
80 |
+
gr.Radio(label='Is the customer a senior citizen?', choices=['No', 'Yes']),
|
81 |
+
gr.Radio(label='Does the customer have a partner?', choices=['No', 'Yes']),
|
82 |
+
gr.Radio(label='Does the customer have dependents?', choices=['No', 'Yes']),
|
83 |
+
gr.Number(label='Number of months the customer has been with the company.', minimum=0, maximum=72),
|
84 |
+
gr.Radio(label='Does the customer have a phone service?', choices=['No', 'Yes']),
|
85 |
+
gr.Radio(label='Does the customer have multiple lines?', choices=['No', 'Yes']),
|
86 |
+
gr.Radio(label='Type of internet service', choices=['DSL', 'Fiber optic', 'No']),
|
87 |
+
gr.Radio(label='Does the customer have online security?', choices=['No', 'Yes']),
|
88 |
+
gr.Radio(label='Does the customer have online backup?', choices=['No', 'Yes']),
|
89 |
+
gr.Radio(label='Does the customer have device protection?', choices=['No', 'Yes']),
|
90 |
+
gr.Radio(label='Does the customer have tech support?', choices=['No', 'Yes']),
|
91 |
+
gr.Radio(label='Does the customer have streaming TV?', choices=['No', 'Yes']),
|
92 |
+
gr.Radio(label='Does the customer have streaming movies?', choices=['No', 'Yes']),
|
93 |
+
gr.Radio(label='Contract type', choices=['Month-to-month', 'One year', 'Two year']),
|
94 |
+
gr.Radio(label='Does the customer use paperless billing?', choices=['No', 'Yes']),
|
95 |
+
gr.Radio(label='Payment method', choices=['Electronic check', 'Mailed check', 'Bank transfer (automatic)', 'Credit card (automatic)']),
|
96 |
+
gr.Number(label='Monthly charges for the customer.', minimum=18, maximum=119),
|
97 |
+
gr.Number(label='Total charges for the customer.', minimum=19, maximum=8670),
|
98 |
+
gr.Slider(label='Ratio of monthly charges to total charges.', minimum=0.00, maximum=1.0),
|
99 |
+
gr.Number(label='Average monthly charges for the customer.', minimum=0, maximum=120)
|
100 |
+
]
|
101 |
+
|
102 |
+
# Create and launch the Gradio interface
|
103 |
+
iface = gr.Interface(
|
104 |
+
fn=churn_prediction,
|
105 |
+
inputs=input_components,
|
106 |
+
outputs="text",
|
107 |
+
title="Customer Churn Prediction App",
|
108 |
+
description="This app predicts whether a customer is likely to churn (leave) a telecommunications company. It uses machine learning to analyze customer data, such as gender, age, tenure, and service usage. The app can be used by stakeholders and customers to make informed decisions about customer retention.",
|
109 |
+
live=False,
|
110 |
+
share=True,
|
111 |
+
|
112 |
+
)
|
113 |
+
|
114 |
+
iface.launch()
|