Benjamin S Liang
Fixed app.py and added requirements
ee38f19
import streamlit as st
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer, TFAutoModelForSequenceClassification, DistillbertForSequenceClassification
# Options for models from transformers library
MODEL_OPTS = ['default', 'bertweet-base-sentiment-analysis', 'twitter-roberta-base', 'distilRoberta-financial-sentiment']
DEFAULT_OPT = MODEL_OPTS[0]
# returns loaded model and tokenizer, if any
def load_model(opt):
if opt not in MODEL_OPTS: print("Incorrect model selection. Try again!")
model, tokenizer = None, None
# Load the chosen sentiment analysis model from transformers
if opt == DEFAULT_OPT:
return pipeline("sentiment-analysis"), tokenizer
elif opt == 'bertweet-base-sentiment-analysis':
tokenizer = AutoTokenizer.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
model = AutoModelForSequenceClassification.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
elif opt == 'twitter-roberta-base-sentiment':
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
elif opt == 'distilRoberta-financial-sentiment'
tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
elif opt == 'bert-base-multilingual-uncased-sentiment ':
tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncas
elif not model and not tokenizer:
print("Model not loaded correctly. Try again!")
return model, tokenizer
def sentiment_analysis(model, tokenizer):
if tokenizer:
return pipeline('text-classification', model=model, tokenizer=tokenizer)
else: return pipeline('text-classification', model=model)
# Title the Streamlit app 'Sentiment Analysis'
st.title('Sentiment Analysis')
# Take in user input
user_text = st.text_input('Input text to perform sentiment analysis on here.')
# The user can interact with a dropdown menu to choose a sentiment analysis model.
dropdown_value = st.selectbox('Select one of the following sentiment analysis models', MODEL_OPTS, index=MODEL_OPTS.index(DEFAULT_OPT))
model, tokenizer = load_model(dropdown_value)
# Perform sentiment analysis on the user's input
result = sentiment_analysis(text_input)
# Display the sentiment analysis results
st.write('Sentiment:', result[0]['label'], '; Score:', result[0]['score'])