SanthiSastra commited on
Commit
711b517
·
verified ·
1 Parent(s): a84c3ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import pandas as pd
3
+ import gradio as gr
4
+
5
+
6
+ def emi_calc(P, annual_rate, years):
7
+ # Validate inputs
8
+ if P is None or P <= 0:
9
+ return "Enter valid principal.", pd.DataFrame()
10
+
11
+ if annual_rate is None or annual_rate < 0:
12
+ return "Enter valid rate.", pd.DataFrame()
13
+
14
+ if years is None or years <= 0:
15
+ return "Enter valid tenure.", pd.DataFrame()
16
+
17
+ # Convert
18
+ P = float(P)
19
+ r = (float(annual_rate) / 100.0) / 12.0
20
+ n = int(round(float(years) * 12))
21
+
22
+ if n <= 0:
23
+ return "Tenure is too small.", pd.DataFrame()
24
+
25
+ # EMI formula
26
+ if r == 0:
27
+ emi = P / n
28
+ else:
29
+ emi = P * r * ((1 + r) ** n) / (((1 + r) ** n) - 1)
30
+
31
+ total = emi * n
32
+ interest_total = total - P
33
+
34
+ md = (
35
+ f"### Results\n"
36
+ f"- Monthly EMI: ₹{emi:,.2f}\n"
37
+ f"- Total Interest: ₹{interest_total:,.2f}\n"
38
+ f"- Total Payable: ₹{total:,.2f}\n"
39
+ f"- Tenure: {n} months"
40
+ )
41
+
42
+ # Amortization schedule
43
+ bal = P
44
+ rows = []
45
+ for m in range(1, n + 1):
46
+ interest = bal * r
47
+ principal = emi - interest
48
+
49
+ # Adjust last month to avoid tiny rounding balance
50
+ if m == n:
51
+ principal = bal
52
+ emi_last = principal + interest
53
+ bal = 0.0
54
+ rows.append([m, round(emi_last, 2), round(principal, 2), round(interest, 2), round(bal, 2)])
55
+ break
56
+
57
+ bal -= principal
58
+ rows.append([m, round(emi, 2), round(principal, 2), round(interest, 2), round(bal, 2)])
59
+
60
+ df = pd.DataFrame(rows, columns=["Month", "EMI", "Principal Paid", "Interest Paid", "Balance"])
61
+ return md, df
62
+
63
+
64
+ with gr.Blocks(title="Loan EMI Calculator") as demo:
65
+ gr.Markdown("## Simple Loan EMI Calculator")
66
+
67
+ with gr.Row():
68
+ P = gr.Number(label="Loan Amount (₹)", value=500000, precision=0)
69
+ rate = gr.Number(label="Annual Interest Rate (%)", value=9.0, precision=2)
70
+ years = gr.Number(label="Tenure (Years)", value=5, precision=2)
71
+
72
+ btn = gr.Button("Calculate")
73
+ out_md = gr.Markdown()
74
+ out_df = gr.Dataframe(interactive=False)
75
+
76
+ btn.click(emi_calc, inputs=[P, rate, years], outputs=[out_md, out_df])
77
+
78
+ demo.launch()