Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
|
| 7 |
import torch
|
| 8 |
import yfinance as yf # For dynamic data fetching
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
available_symbols = ['TSLA', 'MSFT', 'NVDA', 'GOOG', 'AMZN', 'SPY']
|
| 12 |
|
| 13 |
# Financial calculation functions
|
| 14 |
def parse_period(query):
|
|
@@ -70,31 +70,39 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 70 |
def generate_response(user_query, enable_thinking=False):
|
| 71 |
# Parse query for symbol and period
|
| 72 |
symbol_match = re.search(r'\b([A-Z]{1,5})\b', user_query.upper())
|
| 73 |
-
symbol = find_closest_symbol(symbol_match.group(1)) if symbol_match else
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
messages = [
|
| 96 |
{"role": "system", "content": system_prompt},
|
| 97 |
-
{"role": "user", "content": f"{summary} {user_query}"}
|
| 98 |
]
|
| 99 |
|
| 100 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 7 |
import torch
|
| 8 |
import yfinance as yf # For dynamic data fetching
|
| 9 |
|
| 10 |
+
# Expanded list of common symbols for fuzzy matching (added more tickers for better coverage)
|
| 11 |
+
available_symbols = ['TSLA', 'MSFT', 'NVDA', 'GOOG', 'AMZN', 'SPY', 'AAPL', 'META', 'NFLX', 'INTC', 'AMD', 'IBM', 'ORCL', 'CSCO', 'JPM', 'BAC', 'WFC', 'V', 'MA', 'XOM', 'CVX', 'PFE', 'JNJ', 'MRK']
|
| 12 |
|
| 13 |
# Financial calculation functions
|
| 14 |
def parse_period(query):
|
|
|
|
| 70 |
def generate_response(user_query, enable_thinking=False):
|
| 71 |
# Parse query for symbol and period
|
| 72 |
symbol_match = re.search(r'\b([A-Z]{1,5})\b', user_query.upper())
|
| 73 |
+
symbol = find_closest_symbol(symbol_match.group(1)) if symbol_match else None # No default; skip if no match
|
| 74 |
+
summary = "" # Initialize empty to handle general queries
|
| 75 |
+
|
| 76 |
+
if symbol: # Only perform calculations if a valid symbol is detected
|
| 77 |
+
period = parse_period(user_query)
|
| 78 |
+
end_date = datetime.now()
|
| 79 |
+
start_date = end_date - period
|
| 80 |
+
|
| 81 |
+
# Calculate growth rate if applicable
|
| 82 |
+
growth_rate = calculate_growth_rate(start_date, end_date, symbol)
|
| 83 |
+
if growth_rate is not None:
|
| 84 |
+
summary = f"The CAGR for {symbol} over the period is {growth_rate:.2f}%."
|
| 85 |
+
else:
|
| 86 |
+
summary = f"No data available for {symbol} in the specified period."
|
| 87 |
+
|
| 88 |
+
# Handle investment projection
|
| 89 |
+
investment_match = re.search(r'\$(\d+)', user_query)
|
| 90 |
+
if investment_match:
|
| 91 |
+
principal = float(investment_match.group(1))
|
| 92 |
+
years = period.days / 365.25
|
| 93 |
+
projected = calculate_investment(principal, years)
|
| 94 |
+
summary += f" Projecting ${principal} at 7% return over {years:.1f} years: ${projected:.2f}."
|
| 95 |
+
|
| 96 |
+
# Prepare prompt for model with enhanced instructions for natural chatbot behavior
|
| 97 |
+
system_prompt = (
|
| 98 |
+
"You are FinChat, a knowledgeable financial advisor. Always respond in a friendly, professional manner like a helpful chatbot. "
|
| 99 |
+
"For greetings such as 'Hi' or 'Hello', reply warmly, e.g., 'Hi! I'm FinChat, your financial advisor. What can I help you with today regarding stocks, investments, or advice?' "
|
| 100 |
+
"Provide accurate, concise advice based on any provided data. If no specific data is available, offer general financial insights or ask for clarification. "
|
| 101 |
+
"Disclaimer: This is not professional financial advice; consult experts for decisions."
|
| 102 |
+
)
|
| 103 |
messages = [
|
| 104 |
{"role": "system", "content": system_prompt},
|
| 105 |
+
{"role": "user", "content": f"{summary} {user_query}" if summary else user_query}
|
| 106 |
]
|
| 107 |
|
| 108 |
text = tokenizer.apply_chat_template(
|