joanhightech's picture
JM: Add computation goals, either initial capital, end capital, term, interest or dynamics
41f4f33
import gradio as gr
COMPUTATION_GOALS = [
"Calculate initial capital",
"Calculate savings rate",
"Calculate dynamics",
"Calculate interest rate",
"Calculate accumulation period",
"Calculate final capital"
]
USAGE_EXAMPLES = [
["Pay-off mortgage quickier", COMPUTATION_GOALS[-1], "EUR", 1000, 100,
"Once a year", 7.00, "Once a year", "Years", 10
],
["Invest for retirement", COMPUTATION_GOALS[1], "USD", 5000, 300,
"Once a month", 9.00, "Four times a year", "Years", 20
]
]
def compound_interest_calculation(goals,
computation_goal,
currency,
initial_placement,
regular_deposits,
regular_deposits_frequency,
interest_rate,
compounded_frequency,
year_or_month_horizon,
investment_horizon):
if compounded_frequency == "Once a year":
compounded_frequency = 1
elif compounded_frequency == "Twice a year":
compounded_frequency = 2
elif compounded_frequency == "Four times a year":
compounded_frequency = 4
elif compounded_frequency == "Once a year":
compounded_frequency = 12
final_amount = (
initial_placement
* (1
+ (interest_rate/100)
/ compounded_frequency)**
(compounded_frequency*investment_horizon)
)
interest_generated = final_amount - initial_placement
return f'{final_amount:.2f}', f'{interest_generated:.2f}'
def build_ui():
"""Create the UI for the App"""
goals_ui = gr.Dropdown(
[
"Save to buy home or flat",
"Save for a 6 months emergency fund",
"Pay-off consumer debt",
"Invest for retirement",
"Pay-off mortgage quickier",
"Save for children education"],
value=["Save to buy home or flat", "Save for children education"], multiselect=True, label="Financial goals",
info="Determine the big WHYs you want to invest your money."
)
computation_goal_ui = gr.Radio(
COMPUTATION_GOALS,
value=COMPUTATION_GOALS[-1],
label="Computation goal",
info="Select the scenario you want to simulate"
)
currency_ui = gr.Radio(
[
"EUR",
"USD",
"INR",
"FCFA"
],
value="EUR",
label="Currency",
info="Select the target currency of your investment"
)
initial_placement_ui = gr.Number(
label="Initial placement",
value=1000,
info="Enter the initial amount of your investment"
)
regular_deposits_ui = gr.Number(
label="Regular deposits amount",
value=100,
info="Determine the amount of your regular payments"
)
regular_deposits_frequency_ui = gr.Dropdown([
"Once a year",
"Twice a year",
"Four times a year",
"Once a month"],
value="Once a month",
label="Regular deposits frequency",
info="Determine the frequency of your regular payments"
)
interest_rate_ui = gr.Number(
value=7.00,
label="Interest rate (%)",
info="Enter the interest rate you expect to earn on your investment"
)
compounded_frequency_ui = gr.Dropdown(
[
"Once a year",
"Twice a year",
"Four times a year",
"Once a month"
],
value="Once a year",
label="Frequency of interest calculation ",
info="Choose the frequency with which interest on your investment is calculated"
)
years_or_months_ui = gr.Radio(
[
"Years",
"Months"
],
value="Years",
label="Horizon of placement"
)
min_slider = 1
max_slider = 720
investment_horizon_ui = gr.Slider(1, 60, value=5, step=1,
label="Duration of placement",
info=f"Years ({min_slider} to 60) or Months ({min_slider} to {max_slider})")
final_amount_ui = gr.Number(label="Total value of your investment")
interest_generated_ui = gr.Number(label="Total interest earned")
inputs_ui = [
goals_ui,
computation_goal_ui,
currency_ui,
initial_placement_ui,
regular_deposits_ui,
regular_deposits_frequency_ui,
interest_rate_ui,
compounded_frequency_ui,
years_or_months_ui,
investment_horizon_ui
]
outputs_ui = [
final_amount_ui,
interest_generated_ui
]
compute_btn_ui = gr.Button(value="Compute")
return inputs_ui, compute_btn_ui, outputs_ui
# with gr.Blocks() as demo:
# gr.Markdown(
# """
# # Calculator for Compound Interest
# Calculate how much profit an investment can bring you
# over time using the power of compound interest.
#The interest calculator calculates either the final capital, term, interest rate or initial capital for one-off investments - either with or without compound interest, and with interest during the year either linear or exponential.
# """
# )
#
# input_ui, compute_btn_ui, output_ui = build_ui()
# compute_btn_ui.click(
# fn=compound_interest_calculation,
# inputs=input_ui,
# outputs=output_ui
# )
input_ui, compute_btn_ui, output_ui = build_ui()
demo = gr.Interface(
fn=compound_interest_calculation,
inputs=input_ui,
outputs=output_ui,
examples=USAGE_EXAMPLES,
title="Calculator for Compound Interest",
description="""Calculate how much profit an investment can bring you
over time using the power of compound interest.
It calculates either the final capital, term, interest rate or initial capital
for your investments - either with or without compound interest,
and with interest during the year either linear or exponential.
"""
)
demo.queue(max_size=100)
demo.launch(show_api=True)