Jeffrey Rathgeber Jr
Milestone3begin
cd87a42 unverified
raw
history blame
1.55 kB
import streamlit as st
import tensorflow as tf
from transformers import pipeline
from textblob import TextBlob
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline(task="sentiment-analysis", model=model, tokenizer=tokenizer)
# classifier = pipeline(task="sentiment-analysis")
textIn = st.text_input("Input Text Here:", "I really like the color of your car!")
option = st.selectbox('Which pre-trained model would you like for your sentiment analysis?',('Pipeline', 'TextBlob'))
st.write('You selected:', option)
if option == 'Pipeline':
# pipeline
preds = classifier(textIn)
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
st.write('According to Pipeline, input text is ', preds[0]['label'], ' with a confidence of ', preds[0]['score'])
if option == 'TextBlob':
# textblob
polarity = TextBlob(textIn).sentiment.polarity
subjectivity = TextBlob(textIn).sentiment.subjectivity
sentiment = ''
if polarity < 0:
sentiment = 'Negative'
elif polarity == 0:
sentiment = 'Neutral'
else:
sentiment = 'Positive'
st.write('According to TextBlob, input text is ', sentiment, ' and a subjectivity score (from 0 being objective to 1 being subjective) of ', subjectivity)