"""Loan eligibility without heavy ML deps (safe for HF Docker).""" def calculate_emi(principal: float, rate_percent: float, tenure_years: int) -> float: if rate_percent <= 0: months = tenure_years * 12 return principal / months if months else principal monthly_rate = rate_percent / 100 / 12 months = tenure_years * 12 if monthly_rate == 0: return principal / months return principal * monthly_rate * ((1 + monthly_rate) ** months) / (((1 + monthly_rate) ** months) - 1) def analyze_loan( salary: float, credit_score: int, existing_loans: int, employment_years: float, age: int, loan_amount: float, ) -> dict: issues: list[str] = [] score = 50.0 if credit_score >= 750: score += 20 elif credit_score >= 700: score += 12 elif credit_score >= 650: score += 5 else: score -= 10 issues.append("Credit score below 650 — consider improving before applying") if employment_years >= 3: score += 10 elif employment_years < 1: score -= 8 issues.append("Less than 1 year employment — higher scrutiny expected") if age < 21 or age > 65: issues.append("Age outside typical lending range (21–65)") if existing_loans > 3: score -= 12 issues.append("Multiple existing loans may reduce approval odds") emi = calculate_emi(loan_amount, 12, 10) monthly_salary = salary / 12 emi_ratio = (emi / monthly_salary * 100) if monthly_salary > 0 else 100 if emi_ratio > 50: score -= 15 issues.append(f"EMI is {emi_ratio:.0f}% of monthly income — reduce loan amount") elif emi_ratio < 35: score += 8 if loan_amount > salary * 5: score -= 10 issues.append("Loan amount exceeds 5× annual salary") score = max(0, min(100, score)) approval_probability = score if approval_probability >= 70: status = "APPROVED" risk = "Low" elif approval_probability >= 45: status = "UNDER REVIEW" risk = "Medium" else: status = "LIKELY REJECTED" risk = "High" recommendations = [] if approval_probability >= 70: recommendations.append("Strong profile — you are likely to qualify at competitive rates") elif approval_probability < 45: recommendations.append("Consider a smaller loan or improving credit score before applying") if emi_ratio < 35: recommendations.append(f"Healthy EMI ratio ({emi_ratio:.0f}% of monthly income)") elif emi_ratio > 40: recommendations.append("Try a longer tenure or lower amount to reduce monthly EMI") comparison = [] for rate in (9, 10, 11, 12): for tenure in (5, 7, 10): e = calculate_emi(loan_amount, rate, tenure) total = e * 12 * tenure comparison.append({ "rate": f"{rate}%", "tenure": f"{tenure} years", "emi": round(e, 2), "total_amount": round(total, 2), "interest": round(total - loan_amount, 2), }) return { "approval_probability": round(approval_probability, 1), "approval_status": status, "risk_level": risk, "loan_score": round(score, 1), "emi": round(emi, 2), "monthly_emi": round(emi, 2), "issues": issues, "recommendations": recommendations, "comparison": comparison[:12], }