Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,26 @@ import numpy as np
|
|
| 3 |
import numpy_financial as npf
|
| 4 |
import pandas as pd
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
from matplotlib.backends.backend_pdf import PdfPages
|
| 7 |
-
import tempfile
|
| 8 |
-
import os
|
| 9 |
|
| 10 |
def amortization_report(principal, rate_percent, periods):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
payment = -npf.pmt(rate, periods, principal)
|
| 15 |
-
interest = -npf.ipmt(rate,
|
| 16 |
-
principal_paid = -npf.ppmt(rate,
|
| 17 |
balance = principal - principal_paid.cumsum()
|
| 18 |
|
| 19 |
df = pd.DataFrame({
|
| 20 |
-
'Year':
|
| 21 |
-
'Payment':
|
| 22 |
'Interest': interest.round(2),
|
| 23 |
'Principal': principal_paid.round(2),
|
| 24 |
'Balance': balance.round(2)
|
| 25 |
})
|
| 26 |
|
| 27 |
-
#
|
| 28 |
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
|
| 29 |
|
| 30 |
axs[0].plot(df['Year'], df['Interest'], label='Interest', color='red', marker='o')
|
|
@@ -43,41 +41,21 @@ def amortization_report(principal, rate_percent, periods):
|
|
| 43 |
axs[1].legend()
|
| 44 |
|
| 45 |
plt.tight_layout()
|
|
|
|
| 46 |
|
| 47 |
-
# Save outputs to temporary files
|
| 48 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 49 |
-
# Excel
|
| 50 |
-
excel_path = os.path.join(tmpdir, "amortization_schedule.xlsx")
|
| 51 |
-
df.to_excel(excel_path, index=False)
|
| 52 |
-
|
| 53 |
-
# PNG
|
| 54 |
-
png_path = os.path.join(tmpdir, "amortization_chart.png")
|
| 55 |
-
fig.savefig(png_path)
|
| 56 |
-
|
| 57 |
-
# PDF
|
| 58 |
-
pdf_path = os.path.join(tmpdir, "amortization_report.pdf")
|
| 59 |
-
with PdfPages(pdf_path) as pdf:
|
| 60 |
-
pdf.savefig(fig)
|
| 61 |
-
|
| 62 |
-
return df, fig, excel_path, png_path, pdf_path
|
| 63 |
-
|
| 64 |
-
# Gradio Interface
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=amortization_report,
|
| 67 |
inputs=[
|
| 68 |
-
gr.Number(label="Principal", value=100000),
|
| 69 |
gr.Number(label="Annual Interest Rate (%)", value=5.00, precision=2),
|
| 70 |
gr.Number(label="Number of Periods (Years)", value=10)
|
| 71 |
],
|
| 72 |
outputs=[
|
| 73 |
-
gr.Dataframe(label="Amortization
|
| 74 |
-
gr.Plot(label="
|
| 75 |
-
gr.File(label="Download Excel (.xlsx)"),
|
| 76 |
-
gr.File(label="Download Chart (.png)"),
|
| 77 |
-
gr.File(label="Download Full Report (.pdf)")
|
| 78 |
],
|
| 79 |
-
title="Amortization Report",
|
| 80 |
-
description="Enter loan details to
|
| 81 |
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy_financial as npf
|
| 4 |
import pandas as pd
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def amortization_report(principal, rate_percent, periods):
|
| 8 |
+
"""
|
| 9 |
+
Generate and return an amortization schedule with plots.
|
| 10 |
+
"""
|
| 11 |
+
rate = rate_percent / 100 # Convert from percent to decimal
|
| 12 |
payment = -npf.pmt(rate, periods, principal)
|
| 13 |
+
interest = -npf.ipmt(rate, range(1, periods + 1), periods, principal)
|
| 14 |
+
principal_paid = -npf.ppmt(rate, range(1, periods + 1), periods, principal)
|
| 15 |
balance = principal - principal_paid.cumsum()
|
| 16 |
|
| 17 |
df = pd.DataFrame({
|
| 18 |
+
'Year': range(1, periods + 1),
|
| 19 |
+
'Payment': [round(payment, 2)] * periods,
|
| 20 |
'Interest': interest.round(2),
|
| 21 |
'Principal': principal_paid.round(2),
|
| 22 |
'Balance': balance.round(2)
|
| 23 |
})
|
| 24 |
|
| 25 |
+
# Plot
|
| 26 |
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
|
| 27 |
|
| 28 |
axs[0].plot(df['Year'], df['Interest'], label='Interest', color='red', marker='o')
|
|
|
|
| 41 |
axs[1].legend()
|
| 42 |
|
| 43 |
plt.tight_layout()
|
| 44 |
+
return df, fig
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=amortization_report,
|
| 48 |
inputs=[
|
| 49 |
+
gr.Number(label="Loan Amount (Principal)", value=100000),
|
| 50 |
gr.Number(label="Annual Interest Rate (%)", value=5.00, precision=2),
|
| 51 |
gr.Number(label="Number of Periods (Years)", value=10)
|
| 52 |
],
|
| 53 |
outputs=[
|
| 54 |
+
gr.Dataframe(label="Amortization Schedule"),
|
| 55 |
+
gr.Plot(label="Payment Charts")
|
|
|
|
|
|
|
|
|
|
| 56 |
],
|
| 57 |
+
title="📊 Amortization Report",
|
| 58 |
+
description="Enter your loan details to view the amortization schedule and payment breakdown over time."
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|