LinkLinkWu commited on
Commit
735a2fd
·
verified ·
1 Parent(s): 0bd503e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -1,11 +1,18 @@
1
  import streamlit as st
2
- from func import fetch_news, analyze_sentiment, extract_org_entities
3
- import time
4
 
5
- # 必须放在最前
6
  st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered")
7
 
8
- # ----------- Header Section with Logo and Title (Safe for all environments) -----------
 
 
 
 
 
 
 
 
 
9
  col1, col2 = st.columns([1, 10])
10
  with col1:
11
  st.image(
@@ -15,7 +22,7 @@ with col1:
15
  with col2:
16
  st.markdown("<h1 style='margin-bottom: 0;'>Stock News Sentiment Analysis</h1>", unsafe_allow_html=True)
17
 
18
- # ----------- Description Below Title -----------
19
  st.markdown("""
20
  <p style='font-size:17px; margin-top: 0.5rem;'>
21
  Analyze the latest news sentiment of companies mentioned in your input.
@@ -28,20 +35,22 @@ st.markdown("""
28
  </p>
29
  """, unsafe_allow_html=True)
30
 
31
- # ----------- User Input -----------
32
  free_text = st.text_area("Enter text mentioning companies:", height=100)
33
- tickers = extract_org_entities(free_text)
 
34
 
35
  if tickers:
36
  st.markdown(f"\U0001F50E **Identified Tickers:** `{', '.join(tickers)}`")
37
  else:
38
  tickers = []
39
 
40
- # ----------- Button Trigger Logic -----------
41
  if st.button("Get News and Sentiment"):
42
  if not tickers:
43
  st.warning("Please mention at least one recognizable company.")
44
  else:
 
45
  progress_bar = st.progress(0)
46
  total_stocks = len(tickers)
47
 
@@ -52,7 +61,7 @@ if st.button("Get News and Sentiment"):
52
  if news_list:
53
  sentiments = []
54
  for news in news_list:
55
- sentiment = analyze_sentiment(news['title'])
56
  sentiments.append(sentiment)
57
 
58
  positive_count = sentiments.count("Positive")
 
1
  import streamlit as st
 
 
2
 
3
+ # ✅ 必须作为第一个 Streamlit 命令
4
  st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered")
5
 
6
+ from func import (
7
+ get_sentiment_pipeline,
8
+ get_ner_pipeline,
9
+ fetch_news,
10
+ analyze_sentiment,
11
+ extract_org_entities,
12
+ )
13
+ import time
14
+
15
+ # ----------- Header Section with Logo and Title -----------
16
  col1, col2 = st.columns([1, 10])
17
  with col1:
18
  st.image(
 
22
  with col2:
23
  st.markdown("<h1 style='margin-bottom: 0;'>Stock News Sentiment Analysis</h1>", unsafe_allow_html=True)
24
 
25
+ # ----------- Description -----------
26
  st.markdown("""
27
  <p style='font-size:17px; margin-top: 0.5rem;'>
28
  Analyze the latest news sentiment of companies mentioned in your input.
 
35
  </p>
36
  """, unsafe_allow_html=True)
37
 
38
+ # ----------- Input Area -----------
39
  free_text = st.text_area("Enter text mentioning companies:", height=100)
40
+ ner_pipeline = get_ner_pipeline()
41
+ tickers = extract_org_entities(free_text, ner_pipeline)
42
 
43
  if tickers:
44
  st.markdown(f"\U0001F50E **Identified Tickers:** `{', '.join(tickers)}`")
45
  else:
46
  tickers = []
47
 
48
+ # ----------- Action Button -----------
49
  if st.button("Get News and Sentiment"):
50
  if not tickers:
51
  st.warning("Please mention at least one recognizable company.")
52
  else:
53
+ sentiment_pipeline = get_sentiment_pipeline()
54
  progress_bar = st.progress(0)
55
  total_stocks = len(tickers)
56
 
 
61
  if news_list:
62
  sentiments = []
63
  for news in news_list:
64
+ sentiment = analyze_sentiment(news['title'], sentiment_pipeline)
65
  sentiments.append(sentiment)
66
 
67
  positive_count = sentiments.count("Positive")