adriennkurikiyumukiza commited on
Commit
490f832
β€’
1 Parent(s): d54b89f

Sentiment Analysis using HuggingFace

Files changed (1) hide show
  1. sentimentappstreamlit.py +115 -0
sentimentappstreamlit.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing libraries
2
+ import streamlit as st
3
+ import transformers
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
5
+ import os, pickle
6
+ import datetime
7
+ import time
8
+ import base64
9
+ import pandas as pd
10
+ import altair as alt
11
+ from textblob import TextBlob
12
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
13
+ from PIL import Image
14
+
15
+
16
+
17
+ # Load the tokenizer and the fine-tuned model
18
+ tokenizer = AutoTokenizer.from_pretrained('Adoley/covid-tweets-sentiment-analysis')
19
+ model = AutoModelForSequenceClassification.from_pretrained('Adoley/covid-tweets-sentiment-analysis')
20
+
21
+ # Create a sentiment analysis pipeline with your fine-tuned model
22
+ nlp = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
23
+
24
+
25
+ def predict_sentiment(input_text):
26
+ # Use the nlp pipeline to predict the sentiment of the input text
27
+ result = nlp(input_text)[0]
28
+ # Extract the predicted sentiment label and score
29
+ label = result['label']
30
+ score = result['score']
31
+ # Map the sentiment label to an emoji
32
+ sentiment_emojis = {
33
+ 'LABEL_0': 'πŸ˜”', # Negative
34
+ 'LABEL_1': '😊', # Positive
35
+ 'LABEL_2': '😐' # Neutral
36
+ }
37
+ emoji = sentiment_emojis[label]
38
+ # Return the predicted sentiment label, score, and emoji
39
+ return label, score, emoji
40
+
41
+
42
+
43
+ st.set_page_config(page_title="Sentiment Analysis App")
44
+ st.title('Sentiment Analysis App')
45
+
46
+ # Import the image
47
+ image = Image.open('sentiments.png')
48
+
49
+ # Show the image
50
+ st.image(image, caption='Sentiment Analysis', use_column_width=True)
51
+
52
+ # Set up sidebar
53
+ st.sidebar.header('Navigation')
54
+ menu = ['Home', 'About']
55
+ choice = st.sidebar.selectbox("Select an option", menu)
56
+
57
+ # Home section
58
+ if choice == 'Home':
59
+ st.markdown("<h1 style='text-align: center;'>Welcome</h1>", unsafe_allow_html=True)
60
+ st.markdown("<p style='text-align: center;'>This App predicts the sentiments expressed in an input.</p>", unsafe_allow_html=True)
61
+
62
+ # Set Page Title
63
+ st.title('Sentiment Prediction App')
64
+ st.markdown('Type your text and click on Submit')
65
+ text_input = st.text_area(label="Enter some text here", height=100, max_chars=1000)
66
+ st.write('You entered:', text_input)
67
+
68
+ # Create a dataframe to hold the sentiment scores
69
+ data = {'sentiment': ['Negative', 'Neutral', 'Positive'], 'score': [0, 0, 0]}
70
+ df = pd.DataFrame(data)
71
+
72
+ if st.button('Predict'):
73
+ if text_input.strip() == '':
74
+ st.warning('Please enter some text.')
75
+ else:
76
+ label, score, emoji = predict_sentiment(text_input)
77
+ sentiment_map = {
78
+ 'LABEL_0': 'Negative',
79
+ 'LABEL_1': 'Positive',
80
+ 'LABEL_2': 'Neutral'
81
+ }
82
+ sentiment = sentiment_map[label]
83
+ st.success(f'The sentiment is {sentiment} {emoji} with a score of {score:.2f}')
84
+
85
+ # Create a string that explains the sentiment label and score
86
+ if sentiment == 'Neutral':
87
+ explanation = f"The sentiment of the input text is {sentiment}."
88
+ else:
89
+ explanation = f"The sentiment of the input text is {sentiment} with a confidence score of {score:.2f}. A score closer to 1 indicates a strong {sentiment.lower()} sentiment, while a score closer to 0.5 indicates a weaker sentiment."
90
+
91
+ # Update the sentiment scores in the dataframe
92
+ if sentiment == 'Negative':
93
+ df.loc[df['sentiment'] == 'Negative', 'score'] += score
94
+ elif sentiment == 'Neutral':
95
+ df.loc[df['sentiment'] == 'Neutral', 'score'] += score
96
+ elif sentiment == 'Positive':
97
+ df.loc[df['sentiment'] == 'Positive', 'score'] += score
98
+
99
+
100
+ # Sentiment Analysis Visualization
101
+ df = pd.DataFrame({'sentiment': [sentiment], 'score': [score]})
102
+ chart = alt.Chart(df).mark_bar(width=30).encode(
103
+ x='sentiment',
104
+ y='score'
105
+ ).properties(
106
+ width=500
107
+ )
108
+ st.altair_chart(chart, use_container_width= True)
109
+
110
+ # About section
111
+ elif choice == 'About':
112
+ st.title('About')
113
+ # Add information about the model
114
+ st.write('The model used in this app is a pre-trained BERT model fine-tuned on the Stanford Sentiment Treebank (SST-2) dataset. The SST-2 dataset consists of movie reviews labeled as either positive or negative. The model has achieved state-of-the-art performance on the SST-2 dataset and is known for its speed and efficiency. However, like all machine learning models, it has its limitations and may not perform well on certain types of text or in certain contexts. It is important to keep this in mind when interpreting the results of this app.')
115
+ st.write('This WebApp was designed using Streamlit.')