File size: 4,756 Bytes
490f832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# importing libraries
import streamlit as st
import transformers
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import os, pickle
import datetime
import time
import base64
import pandas as pd
import altair as alt
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from PIL import Image



# Load the tokenizer and the fine-tuned model
tokenizer = AutoTokenizer.from_pretrained('Adoley/covid-tweets-sentiment-analysis')
model = AutoModelForSequenceClassification.from_pretrained('Adoley/covid-tweets-sentiment-analysis')

# Create a sentiment analysis pipeline with your fine-tuned model
nlp = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)


def predict_sentiment(input_text):
    # Use the nlp pipeline to predict the sentiment of the input text
    result = nlp(input_text)[0]
    # Extract the predicted sentiment label and score
    label = result['label']
    score = result['score']
    # Map the sentiment label to an emoji
    sentiment_emojis = {
        'LABEL_0': 'πŸ˜”',  # Negative
        'LABEL_1': '😊',  # Positive
        'LABEL_2': '😐'   # Neutral
    }
    emoji = sentiment_emojis[label]
    # Return the predicted sentiment label, score, and emoji
    return label, score, emoji



st.set_page_config(page_title="Sentiment Analysis App")
st.title('Sentiment Analysis App')

# Import the image
image = Image.open('sentiments.png')

# Show the image
st.image(image, caption='Sentiment Analysis', use_column_width=True)

# Set up sidebar
st.sidebar.header('Navigation')
menu = ['Home', 'About']
choice = st.sidebar.selectbox("Select an option", menu)

# Home section
if choice == 'Home':
    st.markdown("<h1 style='text-align: center;'>Welcome</h1>", unsafe_allow_html=True)
    st.markdown("<p style='text-align: center;'>This App predicts the sentiments expressed in an input.</p>", unsafe_allow_html=True)

    # Set Page Title
    st.title('Sentiment Prediction App')
    st.markdown('Type your text and click on Submit')
    text_input = st.text_area(label="Enter some text here", height=100, max_chars=1000)
    st.write('You entered:', text_input)

    # Create a dataframe to hold the sentiment scores
    data = {'sentiment': ['Negative', 'Neutral', 'Positive'], 'score': [0, 0, 0]}
    df = pd.DataFrame(data)

    if st.button('Predict'):
        if text_input.strip() == '':
         st.warning('Please enter some text.')
        else:
            label, score, emoji = predict_sentiment(text_input)
            sentiment_map = {
            'LABEL_0': 'Negative',
            'LABEL_1': 'Positive',
            'LABEL_2': 'Neutral'
        }
            sentiment = sentiment_map[label]
            st.success(f'The sentiment is {sentiment} {emoji} with a score of {score:.2f}')
        
         # Create a string that explains the sentiment label and score
            if sentiment == 'Neutral':
                explanation = f"The sentiment of the input text is {sentiment}."
            else:
                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."
        
        # Update the sentiment scores in the dataframe
            if sentiment == 'Negative':
                df.loc[df['sentiment'] == 'Negative', 'score'] += score
            elif sentiment == 'Neutral':
                df.loc[df['sentiment'] == 'Neutral', 'score'] += score
            elif sentiment == 'Positive':
                df.loc[df['sentiment'] == 'Positive', 'score'] += score


        # Sentiment Analysis Visualization
            df = pd.DataFrame({'sentiment': [sentiment], 'score': [score]})
            chart = alt.Chart(df).mark_bar(width=30).encode(
            x='sentiment',
            y='score'
            ).properties(
            width=500
            )
            st.altair_chart(chart, use_container_width= True)

# About section
elif choice == 'About':
    st.title('About')
    # Add information about the model
    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.')
    st.write('This WebApp was designed using Streamlit.')