File size: 2,348 Bytes
b0f3a2d
 
 
 
 
bfa1d06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0f3a2d
 
bfa1d06
b0f3a2d
 
bfa1d06
 
 
 
b0f3a2d
 
 
 
 
bfa1d06
b0f3a2d
 
 
 
 
 
bfa1d06
 
b0f3a2d
 
 
 
 
bfa1d06
b0f3a2d
 
bfa1d06
b0f3a2d
 
bfa1d06
 
b0f3a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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}")