Akshayram1 commited on
Commit
1d4fc4e
·
verified ·
1 Parent(s): e8d7188

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -6,14 +6,16 @@ import plotly.express as px
6
  from dateutil import parser
7
  import datetime
8
  import requests
9
- from transformers import pipeline
10
 
11
  st.set_page_config(page_title="Stock News Sentiment Analyzer", layout="wide")
12
 
13
- # Initialize FinBERT pipeline
14
  @st.cache_resource
15
  def load_model():
16
- return pipeline("text-classification", model="yiyanghkust/finbert-tone")
 
 
17
 
18
  finbert = load_model()
19
 
@@ -78,10 +80,10 @@ def score_news(parsed_news_df):
78
  label = pred['label']
79
  score = pred['score']
80
 
81
- # Convert to -1 to 1 scale
82
- if label == 'positive':
83
  sentiment_score = score
84
- elif label == 'negative':
85
  sentiment_score = -score
86
  else: # neutral
87
  sentiment_score = 0
@@ -172,7 +174,7 @@ try:
172
  The above charts average the sentiment scores of {ticker} stock hourly and daily.
173
  The table below shows recent headlines with their sentiment scores and classifications.
174
  The news headlines are obtained from the FinViz website.
175
- Sentiments are analyzed using the ProsusAI/finbert model, which is specifically trained for financial text.
176
  Links have been verified for validity.
177
  """
178
 
@@ -196,4 +198,4 @@ hide_streamlit_style = """
196
  footer {visibility: hidden;}
197
  </style>
198
  """
199
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
6
  from dateutil import parser
7
  import datetime
8
  import requests
9
+ from transformers import BertTokenizer, BertForSequenceClassification, pipeline
10
 
11
  st.set_page_config(page_title="Stock News Sentiment Analyzer", layout="wide")
12
 
13
+ # Initialize FinBERT (yiyanghkust/finbert-tone) pipeline
14
  @st.cache_resource
15
  def load_model():
16
+ model = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert-tone', num_labels=3)
17
+ tokenizer = BertTokenizer.from_pretrained('yiyanghkust/finbert-tone')
18
+ return pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
19
 
20
  finbert = load_model()
21
 
 
80
  label = pred['label']
81
  score = pred['score']
82
 
83
+ # Map 'LABEL_0' (neutral), 'LABEL_1' (positive), and 'LABEL_2' (negative)
84
+ if label == 'LABEL_1':
85
  sentiment_score = score
86
+ elif label == 'LABEL_2':
87
  sentiment_score = -score
88
  else: # neutral
89
  sentiment_score = 0
 
174
  The above charts average the sentiment scores of {ticker} stock hourly and daily.
175
  The table below shows recent headlines with their sentiment scores and classifications.
176
  The news headlines are obtained from the FinViz website.
177
+ Sentiments are analyzed using the yiyanghkust/finbert-tone model, which is specifically trained for financial text.
178
  Links have been verified for validity.
179
  """
180
 
 
198
  footer {visibility: hidden;}
199
  </style>
200
  """
201
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)