File size: 2,809 Bytes
6bc3901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import time
from transformers import pipeline
import os

os.environ['KMP_DUPLICATE_LIB_OK'] = "True"



st.title("Sentiment Analysis App")

form = st.form(key='Sentiment Analysis')
box = form.selectbox('Select Pre-trained Model:', ['bertweet-base-sentiment-analysis',
                                                   'distilbert-base-uncased-finetuned-sst-2-english',
                                                   'twitter-roberta-base-sentiment'
                                                   ], key=1)
tweet = form.text_input(label='Enter text to analyze:', value="\"We've seen in the last few months, unprecedented amounts of Voter Fraud.\" @SenTedCruz True!")
submit = form.form_submit_button(label='Submit')

if submit and tweet:
    with st.spinner('Analyzing...'):
        time.sleep(1)
    # st.header(tweet)

    if tweet is not None:
        col1, col2, col3 = st.columns(3)
        if box == 'bertweet-base-sentiment-analysis':
            pipeline = pipeline(task="sentiment-analysis", model="finiteautomata/bertweet-base-sentiment-analysis")
        elif box == 'twitter-xlm-roberta-base-sentiment':
            pipeline = pipeline(task="sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
        else:
            pipeline = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
        predictions = pipeline(tweet)
        print(predictions)
        col1.header("Tweet")
        col1.subheader(tweet)
        col2.header("Judgement")
        col3.header("Probability")
        for p in predictions:
            if box == 'bertweet-base-sentiment-analysis':
                if p['label'] == "POS":
                    col2.success(f"{ p['label'] }")
                    col3.success(f"{ round(p['score'] * 100, 1)}%")
                elif p['label'] == "NEU":
                    col2.warning(f"{ p['label'] }")
                    col3.warning(f"{round(p['score'] * 100, 1)}%")
                else:
                    col2.error(f"{p['label']}")
                    col3.error(f"{round(p['score'] * 100, 1)}%")
            elif box == 'distilbert-base-uncased-finetuned-sst-2-english':
                if p['label'] == "POSITIVE":
                    col2.success(f"{p['label']}")
                    col3.success(f"{round(p['score'] * 100, 1)}%")
                else:
                    col2.error(f"{p['label']}")
                    col3.error(f"{round(p['score'] * 100, 1)}%")
            else:
                if p['label'] == "POSITIVE":
                    col2.success(f"{p['label']}")
                    col3.success(f"{round(p['score'] * 100, 1)}%")
                else:
                    col2.error(f"{p['label']}")
                    col3.error(f"{round(p['score'] * 100, 1)}%")