Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import numpy_financial as npf
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
def amortization_report(principal, rate, periods):
|
| 8 |
+
payment = -npf.pmt(rate, periods, principal)
|
| 9 |
+
interest = -npf.ipmt(rate, np.arange(1, periods + 1), periods, principal)
|
| 10 |
+
principal_paid = -npf.ppmt(rate, np.arange(1, periods + 1), periods, principal)
|
| 11 |
+
balance = principal - principal_paid.cumsum()
|
| 12 |
+
|
| 13 |
+
df = pd.DataFrame({
|
| 14 |
+
'Year': np.arange(1, periods + 1),
|
| 15 |
+
'Payment': np.full(periods, round(payment, 2)),
|
| 16 |
+
'Interest': interest.round(2),
|
| 17 |
+
'Principal': principal_paid.round(2),
|
| 18 |
+
'Balance': balance.round(2)
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
# Plot
|
| 22 |
+
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
|
| 23 |
+
|
| 24 |
+
axs[0].plot(df['Year'], df['Interest'], label='Interest', color='red', marker='o')
|
| 25 |
+
axs[0].plot(df['Year'], df['Principal'], label='Principal', color='green', marker='o')
|
| 26 |
+
axs[0].set_title('Payment Breakdown')
|
| 27 |
+
axs[0].set_xlabel('Year')
|
| 28 |
+
axs[0].set_ylabel('Amount')
|
| 29 |
+
axs[0].grid(True)
|
| 30 |
+
axs[0].legend()
|
| 31 |
+
|
| 32 |
+
axs[1].plot(df['Year'], df['Balance'], label='Remaining Balance', color='blue', marker='o')
|
| 33 |
+
axs[1].set_title('Remaining Balance Over Time')
|
| 34 |
+
axs[1].set_xlabel('Year')
|
| 35 |
+
axs[1].set_ylabel('Balance')
|
| 36 |
+
axs[1].grid(True)
|
| 37 |
+
axs[1].legend()
|
| 38 |
+
|
| 39 |
+
plt.tight_layout()
|
| 40 |
+
|
| 41 |
+
return df, fig
|
| 42 |
+
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=amortization_report,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Number(label="Principal", value=100000),
|
| 47 |
+
gr.Number(label="Annual Interest Rate (e.g., 0.05 for 5%)", value=0.05),
|
| 48 |
+
gr.Number(label="Number of Periods (Years)", value=10)
|
| 49 |
+
],
|
| 50 |
+
outputs=[
|
| 51 |
+
gr.Dataframe(label="Amortization Table"),
|
| 52 |
+
gr.Plot(label="Amortization Charts")
|
| 53 |
+
],
|
| 54 |
+
title="Amortization Report",
|
| 55 |
+
description="Enter loan parameters to see amortization schedule and visualization."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|