Spaces:
Sleeping

File size: 1,706 Bytes
9edaa5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt

def calculate_emi(principal, years, rate):
    r = rate / (12 * 100)  # Monthly interest rate
    n = years * 12  # Total number of monthly installments
    emi = (principal * r * ((1 + r) ** n)) / (((1 + r) ** n) - 1)
    total = emi * n
    interest = total - principal

    # Generate pie chart
    fig, ax = plt.subplots()
    ax.pie([principal, interest], labels=["Principal", "Interest"], autopct='%1.1f%%', colors=["#80aaff", "#004080"])
    plt.tight_layout()

    return (
        f"₹{emi:,.0f}",
        f"₹{principal:,.0f}",
        f"₹{interest:,.0f}",
        f"₹{total:,.0f}",
        fig
    )

with gr.Blocks() as demo:
    gr.Markdown("## 🏠 Home Loan EMI Calculator")

    with gr.Row():
        loan = gr.Slider(1e5, 10e7, value=2.5e6, label="Loan Amount (₹)", step=1e5)
        tenure = gr.Slider(1, 30, value=30, label="Tenure (Years)")
        rate = gr.Slider(0.5, 15.0, value=8.7, label="Interest Rate (% P.A.)")

    with gr.Row():
        emi = gr.Textbox(label="Monthly EMI")
        principal_out = gr.Textbox(label="Principal Amount")
        interest_out = gr.Textbox(label="Interest Amount")
        total_out = gr.Textbox(label="Total Payable Amount")

    chart = gr.Plot(label="Loan Breakdown")

    loan.change(fn=calculate_emi, inputs=[loan, tenure, rate], outputs=[emi, principal_out, interest_out, total_out, chart])
    tenure.change(fn=calculate_emi, inputs=[loan, tenure, rate], outputs=[emi, principal_out, interest_out, total_out, chart])
    rate.change(fn=calculate_emi, inputs=[loan, tenure, rate], outputs=[emi, principal_out, interest_out, total_out, chart])

demo.launch()