AnilNiraula commited on
Commit
58aed73
·
verified ·
1 Parent(s): a7e3f87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -57,6 +57,7 @@ llm = Llama(
57
  )
58
 
59
  DEFAULT_SYSTEM_PROMPT = """You are FinChat, a knowledgeable AI assistant specializing in investing and finance. Provide accurate, helpful, reasoned, detailed, and comprehensive answers to investing questions. Always base responses on reliable information and advise users to consult professionals for personalized advice.
 
60
 
61
  Example:
62
  User: average return for TSLA between 2010 and 2020
@@ -85,8 +86,7 @@ def generate(
85
  # Parse tickers: split by comma, strip spaces, handle "and"
86
  tickers = [t.strip().upper() for t in re.split(r',|\band\b', tickers_str) if t.strip()]
87
  responses = []
88
- years = int(end_year) - int(start_year)
89
- if years <= 0:
90
  yield "The specified time period is invalid (end year must be after start year)."
91
  return
92
  for ticker in tickers:
@@ -98,13 +98,20 @@ def generate(
98
  data = data.droplevel('Ticker', axis=1)
99
  initial = data['Adj Close'].iloc[0]
100
  final = data['Adj Close'].iloc[-1]
101
- cagr = ((final / initial) ** (1 / years) - 1) * 100
102
- responses.append(f"{ticker}: approximately {cagr:.2f}%")
 
 
 
 
 
 
 
103
  else:
104
  responses.append(f"{ticker}: No historical data available between {start_year} and {end_year}.")
105
  except Exception as e:
106
  responses.append(f"{ticker}: Error calculating CAGR - {str(e)}")
107
- full_response = "The compound annual growth rates (CAGR) for the requested stocks from {} to {} are:\n".format(start_year, end_year)
108
  full_response += "\n".join(responses)
109
  full_response += "\nThese represent the average annual returns over that period, accounting for compounding. Note that past performance is not indicative of future results, and I recommend consulting a financial advisor for personalized advice."
110
  yield full_response
 
57
  )
58
 
59
  DEFAULT_SYSTEM_PROMPT = """You are FinChat, a knowledgeable AI assistant specializing in investing and finance. Provide accurate, helpful, reasoned, detailed, and comprehensive answers to investing questions. Always base responses on reliable information and advise users to consult professionals for personalized advice.
60
+ Always respond exclusively in English. Do not use any other language in your responses.
61
 
62
  Example:
63
  User: average return for TSLA between 2010 and 2020
 
86
  # Parse tickers: split by comma, strip spaces, handle "and"
87
  tickers = [t.strip().upper() for t in re.split(r',|\band\b', tickers_str) if t.strip()]
88
  responses = []
89
+ if int(end_year) <= int(start_year):
 
90
  yield "The specified time period is invalid (end year must be after start year)."
91
  return
92
  for ticker in tickers:
 
98
  data = data.droplevel('Ticker', axis=1)
99
  initial = data['Adj Close'].iloc[0]
100
  final = data['Adj Close'].iloc[-1]
101
+ start_date = data.index[0]
102
+ end_date = data.index[-1]
103
+ days = (end_date - start_date).days
104
+ years = days / 365.25
105
+ if years > 0:
106
+ cagr = ((final / initial) ** (1 / years) - 1) * 100
107
+ responses.append(f"{ticker}: approximately {cagr:.2f}%")
108
+ else:
109
+ responses.append(f"{ticker}: Invalid period (no elapsed time).")
110
  else:
111
  responses.append(f"{ticker}: No historical data available between {start_year} and {end_year}.")
112
  except Exception as e:
113
  responses.append(f"{ticker}: Error calculating CAGR - {str(e)}")
114
+ full_response = f"The compound annual growth rates (CAGR) for the requested stocks from {start_year} to {end_year} are:\n"
115
  full_response += "\n".join(responses)
116
  full_response += "\nThese represent the average annual returns over that period, accounting for compounding. Note that past performance is not indicative of future results, and I recommend consulting a financial advisor for personalized advice."
117
  yield full_response