anasmkh commited on
Commit
bfa1d06
Β·
verified Β·
1 Parent(s): 5bb9cf2
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -3,44 +3,57 @@ import numpy as np
3
  import pandas as pd
4
  import tensorflow as tf
5
  import joblib
6
- from sklearn.preprocessing import LabelEncoder, StandardScaler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  try:
9
- model = tf.keras.models.load_model("banking_model.keras")
10
  scaler = joblib.load("scaler.pkl")
11
  label_encoders = joblib.load("label_encoders.pkl")
 
 
 
 
12
  except Exception as e:
13
  st.error(f"Error loading model or preprocessors: {e}")
14
  st.stop()
15
 
16
 
17
- st.title("πŸ“Š Banking App")
18
  st.write("Enter the feature values below to predict the classification stage.")
19
 
20
- if not label_encoders:
21
- st.error("Label encoders are empty. Make sure the model was trained correctly.")
22
- st.stop()
23
-
24
  numerical_inputs = {}
25
  categorical_inputs = {}
26
 
27
  try:
28
- numerical_features = list(scaler.feature_names_in_)
29
- categorical_features = list(label_encoders.keys())
30
  except AttributeError:
31
  st.error("Scaler or encoders are not properly loaded.")
32
  st.stop()
33
 
34
- # βœ… FIXED: Ensure numerical inputs always have valid default values
35
  for feature in numerical_features:
36
- numerical_inputs[feature] = st.number_input(f"Enter {feature}", value=0.0, step=0.1, format="%.2f")
37
 
38
  for feature in categorical_features:
39
- if label_encoders[feature].classes_.size > 0:
40
  categorical_inputs[feature] = st.selectbox(f"Select {feature}", label_encoders[feature].classes_)
41
  else:
42
- st.error(f"Label encoder for {feature} is empty.")
43
- st.stop()
44
 
45
  if st.button("Predict"):
46
  try:
@@ -58,4 +71,3 @@ if st.button("Predict"):
58
 
59
  except Exception as e:
60
  st.error(f"Prediction error: {e}")
61
-
 
3
  import pandas as pd
4
  import tensorflow as tf
5
  import joblib
6
+ import os
7
+
8
+
9
+ if not os.path.exists("banking_model.keras"):
10
+ st.error("🚨 Model file not found! Train and save the model first.")
11
+ st.stop()
12
+
13
+ if not os.path.exists("scaler.pkl"):
14
+ st.error("🚨 Scaler file 'scaler.pkl' not found! Make sure preprocessing was done correctly.")
15
+ st.stop()
16
+
17
+ if not os.path.exists("label_encoders.pkl"):
18
+ st.error("🚨 Label encoder file 'label_encoders.pkl' not found! Ensure encoding was saved properly.")
19
+ st.stop()
20
+
21
 
22
  try:
23
+ model = tf.keras.models.load_model("banking_model.keras")
24
  scaler = joblib.load("scaler.pkl")
25
  label_encoders = joblib.load("label_encoders.pkl")
26
+
27
+ if not label_encoders:
28
+ raise ValueError("Label encoders are empty!")
29
+
30
  except Exception as e:
31
  st.error(f"Error loading model or preprocessors: {e}")
32
  st.stop()
33
 
34
 
35
+ st.title("πŸ“Š Classification Prediction App")
36
  st.write("Enter the feature values below to predict the classification stage.")
37
 
 
 
 
 
38
  numerical_inputs = {}
39
  categorical_inputs = {}
40
 
41
  try:
42
+ numerical_features = list(scaler.feature_names_in_)
43
+ categorical_features = list(label_encoders.keys())
44
  except AttributeError:
45
  st.error("Scaler or encoders are not properly loaded.")
46
  st.stop()
47
 
 
48
  for feature in numerical_features:
49
+ numerical_inputs[feature] = float(st.number_input(f"Enter {feature}", value=0.0, step=0.1, format="%.2f"))
50
 
51
  for feature in categorical_features:
52
+ if len(label_encoders[feature].classes_) > 0:
53
  categorical_inputs[feature] = st.selectbox(f"Select {feature}", label_encoders[feature].classes_)
54
  else:
55
+ st.warning(f"⚠️ No classes found for '{feature}'. Skipping this feature.")
56
+
57
 
58
  if st.button("Predict"):
59
  try:
 
71
 
72
  except Exception as e:
73
  st.error(f"Prediction error: {e}")