Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
|
|
59 |
|
60 |
else:
|
61 |
-
|
|
|
|
|
|
|
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"):
|