gff / app.py
luqmanabban's picture
Create app.py
57605a4 verified
import gradio as gr
import pickle
import pandas as pd
import joblib
from tensorflow.keras.models import load_model
# Load your pre-trained model
model = load_model('/content/best_model.h5')
# Define the prediction function
def predict(age, job, marital, education, default, balance, housing, loan, contact, day, month, duration, campaign, pdays, previous, poutcome):
# Define the expected input order and preprocess accordingly
columns = [
'age', 'job', 'marital', 'education', 'default', 'balance', 'housing',
'loan', 'contact', 'day', 'month', 'duration', 'campaign', 'pdays',
'previous', 'poutcome'
]
# Prepare the input values
data = [
age, job, marital, education, default, balance, housing, loan,
contact, day, month, duration, campaign, pdays, previous, poutcome
]
# Convert to DataFrame
df = pd.DataFrame([data], columns=columns)
# Preprocess: One-hot encode categorical features (simulating as example)
# Normally, ensure you replicate the preprocessing steps used during training
df_processed = pd.get_dummies(df)
# Align processed DataFrame with model input (add missing columns if any)
model_columns = model.feature_names_in_ # Assuming the model has this attribute
for col in model_columns:
if col not in df_processed:
df_processed[col] = 0
df_processed = df_processed[model_columns]
# Predict
prediction = model.predict(df_processed)[0]
return "Yes" if prediction == 1 else "No"
# Define Gradio interface
inputs = [
gr.Number(label="Age"),
gr.Dropdown(['management', 'technician', 'entrepreneur', 'blue-collar', 'unknown', 'retired', 'admin.', 'services', 'self-employed', 'unemployed', 'student', 'housemaid'], label="Job"),
gr.Dropdown(['married', 'single', 'divorced'], label="Marital Status"),
gr.Dropdown(['primary', 'secondary', 'tertiary', 'unknown'], label="Education"),
gr.Dropdown(['yes', 'no'], label="Default"),
gr.Number(label="Balance"),
gr.Dropdown(['yes', 'no'], label="Housing Loan"),
gr.Dropdown(['yes', 'no'], label="Personal Loan"),
gr.Dropdown(['unknown', 'telephone', 'cellular'], label="Contact"),
gr.Number(label="Day"),
gr.Dropdown(['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], label="Month"),
gr.Number(label="Duration"),
gr.Number(label="Campaign"),
gr.Number(label="Pdays"),
gr.Number(label="Previous"),
gr.Dropdown(['unknown', 'other', 'failure', 'success'], label="Poutcome")
]
output = gr.Textbox(label="Subscription Prediction")
gui = gr.Interface(fn=predict, inputs=inputs, outputs=output, title="Term Deposit Subscription Prediction")
gui.launch()