File size: 1,972 Bytes
b8114bb
 
 
 
5040e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8114bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5040e1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
os.environ["STREAMLIT_NO_ALT"] = "true"

import streamlit as st
import matplotlib.pyplot as plt

# Install dependencies
st.write("Installing dependencies...")
streamlit_deps = """
streamlit
textblob
vadersentiment
flair
matplotlib
""".strip().split('\n')

for lib in streamlit_deps:
    os.system(f"pip install {lib}")

# Now you can import them
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from flair.models import TextClassifier
from flair.data import Sentence

# 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)