|
|
|
|
|
import streamlit as st |
|
import pandas as pd |
|
from transformers import BertTokenizer, BertForSequenceClassification |
|
from transformers import pipeline |
|
from scraper import get_latest_news |
|
|
|
|
|
finbert = BertForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone", num_labels=3) |
|
tokenizer = BertTokenizer.from_pretrained("yiyanghkust/finbert-tone") |
|
|
|
|
|
nlp = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer) |
|
|
|
|
|
def analyze_sentiment(text): |
|
results = nlp(text) |
|
sentiment_label = results[0]["label"] |
|
return sentiment_label |
|
|
|
|
|
def get_sentiment_labels(headlines_list): |
|
sentiment_labels = [] |
|
for headline in headlines_list: |
|
label = analyze_sentiment(headline) |
|
sentiment_labels.append(label) |
|
return sentiment_labels |
|
|
|
|
|
def display_news_sentiment_table(headlines_list, sentiment_labels): |
|
df = pd.DataFrame({ |
|
"Headlines": headlines_list, |
|
"Sentiment": sentiment_labels |
|
}) |
|
|
|
|
|
def style_func(val): |
|
color_dict = { |
|
"negative": 'red', |
|
"positive": 'green', |
|
"neutral": 'gray' |
|
} |
|
return f"background-color: {color_dict[val.lower()]}" |
|
|
|
|
|
st.dataframe(df.set_index("Headlines").style.applymap(style_func, subset=["Sentiment"])) |
|
|
|
|
|
st.title("Financial News Sentiment Analysis") |
|
|
|
|
|
latest_news_headlines = get_latest_news() |
|
sentiment_labels = get_sentiment_labels(latest_news_headlines) |
|
|
|
|
|
display_news_sentiment_table(latest_news_headlines, sentiment_labels) |
|
|
|
|
|
if st.button("Refresh"): |
|
st.experimental_rerun() |
|
|
|
|
|
st.markdown("---") |
|
st.subheader("Description") |
|
st.info("This app uses the [FinBERT](https://huggingface.co/yiyanghkust/finbert-tone) model from Hugging Face to perform sentiment analysis on financial news headlines. The headlines are scraped in real-time from [Finviz](https://finviz.com/). The news headlines displayed on the web app are the latest, and you can click the 'Refresh' button to update the headlines and sentiment analysis.") |
|
st.markdown("---") |