Spaces:
Sleeping
Sleeping
| import math | |
| import pandas as pd | |
| import gradio as gr | |
| def emi_calc(P, annual_rate, years): | |
| # Validate inputs | |
| if P is None or P <= 0: | |
| return "Enter valid principal.", pd.DataFrame() | |
| if annual_rate is None or annual_rate < 0: | |
| return "Enter valid rate.", pd.DataFrame() | |
| if years is None or years <= 0: | |
| return "Enter valid tenure.", pd.DataFrame() | |
| # Convert | |
| P = float(P) | |
| r = (float(annual_rate) / 100.0) / 12.0 | |
| n = int(round(float(years) * 12)) | |
| if n <= 0: | |
| return "Tenure is too small.", pd.DataFrame() | |
| # EMI formula | |
| if r == 0: | |
| emi = P / n | |
| else: | |
| emi = P * r * ((1 + r) ** n) / (((1 + r) ** n) - 1) | |
| total = emi * n | |
| interest_total = total - P | |
| md = ( | |
| f"### Results\n" | |
| f"- Monthly EMI: ₹{emi:,.2f}\n" | |
| f"- Total Interest: ₹{interest_total:,.2f}\n" | |
| f"- Total Payable: ₹{total:,.2f}\n" | |
| f"- Tenure: {n} months" | |
| ) | |
| # Amortization schedule | |
| bal = P | |
| rows = [] | |
| for m in range(1, n + 1): | |
| interest = bal * r | |
| principal = emi - interest | |
| # Adjust last month to avoid tiny rounding balance | |
| if m == n: | |
| principal = bal | |
| emi_last = principal + interest | |
| bal = 0.0 | |
| rows.append([m, round(emi_last, 2), round(principal, 2), round(interest, 2), round(bal, 2)]) | |
| break | |
| bal -= principal | |
| rows.append([m, round(emi, 2), round(principal, 2), round(interest, 2), round(bal, 2)]) | |
| df = pd.DataFrame(rows, columns=["Month", "EMI", "Principal Paid", "Interest Paid", "Balance"]) | |
| return md, df | |
| with gr.Blocks(title="Loan EMI Calculator") as demo: | |
| gr.Markdown("## Simple Loan EMI Calculator") | |
| with gr.Row(): | |
| P = gr.Number(label="Loan Amount (₹)", value=500000, precision=0) | |
| rate = gr.Number(label="Annual Interest Rate (%)", value=9.0, precision=2) | |
| years = gr.Number(label="Tenure (Years)", value=5, precision=2) | |
| btn = gr.Button("Calculate") | |
| out_md = gr.Markdown() | |
| out_df = gr.Dataframe(interactive=False) | |
| btn.click(emi_calc, inputs=[P, rate, years], outputs=[out_md, out_df]) | |
| demo.launch() |