Spaces:
Runtime error
Runtime error
import streamlit as st | |
from textblob import TextBlob | |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
from flair.models import TextClassifier | |
from flair.data import Sentence | |
import matplotlib.pyplot as plt | |
# Function to perform sentiment analysis using TextBlob model | |
def textblob_sentiment(text): | |
blob = TextBlob(text) | |
return blob.sentiment.polarity | |
# Function to perform sentiment analysis using VADER model | |
def vader_sentiment(text): | |
analyzer = SentimentIntensityAnalyzer() | |
scores = analyzer.polarity_scores(text) | |
return scores['compound'] | |
# Function to perform sentiment analysis using Flair model | |
def flair_sentiment(text): | |
classifier = TextClassifier.load('en-sentiment') | |
sentence = Sentence(text) | |
classifier.predict(sentence) | |
if len(sentence.labels) > 0: | |
if sentence.labels[0].value == 'POSITIVE': | |
return 1.0 | |
elif sentence.labels[0].value == 'NEGATIVE': | |
return -1.0 | |
return 0.0 | |
# Set up the Streamlit app | |
st.title('Sentiment Analysis App') | |
# Get user input | |
text = st.text_input('Enter text to analyze') | |
# Perform sentiment analysis using each model | |
textblob_score = textblob_sentiment(text) | |
vader_score = vader_sentiment(text) | |
flair_score = flair_sentiment(text) | |
# Display the sentiment scores | |
st.write('TextBlob score:', textblob_score) | |
st.write('VADER score:', vader_score) | |
st.write('Flair score:', flair_score) | |
# Create a graph of the sentiment scores | |
fig, ax = plt.subplots() | |
ax.bar(['TextBlob', 'VADER', 'Flair'], [textblob_score, vader_score, flair_score]) | |
ax.axhline(y=0, color='gray', linestyle='--') | |
ax.set_title('Sentiment Scores') | |
st.pyplot(fig) |