File size: 3,056 Bytes
6ce1db6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
import numpy as np
import transformers
import torch
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import select
from PIL import Image



# Load the dataset
df = pd.read_csv('Eval_subset.csv')
st.set_page_config(
    page_title="Twitter Sentiment Analyzer", page_icon="📊", layout="wide"
)

#Image Description
image = Image.open('image1.jpg')
image_y = Image.open('image2.png')
st.image([image,image_y])


@st.cache_resource
def get_model():
    model = transformers.AutoModelForSequenceClassification.from_pretrained("Kwasiasomani/Finetuned-Distilbert-base-model")
    tokenizer = transformers.AutoTokenizer.from_pretrained("Kwasiasomani/Finetuned-Distilbert-base-model")
    return tokenizer,model

tokenizer, model = get_model()

st.header("Covid19 Twitter Sentimental Analysis")
st.text("This app uses Distilbert-base-uncased for the analysis.")
add_text_sidebar = st.sidebar.title("Menu")
with st.sidebar:
    st.title("Twitter Sentiment Analyzer")

    st.markdown(
        """
        <div style="text-align: justify;">
            This app performs sentiment analysis on the latest tweets based on 
            the entered search term. Since the app can only predict positive or 
            negative, and neutral sentiment, it is more suitable towards analyzing the 
            sentiment of brand, product, service, company, or person. 
            Only English tweets are supported.
        </div>
        """,
        unsafe_allow_html=True,
    )

#Grapjical representation
st.sidebar.markdown("### Number of tweets by sentiment")
sentiment_count = df['safe_text'].value_counts()
sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values})
if not st.sidebar.checkbox("Hide", True):
    st.markdown("### Number of tweets by sentiment")
    if select == 'Bar plot':
        fig = px.bar(sentiment_count, x='Sentiment', y='Tweets', color='Tweets', height=50)
        st.plotly_chart(fig)
    else:
        fig = px.pie(sentiment_count, values='Tweets', names='Sentiment')
        st.plotly_chart(fig)





# Side view description 
st.sidebar.subheader("Show random tweet")
random_tweet = st.sidebar.radio('Sentiment', ('positive', 'neutral', 'negative'))
st.sidebar.markdown(df['safe_text'])


st.markdown("[Github link](https://github.com/kwasiasomani)")
st.markdown("[Medium link](https://medium.com/@kwasiasomani85)")
st.markdown('Created by Foster,Kwasi,Linda,Stella,Joshua and Bright')
user_input = st.text_area('Enter text to predict')
button = st.button('predict')


# Define Helper Function
label = {0: 'Negative', 1:'Neutral', 2:'Positive'}


      
# Prediction
if user_input and button:
    test_input = tokenizer([user_input],return_tensors='pt')
    st.slider("Number of tweets", min_value=100, max_value=2000, key="user_input")

    # Test output
    output = model(**test_input)
    st.write('Logits:',output.logits)
    predicted_class = np.argmax(output.logits.detach().numpy())
    st.write('prediction:',label[predicted_class])