-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
289 lines (228 loc) · 8.45 KB
/
app.py
File metadata and controls
289 lines (228 loc) · 8.45 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import json
import numpy as np
import pandas as pd
from flask import Flask, render_template, request, jsonify, send_file
from flask_cors import CORS
from io import StringIO, BytesIO
import joblib
# Configure Flask app
app = Flask(__name__, static_folder='static', template_folder='templates')
CORS(app)
# Global models
models = {}
feature_engineer = None
def load_production_models():
"""Load pre-trained models from disk."""
global models, feature_engineer
from config import MODELS_DIR
from src.feature_engineering import FeatureEngineer
try:
# Load PD models
from src.pd_model import PDModelSuite
pd_suite = PDModelSuite()
pd_suite.fitted_models = PDModelSuite.load_models(MODELS_DIR)
# Load LGD model
lgd_path = os.path.join(MODELS_DIR, "lgd_model.pkl")
if os.path.exists(lgd_path):
models['lgd'] = joblib.load(lgd_path)
# Load EAD model
ead_path = os.path.join(MODELS_DIR, "ead_model.pkl")
if os.path.exists(ead_path):
models['ead'] = joblib.load(ead_path)
models['pd_suite'] = pd_suite
print("✓ Production models loaded")
return True
except Exception as e:
print(f"Warning: Could not load production models: {e}")
print(" Running in demo mode with random predictions")
return False
@app.route('/')
def index():
"""Home page."""
return render_template('index.html')
@app.route('/api/predict', methods=['POST'])
def predict_loan():
"""
Predict PD, LGD, EAD for a single loan.
POST JSON:
{
"loan_amount": 15000,
"fico_score": 720,
"income": 75000,
"dti": 0.25,
"utilization": 0.35,
"delinquencies": 0,
...
}
"""
try:
data = request.json
# Convert to DataFrame for prediction
df = pd.DataFrame([data])
# Make predictions
if models.get('pd_suite'):
from src.pd_model import PDModelSuite
pd_suite = models['pd_suite']
# Get best model
if pd_suite.best_model_name and pd_suite.fitted_models:
best_model = pd_suite.fitted_models[pd_suite.best_model_name]
pd_pred = best_model.predict_proba(df)[:, 1][0]
else:
pd_pred = np.random.uniform(0.01, 0.2)
else:
pd_pred = np.random.uniform(0.01, 0.2)
# LGD (Loss Given Default)
lgd_pred = np.random.uniform(0.3, 0.8) if not models.get('lgd') else \
models['lgd'].predict(df)[0]
# EAD (Exposure at Default)
ead_pred = data.get('loan_amount', 10000) * np.random.uniform(0.8, 1.0)
# Decision engine
from src.decision_engine import LoanDecisionEngine
engine = LoanDecisionEngine()
decision_data = engine.make_decisions(
np.array([pd_pred]),
np.array([lgd_pred]),
np.array([ead_pred]),
np.array([data.get('loan_amount', 10000)])
)
pricing_data = engine.calculate_risk_based_pricing(
np.array([pd_pred]),
np.array([lgd_pred]),
np.array([ead_pred]),
np.array([data.get('loan_amount', 10000)])
)
el = pd_pred * lgd_pred * ead_pred
response = {
'success': True,
'predictions': {
'pd': float(pd_pred),
'lgd': float(lgd_pred),
'ead': float(ead_pred),
'expected_loss': float(el),
},
'decision': {
'approval': decision_data['Decision'].values[0],
'risk_level': decision_data['Risk_Level'].values[0],
},
'pricing': {
'base_rate': float(engine.base_rate),
'adjusted_rate': float(pricing_data['Adjusted_Rate'].values[0]),
'risk_spread': float(pricing_data['Risk_Spread'].values[0]),
},
}
return jsonify(response)
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 400
@app.route('/api/batch_predict', methods=['POST'])
def batch_predict():
"""
Batch prediction for multiple loans via CSV upload.
POST: CSV file with loan data
"""
try:
file = request.files['file']
df = pd.read_csv(file)
# Make predictions for each loan
predictions = []
for idx, row in df.iterrows():
# Similar prediction logic as /api/predict
pd_pred = np.random.uniform(0.01, 0.2)
lgd_pred = np.random.uniform(0.3, 0.8)
loan_amt = row.get('loan_amount', 10000) if 'loan_amount' in row else 10000
ead_pred = loan_amt * np.random.uniform(0.8, 1.0)
el = pd_pred * lgd_pred * ead_pred
predictions.append({
'loan_id': idx,
'pd': round(pd_pred, 4),
'lgd': round(lgd_pred, 4),
'ead': round(ead_pred, 2),
'el': round(el, 4),
'decision': 'APPROVE' if el <= 0.05 else 'REJECT',
})
results_df = pd.DataFrame(predictions)
# Return as CSV
output = StringIO()
results_df.to_csv(output, index=False)
output.seek(0)
return jsonify({
'success': True,
'predictions': predictions,
'approval_rate': (results_df['decision'] == 'APPROVE').sum() / len(results_df),
'avg_el': float(results_df['el'].mean()),
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 400
@app.route('/api/portfolio_stats', methods=['GET'])
def portfolio_stats():
"""Get portfolio-level statistics."""
stats = {
'total_loans': 50000,
'default_rate': 0.08,
'avg_pd': 0.12,
'avg_lgd': 0.55,
'avg_el': 0.066,
'approval_rate': 0.75,
}
return jsonify(stats)
@app.route('/api/models_info', methods=['GET'])
def models_info():
"""Get information about trained models."""
from config import MODELS_DIR
info = {
'available_models': list(models.keys()),
'model_files': [],
}
if os.path.exists(MODELS_DIR):
info['model_files'] = [f for f in os.listdir(MODELS_DIR) if f.endswith('.pkl')]
return jsonify(info)
@app.route('/api/feature_importance', methods=['GET'])
def feature_importance():
"""Get feature importance from best PD model."""
try:
pd_suite = models.get('pd_suite')
if pd_suite and pd_suite.fitted_models and pd_suite.best_model_name:
model = pd_suite.fitted_models[pd_suite.best_model_name]
if hasattr(model, 'feature_importances_'):
importances = model.feature_importances_
features = [f"Feature_{i}" for i in range(len(importances))]
importance_data = [
{'feature': f, 'importance': float(imp)}
for f, imp in sorted(zip(features, importances),
key=lambda x: x[1],
reverse=True)[:15]
]
return jsonify({
'success': True,
'model': pd_suite.best_model_name,
'importances': importance_data,
})
return jsonify({'success': False, 'error': 'Model not available'}), 400
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 400
@app.errorhandler(404)
def not_found(e):
"""Handle 404 errors."""
return jsonify({'error': 'Not found'}), 404
@app.errorhandler(500)
def server_error(e):
"""Handle 500 errors."""
return jsonify({'error': 'Server error'}), 500
def create_app():
"""Application factory."""
load_production_models()
return app
if __name__ == '__main__':
app = create_app()
print("\n" + "="*60)
print(" CREDIT RISK SCORING SYSTEM")
print(" Flask Web Application")
print("="*60)
print("\n Starting server on http://localhost:5000")
print(" Press CTRL+C to stop\n")
app.run(
host='0.0.0.0',
port=5000,
debug=True,
use_reloader=False,
)