Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import pickle | |
| import gradio as gr | |
| # ========================= | |
| # Load Model & Encoders | |
| # ========================= | |
| with open("bank_dt_model.pkl", "rb") as f: | |
| model = pickle.load(f) | |
| with open("le_dict.pkl", "rb") as f: | |
| le_dict = pickle.load(f) | |
| categorical_cols = [ | |
| 'job', 'marital', 'education', 'default', | |
| 'housing', 'loan', 'contact', 'month', 'poutcome' | |
| ] | |
| feature_order = [ | |
| 'age', 'job', 'marital', 'education', 'default', | |
| 'balance', 'housing', 'loan', 'contact', 'day', | |
| 'month', 'duration', 'campaign', 'pdays', | |
| 'previous', 'poutcome' | |
| ] | |
| # ========================= | |
| # Prediction Function | |
| # ========================= | |
| def predict( | |
| age, job, marital, education, default, | |
| balance, housing, loan, contact, day, | |
| month, duration, campaign, pdays, | |
| previous, poutcome | |
| ): | |
| try: | |
| input_data = { | |
| 'age': int(age), | |
| 'job': le_dict['job'].transform([job])[0], | |
| 'marital': le_dict['marital'].transform([marital])[0], | |
| 'education': le_dict['education'].transform([education])[0], | |
| 'default': le_dict['default'].transform([default])[0], | |
| 'balance': int(balance), | |
| 'housing': le_dict['housing'].transform([housing])[0], | |
| 'loan': le_dict['loan'].transform([loan])[0], | |
| 'contact': le_dict['contact'].transform([contact])[0], | |
| 'day': int(day), | |
| 'month': le_dict['month'].transform([month])[0], | |
| 'duration': int(duration), | |
| 'campaign': int(campaign), | |
| 'pdays': int(pdays), | |
| 'previous': int(previous), | |
| 'poutcome': le_dict['poutcome'].transform([poutcome])[0], | |
| } | |
| df_input = pd.DataFrame([input_data], columns=feature_order) | |
| pred = model.predict(df_input)[0] | |
| confidence = model.predict_proba(df_input)[0][pred] | |
| message = ( | |
| "✅ Likely to subscribe. Recommend follow-up." | |
| if pred == 1 else | |
| "❌ Unlikely to subscribe." | |
| ) | |
| return message, round(float(confidence), 3) | |
| except Exception as e: | |
| return f"Error: {str(e)}", None | |
| # ========================= | |
| # Gradio UI | |
| # ========================= | |
| inputs = [ | |
| gr.Number(label="Age"), | |
| gr.Dropdown(le_dict['job'].classes_.tolist(), label="Job"), | |
| gr.Dropdown(le_dict['marital'].classes_.tolist(), label="Marital Status"), | |
| gr.Dropdown(le_dict['education'].classes_.tolist(), label="Education"), | |
| gr.Dropdown(le_dict['default'].classes_.tolist(), label="Credit Default"), | |
| gr.Number(label="Account Balance"), | |
| gr.Dropdown(le_dict['housing'].classes_.tolist(), label="Housing Loan"), | |
| gr.Dropdown(le_dict['loan'].classes_.tolist(), label="Personal Loan"), | |
| gr.Dropdown(le_dict['contact'].classes_.tolist(), label="Contact Type"), | |
| gr.Number(label="Day of Month"), | |
| gr.Dropdown(le_dict['month'].classes_.tolist(), label="Month"), | |
| gr.Number(label="Call Duration (seconds)"), | |
| gr.Number(label="Campaign Contacts"), | |
| gr.Number(label="Days Since Last Contact"), | |
| gr.Number(label="Previous Contacts"), | |
| gr.Dropdown(le_dict['poutcome'].classes_.tolist(), label="Previous Campaign Outcome"), | |
| ] | |
| outputs = [ | |
| gr.Textbox(label="Prediction"), | |
| gr.Number(label="Confidence") | |
| ] | |
| app = gr.Interface( | |
| fn=predict, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="🏦 Bank Term Deposit Predictor", | |
| description="Decision Tree model deployed using Hugging Face Spaces" | |
| ) | |
| app.launch() |