import streamlit as st import pandas as pd import numpy as np from src.data_loader import load_data from src.preprocessing import feature_engineering, encode_data from src.model import train_model from src.predict import make_prediction from src.utils import calculate_emi # ----------------------------------------------------------------------------- # PAGE CONFIG # ----------------------------------------------------------------------------- st.set_page_config(page_title="Loan Prediction System", page_icon="🏦", layout="centered") # ----------------------------------------------------------------------------- # NAVIGATION STATE # ----------------------------------------------------------------------------- if 'page' not in st.session_state: st.session_state['page'] = 'Home' c1, c2, c3, c4, c5 = st.columns([2,1,1,1,2]) with c2: if st.button("Home"): st.session_state['page'] = 'Home' with c3: if st.button("Predict"): st.session_state['page'] = 'Predict' with c4: if st.button("About"): st.session_state['page'] = 'About' st.markdown("---") # ----------------------------------------------------------------------------- # LOAD + TRAIN # ----------------------------------------------------------------------------- DATA_PATH = "data/train.csv" df = load_data(DATA_PATH) if df is not None: df = feature_engineering(df) df, encoders, target_encoder = encode_data(df) X = df.drop("Loan_Status", axis=1) y = df["Loan_Status"] feature_columns = X.columns.tolist() model = train_model(X, y) else: st.error("Dataset not found") # ----------------------------------------------------------------------------- # HOME PAGE # ----------------------------------------------------------------------------- if st.session_state['page'] == "Home": st.title("Loan Approval Prediction System") st.subheader("Using Machine Learning") st.markdown("### About This Application") st.info(""" Welcome to the next generation of banking technology. This application utilizes advanced **Machine Learning** algorithms to automate the loan eligibility assessment process. By analyzing key financial indicators—such as **Income, Credit History, and Loan Term**—our system provides an instant, objective, and data-driven prediction. """) st.markdown("### Why Use This System?") col1, col2 = st.columns(2) with col1: st.success("⚡ **Real-Time Analysis**\n\nGet instant results without manual verification.") st.success("🛡️ **Financial Guardrails**\n\nDetects risky applications automatically.") with col2: st.success("🎯 **High Accuracy**\n\nUses Random Forest model for reliable prediction.") st.success("💡 **Smart Suggestions**\n\nGives tips to improve approval chances.") st.markdown("---") st.markdown(""" ### How It Works 1. Click Predict 2. Fill details 3. Click Predict Status 4. Get result instantly """) # ----------------------------------------------------------------------------- # PREDICT PAGE # ----------------------------------------------------------------------------- elif st.session_state['page'] == "Predict": st.title("📋 Loan Application Form") if df is None: st.error("Dataset not found") else: with st.form("prediction_form"): st.subheader("Applicant Details") c1, c2 = st.columns(2) with c1: gender = st.selectbox("Gender", ["Male", "Female"]) married = st.selectbox("Married", ["No", "Yes"]) dependents = st.selectbox("Dependents", ["0", "1", "2", "3+"]) education = st.selectbox("Education", ["Graduate", "Not Graduate"]) self_employed = st.selectbox("Self Employed", ["No", "Yes"]) with c2: applicant_income = st.number_input("Applicant Income (Monthly ₹)", value=5000) coapplicant_income = st.number_input("Co-Applicant Income (Monthly ₹)", value=0) loan_amount = st.number_input("Loan Amount (₹)", value=100000) loan_term_years = st.number_input("Loan Term (Years)", value=15) property_area = st.selectbox("Property Area", ["Urban", "Semiurban", "Rural"]) cibil_score = st.number_input("CIBIL Score", 300, 900, 750) submit = st.form_submit_button("Predict Status") if submit: loan_amt_k = loan_amount / 1000 loan_term_m = loan_term_years * 12 total_income = applicant_income + coapplicant_income model_emi = (loan_amt_k * 1000) / loan_term_m balance_income = total_income - model_emi credit_history = 1.0 if cibil_score >= 600 else 0.0 input_data = { "Gender": gender, "Married": married, "Dependents": dependents, "Education": education, "Self_Employed": self_employed, "ApplicantIncome": applicant_income, "CoapplicantIncome": coapplicant_income, "LoanAmount": loan_amt_k, "Loan_Amount_Term": loan_term_m, "Credit_History": credit_history, "Property_Area": property_area, "Total_Income": total_income, "EMI": model_emi, "Balance_Income": balance_income } result, confidence = make_prediction( input_data, model, encoders, target_encoder, feature_columns ) st.markdown("### Result") if result == "Y": st.success(f"✅ Approved ({confidence:.2f}%)") else: st.error(f"❌ Rejected ({confidence:.2f}%)") # ----------------------------------------------------------------------------- # ABOUT PAGE # ----------------------------------------------------------------------------- elif st.session_state['page'] == "About": st.title("About the Project") # 1. PROBLEM st.error(""" **The Problem: Manual Underwriting** Traditionally, banks relied on manual verification processes which had major disadvantages: - **High Turnaround Time:** It took days or weeks to process a single application. - **Human Bias:** Decisions often varied from officer to officer. - **Static Rules:** Simple rules failed to see the bigger picture. """) st.write("") # 2. SOLUTION st.success(""" **The Solution: Intelligent Automation** This project replaces the manual process with a **Hybrid Machine Learning Architecture**. It combines strict financial logic (Guardrails) with AI pattern recognition (Random Forest) to make safer, faster decisions. """) st.write("") # 3. WORKFLOW st.markdown("### 🔄 Project Workflow") st.info(""" This system was built in **4 key stages**: 1. **Data Analysis (Jupyter Notebook):** Data cleaning and preprocessing 2. **Model Training:** Random Forest model (~81% accuracy) 3. **Backend Logic:** Financial guardrails implementation 4. **Frontend:** Streamlit UI for user interaction """) st.divider() # 4. TECH SPECS st.subheader("🛠️ Technical Architecture") c1, c2 = st.columns(2) with c1: st.markdown("**Machine Learning:**") st.caption(""" - Algorithm: Random Forest Classifier - Trees: 200 Estimators - Accuracy: ~81% """) with c2: st.markdown("**Tech Stack:**") st.caption(""" - Python - Streamlit - Pandas - Scikit-learn """)