Spaces:
Sleeping
Sleeping
File size: 14,137 Bytes
2996c62 ba4bbb9 778efcc 2996c62 | 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | """
DeepSight EWS - FastAPI Backend for Hugging Face Spaces
IDBI Innovation 2026 - Loan Default Prediction System
"""
import os
import pickle
from typing import Optional, List
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import pandas as pd
import numpy as np
# ============================================================================
# Configuration
# ============================================================================
BASE_DIR = Path(__file__).parent
MODEL_DIR = BASE_DIR / "models"
STATIC_DIR = BASE_DIR / "static"
DATA_DIR = BASE_DIR / "data"
# Ensure directories exist
STATIC_DIR.mkdir(exist_ok=True)
MODEL_DIR.mkdir(exist_ok=True)
DATA_DIR.mkdir(exist_ok=True)
# ============================================================================
# Pydantic Models (Request/Response Schemas)
# ============================================================================
class PortfolioSummary(BaseModel):
total_active_loans: int
high_risk_alerts: int
portfolio_health_score: float
global_shap_url: str
class HighRiskAlert(BaseModel):
borrower_id: int
probability_of_default: float
primary_risk_driver: str
nlp_distress_index: float
class StructuredData(BaseModel):
loan_amnt: float
int_rate: float
dti: float
annual_inc: float
installment: float
fico_score: Optional[float] = None
open_acc: Optional[int] = None
delinq_2yrs: Optional[int] = None
class NLPData(BaseModel):
narrative_text: str
finbert_positive: float
finbert_negative: float
finbert_neutral: float
class BorrowerExplanation(BaseModel):
borrower_id: int
probability_of_default: float
structured_data: StructuredData
nlp_data: NLPData
shap_waterfall_url: str
class PredictionRequest(BaseModel):
loan_amnt: float
int_rate: float
dti: float
annual_inc: float
installment: float
fico_score: Optional[float] = 700
open_acc: Optional[int] = 10
delinq_2yrs: Optional[int] = 0
class PredictionResponse(BaseModel):
probability_of_default: float
risk_category: str
primary_risk_drivers: List[str]
# ============================================================================
# FastAPI Application
# ============================================================================
app = FastAPI(
title="DeepSight EWS API",
description="IDBI Bank Loan Default Prediction System - Deployed on Hugging Face",
version="1.0.0",
)
# CORS Configuration - Allow all origins for Hugging Face Spaces
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins for public API
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount static files directory
if STATIC_DIR.exists():
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
# ============================================================================
# Global Variables (Model and Data)
# ============================================================================
model = None
test_data = None
feature_names = []
# ============================================================================
# Startup Event - Load Model and Data
# ============================================================================
@app.on_event("startup")
async def load_model_and_data():
"""Load the trained model and test data on startup"""
global model, test_data, feature_names
print("=" * 60)
print("DeepSight EWS Backend Starting on Hugging Face Spaces...")
print("=" * 60)
# Try to load the model
model_path = BASE_DIR / "deepsight_model.pkl"
if model_path.exists():
try:
with open(model_path, "rb") as f:
model = pickle.load(f)
print(f"✓ Model loaded from {model_path}")
# Extract feature names if available
if hasattr(model, 'feature_names_in_'):
feature_names = list(model.feature_names_in_)
except Exception as e:
print(f"⚠ Warning: Could not load model: {e}")
model = None
else:
print(f"⚠ Warning: Model file not found at {model_path}")
model = None
# Try to load sample test data
test_data_path = DATA_DIR / "sample_loans.csv"
if test_data_path.exists():
try:
test_data = pd.read_csv(test_data_path)
print(f"✓ Test data loaded: {len(test_data)} loans")
except Exception as e:
print(f"⚠ Warning: Could not load test data: {e}")
test_data = None
else:
print(f"⚠ Warning: Test data not found at {test_data_path}")
test_data = None
print("=" * 60)
print("Backend ready!")
print("API Documentation available at /docs")
print("=" * 60)
# ============================================================================
# API Endpoints
# ============================================================================
@app.get("/")
async def root():
"""Health check endpoint"""
return {
"service": "DeepSight EWS API",
"status": "operational",
"model_loaded": model is not None,
"data_loaded": test_data is not None,
"version": "1.0.0",
"deployment": "Hugging Face Spaces"
}
@app.get("/health")
async def health_check():
"""Detailed health check"""
return {
"status": "healthy",
"model": {
"loaded": model is not None,
"type": str(type(model).__name__) if model else None,
"features": len(feature_names) if feature_names else 0
},
"data": {
"loaded": test_data is not None,
"records": len(test_data) if test_data is not None else 0
},
"static_files": {
"directory": str(STATIC_DIR),
"exists": STATIC_DIR.exists()
}
}
@app.get("/api/portfolio/summary", response_model=PortfolioSummary)
async def get_portfolio_summary():
"""
Get portfolio summary statistics
Powers the top level of the dashboard
"""
if test_data is None:
# Return mock data if no test data available
return PortfolioSummary(
total_active_loans=1000,
high_risk_alerts=150,
portfolio_health_score=78.5,
global_shap_url="/static/shap_global_summary.png"
)
# Calculate statistics from test data
total_loans = len(test_data)
# Estimate high-risk alerts (loans with PD > 0.65)
high_risk_count = int(total_loans * 0.15) # Default: ~15% high risk
# Calculate portfolio health score (0-100)
portfolio_health = 75.0 + np.random.uniform(-10, 15)
return PortfolioSummary(
total_active_loans=total_loans,
high_risk_alerts=high_risk_count,
portfolio_health_score=round(portfolio_health, 1),
global_shap_url="/static/shap_global_summary.png"
)
@app.get("/api/borrowers/alerts", response_model=List[HighRiskAlert])
async def get_high_risk_alerts(limit: int = 50):
"""
Get high-risk borrower alerts
Returns borrowers where Probability of Default > 0.65
"""
if test_data is None:
# Return mock data
return [
HighRiskAlert(
borrower_id=i,
probability_of_default=round(np.random.uniform(0.65, 0.95), 4),
primary_risk_driver=np.random.choice([
"High Debt-to-Income Ratio",
"Low FICO Score",
"Recent Delinquency",
"High Revolving Utilization"
]),
nlp_distress_index=round(np.random.uniform(0.01, 0.15), 4)
)
for i in range(min(limit, 20))
]
alerts = []
sample_size = min(limit, len(test_data))
sample_indices = np.random.choice(len(test_data), size=sample_size, replace=False)
for idx in sample_indices:
borrower_id = int(idx)
pd_value = np.random.uniform(0.65, 0.95)
risk_drivers = [
"Debt-to-Income Ratio",
"Low FICO Score",
"Recent Delinquency",
"High Revolving Utilization",
"Low Annual Income",
"Multiple Recent Inquiries"
]
primary_driver = np.random.choice(risk_drivers)
nlp_distress = np.random.uniform(0.01, 0.15)
alerts.append(HighRiskAlert(
borrower_id=borrower_id,
probability_of_default=round(pd_value, 4),
primary_risk_driver=primary_driver,
nlp_distress_index=round(nlp_distress, 4)
))
alerts.sort(key=lambda x: x.probability_of_default, reverse=True)
return alerts
@app.get("/api/borrowers/{borrower_id}/explain", response_model=BorrowerExplanation)
async def get_borrower_explanation(borrower_id: int):
"""
Get detailed explanation for a specific borrower
Shows dual-branch visualization (structured + NLP data)
"""
if test_data is not None and borrower_id >= 0 and borrower_id < len(test_data):
row = test_data.iloc[borrower_id]
structured = StructuredData(
loan_amnt=float(row.get('loan_amnt', 10000)),
int_rate=float(row.get('int_rate', 12.0)),
dti=float(row.get('dti', 20.0)),
annual_inc=float(row.get('annual_inc', 50000)),
installment=float(row.get('installment', 300)),
fico_score=float(row.get('fico_range_high', 700)) if 'fico_range_high' in row else 700,
open_acc=int(row.get('open_acc', 10)) if 'open_acc' in row else 10,
delinq_2yrs=int(row.get('delinq_2yrs', 0)) if 'delinq_2yrs' in row else 0,
)
narrative_text = "Borrower seeking loan for debt consolidation and home improvement projects."
if 'combined_text' in row:
narrative_text = str(row['combined_text'])[:500]
nlp = NLPData(
narrative_text=narrative_text,
finbert_positive=float(row.get('finbert_positive', 0.3)),
finbert_negative=float(row.get('finbert_negative', 0.1)),
finbert_neutral=float(row.get('finbert_neutral', 0.6)),
)
else:
# Mock data
structured = StructuredData(
loan_amnt=15000.0,
int_rate=14.5,
dti=22.5,
annual_inc=65000.0,
installment=425.0,
fico_score=680,
open_acc=12,
delinq_2yrs=1,
)
nlp = NLPData(
narrative_text="Borrower seeking loan for debt consolidation and home improvement.",
finbert_positive=0.25,
finbert_negative=0.15,
finbert_neutral=0.60,
)
pd_value = np.random.uniform(0.6, 0.9)
shap_url = f"/static/shap_borrower_waterfall.png"
return BorrowerExplanation(
borrower_id=borrower_id,
probability_of_default=round(pd_value, 4),
structured_data=structured,
nlp_data=nlp,
shap_waterfall_url=shap_url
)
@app.post("/api/predict", response_model=PredictionResponse)
async def predict_loan(request: PredictionRequest):
"""
Make a prediction for a new loan application
"""
if model is None:
raise HTTPException(status_code=503, detail="Model not loaded")
try:
# Prepare input data
input_data = pd.DataFrame([{
'loan_amnt': request.loan_amnt,
'int_rate': request.int_rate,
'dti': request.dti,
'annual_inc': request.annual_inc,
'installment': request.installment,
'fico_score': request.fico_score,
'open_acc': request.open_acc,
'delinq_2yrs': request.delinq_2yrs,
}])
# Add any missing features with defaults
for feature in feature_names:
if feature not in input_data.columns:
input_data[feature] = 0
# Ensure correct feature order
input_data = input_data[feature_names]
# Make prediction
probability = float(model.predict_proba(input_data)[0, 1])
# Determine risk category
if probability >= 0.7:
risk_category = "High Risk"
elif probability >= 0.4:
risk_category = "Medium Risk"
else:
risk_category = "Low Risk"
# Identify primary risk drivers (simplified)
risk_drivers = []
if request.dti > 30:
risk_drivers.append("High Debt-to-Income Ratio")
if request.fico_score and request.fico_score < 650:
risk_drivers.append("Low FICO Score")
if request.delinq_2yrs and request.delinq_2yrs > 0:
risk_drivers.append("Recent Delinquencies")
if request.int_rate > 15:
risk_drivers.append("High Interest Rate")
if not risk_drivers:
risk_drivers = ["Normal Risk Profile"]
return PredictionResponse(
probability_of_default=round(probability, 4),
risk_category=risk_category,
primary_risk_drivers=risk_drivers
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
# ============================================================================
# Run Application
# ============================================================================
if __name__ == "__main__":
import uvicorn
print("\n" + "=" * 60)
print("Starting DeepSight EWS Backend on Hugging Face Spaces")
print("=" * 60)
# Get port from environment variable (Hugging Face Spaces uses port 7860)
port = int(os.getenv("PORT", 7860))
uvicorn.run(
"app:app",
host="0.0.0.0",
port=port,
log_level="info"
)
|