gauri-sharan commited on
Commit
d6b4c2d
·
verified ·
1 Parent(s): 228308a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -12,7 +12,7 @@ if "messages" not in st.session_state:
12
  # Display chat messages from history
13
  for message in st.session_state.messages:
14
  with st.chat_message(message["role"]):
15
- st.write(message["content"])
16
 
17
  # Accept user input
18
  if prompt := st.chat_input("Ask me about stocks..."):
@@ -30,21 +30,29 @@ if prompt := st.chat_input("Ask me about stocks..."):
30
  try:
31
  stock_data = yf.Ticker(company_name).info
32
 
33
- # Check if stock_data contains valid information
34
- if 'currentPrice' in stock_data:
35
- response = f"Here is the data for {company_name}:\n"
36
- response += f"Current Price: {stock_data.get('currentPrice', 'N/A')}\n"
37
- response += f"Market Cap: {stock_data.get('marketCap', 'N/A')}\n"
38
- response += f"PE Ratio: {stock_data.get('trailingPE', 'N/A')}\n"
39
- response += f"Dividend Yield: {stock_data.get('dividendYield', 'N/A')}\n"
40
-
41
- # Simple investment recommendation logic (this can be improved)
42
- if stock_data.get('trailingPE', 0) < 20: # Example condition for recommendation
43
- response += "\n**Recommendation:** Yes, consider investing!"
44
- else:
45
- response += "\n**Recommendation:** No, it might not be a good time to invest."
 
 
 
 
 
 
 
 
46
  else:
47
- response = f"Sorry, I couldn't find data for {company_name}. Please check the ticker symbol."
48
 
49
  except Exception as e:
50
  response = f"An error occurred while fetching data: {str(e)}"
@@ -57,7 +65,7 @@ if prompt := st.chat_input("Ask me about stocks..."):
57
 
58
  # Display assistant response in chat message container with line breaks for readability
59
  with st.chat_message("assistant"):
60
- st.write(response.replace("\n", "<br>"), unsafe_allow_html=True)
61
 
62
- # Add assistant response to chat history
63
- st.session_state.messages.append({"role": "assistant", "content": response})
 
12
  # Display chat messages from history
13
  for message in st.session_state.messages:
14
  with st.chat_message(message["role"]):
15
+ st.markdown(message["content"], unsafe_allow_html=True)
16
 
17
  # Accept user input
18
  if prompt := st.chat_input("Ask me about stocks..."):
 
30
  try:
31
  stock_data = yf.Ticker(company_name).info
32
 
33
+ # Extract relevant information into a structured format
34
+ stock_info = {
35
+ "Company": stock_data.get("longName", "N/A"),
36
+ "Current Price": stock_data.get("currentPrice", "N/A"),
37
+ "Market Cap": stock_data.get("marketCap", "N/A"),
38
+ "PE Ratio": stock_data.get("trailingPE", "N/A"),
39
+ "Dividend Yield": stock_data.get("dividendYield", "N/A"),
40
+ "52 Week High": stock_data.get("fiftyTwoWeekHigh", "N/A"),
41
+ "52 Week Low": stock_data.get("fiftyTwoWeekLow", "N/A"),
42
+ "Sector": stock_data.get("sector", "N/A"),
43
+ "Industry": stock_data.get("industry", "N/A")
44
+ }
45
+
46
+ # Prepare response string with line breaks for readability
47
+ response = f"Here is the data for {company_name}:\n"
48
+ for key, value in stock_info.items():
49
+ response += f"{key}: {value}\n"
50
+
51
+ # Simple investment recommendation logic (this can be improved)
52
+ if stock_info["PE Ratio"] != "N/A" and float(stock_info["PE Ratio"]) < 20: # Example condition for recommendation
53
+ response += "\n**Recommendation:** Yes, consider investing!"
54
  else:
55
+ response += "\n**Recommendation:** No, it might not be a good time to invest."
56
 
57
  except Exception as e:
58
  response = f"An error occurred while fetching data: {str(e)}"
 
65
 
66
  # Display assistant response in chat message container with line breaks for readability
67
  with st.chat_message("assistant"):
68
+ st.markdown(response.replace("\n", "<br>"), unsafe_allow_html=True)
69
 
70
+ # Add assistant response to chat history, preserving line breaks
71
+ st.session_state.messages.append({"role": "assistant", "content": response.replace("\n", "<br>")})