anasmkh commited on
Commit
17c19e6
Β·
verified Β·
1 Parent(s): bfa1d06

update interface

Browse files
Files changed (1) hide show
  1. app.py +55 -70
app.py CHANGED
@@ -1,73 +1,58 @@
1
  import streamlit as st
2
- import numpy as np
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:
60
- for feature in categorical_inputs:
61
- categorical_inputs[feature] = label_encoders[feature].transform([categorical_inputs[feature]])[0]
62
-
63
- input_data = pd.DataFrame([{**numerical_inputs, **categorical_inputs}])
64
-
65
- input_data[numerical_features] = scaler.transform(input_data[numerical_features])
66
-
67
- prediction = model.predict(input_data)
68
- predicted_class = np.argmax(prediction)
69
-
70
- st.success(f"βœ… Predicted Classification Stage: {predicted_class}")
71
-
72
- except Exception as e:
73
- st.error(f"Prediction error: {e}")
 
1
  import streamlit as st
 
2
  import pandas as pd
3
+ import numpy as np
4
  import tensorflow as tf
5
+ from sklearn.preprocessing import StandardScaler, LabelEncoder
6
+
7
+ # Load the model
8
+ model = tf.keras.models.load_model("banking_model.keras")
9
+
10
+ # Function to preprocess input data
11
+ def preprocess_input(input_data, label_encoders, scaler):
12
+ # Convert input data to DataFrame
13
+ input_df = pd.DataFrame([input_data])
14
+
15
+ # Encode categorical variables
16
+ for col in label_encoders:
17
+ input_df[col] = label_encoders[col].transform(input_df[col])
18
+
19
+ # Scale numerical variables
20
+ numerical_columns = input_df.select_dtypes(include=["int64", "float64"]).columns
21
+ input_df[numerical_columns] = scaler.transform(input_df[numerical_columns])
22
+
23
+ return input_df
24
+
25
+ # Streamlit app
26
+ def main():
27
+ st.title("Banking Stage Classification")
28
+
29
+ # Input fields
30
+ st.sidebar.header("User Input Features")
31
+
32
+ # Example feature inputs, adjust according to your actual features
33
+ credit_expiration = st.sidebar.number_input("Credit Expiration", min_value=0, value=0)
34
+ dpd = st.sidebar.number_input("DPD", min_value=0, value=0)
35
+ feature1 = st.sidebar.selectbox("Feature 1", options=["Yes", "No"])
36
+ feature2 = st.sidebar.selectbox("Feature 2", options=["Yes", "No"])
37
+ stage_last_month = st.sidebar.selectbox("Stage As Last Month", options=[1, 2, 3])
38
+
39
+ # Prepare input data
40
+ input_data = {
41
+ 'Credit Expiration': credit_expiration,
42
+ 'DPD': dpd,
43
+ 'Feature 1': feature1,
44
+ 'Feature 2': feature2,
45
+ 'Stage As Last Month': stage_last_month
46
+ }
47
+
48
+ # Preprocess the input
49
+ processed_input = preprocess_input(input_data, label_encoders, scaler)
50
+
51
+ # Make prediction
52
+ if st.sidebar.button("Predict"):
53
+ prediction = model.predict(processed_input)
54
+ predicted_stage = np.argmax(prediction, axis=1) + 1 # Adjust if necessary
55
+ st.success(f"Predicted Current Stage: {predicted_stage[0]}")
56
+
57
+ if __name__ == "__main__":
58
+ main()