Finance-Planner / app.py
zayeem00's picture
Update app.py
5eadea7 verified
raw
history blame
6.38 kB
import gradio as gr
import openai
def financial_planning(prompt, api_key):
openai.api_key = api_key
message=[
{"role": "system", "content": "You are a knowledgeable and insightful Financial Planner dedicated to assisting users in making well-informed investment decisions. Your goal is to provide personalized financial advice tailored to each user’s unique financial profile, including their income, expenses, savings, debts, investment preferences, risk tolerance, retirement goals, and other relevant financial details. With your expertise, you aim to help users achieve their financial goals, whether it’s planning for retirement, saving for major purchases, or optimizing their investments. Guide them with detailed and practical financial plans that consider both their short-term needs and long-term aspirations."},
{"role": "user", "content": prompt}
]
response = openai.ChatCompletion.create(
model="gpt-4o",
messages= message,
max_tokens=800,
temperature=0.7,
)
return response.choices[0]["message"]["content"]
def generate_advice(name, age, income, expenses, savings, debts, investment_preferences, risk_tolerance, retirement_age, retirement_savings_goal, short_term_goals, long_term_goals, dependents, health_expenses, education_expenses, insurance_policies, major_purchases, emergency_fund, annual_income_growth, annual_expense_growth, currency, api_key):
prompt = (
f"Name: {name}\n"
f"Age: {age}\n"
f"Monthly Income: {currency}{income}\n"
f"Monthly Expenses: {currency}{expenses}\n"
f"Savings: {currency}{savings}\n"
f"Debts: {currency}{debts}\n"
f"Investment Preferences: {investment_preferences}\n"
f"Risk Tolerance: {risk_tolerance}\n"
f"Retirement Age: {retirement_age}\n"
f"Retirement Savings Goal: {currency}{retirement_savings_goal}\n"
f"Short-term Goals: {short_term_goals}\n"
f"Long-term Goals: {long_term_goals}\n"
f"Dependents: {dependents}\n"
f"Health Expenses: {currency}{health_expenses}\n"
f"Education Expenses: {currency}{education_expenses}\n"
f"Insurance Policies: {insurance_policies}\n"
f"Major Purchases: {major_purchases}\n"
f"Emergency Fund: {currency}{emergency_fund}\n"
f"Annual Income Growth: {annual_income_growth}%\n"
f"Annual Expense Growth: {annual_expense_growth}%\n"
f"Provide a detailed financial plan based on the above information."
)
advice = financial_planning(prompt, api_key)
return advice
# Function to clear inputs
def clear_inputs():
return "", 0, 0, 0, 0, 0, "", "Low", 0, 0, "", "", 0, 0, 0, "", "", 0, 0, 0, "HKD"
with gr.Blocks(gr.themes.Base()) as demo:
gr.Markdown("<h1 style='text-align: center'>Smart Finance Planner: Your Personalized Financial Guide</h1>")
gr.Markdown("<p style='text-align: center'>Unlock your financial potential with Smart Finance Planner. Input your financial details and receive a customized, comprehensive financial plan tailored to your goals and lifestyle. Whether you're planning for retirement, saving for a major purchase, or just looking to optimize your investments, our intelligent tool provides expert advice to help you achieve financial success. Get started today and take control of your financial future!</p>")
with gr.Row():
name = gr.Textbox(label="Name")
age = gr.Number(label="Age")
income = gr.Textbox(label="Monthly Income")
expenses = gr.Textbox(label="Monthly Expenses")
savings = gr.Textbox(label="Savings", placeholder = "Overall Savings")
debts = gr.Textbox(label="Debts", placeholder="Current Debts")
investment_preferences = gr.Textbox(label="Investment Preferences", placeholder="Eg: Stocks, Real Estate")
risk_tolerance = gr.Dropdown(label="Risk Tolerance", choices=["Low", "Medium", "High"])
retirement_age = gr.Number(label="Retirement Age")
retirement_savings_goal = gr.Textbox(label="Retirement Savings Goal", placeholder = "Eg: 5 Million")
short_term_goals = gr.Textbox(label="Short-term Goals", placeholder="Eg: Vacation, Vehicle Purchase")
long_term_goals = gr.Textbox(label="Long-term Goals", placeholder="Eg: Retirement Plan, Children's Education")
dependents = gr.Number(label="Dependents")
health_expenses = gr.Textbox(label="Monthly Health Expenses")
education_expenses = gr.Textbox(label="Education Expenses", placeholder= "Monthly Expense")
insurance_policies = gr.Textbox(label="Insurance Policies", placeholder="Eg: Health, Term")
major_purchases = gr.Textbox(label="Major Purchases", placeholder="Enter estimated costs")
emergency_fund = gr.Textbox(label="Emergency Fund", placeholder="Liquid Funds")
annual_income_growth = gr.Textbox(label="Annual Income Growth", placeholder="In %")
annual_expense_growth = gr.Textbox(label="Annual Expense Growth", placeholder="In %")
currency = gr.Dropdown(label="Currency", choices=["HKD", "USD", "EUR", "GBP", "JPY", "INR", "AUD", "CAD", "CHF", "CNY", "Other"])
with gr.Row():
api_key = gr.Textbox(label='OpenAI API Key', type='password')
with gr.Row():
submit_btn = gr.Button("Generate Financial Plan")
clear_btn = gr.Button("Clear")
with gr.Row():
output = gr.Markdown(label="Financial Plan")
submit_btn.click(
generate_advice,
inputs=[name, age, income, expenses, savings, debts, investment_preferences, risk_tolerance, retirement_age, retirement_savings_goal, short_term_goals, long_term_goals, dependents, health_expenses, education_expenses, insurance_policies, major_purchases, emergency_fund, annual_income_growth, annual_expense_growth, currency, api_key],
outputs=output
)
clear_btn.click(
clear_inputs,
outputs=[name, age, income, expenses, savings, debts, investment_preferences, risk_tolerance, retirement_age, retirement_savings_goal, short_term_goals, long_term_goals, dependents, health_expenses, education_expenses, insurance_policies, major_purchases, emergency_fund, annual_income_growth, annual_expense_growth, currency]
)
demo.launch()