Overview
This endpoint predicts whether a customer’s credit risk is “good” or “bad” based on their demographic and financial information. The prediction is made using a PyTorch deep learning model that has been trained on historical credit data.
Endpoint
POST /credit_score_prediction
Request Schema
The request body must be a JSON object with the following fields:
Required Fields
Age of the applicant in years. Must be greater than 0. Example : 35
Gender of the applicant. Allowed values :Example : "male"
Skill level of the applicant’s job. Allowed values :
"unskilled and non-resident"
"unskilled and resident"
"skilled"
"highly skilled"
Example : "skilled"
Type of housing for the applicant. Allowed values :
"own" - Owns housing
"rent" - Rents housing
"free" - Free housing
Example : "own"
Status of the savings account. Note the space in the field name. Allowed values :
"NA" - No savings account
"little" - Little savings
"moderate" - Moderate savings
"quite rich" - Quite rich savings
"rich" - Rich savings
Example : "NA"
Status of the checking account. Note the space in the field name. Allowed values :
"NA" - No checking account
"little" - Little balance
"moderate" - Moderate balance
"rich" - Rich balance
Example : "little"
Amount of credit requested. Must be greater than 0. Example : 9055.0
Duration of the credit in months. Must be greater than 0. Example : 36
Purpose of the credit. Allowed values :
"car"
"furniture/equipment"
"radio/TV"
"domestic appliances"
"repairs"
"education"
"business"
"vacation/others"
Example : "education"
Response Schema
The response is a JSON object with the following fields:
The predicted credit risk category. Possible values :
"good" - Good credit risk
"bad" - Bad credit risk
The probability that the credit risk is “good”. Value between 0 and 1. Range : 0.0 to 1.0Note : Higher values indicate higher confidence in a “good” prediction.
Example Request
Request Body
Python
JavaScript/TypeScript
cURL
{
"Age" : 35 ,
"Sex" : "male" ,
"Job" : "unskilled and resident" ,
"Housing" : "free" ,
"Saving accounts" : "NA" ,
"Checking account" : "NA" ,
"Credit amount" : 9055 ,
"Duration" : 36 ,
"Purpose" : "education"
}
Example Response
200 Success
422 Validation Error
500 Internal Server Error
{
"prediction" : "good" ,
"probability" : 0.8542
}
Response Status Codes
Prediction completed successfully
Invalid input data - validation failed
Error during model inference
Inference Pipeline
The prediction process follows these steps:
Input Validation
The request data is validated against the CreditRiskInput Pydantic schema to ensure all fields are present and have correct types and values.
Data Preprocessing
The input is transformed into a pandas DataFrame and processed using a scikit-learn preprocessor that handles:
Categorical encoding
Feature scaling
Feature engineering
Model Inference
The preprocessed data is converted to a PyTorch tensor and passed through the deep learning model:
The model outputs probabilities for both “bad” and “good” classes
The prediction is determined by the class with the highest probability
Response Generation
The prediction result and probability are packaged into a CreditRiskOutput response object and returned as JSON.
Implementation Details
Show Source Code Reference
The endpoint is implemented in server/api.py:58-84:
Handler : predict_credit_risk()
Input Schema : CreditRiskInput (defined in server/schemas.py)
Output Schema : CreditRiskOutput (defined in server/schemas.py)
Predictor : Singleton instance from inference/inference.py
The prediction model uses:
Framework : PyTorch
Architecture : Configurable multi-layer neural network
Activation Functions : Configurable (ReLU, Sigmoid, etc.)
Dropout : Applied for regularization
Output : Binary classification with probability scores
Important Notes
Field Names with Spaces : The fields "Saving accounts" and "Checking account" contain spaces. Ensure you include the space when making requests, or the validation will fail.
Singleton Pattern : The model is loaded once at startup using a singleton pattern. All prediction requests share the same model instance for optimal performance.
CPU Inference : The model runs on CPU by default. For high-throughput production deployments, consider configuring GPU support.
API Overview Learn about the API architecture
Interactive Docs Try the API in Swagger UI