|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
st.title("Financial Sentiment Analysis App") |
|
|
|
|
|
text_input = st.text_area("Enter Financial News:", "Tesla stock is soaring after record-breaking earnings.") |
|
|
|
|
|
def predict_sentiment(text): |
|
inputs = tokenizer(text, return_tensors="pt") |
|
outputs = model(**inputs) |
|
sentiment_class = outputs.logits.argmax(dim=1).item() |
|
sentiment_mapping = {0: 'Negative', 1: 'Neutral', 2: 'Positive'} |
|
predicted_sentiment = sentiment_mapping.get(sentiment_class, 'Unknown') |
|
return predicted_sentiment |
|
|
|
|
|
if st.button("Analyze Sentiment"): |
|
|
|
if text_input: |
|
|
|
with st.spinner("Analyzing sentiment..."): |
|
sentiment = predict_sentiment(text_input) |
|
|
|
st.success(f"Sentiment: {sentiment}") |
|
if sentiment == 'Positive': |
|
st.balloons() |
|
|
|
else: |
|
st.warning("Please enter some text for sentiment analysis.") |
|
|
|
|
|
if st.checkbox("Show Raw Sentiment Scores"): |
|
if text_input: |
|
inputs = tokenizer(text_input, return_tensors="pt") |
|
outputs = model(**inputs) |
|
raw_scores = outputs.logits[0].tolist() |
|
st.info(f"Raw Sentiment Scores: {raw_scores}") |
|
|
|
|
|
|
|
|
|
|
|
st.text("Built with Streamlit and Transformers") |
|
|