alidenewade's picture
Update app.py
d2245d6 verified
import gradio as gr
import numpy as np
import numpy_financial as npf
import pandas as pd
import matplotlib.pyplot as plt
def amortization_report(principal, rate_percent, periods):
"""
Generate and return an amortization schedule with plots.
"""
rate = rate_percent / 100 # Convert from percent to decimal
payment = -npf.pmt(rate, periods, principal)
interest = -npf.ipmt(rate, range(1, periods + 1), periods, principal)
principal_paid = -npf.ppmt(rate, range(1, periods + 1), periods, principal)
balance = principal - principal_paid.cumsum()
df = pd.DataFrame({
'Year': range(1, periods + 1),
'Payment': [round(payment, 2)] * periods,
'Interest': interest.round(2),
'Principal': principal_paid.round(2),
'Balance': balance.round(2)
})
# Plot
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].plot(df['Year'], df['Interest'], label='Interest', color='red', marker='o')
axs[0].plot(df['Year'], df['Principal'], label='Principal', color='green', marker='o')
axs[0].set_title('Payment Breakdown')
axs[0].set_xlabel('Year')
axs[0].set_ylabel('Amount')
axs[0].grid(True)
axs[0].legend()
axs[1].plot(df['Year'], df['Balance'], label='Remaining Balance', color='blue', marker='o')
axs[1].set_title('Remaining Balance Over Time')
axs[1].set_xlabel('Year')
axs[1].set_ylabel('Balance')
axs[1].grid(True)
axs[1].legend()
plt.tight_layout()
return df, fig
demo = gr.Interface(
fn=amortization_report,
inputs=[
gr.Number(label="Loan Amount (Principal)", value=100000),
gr.Number(label="Annual Interest Rate (%)", value=5.00, precision=2),
gr.Number(label="Number of Periods (Years)", value=10)
],
outputs=[
gr.Dataframe(label="Amortization Schedule"),
gr.Plot(label="Payment Charts")
],
title="πŸ“Š Amortization Report",
description="Enter your loan details to view the amortization schedule and payment breakdown over time."
)
if __name__ == "__main__":
demo.launch()