Spaces:
Runtime error
Runtime error
luqmanabban
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
import joblib
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
|
7 |
+
|
8 |
+
# Load your pre-trained model
|
9 |
+
model = load_model('/content/best_model.h5')
|
10 |
+
# Define the prediction function
|
11 |
+
def predict(age, job, marital, education, default, balance, housing, loan, contact, day, month, duration, campaign, pdays, previous, poutcome):
|
12 |
+
# Define the expected input order and preprocess accordingly
|
13 |
+
columns = [
|
14 |
+
'age', 'job', 'marital', 'education', 'default', 'balance', 'housing',
|
15 |
+
'loan', 'contact', 'day', 'month', 'duration', 'campaign', 'pdays',
|
16 |
+
'previous', 'poutcome'
|
17 |
+
]
|
18 |
+
|
19 |
+
# Prepare the input values
|
20 |
+
data = [
|
21 |
+
age, job, marital, education, default, balance, housing, loan,
|
22 |
+
contact, day, month, duration, campaign, pdays, previous, poutcome
|
23 |
+
]
|
24 |
+
|
25 |
+
# Convert to DataFrame
|
26 |
+
df = pd.DataFrame([data], columns=columns)
|
27 |
+
|
28 |
+
# Preprocess: One-hot encode categorical features (simulating as example)
|
29 |
+
# Normally, ensure you replicate the preprocessing steps used during training
|
30 |
+
df_processed = pd.get_dummies(df)
|
31 |
+
|
32 |
+
# Align processed DataFrame with model input (add missing columns if any)
|
33 |
+
model_columns = model.feature_names_in_ # Assuming the model has this attribute
|
34 |
+
for col in model_columns:
|
35 |
+
if col not in df_processed:
|
36 |
+
df_processed[col] = 0
|
37 |
+
|
38 |
+
df_processed = df_processed[model_columns]
|
39 |
+
|
40 |
+
# Predict
|
41 |
+
prediction = model.predict(df_processed)[0]
|
42 |
+
|
43 |
+
return "Yes" if prediction == 1 else "No"
|
44 |
+
|
45 |
+
# Define Gradio interface
|
46 |
+
inputs = [
|
47 |
+
gr.Number(label="Age"),
|
48 |
+
gr.Dropdown(['management', 'technician', 'entrepreneur', 'blue-collar', 'unknown', 'retired', 'admin.', 'services', 'self-employed', 'unemployed', 'student', 'housemaid'], label="Job"),
|
49 |
+
gr.Dropdown(['married', 'single', 'divorced'], label="Marital Status"),
|
50 |
+
gr.Dropdown(['primary', 'secondary', 'tertiary', 'unknown'], label="Education"),
|
51 |
+
gr.Dropdown(['yes', 'no'], label="Default"),
|
52 |
+
gr.Number(label="Balance"),
|
53 |
+
gr.Dropdown(['yes', 'no'], label="Housing Loan"),
|
54 |
+
gr.Dropdown(['yes', 'no'], label="Personal Loan"),
|
55 |
+
gr.Dropdown(['unknown', 'telephone', 'cellular'], label="Contact"),
|
56 |
+
gr.Number(label="Day"),
|
57 |
+
gr.Dropdown(['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], label="Month"),
|
58 |
+
gr.Number(label="Duration"),
|
59 |
+
gr.Number(label="Campaign"),
|
60 |
+
gr.Number(label="Pdays"),
|
61 |
+
gr.Number(label="Previous"),
|
62 |
+
gr.Dropdown(['unknown', 'other', 'failure', 'success'], label="Poutcome")
|
63 |
+
]
|
64 |
+
|
65 |
+
|
66 |
+
output = gr.Textbox(label="Subscription Prediction")
|
67 |
+
|
68 |
+
gui = gr.Interface(fn=predict, inputs=inputs, outputs=output, title="Term Deposit Subscription Prediction")
|
69 |
+
|
70 |
+
gui.launch()
|