ynp3 commited on
Commit
470f394
·
1 Parent(s): 658979e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -1,42 +1,52 @@
1
  import streamlit as st
2
- import pandas as pd
3
- import numpy as np
 
 
4
  import matplotlib.pyplot as plt
5
- import seaborn as sns
6
- import transformers
7
- from transformers import pipeline
8
-
9
- # Set up sentiment analysis pipeline
10
- model_names = ['nlptown/bert-base-multilingual-uncased-sentiment',
11
- 'cardiffnlp/twitter-roberta-base-sentiment',
12
- 'distilbert-base-uncased-finetuned-sst-2-english',
13
- 'nlptown/roberta-base-sentiment-english']
14
- models = {}
15
- for model_name in model_names:
16
- models[model_name] = pipeline('sentiment-analysis', model=model_name)
17
-
18
- # Set up Streamlit app
19
- st.title("Sentiment Analysis App")
20
-
21
- # User input
22
- text_input = st.text_input("Enter text to analyze:")
23
-
24
- if text_input:
25
- # Analyze text with each model and store results in a DataFrame
26
- results = []
27
- for model_name, model in models.items():
28
- result = model(text_input)[0]
29
- results.append({'Model': model_name, 'Score': result['score'], 'Label': result['label']})
30
- results_df = pd.DataFrame(results)
31
-
32
- # Display results in a table
33
- st.write("Results:")
34
- st.table(results_df)
35
-
36
- # Plot results in a bar chart
37
- fig, ax = plt.subplots(figsize=(8, 6))
38
- sns.barplot(x='Model', y='Score', hue='Label', data=results_df, ax=ax)
39
- ax.set_title('Sentiment Analysis Results')
40
- ax.set_ylabel('Score')
41
- ax.set_xlabel('Model')
42
-   st.pyplot(fig)
 
 
 
 
 
 
 
 
 
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)