FinChat / app.py
AnilNiraula's picture
Update app.py
5066868 verified
raw
history blame
11.1 kB
import logging
import os
import time
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import pandas as pd
import re
import numpy as np
import json
# Set up logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
# Define device (force CPU for Spaces free tier)
device = torch.device("cpu")
logger.info(f"Using device: {device}")
# Load dataset at startup
csv_path = "flat-ui__data-Sun Jul 06 2025.csv"
try:
df = pd.read_csv(csv_path)
df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values('Date')
df['Return'] = df['SP500'].pct_change(12) * 100
df['Real Return'] = df['Real Price'].pct_change(12) * 100
logger.info("Loaded dataset successfully")
except Exception as e:
logger.error(f"Error loading dataset: {e}")
df = None
# Response cache with financial data entries
response_cache = {
"hi": "Hello! I'm FinChat, your financial advisor. How can I help with investing?",
"hello": "Hello! I'm FinChat, your financial advisor. How can I help with investing?",
"hey": "Hi there! Ready to discuss investment goals with FinChat?",
"what is better individual stocks or etfs?": (
"Here’s a comparison of individual stocks vs. ETFs:\n"
"1. **Individual Stocks**: High returns possible (e.g., Apple up 80% in 2020) but riskier due to lack of diversification. Require active research.\n"
"2. **ETFs**: Diversify risk by tracking indices (e.g., SPY, S&P 500, ~12% avg. return 2015–2024). Lower fees and less research needed.\n"
"3. **Recommendation**: Beginners should start with ETFs; experienced investors may add stocks.\n"
"Consult a financial planner."
),
"is $100 per month enough to invest?": (
"Yes, $100 per month is enough to start investing. Here’s why and how:\n"
"1. **Feasibility**: Brokerages like Fidelity have no minimums, and commission-free trading eliminates fees.\n"
"2. **Options**: Buy fractional shares of ETFs (e.g., SPY, ~$622/share in 2025) with $100.\n"
"3. **Strategy**: Use dollar-cost averaging to invest monthly, reducing market timing risks.\n"
"4. **Growth**: At 10% annual return, $100 monthly could grow to ~$41,000 in 20 years.\n"
"5. **Tips**: Ensure an emergency fund; diversify.\n"
"Consult a financial planner."
),
"can i invest $100 a month?": (
"Yes, $100 a month is sufficient. Here’s how:\n"
"1. **Brokerage**: Open an account with Fidelity or Vanguard (no minimums).\n"
"2. **Investments**: Buy fractional shares of ETFs like SPY ($100 buys ~0.16 shares in 2025).\n"
"3. **Approach**: Use dollar-cost averaging for steady growth.\n"
"4. **Long-Term**: At 10% return, $100 monthly could reach ~$41,000 in 20 years.\n"
"5. **Tips**: Prioritize an emergency fund and diversify.\n"
"Consult a financial planner."
),
"hi, give me step-by-step investing advice": (
"Here’s a step-by-step guide to start investing:\n"
"1. Open a brokerage account (e.g., Fidelity, Vanguard) if 18 or older.\n"
"2. Deposit an affordable amount, like $100, after an emergency fund.\n"
"3. Research and buy an ETF (e.g., SPY) using Yahoo Finance.\n"
"4. Monitor monthly and enable dividend reinvesting.\n"
"5. Use dollar-cost averaging ($100 monthly) to reduce risk.\n"
"6. Diversify across sectors.\n"
"Consult a financial planner."
),
"hi, pretend you are a financial advisor. now tell me how can i start investing in stock market?": (
"Here’s a guide to start investing:\n"
"1. Learn from Investopedia or 'The Intelligent Investor.'\n"
"2. Set goals (e.g., retirement) and assess risk.\n"
"3. Choose a brokerage (Fidelity, Vanguard).\n"
"4. Start with ETFs (e.g., SPY) or mutual funds.\n"
"5. Use dollar-cost averaging ($100-$500 monthly).\n"
"6. Diversify and monitor.\n"
"Consult a financial planner."
),
"do you have a list of companies you recommend?": (
"I can’t recommend specific companies without data. Try ETFs like SPY (S&P 500, ~12% avg. return 2015–2024) or QQQ (tech). "
"Research stocks like Apple (AAPL, ~80% return in 2020) or Johnson & Johnson on Yahoo Finance.\n"
"Consult a financial planner."
),
"how do i start investing in stocks?": (
"Learn from Investopedia. Set goals and assess risk. Open a brokerage account (Fidelity, Vanguard) "
"and start with ETFs (e.g., SPY, ~12% avg. return 2015–2024). Consult a financial planner."
),
"what's the difference between stocks and bonds?": (
"Stocks are company ownership with high risk and growth potential (e.g., S&P 500 ~12% avg. return 2015–2024). Bonds are loans to companies/governments "
"with lower risk and steady interest. Diversify for balance."
),
"how much should i invest?": (
"Invest what you can afford after expenses and an emergency fund. Start with $100-$500 monthly "
"in ETFs like SPY (~12% avg. return 2015–2024). Consult a financial planner."
),
"what is dollar-cost averaging?": (
"Dollar-cost averaging is investing a fixed amount regularly (e.g., $100 monthly) in ETFs, "
"reducing risk by spreading purchases over time."
),
"give me few investing idea": (
"Here are investing ideas:\n"
"1. Open a brokerage account (e.g., Fidelity) if 18 or older.\n"
"2. Deposit $100 or what you can afford.\n"
"3. Buy a researched ETF (e.g., SPY, ~12% avg. return 2015–2024) or index fund.\n"
"4. Check regularly and enable dividend reinvesting.\n"
"5. Use dollar-cost averaging (e.g., monthly buys).\n"
"Consult a financial planner."
),
"give me investing tips": (
"Here are investing tips:\n"
"1. Educate yourself with Investopedia or books.\n"
"2. Open a brokerage account (e.g., Vanguard).\n"
"3. Start small with ETFs like SPY (~12% avg. return 2015–2024).\n"
"4. Invest regularly using dollar-cost averaging.\n"
"5. Diversify to manage risk.\n"
"Consult a financial planner."
),
"how to start investing": (
"Here’s how to start investing:\n"
"1. Study basics on Investopedia.\n"
"2. Open a brokerage account (e.g., Fidelity).\n"
"3. Deposit $100 or more after securing savings.\n"
"4. Buy an ETF like SPY (~12% avg. return 2015–2024) after research.\n"
"5. Invest monthly with dollar-cost averaging.\n"
"Consult a financial planner."
),
"investing advice": (
"Here’s investing advice:\n"
"1. Learn basics from Investopedia.\n"
"2. Open a brokerage account (e.g., Vanguard).\n"
"3. Start with $100 in an ETF like SPY (~12% avg. return 2015–2024).\n"
"4. Use dollar-cost averaging for regular investments.\n"
"5. Monitor and diversify your portfolio.\n"
"Consult a financial planner."
),
"steps to invest": (
"Here are steps to invest:\n"
"1. Educate yourself using Investopedia.\n"
"2. Open a brokerage account (e.g., Fidelity).\n"
"3. Deposit an initial $100 after savings.\n"
"4. Buy an ETF like SPY (~12% avg. return 2015–2024) after research.\n"
"5. Use dollar-cost averaging monthly.\n"
"Consult a financial planner."
),
"what is the s&p 500 index fund average growth rate?": (
"The S&P 500 index fund’s average annual return is approximately 10–12% over the long term (1927–2025), including dividends, based on historical data. "
"For example, from 2015 to 2024, it averaged ~12.2% annually. Returns vary yearly due to market conditions. Consult a financial planner."
),
"what was the s&p 500 return in 2020?": (
"The S&P 500 returned approximately 16.3% in 2020, including dividends, driven by recovery from the COVID-19 market crash."
),
"what was the s&p 500 return in 2022?": (
"The S&P 500 returned approximately -18.1% in 2022, impacted by high inflation and interest rate hikes."
),
"what is the average annual growth rate of the s&p 500 from 2000 to 2010?": (
"The S&P 500’s average annual growth rate from 2000 to 2010 was approximately 0.4%, including dividends, impacted by the dot-com crash and 2008 financial crisis."
),
"what is the average annual growth rate of the s&p 500 from 2011 to 2016?": (
"The S&P 500’s average annual growth rate from 2011 to 2016 was approximately 12.7%, including dividends, driven by post-financial crisis recovery."
),
"what will my return be in 10 years if i invest $5000 into s&p 500 right now?": (
"Assuming a 10% average annual return, a $5,000 investment in the S&P 500 would grow to approximately $12,974 in 10 years with annual compounding. "
"This is based on the historical average return of 10–12% (1927–2025). Future returns vary and are not guaranteed. Consult a financial planner."
),
"what was the 1-year average annual growth rate of the s&p 500 from 2020?": (
"The S&P 500 returned approximately 16.3% in 2020, including dividends, driven by recovery from the COVID-19 market crash."
),
"what was the 3-year average annual growth rate of the s&p 500 from 2018?": (
"The S&P 500’s average annual growth rate from 2018 to 2020 was approximately 10.2%, including dividends, based on historical data."
),
"what was the 5-year average annual growth rate of the s&p 500 from 2016?": (
"The S&P 500’s average annual growth rate from 2016 to 2020 was approximately 13.6%, including dividends, driven by strong market recovery."
),
"what is the average return rate of the s&p 500 in the past 10 years?": (
"The S&P 500’s average annual return rate from 2015 to 2024 was approximately 12.2%, including dividends, based on historical data."
)
}
# Load persistent cache
cache_file = "cache.json"
try:
if os.path.exists(cache_file):
with open(cache_file, 'r') as f:
response_cache.update(json.load(f))
logger.info("Loaded persistent cache from cache.json")
except Exception as e:
logger.warning(f"Failed to load cache.json: {e}")
# Load model and tokenizer (use fine-tuned model if available)
model_name = "./finetuned_model" if os.path.exists("./finetuned_model") else "distilgpt2"
try:
logger.info(f"Loading tokenizer for {model_name}")
tokenizer = AutoTokenizer.from_pretrained(model_name, clean_up_tokenization_spaces=False)
tokenizer.pad_token = tokenizer.eos_token
logger.info(f"Loading model {model_name}")
with torch.inference_mode():
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
low_cpu_mem_usage=True
).to(device)
logger.info(f"Successfully