Spaces:
Sleeping
Sleeping
Chandra Prakash Bathula commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,22 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import pickle
|
| 3 |
import gradio as gr
|
| 4 |
-
from sklearn.model_selection import train_test_split
|
| 5 |
-
from sklearn.preprocessing import LabelEncoder
|
| 6 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 7 |
|
| 8 |
# =========================
|
| 9 |
-
#
|
| 10 |
# =========================
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
categorical_cols = [
|
| 15 |
'job', 'marital', 'education', 'default',
|
| 16 |
'housing', 'loan', 'contact', 'month', 'poutcome'
|
| 17 |
]
|
| 18 |
|
| 19 |
-
le_dict = {}
|
| 20 |
-
for col in categorical_cols:
|
| 21 |
-
le = LabelEncoder()
|
| 22 |
-
df[col] = le.fit_transform(df[col])
|
| 23 |
-
le_dict[col] = le
|
| 24 |
-
|
| 25 |
-
le_y = LabelEncoder()
|
| 26 |
-
df['y'] = le_y.fit_transform(df['y'])
|
| 27 |
-
|
| 28 |
-
X = df.drop('y', axis=1)
|
| 29 |
-
y = df['y']
|
| 30 |
-
|
| 31 |
-
X_train, X_test, y_train, y_test = train_test_split(
|
| 32 |
-
X, y, test_size=0.2, random_state=42, stratify=y
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
model = DecisionTreeClassifier(
|
| 36 |
-
criterion='gini',
|
| 37 |
-
random_state=42,
|
| 38 |
-
min_samples_leaf=5
|
| 39 |
-
)
|
| 40 |
-
model.fit(X_train, y_train)
|
| 41 |
-
|
| 42 |
-
# =========================
|
| 43 |
-
# Feature Order
|
| 44 |
-
# =========================
|
| 45 |
-
|
| 46 |
feature_order = [
|
| 47 |
'age', 'job', 'marital', 'education', 'default',
|
| 48 |
'balance', 'housing', 'loan', 'contact', 'day',
|
|
@@ -51,7 +25,7 @@ feature_order = [
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# =========================
|
| 54 |
-
#
|
| 55 |
# =========================
|
| 56 |
|
| 57 |
def predict(
|
|
@@ -61,7 +35,7 @@ def predict(
|
|
| 61 |
previous, poutcome
|
| 62 |
):
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
'age': int(age),
|
| 66 |
'job': le_dict['job'].transform([job])[0],
|
| 67 |
'marital': le_dict['marital'].transform([marital])[0],
|
|
@@ -80,25 +54,25 @@ def predict(
|
|
| 80 |
'poutcome': le_dict['poutcome'].transform([poutcome])[0],
|
| 81 |
}
|
| 82 |
|
| 83 |
-
df_input = pd.DataFrame([
|
| 84 |
|
| 85 |
pred = model.predict(df_input)[0]
|
| 86 |
-
|
| 87 |
|
| 88 |
message = (
|
| 89 |
-
"✅ Likely to subscribe. Recommend follow-up
|
| 90 |
if pred == 1 else
|
| 91 |
"❌ Unlikely to subscribe."
|
| 92 |
)
|
| 93 |
|
| 94 |
-
return message, round(
|
| 95 |
|
| 96 |
except Exception as e:
|
| 97 |
return f"Error: {str(e)}", None
|
| 98 |
|
| 99 |
|
| 100 |
# =========================
|
| 101 |
-
#
|
| 102 |
# =========================
|
| 103 |
|
| 104 |
inputs = [
|
|
@@ -122,15 +96,15 @@ inputs = [
|
|
| 122 |
|
| 123 |
outputs = [
|
| 124 |
gr.Textbox(label="Prediction"),
|
| 125 |
-
gr.Number(label="Confidence
|
| 126 |
]
|
| 127 |
|
| 128 |
app = gr.Interface(
|
| 129 |
fn=predict,
|
| 130 |
inputs=inputs,
|
| 131 |
outputs=outputs,
|
| 132 |
-
title="
|
| 133 |
-
description="
|
| 134 |
)
|
| 135 |
|
| 136 |
app.launch()
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import pickle
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# =========================
|
| 6 |
+
# Load Model & Encoders
|
| 7 |
# =========================
|
| 8 |
|
| 9 |
+
with open("bank_dt_model.pkl", "rb") as f:
|
| 10 |
+
model = pickle.load(f)
|
| 11 |
+
|
| 12 |
+
with open("le_dict.pkl", "rb") as f:
|
| 13 |
+
le_dict = pickle.load(f)
|
| 14 |
|
| 15 |
categorical_cols = [
|
| 16 |
'job', 'marital', 'education', 'default',
|
| 17 |
'housing', 'loan', 'contact', 'month', 'poutcome'
|
| 18 |
]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
feature_order = [
|
| 21 |
'age', 'job', 'marital', 'education', 'default',
|
| 22 |
'balance', 'housing', 'loan', 'contact', 'day',
|
|
|
|
| 25 |
]
|
| 26 |
|
| 27 |
# =========================
|
| 28 |
+
# Prediction Function
|
| 29 |
# =========================
|
| 30 |
|
| 31 |
def predict(
|
|
|
|
| 35 |
previous, poutcome
|
| 36 |
):
|
| 37 |
try:
|
| 38 |
+
input_data = {
|
| 39 |
'age': int(age),
|
| 40 |
'job': le_dict['job'].transform([job])[0],
|
| 41 |
'marital': le_dict['marital'].transform([marital])[0],
|
|
|
|
| 54 |
'poutcome': le_dict['poutcome'].transform([poutcome])[0],
|
| 55 |
}
|
| 56 |
|
| 57 |
+
df_input = pd.DataFrame([input_data], columns=feature_order)
|
| 58 |
|
| 59 |
pred = model.predict(df_input)[0]
|
| 60 |
+
confidence = model.predict_proba(df_input)[0][pred]
|
| 61 |
|
| 62 |
message = (
|
| 63 |
+
"✅ Likely to subscribe. Recommend follow-up."
|
| 64 |
if pred == 1 else
|
| 65 |
"❌ Unlikely to subscribe."
|
| 66 |
)
|
| 67 |
|
| 68 |
+
return message, round(float(confidence), 3)
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
return f"Error: {str(e)}", None
|
| 72 |
|
| 73 |
|
| 74 |
# =========================
|
| 75 |
+
# Gradio UI
|
| 76 |
# =========================
|
| 77 |
|
| 78 |
inputs = [
|
|
|
|
| 96 |
|
| 97 |
outputs = [
|
| 98 |
gr.Textbox(label="Prediction"),
|
| 99 |
+
gr.Number(label="Confidence")
|
| 100 |
]
|
| 101 |
|
| 102 |
app = gr.Interface(
|
| 103 |
fn=predict,
|
| 104 |
inputs=inputs,
|
| 105 |
outputs=outputs,
|
| 106 |
+
title="🏦 Bank Term Deposit Predictor",
|
| 107 |
+
description="Decision Tree model deployed using Hugging Face Spaces"
|
| 108 |
)
|
| 109 |
|
| 110 |
app.launch()
|