Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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.
|
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 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
-
response
|
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.
|
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>")})
|