gauri-sharan commited on
Commit
981e745
·
verified ·
1 Parent(s): d6b4c2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -30,36 +30,41 @@ 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)}"
59
 
60
  else:
61
  try:
62
- response = llm.invoke(prompt) # Use the LLM for general questions
 
63
  except Exception as e:
64
  response = f"An error occurred while processing your request: {str(e)}"
65
 
 
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 and stock_data['currentPrice'] is not None:
35
+ # Extract relevant information into a structured format
36
+ stock_info = {
37
+ "Company": stock_data.get("longName", "N/A"),
38
+ "Current Price": stock_data.get("currentPrice", "N/A"),
39
+ "Market Cap": stock_data.get("marketCap", "N/A"),
40
+ "PE Ratio": stock_data.get("trailingPE", "N/A"),
41
+ "Dividend Yield": stock_data.get("dividendYield", "N/A"),
42
+ "52 Week High": stock_data.get("fiftyTwoWeekHigh", "N/A"),
43
+ "52 Week Low": stock_data.get("fiftyTwoWeekLow", "N/A"),
44
+ "Sector": stock_data.get("sector", "N/A"),
45
+ "Industry": stock_data.get("industry", "N/A")
46
+ }
47
 
48
+ # Prepare response string with line breaks for readability
49
+ response = f"Here is the data for {company_name}:\n"
50
+ for key, value in stock_info.items():
51
+ response += f"{key}: {value}\n"
52
 
53
+ # Simple investment recommendation logic (this can be improved)
54
+ if stock_info["PE Ratio"] != "N/A" and float(stock_info["PE Ratio"]) < 20: # Example condition for recommendation
55
+ response += "\n**Recommendation:** Yes, consider investing!"
56
+ else:
57
+ response += "\n**Recommendation:** No, it might not be a good time to invest."
58
  else:
59
+ response = f"Sorry, I couldn't find valid data for {company_name}. Please check the ticker symbol."
60
 
61
  except Exception as e:
62
  response = f"An error occurred while fetching data: {str(e)}"
63
 
64
  else:
65
  try:
66
+ # Use the LLM for general questions or topics not related to stocks
67
+ response = llm.invoke(prompt)
68
  except Exception as e:
69
  response = f"An error occurred while processing your request: {str(e)}"
70