Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["STREAMLIT_NO_ALT"] = "true"
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from textblob import TextBlob
|
| 6 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 7 |
+
from flair.models import TextClassifier
|
| 8 |
+
from flair.data import Sentence
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
|
| 11 |
+
# Function to perform sentiment analysis using TextBlob model
|
| 12 |
+
def textblob_sentiment(text):
|
| 13 |
+
blob = TextBlob(text)
|
| 14 |
+
return blob.sentiment.polarity
|
| 15 |
+
|
| 16 |
+
# Function to perform sentiment analysis using VADER model
|
| 17 |
+
def vader_sentiment(text):
|
| 18 |
+
analyzer = SentimentIntensityAnalyzer()
|
| 19 |
+
scores = analyzer.polarity_scores(text)
|
| 20 |
+
return scores['compound']
|
| 21 |
+
|
| 22 |
+
# Function to perform sentiment analysis using Flair model
|
| 23 |
+
def flair_sentiment(text):
|
| 24 |
+
classifier = TextClassifier.load('en-sentiment')
|
| 25 |
+
sentence = Sentence(text)
|
| 26 |
+
classifier.predict(sentence)
|
| 27 |
+
if len(sentence.labels) > 0:
|
| 28 |
+
if sentence.labels[0].value == 'POSITIVE':
|
| 29 |
+
return 1.0
|
| 30 |
+
elif sentence.labels[0].value == 'NEGATIVE':
|
| 31 |
+
return -1.0
|
| 32 |
+
return 0.0
|
| 33 |
+
|
| 34 |
+
# Set up the Streamlit app
|
| 35 |
+
st.title('Sentiment Analysis App')
|
| 36 |
+
|
| 37 |
+
# Get user input
|
| 38 |
+
text = st.text_input('Enter text to analyze')
|
| 39 |
+
|
| 40 |
+
# Perform sentiment analysis using each model
|
| 41 |
+
textblob_score = textblob_sentiment(text)
|
| 42 |
+
vader_score = vader_sentiment(text)
|
| 43 |
+
flair_score = flair_sentiment(text)
|
| 44 |
+
|
| 45 |
+
# Display the sentiment scores
|
| 46 |
+
st.write('TextBlob score:', textblob_score)
|
| 47 |
+
st.write('VADER score:', vader_score)
|
| 48 |
+
st.write('Flair score:', flair_score)
|
| 49 |
+
|
| 50 |
+
# Create a graph of the sentiment scores
|
| 51 |
+
fig, ax = plt.subplots()
|
| 52 |
+
ax.bar(['TextBlob', 'VADER', 'Flair'], [textblob_score, vader_score, flair_score])
|
| 53 |
+
ax.axhline(y=0, color='gray', linestyle='--')
|
| 54 |
+
ax.set_title('Sentiment Scores')
|
| 55 |
+
st.pyplot(fig)
|