A FastAPI-based web application for credit default risk prediction, adapted from my Kaggle notebook
π Credit Risk Scorecard for Default Prediction.
The notebook covers the entire modeling pipeline, while this repository turns that work into a production-ready, Dockerized API + Web UI with CI/CD automation and Azure Cloud deployment.
π Production App (Azure):
π https://creditscorecardwoeiv-bgc3edb0fzgzdzfg.eastasia-01.azurewebsites.net/
βοΈ Startup may take 30β60 seconds on first access (free tier cold start).
- Credit Score Prediction using a Logistic Regression model with Weight of Evidence (WOE) transformation
- Industry-Standard Credit Ratings: Assigns S&P/Moody's/Fitch-style ratings (AAA, AA, A, BBB, BB, B, CCC, CC, C, D) based on credit score
- Default Risk Assessment: Predicts default probability + standardized credit rating
- Preprocessing Pipeline (feature binning, scaling, encoding)
- RESTful API built with FastAPI
- Interactive Swagger Docs at
/docs - Responsive Web UI for non-technical users
- Dockerized Deployment via Azure App Service or Docker Compose
- Full CI/CD Workflow using GitHub Actions (lint β test β build β push β deploy)
Credit Scorecard - Default Prediction/
βββ app/ # FastAPI application
β βββ main.py # App entrypoint
β βββ api/ # API layer: routes & schemas
β β βββ routes.py
β β βββ schemas.py
β βββ utils/ # Helper utilities
β β βββ helpers.py
β βββ static/ # Web UI (HTML, CSS, JS, assets)
βββ tests/ # Unit & integration tests
βββ models/ # Exported pipeline, model, scorecard, metadata
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ requirements-dev.txt
βββ .github/
βββ workflows/
βββ deploy.yml # CI/CD pipelineπ Background
This project is based on the Kaggle notebook: π Credit Risk Scorecard for Default Prediction
The data preparation, WOE binning, and logistic regression model training are all explained in the notebook.
The exported preprocessing pipeline, trained model, and scorecard are taken from that notebook.
This repository focuses on wrapping the trained pipeline + model into a FastAPI application, with a frontend and Dockerized CI/CD pipeline for real-world usage.
git clone https://github.com/thaitri2005/Credit-Scorecard-for-Default-Prediction.git
cd credit-risk-scorecard
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements-dev.txt
uvicorn app.main:app --reload
docker build -t credit-risk-api .
docker run -p 8000:80 credit-risk-apior with Compose:
docker-compose up --buildor pull prebuilt image (from Docker Hub CI/CD)
docker pull thaitri2005/credit_scorecard_for_default_prediction:latest
docker run -p 8000:80 thaitri2005/credit_scorecard_for_default_prediction:latestGET /- Web UIGET /api/v1/health- Health checkPOST /api/v1/predict- Single predictionPOST /api/v1/predict/batch- Batch predictionsGET /api/v1/model/info- Model informationGET /api/v1/model/features/importance- Feature importanceGET /docs- Interactive API documentation
curl -X POST "http://localhost:8000/api/v1/predict" \
-H "Content-Type: application/json" \
-d '{
"annual_inc": 75000,
"int_rate": 12.5,
"credit_history_length": 5.5,
"purpose": "debt_consolidation",
"verification_status": "verified"
}'{
"credit_score": 650.25,
"default_probability": 0.1234,
"risk_level": "A",
"log_odds": -1.9876,
"message": "Prediction completed successfully"
}Note: The
risk_levelfield uses industry-standard credit ratings (AAA, AA, A, BBB, BB, B, CCC, CC, C, D) based on the calculated credit score, following S&P/Moody's/Fitch rating conventions.
This application follows industry-standard credit scoring practices:
The credit score is calculated using the standard scorecard transformation formula:
Score = Offset - Factor Γ log(odds)
Where:
- Factor = PDO / ln(2) (Points to Double Odds, typically 20)
- Offset = BaseScore - Factor Γ ln(BaseOdds)
- log(odds) = ln(probability / (1 - probability))
Default Parameters:
- PDO (Points to Double Odds): 20
- Base Score: 600
- Base Odds: 50:1 (Good:Bad ratio)
Risk levels are assigned using industry-standard credit ratings based on the calculated credit score:
| Credit Score | Rating | Risk Level |
|---|---|---|
| β₯ 750 | AAA | Highest quality, minimal default risk |
| 700-749 | AA | Very high quality, very low default risk |
| 650-699 | A | High quality, low default risk |
| 600-649 | BBB | Good quality, moderate default risk |
| 550-599 | BB | Speculative, elevated default risk |
| 500-549 | B | Highly speculative, material default risk |
| 450-499 | CCC | Substantial credit risk |
| 400-449 | CC | Very high credit risk |
| 350-399 | C | Near default |
| < 350 | D | Default |
This rating system aligns with S&P, Moody's, and Fitch credit rating scales, providing familiar and interpretable risk assessments for financial professionals.
The prediction pipeline follows this sequence:
- Model Prediction β Default probability (0-1)
- Score Transformation β Credit score (300-900 range)
- Rating Assignment β Standard credit rating (AAA-D)
The CI/CD pipeline performs the following steps:
- Lint & Type Check
- Run Unit Tests
- Security Scan (Trivy)
- Build Docker Image
- Push to Azure Container Registry
- Deploy to Azure Web App (App Service)
For more details, see the GitHub Actions workflow.
annual_inc: Annual incomeint_rate: Interest ratepurpose: Loan purposeverification_status: Income verification statustotal_rev_hi_lim: Total revolving high limittot_cur_bal: Total current balanceloan_burden: Loan burden ratiorevol_util: Revolving utilizationcredit_history_length: Credit history length
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue on GitHub
- Check the API documentation at
/docs - Review the test cases for usage examples