Spaces:
Runtime error
Runtime error
AlbieCofie
commited on
Commit
•
deb6a65
1
Parent(s):
d2bc29b
Upload 6 files
Browse files- app.py +86 -0
- data.csv +0 -0
- encoder.pkl +3 -0
- model.pkl +3 -0
- requirements.txt +2 -0
- scaler.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import pickle
|
4 |
+
|
5 |
+
# Load the model and encoder and scaler
|
6 |
+
model = pickle.load(open("model.pkl", "rb"))
|
7 |
+
encoder = pickle.load(open("encoder.pkl", "rb"))
|
8 |
+
scaler = pickle.load(open("scaler.pkl", "rb"))
|
9 |
+
|
10 |
+
# Load the data
|
11 |
+
data = pd.read_csv('data.csv')
|
12 |
+
|
13 |
+
# Define the input and output interfaces for the Gradio app
|
14 |
+
def create_gradio_inputs(data):
|
15 |
+
input_components = []
|
16 |
+
for column in data.columns:
|
17 |
+
if data[column].dtype == 'object' and len(data[column].unique()) > 3:
|
18 |
+
input_components.append(gr.Dropdown(choices=list(data[column].unique()), label=column))
|
19 |
+
elif data[column].dtype == 'object' and len(data[column].unique()) <= 3:
|
20 |
+
input_components.append(gr.Radio(choices=list(data[column].unique()), label=column))
|
21 |
+
elif data[column].dtype in ['int64', 'float64']:
|
22 |
+
if data[column].min() == 1:
|
23 |
+
input_components.append(gr.Slider(minimum=1, maximum=data[column].max(), step=1, label=column))
|
24 |
+
else:
|
25 |
+
input_components.append(gr.Slider(maximum=data[column].max(), step=0.5, label=column))
|
26 |
+
return input_components
|
27 |
+
|
28 |
+
input_components = create_gradio_inputs(data)
|
29 |
+
|
30 |
+
output_components = [
|
31 |
+
gr.Label(label="Churn Prediction"),
|
32 |
+
]
|
33 |
+
|
34 |
+
# Convert the input values to a pandas DataFrame with the appropriate column names
|
35 |
+
def input_df_creator(gender, SeniorCitizen, Partner, Dependents, tenure,
|
36 |
+
PhoneService, InternetService, OnlineBackup, TechSupport,
|
37 |
+
Contract, PaperlessBilling, PaymentMethod, MonthlyCharges,
|
38 |
+
TotalCharges, StreamingService, SecurityService):
|
39 |
+
input_data = pd.DataFrame({
|
40 |
+
"gender": [gender],
|
41 |
+
"SeniorCitizen": [SeniorCitizen],
|
42 |
+
"Partner": [Partner],
|
43 |
+
"Dependents": [Dependents],
|
44 |
+
"tenure": [int(tenure)],
|
45 |
+
"PhoneService": [PhoneService],
|
46 |
+
"InternetService": [InternetService],
|
47 |
+
"OnlineBackup": [OnlineBackup],
|
48 |
+
"TechSupport": [TechSupport],
|
49 |
+
"Contract": [Contract],
|
50 |
+
"PaperlessBilling": [PaperlessBilling],
|
51 |
+
"PaymentMethod": [PaymentMethod],
|
52 |
+
"StreamingService": [StreamingService],
|
53 |
+
"SecurityService": [SecurityService],
|
54 |
+
"MonthlyCharges": [float(MonthlyCharges)],
|
55 |
+
"TotalCharges": [float(TotalCharges)],
|
56 |
+
})
|
57 |
+
return input_data
|
58 |
+
|
59 |
+
# Define the function to be called when the Gradio app is run
|
60 |
+
def predict_churn(gender, SeniorCitizen, Partner, Dependents, tenure,
|
61 |
+
PhoneService, InternetService, OnlineBackup, TechSupport,
|
62 |
+
Contract, PaperlessBilling, PaymentMethod, MonthlyCharges,
|
63 |
+
TotalCharges, StreamingService, SecurityService):
|
64 |
+
input_df = input_df_creator(gender, SeniorCitizen, Partner, Dependents, tenure,
|
65 |
+
PhoneService, InternetService, OnlineBackup, TechSupport,
|
66 |
+
Contract, PaperlessBilling, PaymentMethod, MonthlyCharges,
|
67 |
+
TotalCharges, StreamingService, SecurityService)
|
68 |
+
|
69 |
+
# Encode categorical variables
|
70 |
+
cat_cols = data.select_dtypes(include=['object']).columns
|
71 |
+
cat_encoded = encoder.transform(input_df[cat_cols])
|
72 |
+
|
73 |
+
# Scale numerical variables
|
74 |
+
num_cols = data.select_dtypes(include=['int64', 'float64']).columns
|
75 |
+
num_scaled = scaler.transform(input_df[num_cols])
|
76 |
+
|
77 |
+
# joining encoded and scaled columns back together
|
78 |
+
processed_df = pd.concat([num_scaled, cat_encoded], axis=1)
|
79 |
+
|
80 |
+
# Make prediction
|
81 |
+
prediction = model.predict(processed_df)
|
82 |
+
return "Churn" if prediction[0] == 1 else "No Churn"
|
83 |
+
|
84 |
+
# Launch the Gradio app
|
85 |
+
iface = gr.Interface(predict_churn, inputs=input_components, outputs=output_components)
|
86 |
+
iface.launch(inbrowser= True, show_error= True)
|
data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
encoder.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:425975f1f8685775681e0b1cdd172192f118bf5738245a93e559c3ab3ce5816e
|
3 |
+
size 1614
|
model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0d69efd028f0febbb979a392353c8481ca60facf77d8123488f9d3be6c12549c
|
3 |
+
size 4740256
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
scikit-learn==1.2.1
|
scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:82685d10226631ab8fa9d4e3745af304f12a8f8a1333d8931bcd30b412f12fb9
|
3 |
+
size 698
|