Keyurjotaniya007 commited on
Commit
02fbfb3
Β·
verified Β·
1 Parent(s): e484048

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -12
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import streamlit as st
3
  import joblib
4
  import numpy as np
@@ -8,18 +7,16 @@ st.set_page_config(page_title="Loan Default Predictor", layout="centered")
8
  st.title("Loan Default Predictor β€” Give Me Some Credit")
9
  st.write("Enter applicant features below and click **Predict** to get `SeriousDlqin2yrs` prediction.")
10
 
11
- # load model once
12
  @st.cache_resource
13
  def load_model(path="model.pkl"):
14
  return joblib.load(path)
15
 
16
  try:
17
- model = load_model("model.pkl") # update path if different
18
  except Exception as e:
19
  st.error(f"Unable to load model.pkl β€” check file path. Error: {e}")
20
  st.stop()
21
 
22
- # --- Input widgets (use your defaults) ---
23
  st.subheader("Applicant features")
24
 
25
  col1, col2 = st.columns(2)
@@ -62,8 +59,6 @@ with col4:
62
  HighDebtRatio = st.selectbox("HighDebtRatio", options=[0, 1], index=0)
63
  HighUtilization = st.selectbox("HighUtilization", options=[0, 1], index=0)
64
 
65
- # Build feature vector in the same order used during training
66
- # IMPORTANT: ensure this order matches exactly the order your model expects
67
  feature_order = [
68
  RevolvingUtilizationOfUnsecuredLines,
69
  age,
@@ -86,7 +81,6 @@ feature_order = [
86
 
87
  X_input = np.array(feature_order).reshape(1, -1)
88
 
89
- # Predict button
90
  if st.button("Predict"):
91
  try:
92
  pred = model.predict(X_input)[0]
@@ -94,15 +88,12 @@ if st.button("Predict"):
94
  st.error(f"Prediction failed. Check feature order/types. Error: {e}")
95
  st.stop()
96
 
97
- # mapping
98
  label_map = {0: "No Default (Good Credit)", 1: "Default (High Risk)"}
99
  meaning = label_map.get(int(pred), str(pred))
100
 
101
- # probability if available
102
  proba_str = ""
103
  try:
104
  proba = model.predict_proba(X_input)[0]
105
- # If binary, proba[1] is probability of class 1
106
  if len(proba) == 2:
107
  proba_str = f" β€” P(Default) = {proba[1]:.4f}"
108
  else:
@@ -115,11 +106,10 @@ if st.button("Predict"):
115
  else:
116
  st.success(f"Prediction: {pred} β†’ {meaning}{proba_str}")
117
 
118
- # show raw output for debugging
119
  with st.expander("Show raw inputs and model output"):
120
  st.write("Input vector (order):", feature_order)
121
  st.write("Raw prediction:", int(pred))
122
  if proba_str:
123
  st.write("Raw probabilities:", proba if 'proba' in locals() else None)
124
 
125
- st.caption("Model expects features in a specific order. If predictions seem off, verify the feature order and preprocessing used during training.")
 
 
1
  import streamlit as st
2
  import joblib
3
  import numpy as np
 
7
  st.title("Loan Default Predictor β€” Give Me Some Credit")
8
  st.write("Enter applicant features below and click **Predict** to get `SeriousDlqin2yrs` prediction.")
9
 
 
10
  @st.cache_resource
11
  def load_model(path="model.pkl"):
12
  return joblib.load(path)
13
 
14
  try:
15
+ model = load_model("model.pkl")
16
  except Exception as e:
17
  st.error(f"Unable to load model.pkl β€” check file path. Error: {e}")
18
  st.stop()
19
 
 
20
  st.subheader("Applicant features")
21
 
22
  col1, col2 = st.columns(2)
 
59
  HighDebtRatio = st.selectbox("HighDebtRatio", options=[0, 1], index=0)
60
  HighUtilization = st.selectbox("HighUtilization", options=[0, 1], index=0)
61
 
 
 
62
  feature_order = [
63
  RevolvingUtilizationOfUnsecuredLines,
64
  age,
 
81
 
82
  X_input = np.array(feature_order).reshape(1, -1)
83
 
 
84
  if st.button("Predict"):
85
  try:
86
  pred = model.predict(X_input)[0]
 
88
  st.error(f"Prediction failed. Check feature order/types. Error: {e}")
89
  st.stop()
90
 
 
91
  label_map = {0: "No Default (Good Credit)", 1: "Default (High Risk)"}
92
  meaning = label_map.get(int(pred), str(pred))
93
 
 
94
  proba_str = ""
95
  try:
96
  proba = model.predict_proba(X_input)[0]
 
97
  if len(proba) == 2:
98
  proba_str = f" β€” P(Default) = {proba[1]:.4f}"
99
  else:
 
106
  else:
107
  st.success(f"Prediction: {pred} β†’ {meaning}{proba_str}")
108
 
 
109
  with st.expander("Show raw inputs and model output"):
110
  st.write("Input vector (order):", feature_order)
111
  st.write("Raw prediction:", int(pred))
112
  if proba_str:
113
  st.write("Raw probabilities:", proba if 'proba' in locals() else None)
114
 
115
+ st.caption("Model expects features in a specific order. If predictions seem off, verify the feature order and preprocessing used during training.")