LinkLinkWu commited on
Commit
df941ba
Β·
verified Β·
1 Parent(s): 6eed361

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -38
app.py CHANGED
@@ -1,7 +1,4 @@
1
  import streamlit as st
2
-
3
- st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered")
4
-
5
  from func import (
6
  get_sentiment_pipeline,
7
  get_ner_pipeline,
@@ -11,79 +8,112 @@ from func import (
11
  )
12
  import time
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # ----------- Header Section with Logo and Title -----------
15
  col1, col2 = st.columns([1, 10])
16
  with col1:
17
  st.image(
18
  "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Finance_Icon_Set_2013_-_Analytics.svg/1024px-Finance_Icon_Set_2013_-_Analytics.svg.png",
19
- width=48
20
  )
21
  with col2:
22
- st.markdown("<h1 style='margin-bottom: 0;'>Stock News Sentiment Analysis</h1>", unsafe_allow_html=True)
 
 
 
23
 
24
- # ----------- Description -----------
25
  st.markdown("""
26
- <p style='font-size:17px; margin-top: 0.5rem;'>
27
- Analyze the latest news sentiment of companies mentioned in your input.
28
- </p>
29
- <p style='font-size:17px;'>
30
- <strong>πŸ’‘ Try input like:</strong> <em>I want to check Apple, Tesla, and Microsoft.</em>
31
- </p>
32
- <p style='font-size:17px;'>
33
- <strong>Note:</strong> News may fail to load if the Finviz website structure changes or access is restricted.
34
- </p>
35
  """, unsafe_allow_html=True)
36
 
37
  # ----------- Input Area -----------
38
- free_text = st.text_area("Enter text mentioning companies:", height=100)
 
 
39
  ner_pipeline = get_ner_pipeline()
40
  tickers = extract_org_entities(free_text, ner_pipeline)
41
 
42
  if tickers:
43
- st.markdown(f"\U0001F50E **Identified Tickers:** `{', '.join(tickers)}`")
44
  else:
45
  tickers = []
46
 
47
  # ----------- Action Button -----------
 
48
  if st.button("Get News and Sentiment"):
49
  if not tickers:
50
- st.warning("Please mention at least one recognizable company.")
51
  else:
52
  sentiment_pipeline = get_sentiment_pipeline()
53
  progress_bar = st.progress(0)
54
  total_stocks = len(tickers)
55
 
56
  for idx, ticker in enumerate(tickers):
57
- st.subheader(f"πŸ” Analyzing {ticker}")
 
58
  news_list = fetch_news(ticker)
59
 
60
  if news_list:
61
- sentiments = []
62
- for news in news_list:
63
- sentiment = analyze_sentiment(news['title'], sentiment_pipeline)
64
- sentiments.append(sentiment)
65
 
66
- positive_count = sentiments.count("Positive")
67
- negative_count = sentiments.count("Negative")
68
  total = len(sentiments)
69
- positive_ratio = positive_count / total if total else 0
70
- negative_ratio = negative_count / total if total else 0
71
 
72
- if positive_ratio >= 0.3:
73
- overall_sentiment = "Positive"
74
- elif negative_ratio >= 0.7:
75
- overall_sentiment = "Negative"
76
  else:
77
- overall_sentiment = "Neutral"
78
 
79
- st.write(f"**Top 3 News Articles for {ticker}:**")
80
- for i, news in enumerate(news_list[:3], 1):
81
- sentiment = sentiments[i - 1]
82
- st.markdown(f"{i}. [{news['title']}]({news['link']}) β€” **{sentiment}**")
83
 
84
- st.success(f"**Overall Sentiment for {ticker}: {overall_sentiment}**")
85
  else:
86
- st.write(f"No news available for {ticker}.")
87
 
88
  progress_bar.progress((idx + 1) / total_stocks)
89
  time.sleep(0.1)
 
1
  import streamlit as st
 
 
 
2
  from func import (
3
  get_sentiment_pipeline,
4
  get_ner_pipeline,
 
8
  )
9
  import time
10
 
11
+ # ----------- Page Config -----------
12
+ st.set_page_config(
13
+ page_title="Stock News Sentiment Analysis",
14
+ layout="centered",
15
+ initial_sidebar_state="collapsed"
16
+ )
17
+
18
+ # ----------- Custom CSS Styling -----------
19
+ st.markdown("""
20
+ <style>
21
+ body {
22
+ background-color: #f9fbfc;
23
+ }
24
+ h1 {
25
+ color: #003366;
26
+ font-family: 'Segoe UI', sans-serif;
27
+ }
28
+ .stTextInput > div > div > input,
29
+ .stTextArea textarea {
30
+ font-size: 16px;
31
+ }
32
+ .stButton>button {
33
+ background-color: #003366;
34
+ color: white;
35
+ font-size: 16px;
36
+ padding: 0.4rem 1rem;
37
+ border-radius: 6px;
38
+ }
39
+ .stButton>button:hover {
40
+ background-color: #005299;
41
+ }
42
+ </style>
43
+ """, unsafe_allow_html=True)
44
+
45
  # ----------- Header Section with Logo and Title -----------
46
  col1, col2 = st.columns([1, 10])
47
  with col1:
48
  st.image(
49
  "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Finance_Icon_Set_2013_-_Analytics.svg/1024px-Finance_Icon_Set_2013_-_Analytics.svg.png",
50
+ width=50
51
  )
52
  with col2:
53
+ st.markdown(
54
+ "<h1 style='margin-bottom: 0.3rem;'>Stock News Sentiment Analysis</h1>",
55
+ unsafe_allow_html=True
56
+ )
57
 
58
+ # ----------- Description Section -----------
59
  st.markdown("""
60
+ <div style='font-size:17px; line-height:1.6;'>
61
+ Gain quick insights into how the market perceives major companies based on news sentiment.<br>
62
+ <b>πŸ’¬ Try:</b> <i>I want to check Apple, Tesla, and Microsoft.</i><br>
63
+ <span style='color:gray; font-size:15px;'>Note: News retrieval depends on Finviz availability and structure.</span>
64
+ </div>
 
 
 
 
65
  """, unsafe_allow_html=True)
66
 
67
  # ----------- Input Area -----------
68
+ st.markdown("### πŸ“ Enter Your Text")
69
+ free_text = st.text_area("Mention company names to analyze:", height=100, placeholder="e.g. I'm interested in Apple, Google, and Nvidia")
70
+
71
  ner_pipeline = get_ner_pipeline()
72
  tickers = extract_org_entities(free_text, ner_pipeline)
73
 
74
  if tickers:
75
+ st.markdown(f"βœ… **Identified Tickers:** `{', '.join(tickers)}`")
76
  else:
77
  tickers = []
78
 
79
  # ----------- Action Button -----------
80
+ st.markdown("### πŸš€ Run Analysis")
81
  if st.button("Get News and Sentiment"):
82
  if not tickers:
83
+ st.warning("⚠️ Please mention at least one recognizable company name.")
84
  else:
85
  sentiment_pipeline = get_sentiment_pipeline()
86
  progress_bar = st.progress(0)
87
  total_stocks = len(tickers)
88
 
89
  for idx, ticker in enumerate(tickers):
90
+ st.markdown(f"---\n### πŸ” Analyzing: `{ticker}`")
91
+
92
  news_list = fetch_news(ticker)
93
 
94
  if news_list:
95
+ sentiments = [analyze_sentiment(news['title'], sentiment_pipeline) for news in news_list]
 
 
 
96
 
97
+ pos_count = sentiments.count("Positive")
98
+ neg_count = sentiments.count("Negative")
99
  total = len(sentiments)
100
+ pos_ratio = pos_count / total if total else 0
101
+ neg_ratio = neg_count / total if total else 0
102
 
103
+ if pos_ratio >= 0.3:
104
+ overall = "Positive"
105
+ elif neg_ratio >= 0.7:
106
+ overall = "Negative"
107
  else:
108
+ overall = "Neutral"
109
 
110
+ st.markdown(f"**πŸ“° Top News for `{ticker}`:**")
111
+ for i, news in enumerate(news_list[:3]):
112
+ st.markdown(f"{i+1}. [{news['title']}]({news['link']}) β€” **{sentiments[i]}**")
 
113
 
114
+ st.success(f"πŸ“ˆ **Overall Sentiment for `{ticker}`: {overall}**")
115
  else:
116
+ st.info(f"No recent news available for `{ticker}`.")
117
 
118
  progress_bar.progress((idx + 1) / total_stocks)
119
  time.sleep(0.1)