Spaces:
Sleeping
Sleeping
File size: 2,089 Bytes
dc70a9b e9a1e7c d2245d6 dc70a9b d2245d6 dc70a9b d2245d6 dc70a9b d2245d6 dc70a9b d2245d6 dc70a9b d2245d6 e9a1e7c dc70a9b d2245d6 dc70a9b d2245d6 dc70a9b 3415aca |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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()
|