ynp3 commited on
Commit
40ab78a
1 Parent(s): 77d38c2

Create app.py

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