gauri-sharan commited on
Commit
927226e
·
verified ·
1 Parent(s): cf7d5e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -3,7 +3,7 @@ from langchain_groq import ChatGroq
3
  import yfinance as yf
4
 
5
  # Initialize the ChatGroq model
6
- llm = ChatGroq(model_name="Llama3-8b-8192", api_key="groq_api_key")
7
 
8
  # Custom CSS for dark blue theme
9
  st.markdown(
@@ -47,18 +47,28 @@ if prompt := st.chat_input("Ask me about stocks..."):
47
  # Fetch stock data or generate response based on user input
48
  if "invest" in prompt.lower() or "should I invest" in prompt.lower():
49
  company_name = prompt.split()[-1] # Assuming the last word is the ticker symbol or company name
50
- stock_data = yf.Ticker(company_name).info
51
 
52
- response = f"Here is the data for {company_name}:\n"
53
- response += f"Current Price: {stock_data.get('currentPrice', 'N/A')}\n"
54
- response += f"Market Cap: {stock_data.get('marketCap', 'N/A')}\n"
55
- response += f"PE Ratio: {stock_data.get('trailingPE', 'N/A')}\n"
56
- response += f"Dividend Yield: {stock_data.get('dividendYield', 'N/A')}\n"
 
 
 
 
 
 
 
57
 
58
- # Add more insights or advice logic here if needed
 
59
 
60
  else:
61
- response = llm.invoke(prompt) # Use the LLM for general questions
 
 
 
62
 
63
  # Display assistant response in chat message container
64
  with st.chat_message("assistant"):
 
3
  import yfinance as yf
4
 
5
  # Initialize the ChatGroq model
6
+ llm = ChatGroq(model_name="Llama3-8b-8192", api_key="groq_api_key") # Ensure this key is set in your Hugging Face secrets
7
 
8
  # Custom CSS for dark blue theme
9
  st.markdown(
 
47
  # Fetch stock data or generate response based on user input
48
  if "invest" in prompt.lower() or "should I invest" in prompt.lower():
49
  company_name = prompt.split()[-1] # Assuming the last word is the ticker symbol or company name
 
50
 
51
+ try:
52
+ stock_data = yf.Ticker(company_name).info
53
+
54
+ # Check if stock_data contains valid information
55
+ if 'currentPrice' in stock_data:
56
+ response = f"Here is the data for {company_name}:\n"
57
+ response += f"Current Price: {stock_data.get('currentPrice', 'N/A')}\n"
58
+ response += f"Market Cap: {stock_data.get('marketCap', 'N/A')}\n"
59
+ response += f"PE Ratio: {stock_data.get('trailingPE', 'N/A')}\n"
60
+ response += f"Dividend Yield: {stock_data.get('dividendYield', 'N/A')}\n"
61
+ else:
62
+ response = f"Sorry, I couldn't find data for {company_name}. Please check the ticker symbol."
63
 
64
+ except Exception as e:
65
+ response = f"An error occurred while fetching data: {str(e)}"
66
 
67
  else:
68
+ try:
69
+ response = llm.invoke(prompt) # Use the LLM for general questions
70
+ except Exception as e:
71
+ response = f"An error occurred while processing your request: {str(e)}"
72
 
73
  # Display assistant response in chat message container
74
  with st.chat_message("assistant"):