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()