Overview

The backend of our Smart Credit Risk System is built using Flask, a lightweight Python web framework. The main file is app.py, which handles all the server logic, model loading, and prediction making.

Think of the backend as the "brain" of our system. When a user submits the loan application form, the backend receives the data, processes it, runs it through our trained Machine Learning model, and returns the prediction result.

1. Imports

At the top of app.py, we import the necessary libraries:

from flask import Flask, render_template, request import joblib import pandas as pd

What Each Import Does:

2. Model Loading

After importing libraries, we load our pre-trained model:

# Load the trained model (our 'brain') model = joblib.load('credit_risk_model.pkl')

💡 How joblib.load Works

joblib.load() retrieves the trained Random Forest model from a file called credit_risk_model.pkl. This file contains all the "knowledge" our model learned during training - all the patterns it discovered about which loan applicants are safe vs risky.

Why do we load it once? Loading the model takes time and memory. By loading it once when the server starts, we can reuse it for every prediction request without reloading it each time. This makes our system much faster!

3. Feature Engineering

Before making a prediction, we need to calculate a special feature called interest_burden. This feature helps the model understand how much of a person's income goes toward paying loan interest.

# Calculate interest burden interest_burden = (loan_amnt * (loan_int_rate / 100)) / person_income
Feature Engineering Code - interest_burden calculation

Above: Code editor screenshot showing the interest_burden feature engineering logic.

Breaking Down the Formula:

Example: If someone wants a $10,000 loan at 5% interest and earns $50,000 per year:

A lower interest burden means the person can more easily afford the loan payments, which is a good sign!

4. Routes

Routes are like different "pages" or "endpoints" in our web application. Each route handles a specific type of request from the user's browser.

Route 1: Home Page (@app.route('/'))

@app.route('/') def home(): return render_template('index.html')

This route shows the main form page. When someone visits our website's homepage, Flask displays the index.html file, which contains the loan application form.

Route 2: Prediction Endpoint (@app.route('/predict'))

@app.route('/predict', methods=['POST']) def predict(): # Get data from the form age = request.form['age'] income = request.form['person_income'] loan_amnt = request.form['loan_amnt'] # ... more fields ... # Prepare data for the model features = pd.DataFrame({...}) # Make prediction prediction = model.predict(features)[0] probability = model.predict_proba(features)[0] # Return result return render_template('index.html', prediction_text=result)

How the Prediction Route Works:

  1. Receives Data: When the user clicks "Submit" on the form, the browser sends a POST request to /predict with all the form data.
  2. Extracts Information: request.form gets each field from the form (age, income, loan amount, etc.)
  3. Prepares Data: We organize the data into a pandas DataFrame (like a table) that matches the format the model expects.
  4. Makes Prediction: model.predict() gives us the answer (SAFE or RISKY), and model.predict_proba() gives us the confidence percentage.
  5. Shows Result: We send the result back to the user's browser, which displays it on the page using {{ prediction_text }} in the HTML template.

Summary

The backend (app.py) is the engine that powers our Smart Credit Risk System:

All of this happens in seconds, making it feel instant to the user!