""" 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" )