Szeyu commited on
Commit
72f2309
·
verified ·
1 Parent(s): 908b282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  from bs4 import BeautifulSoup
4
  from transformers import pipeline
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
6
 
7
  model_id = "LinkLinkWu/Boss_Stock_News_Analysis"
8
 
@@ -11,9 +12,9 @@ tokenizer = AutoTokenizer.from_pretrained(model_id)
11
  model = AutoModelForSequenceClassification.from_pretrained(model_id)
12
 
13
  # Initialize sentiment analysis pipeline
14
- sentiment_pipeline = pipeline("sentiment-analysis")
15
 
16
- # Function to fetch top 3 news articles from FinViz
17
  def fetch_news(ticker):
18
  try:
19
  url = f"https://finviz.com/quote.ashx?t={ticker}"
@@ -22,7 +23,7 @@ def fetch_news(ticker):
22
  soup = BeautifulSoup(response.text, 'html.parser')
23
  news_table = soup.find(id='news-table')
24
  news = []
25
- for row in news_table.findAll('tr')[:3]: # Limit to top 3
26
  title = row.a.get_text()
27
  link = row.a['href']
28
  news.append({'title': title, 'link': link})
@@ -54,16 +55,37 @@ if st.button("Get News and Sentiment"):
54
  if len(tickers) != 5:
55
  st.error("Please enter exactly five stock tickers.")
56
  else:
57
- # Process each ticker
58
- for ticker in tickers:
59
- st.subheader(f"Top 3 News Articles for {ticker}")
 
60
  news_list = fetch_news(ticker)
61
 
62
  if news_list:
63
- for i, news in enumerate(news_list, 1):
 
 
64
  sentiment = analyze_sentiment(news['title'])
 
 
 
 
 
 
 
 
 
 
 
65
  st.markdown(f"{i}. [{news['title']}]({news['link']}) - **{sentiment}**")
 
 
 
66
  else:
67
- st.write("No news available for this ticker.")
 
 
 
 
68
  else:
69
  st.warning("Please enter stock tickers.")
 
3
  from bs4 import BeautifulSoup
4
  from transformers import pipeline
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
+ import time
7
 
8
  model_id = "LinkLinkWu/Boss_Stock_News_Analysis"
9
 
 
12
  model = AutoModelForSequenceClassification.from_pretrained(model_id)
13
 
14
  # Initialize sentiment analysis pipeline
15
+ sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
16
 
17
+ # Function to fetch top 50 news articles from FinViz
18
  def fetch_news(ticker):
19
  try:
20
  url = f"https://finviz.com/quote.ashx?t={ticker}"
 
23
  soup = BeautifulSoup(response.text, 'html.parser')
24
  news_table = soup.find(id='news-table')
25
  news = []
26
+ for row in news_table.findAll('tr')[:50]: # Fetch up to 50 articles
27
  title = row.a.get_text()
28
  link = row.a['href']
29
  news.append({'title': title, 'link': link})
 
55
  if len(tickers) != 5:
56
  st.error("Please enter exactly five stock tickers.")
57
  else:
58
+ progress_bar = st.progress(0)
59
+ total_stocks = len(tickers)
60
+ for idx, ticker in enumerate(tickers):
61
+ st.subheader(f"Analyzing {ticker}...")
62
  news_list = fetch_news(ticker)
63
 
64
  if news_list:
65
+ # Analyze sentiment for all news articles (up to 50)
66
+ sentiments = []
67
+ for news in news_list:
68
  sentiment = analyze_sentiment(news['title'])
69
+ sentiments.append(sentiment)
70
+
71
+ # Determine overall sentiment based on majority
72
+ positive_count = sentiments.count("Positive")
73
+ negative_count = sentiments.count("Negative")
74
+ overall_sentiment = "Positive" if positive_count > negative_count else "Negative"
75
+
76
+ # Display top 3 news articles with sentiment
77
+ st.write(f"**Top 3 News Articles for {ticker}**")
78
+ for i, news in enumerate(news_list[:3], 1):
79
+ sentiment = sentiments[i-1]
80
  st.markdown(f"{i}. [{news['title']}]({news['link']}) - **{sentiment}**")
81
+
82
+ # Display overall sentiment
83
+ st.write(f"**Overall Sentiment for {ticker}: {overall_sentiment}**")
84
  else:
85
+ st.write(f"No news available for {ticker}.")
86
+
87
+ # Update progress bar
88
+ progress_bar.progress((idx + 1) / total_stocks)
89
+ time.sleep(0.1) # Simulate processing time
90
  else:
91
  st.warning("Please enter stock tickers.")