gauri-sharan commited on
Commit
2285a24
·
verified ·
1 Parent(s): e9b5948

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ 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
+ # Set the page configuration for Streamlit
9
+ st.set_page_config(page_title="Stock Chatbot", page_icon="📈")
10
+
11
+ # Custom CSS for dark blue theme
12
+ st.markdown(
13
+ """
14
+ <style>
15
+ .stApp {
16
+ background-color: #1e1e2f;
17
+ color: white;
18
+ }
19
+ .stTextInput>div>input {
20
+ background-color: #2e2e3e;
21
+ color: white;
22
+ }
23
+ .stButton>button {
24
+ background-color: #007bff;
25
+ color: white;
26
+ }
27
+ </style>
28
+ """,
29
+ unsafe_allow_html=True,
30
+ )
31
+
32
+ # Initialize chat history in session state
33
+ if "messages" not in st.session_state:
34
+ st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with stock information today?"}]
35
+
36
+ # Display chat messages from history
37
+ for message in st.session_state.messages:
38
+ with st.chat_message(message["role"]):
39
+ st.write(message["content"])
40
+
41
+ # Accept user input
42
+ if prompt := st.chat_input("Ask me about stocks..."):
43
+ # Display user message in chat message container
44
+ with st.chat_message("user"):
45
+ st.write(prompt)
46
+
47
+ # Add user message to chat history
48
+ st.session_state.messages.append({"role": "user", "content": prompt})
49
+
50
+ # Fetch stock data or generate response based on user input
51
+ if "invest" in prompt.lower() or "should I invest" in prompt.lower():
52
+ company_name = prompt.split()[-1] # Assuming the last word is the ticker symbol or company name
53
+ stock_data = yf.Ticker(company_name).info
54
+
55
+ response = f"Here is the data for {company_name}:\n"
56
+ response += f"Current Price: {stock_data.get('currentPrice', 'N/A')}\n"
57
+ response += f"Market Cap: {stock_data.get('marketCap', 'N/A')}\n"
58
+ response += f"PE Ratio: {stock_data.get('trailingPE', 'N/A')}\n"
59
+ response += f"Dividend Yield: {stock_data.get('dividendYield', 'N/A')}\n"
60
+
61
+ # Add more insights or advice logic here if needed
62
+
63
+ else:
64
+ response = llm.invoke(prompt) # Use the LLM for general questions
65
+
66
+ # Display assistant response in chat message container
67
+ with st.chat_message("assistant"):
68
+ st.write(response)
69
+
70
+ # Add assistant response to chat history
71
+ st.session_state.messages.append({"role": "assistant", "content": response})