Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import tensorflow as tf | |
import joblib | |
import os | |
if not os.path.exists("banking_model.keras"): | |
st.error("π¨ Model file not found! Train and save the model first.") | |
st.stop() | |
if not os.path.exists("scaler.pkl"): | |
st.error("π¨ Scaler file 'scaler.pkl' not found! Make sure preprocessing was done correctly.") | |
st.stop() | |
if not os.path.exists("label_encoders.pkl"): | |
st.error("π¨ Label encoder file 'label_encoders.pkl' not found! Ensure encoding was saved properly.") | |
st.stop() | |
try: | |
model = tf.keras.models.load_model("banking_model.keras") | |
scaler = joblib.load("scaler.pkl") | |
label_encoders = joblib.load("label_encoders.pkl") | |
if not label_encoders: | |
raise ValueError("Label encoders are empty!") | |
except Exception as e: | |
st.error(f"Error loading model or preprocessors: {e}") | |
st.stop() | |
st.title("π Classification Prediction App") | |
st.write("Enter the feature values below to predict the classification stage.") | |
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] = float(st.number_input(f"Enter {feature}", value=0.0, step=0.1, format="%.2f")) | |
for feature in categorical_features: | |
if len(label_encoders[feature].classes_) > 0: | |
categorical_inputs[feature] = st.selectbox(f"Select {feature}", label_encoders[feature].classes_) | |
else: | |
st.warning(f"β οΈ No classes found for '{feature}'. Skipping this feature.") | |
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}") | |