Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ from imblearn.over_sampling import SMOTE
|
|
| 16 |
from imblearn.pipeline import Pipeline as ImbPipeline
|
| 17 |
|
| 18 |
# =====================================================================
|
| 19 |
-
# 1. CORE PIPELINE TRAINING (High-Speed
|
| 20 |
# =====================================================================
|
| 21 |
print("Initializing and training the Stacking Pipeline...")
|
| 22 |
|
|
@@ -26,7 +26,6 @@ df = pd.read_csv('loan_dataset.csv')
|
|
| 26 |
X = df.drop('BAD', axis=1)
|
| 27 |
y = df['BAD']
|
| 28 |
|
| 29 |
-
# Updated column selectors to prevent Deprecated Pandas4Warnings
|
| 30 |
num_cols = X.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
| 31 |
cat_cols = X.select_dtypes(include=['object', 'string']).columns.tolist()
|
| 32 |
|
|
@@ -46,7 +45,7 @@ def cap_outliers_iqr(data, columns):
|
|
| 46 |
|
| 47 |
X_cleaned = cap_outliers_iqr(X, num_cols)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
num_pipeline = Pipeline([
|
| 51 |
('imputer', KNNImputer(n_neighbors=5)),
|
| 52 |
('scaler', StandardScaler())
|
|
@@ -62,7 +61,7 @@ preprocessor = ColumnTransformer([
|
|
| 62 |
('cat', cat_pipeline, cat_cols)
|
| 63 |
])
|
| 64 |
|
| 65 |
-
#
|
| 66 |
rf_base = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42, n_jobs=-1)
|
| 67 |
xgb_base = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=5, eval_metric='logloss', random_state=42, n_jobs=-1)
|
| 68 |
|
|
@@ -110,7 +109,6 @@ def evaluate_loan_risk(loan, mortdue, value, reason, job, yoj, derog, delinq, cl
|
|
| 110 |
prediction_proba = production_pipeline.predict_proba(input_data)[0]
|
| 111 |
risk_of_default = prediction_proba[1]
|
| 112 |
|
| 113 |
-
# Generate high-contrast sleek outcome templates dynamically
|
| 114 |
if risk_of_default > 0.40:
|
| 115 |
bg_gradient = "linear-gradient(135deg, #451a1a 0%, #1f0d0d 100%)"
|
| 116 |
accent_color = "#f87171"
|
|
@@ -145,16 +143,15 @@ def evaluate_loan_risk(loan, mortdue, value, reason, job, yoj, derog, delinq, cl
|
|
| 145 |
# =====================================================================
|
| 146 |
custom_css = """
|
| 147 |
.gradio-container { background-color: #0f172a !important; color: #f8fafc !important; }
|
| 148 |
-
.section_panel { background-color: #1e293b !important; padding: 20px !important; border-radius: 12px !important; border: 1px solid #334155 !important; }
|
| 149 |
label { color: #cbd5e1 !important; font-weight: 600 !important; }
|
| 150 |
input, select { background-color: #0f172a !important; color: white !important; border: 1px solid #475569 !important; }
|
| 151 |
-
button.primary { background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important; border: none !important; font-weight: bold !important; }
|
| 152 |
"""
|
| 153 |
|
| 154 |
-
# FIX: Extracted css and title from here to be compliant with Gradio 6.0+
|
| 155 |
with io.Blocks() as app:
|
| 156 |
|
| 157 |
-
#
|
| 158 |
io.Markdown(
|
| 159 |
"""
|
| 160 |
<div style='text-align: center; margin-bottom: 30px; padding: 10px; border-bottom: 1px solid #334155;'>
|
|
@@ -167,7 +164,7 @@ with io.Blocks() as app:
|
|
| 167 |
# Master Layout Split
|
| 168 |
with io.Row():
|
| 169 |
|
| 170 |
-
# Left
|
| 171 |
with io.Column(scale=2):
|
| 172 |
|
| 173 |
with io.Column(elem_classes="section_panel"):
|
|
@@ -176,8 +173,6 @@ with io.Blocks() as app:
|
|
| 176 |
loan = io.Number(label="Loan Capital ($) [LOAN]", value=15000)
|
| 177 |
reason = io.Dropdown(choices=["DebtCon", "HomeImp"], label="Purpose [REASON]", value="DebtCon")
|
| 178 |
|
| 179 |
-
io.Spacer()
|
| 180 |
-
|
| 181 |
with io.Column(elem_classes="section_panel"):
|
| 182 |
io.Markdown("### 🏠 Segment 2: Assets & Occupation")
|
| 183 |
with io.Row():
|
|
@@ -187,8 +182,6 @@ with io.Blocks() as app:
|
|
| 187 |
job = io.Dropdown(choices=["Other", "Mgr", "Office", "ProfExe", "Sales", "Self"], label="Job Category [JOB]", value="Other")
|
| 188 |
yoj = io.Number(label="Years at Current Job [YOJ]", value=5)
|
| 189 |
|
| 190 |
-
io.Spacer()
|
| 191 |
-
|
| 192 |
with io.Column(elem_classes="section_panel"):
|
| 193 |
io.Markdown("### 💳 Segment 3: Historic Credit Performance")
|
| 194 |
with io.Row():
|
|
@@ -203,7 +196,7 @@ with io.Blocks() as app:
|
|
| 203 |
|
| 204 |
submit_btn = io.Button("⚡ Run Stacking Underwriting Evaluation", variant="primary")
|
| 205 |
|
| 206 |
-
# Right
|
| 207 |
with io.Column(scale=1):
|
| 208 |
with io.Column(elem_classes="section_panel"):
|
| 209 |
io.Markdown("### 🏁 Terminal Output Feed")
|
|
@@ -215,7 +208,7 @@ with io.Blocks() as app:
|
|
| 215 |
"""
|
| 216 |
)
|
| 217 |
|
| 218 |
-
#
|
| 219 |
submit_btn.click(
|
| 220 |
fn=evaluate_loan_risk,
|
| 221 |
inputs=[loan, mortdue, value, reason, job, yoj, derog, delinq, clage, ninq, clno, debtinc],
|
|
@@ -223,5 +216,4 @@ with io.Blocks() as app:
|
|
| 223 |
)
|
| 224 |
|
| 225 |
if __name__ == "__main__":
|
| 226 |
-
# FIX: CSS styling rules and app title parameters passed down here inside .launch()
|
| 227 |
app.launch(css=custom_css, title="AI Loan Underwriting Portal", share=True)
|
|
|
|
| 16 |
from imblearn.pipeline import Pipeline as ImbPipeline
|
| 17 |
|
| 18 |
# =====================================================================
|
| 19 |
+
# 1. CORE PIPELINE TRAINING (High-Speed Framework)
|
| 20 |
# =====================================================================
|
| 21 |
print("Initializing and training the Stacking Pipeline...")
|
| 22 |
|
|
|
|
| 26 |
X = df.drop('BAD', axis=1)
|
| 27 |
y = df['BAD']
|
| 28 |
|
|
|
|
| 29 |
num_cols = X.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
| 30 |
cat_cols = X.select_dtypes(include=['object', 'string']).columns.tolist()
|
| 31 |
|
|
|
|
| 45 |
|
| 46 |
X_cleaned = cap_outliers_iqr(X, num_cols)
|
| 47 |
|
| 48 |
+
# Transformers
|
| 49 |
num_pipeline = Pipeline([
|
| 50 |
('imputer', KNNImputer(n_neighbors=5)),
|
| 51 |
('scaler', StandardScaler())
|
|
|
|
| 61 |
('cat', cat_pipeline, cat_cols)
|
| 62 |
])
|
| 63 |
|
| 64 |
+
# Streamlined High-Speed Base Learners
|
| 65 |
rf_base = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42, n_jobs=-1)
|
| 66 |
xgb_base = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=5, eval_metric='logloss', random_state=42, n_jobs=-1)
|
| 67 |
|
|
|
|
| 109 |
prediction_proba = production_pipeline.predict_proba(input_data)[0]
|
| 110 |
risk_of_default = prediction_proba[1]
|
| 111 |
|
|
|
|
| 112 |
if risk_of_default > 0.40:
|
| 113 |
bg_gradient = "linear-gradient(135deg, #451a1a 0%, #1f0d0d 100%)"
|
| 114 |
accent_color = "#f87171"
|
|
|
|
| 143 |
# =====================================================================
|
| 144 |
custom_css = """
|
| 145 |
.gradio-container { background-color: #0f172a !important; color: #f8fafc !important; }
|
| 146 |
+
.section_panel { background-color: #1e293b !important; padding: 20px !important; border-radius: 12px !important; border: 1px solid #334155 !important; margin-bottom: 15px !important; }
|
| 147 |
label { color: #cbd5e1 !important; font-weight: 600 !important; }
|
| 148 |
input, select { background-color: #0f172a !important; color: white !important; border: 1px solid #475569 !important; }
|
| 149 |
+
button.primary { background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important; border: none !important; font-weight: bold !important; margin-top: 10px !important; }
|
| 150 |
"""
|
| 151 |
|
|
|
|
| 152 |
with io.Blocks() as app:
|
| 153 |
|
| 154 |
+
# Header Section
|
| 155 |
io.Markdown(
|
| 156 |
"""
|
| 157 |
<div style='text-align: center; margin-bottom: 30px; padding: 10px; border-bottom: 1px solid #334155;'>
|
|
|
|
| 164 |
# Master Layout Split
|
| 165 |
with io.Row():
|
| 166 |
|
| 167 |
+
# Left Panel (Inputs)
|
| 168 |
with io.Column(scale=2):
|
| 169 |
|
| 170 |
with io.Column(elem_classes="section_panel"):
|
|
|
|
| 173 |
loan = io.Number(label="Loan Capital ($) [LOAN]", value=15000)
|
| 174 |
reason = io.Dropdown(choices=["DebtCon", "HomeImp"], label="Purpose [REASON]", value="DebtCon")
|
| 175 |
|
|
|
|
|
|
|
| 176 |
with io.Column(elem_classes="section_panel"):
|
| 177 |
io.Markdown("### 🏠 Segment 2: Assets & Occupation")
|
| 178 |
with io.Row():
|
|
|
|
| 182 |
job = io.Dropdown(choices=["Other", "Mgr", "Office", "ProfExe", "Sales", "Self"], label="Job Category [JOB]", value="Other")
|
| 183 |
yoj = io.Number(label="Years at Current Job [YOJ]", value=5)
|
| 184 |
|
|
|
|
|
|
|
| 185 |
with io.Column(elem_classes="section_panel"):
|
| 186 |
io.Markdown("### 💳 Segment 3: Historic Credit Performance")
|
| 187 |
with io.Row():
|
|
|
|
| 196 |
|
| 197 |
submit_btn = io.Button("⚡ Run Stacking Underwriting Evaluation", variant="primary")
|
| 198 |
|
| 199 |
+
# Right Panel (Output View)
|
| 200 |
with io.Column(scale=1):
|
| 201 |
with io.Column(elem_classes="section_panel"):
|
| 202 |
io.Markdown("### 🏁 Terminal Output Feed")
|
|
|
|
| 208 |
"""
|
| 209 |
)
|
| 210 |
|
| 211 |
+
# Click trigger link
|
| 212 |
submit_btn.click(
|
| 213 |
fn=evaluate_loan_risk,
|
| 214 |
inputs=[loan, mortdue, value, reason, job, yoj, derog, delinq, clage, ninq, clno, debtinc],
|
|
|
|
| 216 |
)
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
|
|
|
| 219 |
app.launch(css=custom_css, title="AI Loan Underwriting Portal", share=True)
|