finbert_finviz /
sushobhan55's picture
Upload 3 files
history blame contribute delete
No virus
2.51 kB
# streamlit app
import streamlit as st
import pandas as pd
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline
from scraper import get_latest_news
# Load FinBERT model and tokenizer
finbert = BertForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone", num_labels=3)
tokenizer = BertTokenizer.from_pretrained("yiyanghkust/finbert-tone")
# Create sentiment analysis pipeline
nlp = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer)
# Function to perform sentiment analysis
def analyze_sentiment(text):
results = nlp(text)
sentiment_label = results[0]["label"]
return sentiment_label
# Function to get sentiment labels for a list of headlines
def get_sentiment_labels(headlines_list):
sentiment_labels = []
for headline in headlines_list:
label = analyze_sentiment(headline)
return sentiment_labels
# Function to print a Streamlit table with news headlines and sentiment labels
def display_news_sentiment_table(headlines_list, sentiment_labels):
df = pd.DataFrame({
"Headlines": headlines_list,
"Sentiment": sentiment_labels
# Function to apply background colors based on sentiment labels
def style_func(val):
color_dict = {
"negative": 'red',
"positive": 'green',
"neutral": 'gray'
return f"background-color: {color_dict[val.lower()]}"
# Display the table
st.dataframe(df.set_index("Headlines").style.applymap(style_func, subset=["Sentiment"]))
# Streamlit app
st.title("Financial News Sentiment Analysis")
# Get the latest news headlines and sentiment labels using the scraper
latest_news_headlines = get_latest_news()
sentiment_labels = get_sentiment_labels(latest_news_headlines)
# Display the table in the Streamlit app
display_news_sentiment_table(latest_news_headlines, sentiment_labels)
# Refresh button
if st.button("Refresh"):
# App Description
st.subheader("Description")"This app uses the [FinBERT]( model from Hugging Face to perform sentiment analysis on financial news headlines. The headlines are scraped in real-time from [Finviz]( 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.")