banking_helper / app.py
anasmkh's picture
add app file
b0f3a2d verified
raw
history blame
1.92 kB
import streamlit as st
import numpy as np
import pandas as pd
import tensorflow as tf
import joblib
from sklearn.preprocessing import LabelEncoder, StandardScaler
try:
model = tf.keras.models.load_model("banking_model.keras")
scaler = joblib.load("scaler.pkl")
label_encoders = joblib.load("label_encoders.pkl")
except Exception as e:
st.error(f"Error loading model or preprocessors: {e}")
st.stop()
st.title("πŸ“Š Banking App")
st.write("Enter the feature values below to predict the classification stage.")
if not label_encoders:
st.error("Label encoders are empty. Make sure the model was trained correctly.")
st.stop()
numerical_inputs = {}
categorical_inputs = {}
try:
numerical_features = list(scaler.feature_names_in_)
categorical_features = list(label_encoders.keys())
except AttributeError:
st.error("Scaler or encoders are not properly loaded.")
st.stop()
for feature in numerical_features:
numerical_inputs[feature] = st.number_input(f"Enter {feature}", value=0.0)
for feature in categorical_features:
if label_encoders[feature].classes_.size > 0:
categorical_inputs[feature] = st.selectbox(f"Select {feature}", label_encoders[feature].classes_)
else:
st.error(f"Label encoder for {feature} is empty.")
st.stop()
if st.button("Predict"):
try:
for feature in categorical_inputs:
categorical_inputs[feature] = label_encoders[feature].transform([categorical_inputs[feature]])[0]
input_data = pd.DataFrame([{**numerical_inputs, **categorical_inputs}])
input_data[numerical_features] = scaler.transform(input_data[numerical_features])
prediction = model.predict(input_data)
predicted_class = np.argmax(prediction)
st.success(f"βœ… Predicted Classification Stage: {predicted_class}")
except Exception as e:
st.error(f"Prediction error: {e}")