Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,101 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import joblib
|
4 |
-
import os
|
5 |
-
|
6 |
-
# Load the models
|
7 |
-
preprocessor_path = os.path.join('.', 'preprocessor.joblib')
|
8 |
-
model_path = os.path.join('.', 'Best_model.joblib')
|
9 |
-
|
10 |
-
preprocessor = joblib.load(
|
11 |
-
best_model = joblib.load(
|
12 |
-
|
13 |
-
# Define prediction function
|
14 |
-
def predict(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed):
|
15 |
-
# Create a DataFrame for the input data
|
16 |
-
data = pd.DataFrame({
|
17 |
-
'age': [age],
|
18 |
-
'job': [job],
|
19 |
-
'marital': [marital],
|
20 |
-
'education': [education],
|
21 |
-
'default': [default],
|
22 |
-
'housing': [housing],
|
23 |
-
'loan': [loan],
|
24 |
-
'contact': [contact],
|
25 |
-
'month': [month],
|
26 |
-
'day_of_week': [day_of_week],
|
27 |
-
'duration': [duration],
|
28 |
-
'campaign': [campaign],
|
29 |
-
'pdays': [pdays],
|
30 |
-
'previous': [previous],
|
31 |
-
'poutcome': [poutcome],
|
32 |
-
'emp.var.rate': [emp_var_rate],
|
33 |
-
'cons.price.idx': [cons_price_idx],
|
34 |
-
'cons.conf.idx': [cons_conf_idx],
|
35 |
-
'euribor3m': [euribor3m],
|
36 |
-
'nr.employed': [nr_employed]
|
37 |
-
})
|
38 |
-
|
39 |
-
# Preprocess the data
|
40 |
-
preprocessed_data = preprocessor.transform(data)
|
41 |
-
|
42 |
-
# Make predictions
|
43 |
-
prediction = best_model.predict(preprocessed_data)
|
44 |
-
probability = best_model.predict_proba(preprocessed_data)
|
45 |
-
|
46 |
-
return {
|
47 |
-
"Prediction": prediction[0],
|
48 |
-
"Probability (Yes)": probability[0][1],
|
49 |
-
"Probability (No)": probability[0][0]
|
50 |
-
}
|
51 |
-
|
52 |
-
# Define the interface
|
53 |
-
def gradio_interface():
|
54 |
-
with gr.Blocks() as app:
|
55 |
-
gr.Markdown("# Bank Marketing Campaign Prediction")
|
56 |
-
|
57 |
-
with gr.Row():
|
58 |
-
age = gr.Number(label="Age", value=30)
|
59 |
-
job = gr.Dropdown(["housemaid", "services", "admin.", "blue-collar", "technician", "retired", "management", "unemployed", "self-employed", "unknown", "entrepreneur", "student"], label="Job")
|
60 |
-
marital = gr.Dropdown(["married", "single", "divorced", "unknown"], label="Marital Status")
|
61 |
-
education = gr.Dropdown(["basic.4y", "high.school", "basic.6y", "basic.9y", "professional.course", "unknown", "university.degree", "illiterate", "tertiary", "secondary", "primary"], label="Education")
|
62 |
-
|
63 |
-
with gr.Row():
|
64 |
-
default = gr.Dropdown(["no", "unknown", "yes"], label="Default")
|
65 |
-
housing = gr.Dropdown(["no", "yes", "unknown"], label="Housing Loan")
|
66 |
-
loan = gr.Dropdown(["no", "yes", "unknown"], label="Personal Loan")
|
67 |
-
contact = gr.Dropdown(["telephone", "cellular", "unknown"], label="Contact Type")
|
68 |
-
|
69 |
-
with gr.Row():
|
70 |
-
month = gr.Dropdown(["may", "jun", "jul", "aug", "oct", "nov", "dec", "mar", "apr", "sep", "jan", "feb"], label="Month")
|
71 |
-
day_of_week = gr.Dropdown(["mon", "tue", "wed", "thu", "fri"], label="Day of Week")
|
72 |
-
|
73 |
-
with gr.Row():
|
74 |
-
duration = gr.Number(label="Call Duration (seconds)", value=100)
|
75 |
-
campaign = gr.Number(label="Number of Contacts during Campaign", value=1)
|
76 |
-
pdays = gr.Number(label="Days since Last Contact", value=999)
|
77 |
-
previous = gr.Number(label="Number of Contacts before Campaign", value=0)
|
78 |
-
poutcome = gr.Dropdown(["nonexistent", "failure", "success", "unknown", "other"], label="Previous Outcome")
|
79 |
-
|
80 |
-
with gr.Row():
|
81 |
-
emp_var_rate = gr.Number(label="Employment Variation Rate", value=1.1)
|
82 |
-
cons_price_idx = gr.Number(label="Consumer Price Index", value=93.994)
|
83 |
-
cons_conf_idx = gr.Number(label="Consumer Confidence Index", value=-36.4)
|
84 |
-
euribor3m = gr.Number(label="Euribor 3-Month Rate", value=4.857)
|
85 |
-
nr_employed = gr.Number(label="Number of Employees", value=5191.0)
|
86 |
-
|
87 |
-
predict_btn = gr.Button("Predict")
|
88 |
-
output = gr.JSON()
|
89 |
-
|
90 |
-
predict_btn.click(
|
91 |
-
predict,
|
92 |
-
inputs=[age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed],
|
93 |
-
outputs=output
|
94 |
-
)
|
95 |
-
|
96 |
-
return app
|
97 |
-
|
98 |
-
# Launch the app
|
99 |
-
if __name__ == "__main__":
|
100 |
-
app = gradio_interface()
|
101 |
-
app.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load the models
|
7 |
+
preprocessor_path = os.path.join('.', 'preprocessor.joblib')
|
8 |
+
model_path = os.path.join('.', 'Best_model.joblib')
|
9 |
+
|
10 |
+
preprocessor = joblib.load(preprocessor_path)
|
11 |
+
best_model = joblib.load(model_path)
|
12 |
+
|
13 |
+
# Define prediction function
|
14 |
+
def predict(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed):
|
15 |
+
# Create a DataFrame for the input data
|
16 |
+
data = pd.DataFrame({
|
17 |
+
'age': [age],
|
18 |
+
'job': [job],
|
19 |
+
'marital': [marital],
|
20 |
+
'education': [education],
|
21 |
+
'default': [default],
|
22 |
+
'housing': [housing],
|
23 |
+
'loan': [loan],
|
24 |
+
'contact': [contact],
|
25 |
+
'month': [month],
|
26 |
+
'day_of_week': [day_of_week],
|
27 |
+
'duration': [duration],
|
28 |
+
'campaign': [campaign],
|
29 |
+
'pdays': [pdays],
|
30 |
+
'previous': [previous],
|
31 |
+
'poutcome': [poutcome],
|
32 |
+
'emp.var.rate': [emp_var_rate],
|
33 |
+
'cons.price.idx': [cons_price_idx],
|
34 |
+
'cons.conf.idx': [cons_conf_idx],
|
35 |
+
'euribor3m': [euribor3m],
|
36 |
+
'nr.employed': [nr_employed]
|
37 |
+
})
|
38 |
+
|
39 |
+
# Preprocess the data
|
40 |
+
preprocessed_data = preprocessor.transform(data)
|
41 |
+
|
42 |
+
# Make predictions
|
43 |
+
prediction = best_model.predict(preprocessed_data)
|
44 |
+
probability = best_model.predict_proba(preprocessed_data)
|
45 |
+
|
46 |
+
return {
|
47 |
+
"Prediction": prediction[0],
|
48 |
+
"Probability (Yes)": probability[0][1],
|
49 |
+
"Probability (No)": probability[0][0]
|
50 |
+
}
|
51 |
+
|
52 |
+
# Define the interface
|
53 |
+
def gradio_interface():
|
54 |
+
with gr.Blocks() as app:
|
55 |
+
gr.Markdown("# Bank Marketing Campaign Prediction")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
age = gr.Number(label="Age", value=30)
|
59 |
+
job = gr.Dropdown(["housemaid", "services", "admin.", "blue-collar", "technician", "retired", "management", "unemployed", "self-employed", "unknown", "entrepreneur", "student"], label="Job")
|
60 |
+
marital = gr.Dropdown(["married", "single", "divorced", "unknown"], label="Marital Status")
|
61 |
+
education = gr.Dropdown(["basic.4y", "high.school", "basic.6y", "basic.9y", "professional.course", "unknown", "university.degree", "illiterate", "tertiary", "secondary", "primary"], label="Education")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
default = gr.Dropdown(["no", "unknown", "yes"], label="Default")
|
65 |
+
housing = gr.Dropdown(["no", "yes", "unknown"], label="Housing Loan")
|
66 |
+
loan = gr.Dropdown(["no", "yes", "unknown"], label="Personal Loan")
|
67 |
+
contact = gr.Dropdown(["telephone", "cellular", "unknown"], label="Contact Type")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
month = gr.Dropdown(["may", "jun", "jul", "aug", "oct", "nov", "dec", "mar", "apr", "sep", "jan", "feb"], label="Month")
|
71 |
+
day_of_week = gr.Dropdown(["mon", "tue", "wed", "thu", "fri"], label="Day of Week")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
duration = gr.Number(label="Call Duration (seconds)", value=100)
|
75 |
+
campaign = gr.Number(label="Number of Contacts during Campaign", value=1)
|
76 |
+
pdays = gr.Number(label="Days since Last Contact", value=999)
|
77 |
+
previous = gr.Number(label="Number of Contacts before Campaign", value=0)
|
78 |
+
poutcome = gr.Dropdown(["nonexistent", "failure", "success", "unknown", "other"], label="Previous Outcome")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
emp_var_rate = gr.Number(label="Employment Variation Rate", value=1.1)
|
82 |
+
cons_price_idx = gr.Number(label="Consumer Price Index", value=93.994)
|
83 |
+
cons_conf_idx = gr.Number(label="Consumer Confidence Index", value=-36.4)
|
84 |
+
euribor3m = gr.Number(label="Euribor 3-Month Rate", value=4.857)
|
85 |
+
nr_employed = gr.Number(label="Number of Employees", value=5191.0)
|
86 |
+
|
87 |
+
predict_btn = gr.Button("Predict")
|
88 |
+
output = gr.JSON()
|
89 |
+
|
90 |
+
predict_btn.click(
|
91 |
+
predict,
|
92 |
+
inputs=[age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed],
|
93 |
+
outputs=output
|
94 |
+
)
|
95 |
+
|
96 |
+
return app
|
97 |
+
|
98 |
+
# Launch the app
|
99 |
+
if __name__ == "__main__":
|
100 |
+
app = gradio_interface()
|
101 |
+
app.launch()
|