import streamlit as st
import pandas as pd
import numpy as np
from inference import predict_emi # Ensure inference.py is in the same folder
# -------------------------------
# PAGE CONFIG
# -------------------------------
st.set_page_config(
page_title="EMI Eligibility Pro",
page_icon="💰",
layout="wide"
)
# Custom CSS for better styling
st.markdown("""
""", unsafe_allow_html=True)
st.title("🚀 EMI Eligibility & Risk Prediction")
st.write("Fill in the details below to check your loan eligibility and maximum safe EMI.")
# -------------------------------
# INPUT FORM
# -------------------------------
with st.container():
# SECTION 1: Personal & Demographic
st.subheader("👤 Personal Information")
col1, col2, col3 = st.columns(3)
with col1:
age = st.number_input("Age", 18, 70, 30)
gender = st.selectbox("Gender", ["Male", "Female"])
marital_status = st.selectbox("Marital Status", ["Single", "Married"])
with col2:
education = st.selectbox("Education", ["High School", "Graduate", "Post Graduate", "Professional"])
family_size = st.number_input("Family Size", 1, 10, 3)
dependents = st.number_input("Dependents", 0, 10, 1)
with col3:
house_type = st.selectbox("House Type", ["Rented", "Own", "Family"])
company_type = st.selectbox("Company Type", ["Startup", "SME", "MNC", "Government"])
st.divider()
# SECTION 2: Employment & Income
st.subheader("💼 Employment & Financials")
col4, col5, col6 = st.columns(3)
with col4:
employment_type = st.selectbox("Employment Type", ["Private", "Government", "Self-employed"])
years_of_employment = st.number_input("Years of Employment", 0, 40, 5)
with col5:
monthly_salary = st.number_input("Monthly Salary (INR)", 10000, 500000, 50000, step=5000)
credit_score = st.number_input("Credit Score", 300, 900, 700)
with col6:
bank_balance = st.number_input("Bank Balance (INR)", 0, 10000000, 200000)
existing_loans = st.selectbox("Existing Loans", ["No", "Yes"])
st.divider()
# SECTION 3: Expenses & Current Debt
st.subheader("📉 Monthly Outgoings")
col7, col8, col9 = st.columns(3)
with col7:
monthly_rent = st.number_input("Monthly Rent (INR)", 0, 100000, 10000)
current_emi_amount = st.number_input("Current EMI Totals", 0, 100000, 0)
with col8:
groceries_utilities = st.number_input("Groceries & Utilities", 0, 50000, 8000)
travel_expenses = st.number_input("Travel Expenses", 0, 50000, 3000)
with col9:
school_college_fees = st.number_input("Education Fees (Total)", 0, 150000, 0)
other_monthly_expenses = st.number_input("Other Expenses", 0, 50000, 5000)
st.divider()
# SECTION 4: Loan Details
st.subheader("📝 Loan Application Details")
col10, col11, col12 = st.columns(3)
with col10:
emi_scenario = st.selectbox("EMI Type", ["Personal Loan EMI", "Vehicle EMI", "Home Appliances EMI", "Education EMI", "E-commerce Shopping EMI"])
with col11:
requested_amount = st.number_input("Requested Loan Amount (INR)", 10000, 20000000, 300000)
with col12:
requested_tenure = st.number_input("Requested Tenure (Months)", 3, 84, 24)
# -------------------------------
# PREDICTION ENGINE
# -------------------------------
st.markdown("
", unsafe_allow_html=True)
if st.button("Analyze Eligibility", use_container_width=True, type="primary"):
# Bundle input for Inference
raw_input = {
"age": age,
"gender": gender,
"marital_status": marital_status,
"education": education,
"monthly_salary": monthly_salary,
"employment_type": employment_type,
"years_of_employment": years_of_employment,
"company_type": company_type,
"house_type": house_type,
"monthly_rent": monthly_rent,
"family_size": family_size,
"dependents": dependents,
"school_fees": school_college_fees * 0.4, # Heuristic split if your model expects separate
"college_fees": school_college_fees * 0.6,
"travel_expenses": travel_expenses,
"groceries_utilities": groceries_utilities,
"other_monthly_expenses": other_monthly_expenses,
"existing_loans": existing_loans,
"current_emi_amount": current_emi_amount,
"credit_score": credit_score,
"bank_balance": bank_balance,
"emergency_fund": bank_balance * 0.2, # Assumption if not provided
"emi_scenario": emi_scenario,
"requested_amount": requested_amount,
"requested_tenure": requested_tenure
}
with st.spinner("Consulting the AI Risk Model..."):
eligibility, max_emi = predict_emi(raw_input)
st.markdown("---")
# DISPLAY RESULTS
res_col1, res_col2 = st.columns([1, 2])
with res_col1:
if eligibility == "Eligible":
st.success(f"### Result: {eligibility} ✅")
st.metric("Safe EMI Limit", f"₹ {max_emi:,.2f}")
elif eligibility == "High Risk":
st.warning(f"### Result: {eligibility} ⚠️")
st.metric("Risk-Adjusted EMI", f"₹ {max_emi:,.2f}")
else:
st.error(f"### Result: {eligibility} ❌")
st.metric("Approved EMI", "₹ 0.00")
with res_col2:
st.write("#### AI Analysis Summary")
if eligibility == "Eligible":
st.write(f"Based on your credit score of **{credit_score}** and disposable income, you are highly likely to be approved. Your monthly surplus supports an EMI of up to **₹{max_emi:,.2f}**.")
elif eligibility == "High Risk":
st.write("You are borderline eligible. We recommend either increasing your loan tenure (to lower the monthly burden) or closing small existing debts to improve your score.")
else:
st.write("Unfortunately, based on current debt-to-income ratios and credit history, we cannot approve this loan. Try again after improving your credit score or increasing your monthly bank balance.")
# Show "What-If" Analysis
if eligibility != "Eligible":
st.info("💡 **Pro-Tip:** Lowering your 'Requested Amount' or increasing your 'Tenure' usually improves eligibility results.")