-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
198 lines (163 loc) · 6.24 KB
/
app.py
File metadata and controls
198 lines (163 loc) · 6.24 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Credit Risk Classification - Gradio App"""
import gradio as gr
import joblib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc, confusion_matrix
from service import predict_and_recommend, get_metrics
# Load test data for showcase
X_test = joblib.load("artifacts/X_test.pkl")
y_test = joblib.load("artifacts/y_test.pkl")
pipeline = joblib.load("artifacts/rf_smote_pipeline.pkl")
# Feature options (must match service.py)
sex_options = ["male", "female"]
housing_options = ["own", "rent", "free"]
saving_options = ["little", "moderate", "quite rich", "rich", "none"]
checking_options = ["little", "moderate", "rich", "none"]
job_options = [0, 1, 2, 3]
def create_roc_plot():
"""Create ROC curve plot"""
y_proba = pipeline.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_proba)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color="blue", lw=2, label=f"ROC curve (AUC = {roc_auc:.3f})")
plt.plot([0, 1], [0, 1], color="gray", lw=1, linestyle="--", label="Random")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel("False Positive Rate", fontsize=12)
plt.ylabel("True Positive Rate", fontsize=12)
plt.title("ROC Curve - Credit Risk Model", fontsize=14)
plt.legend(loc="lower right")
plt.tight_layout()
return plt.gcf()
def create_confusion_matrix_plot():
"""Create confusion matrix plot"""
y_pred = pipeline.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation="nearest", cmap="Blues")
plt.title("Confusion Matrix", fontsize=14)
plt.colorbar()
classes = ["Good (0)", "Bad (1)"]
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes)
plt.yticks(tick_marks, classes)
# Add text annotations
thresh = cm.max() / 2.0
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(
j,
i,
format(cm[i, j], "d"),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black",
)
plt.ylabel("Actual", fontsize=12)
plt.xlabel("Predicted", fontsize=12)
plt.tight_layout()
return plt.gcf()
# =====================
# GRADIO INTERFACE
# =====================
with gr.Blocks(title="Credit Risk Classification") as demo:
gr.Markdown("""
# 🎯 Credit Risk Classification System
**German Credit Risk Dataset** | Random Forest + SMOTE | F1 = 0.84
---
This application predicts credit risk using machine learning and provides
strategic recommendations via LLM analysis.
""")
with gr.Tabs():
# =====================
# TAB 1: MODEL SHOWCASE
# =====================
with gr.Tab("📊 Model Showcase"):
gr.Markdown("## Performance Metrics")
metrics = get_metrics()
gr.JSON(metrics)
gr.Markdown("---")
gr.Markdown("## ROC Curve")
roc_fig = create_roc_plot()
gr.Plot(roc_fig)
gr.Markdown("---")
gr.Markdown("## Confusion Matrix")
cm_fig = create_confusion_matrix_plot()
gr.Plot(cm_fig)
gr.Markdown("""
---
**Model Performance Summary:**
- F1 Score (Bad): 0.84
- ROC AUC: 0.758
- Recall (Bad): 85.7%
- Precision (Bad): 82.8%
""")
# =====================
# TAB 2: MAKE PREDICTION
# =====================
with gr.Tab("🔮 Make Prediction"):
gr.Markdown("## Select Applicant Features")
gr.Markdown(
"Fill in the customer information below to get a risk prediction and strategic recommendation."
)
with gr.Row():
with gr.Column():
age = gr.Slider(18, 80, value=30, step=1, label="Age")
sex = gr.Dropdown(sex_options, value="male", label="Sex")
job = gr.Dropdown(
job_options,
value=1,
label="Job (0=unskilled, 3=highly skilled)",
)
with gr.Column():
housing = gr.Dropdown(housing_options, value="own", label="Housing")
savings = gr.Dropdown(
saving_options, value="moderate", label="Savings Account"
)
checking = gr.Dropdown(
checking_options, value="little", label="Checking Account"
)
with gr.Row():
credit_amount = gr.Number(
value=5000, label="Credit Amount ($)", minimum=0
)
duration = gr.Slider(1, 72, value=24, step=1, label="Duration (months)")
gr.Markdown("---")
with gr.Row():
predict_btn = gr.Button(
"Analyze & Get Recommendation", variant="primary", size="lg"
)
gr.Markdown("---")
with gr.Row():
with gr.Column():
prediction_output = gr.Label(label="Model Prediction")
with gr.Column():
confidence_output = gr.Textbox(label="Confidence", lines=1)
gr.Markdown("---")
# Display LLM recommendation as Markdown
recommendation_output = gr.Markdown(label="Strategic Recommendation (LLM)")
# Connect button to prediction function
predict_btn.click(
predict_and_recommend,
inputs=[
age,
sex,
job,
housing,
savings,
checking,
credit_amount,
duration,
],
outputs=[prediction_output, confidence_output, recommendation_output],
)
gr.Markdown("""
---
**© 2024 Credit Risk Classification System**
Built with ❤️ using Gradio, scikit-learn, and OpenAI-compatible LLM
""")
# Launch the app
if __name__ == "__main__":
demo.launch(share=False)