File size: 5,265 Bytes
c9afa60
 
 
f49e049
3514517
c9afa60
 
 
 
f49e049
 
 
c9afa60
f49e049
 
 
 
 
 
c9afa60
f49e049
c9afa60
 
 
 
 
f49e049
c9afa60
 
 
f49e049
c9afa60
f49e049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9afa60
f49e049
 
 
 
 
 
 
 
c9afa60
 
 
f49e049
c9afa60
 
 
 
 
 
 
f49e049
 
c9afa60
 
 
f49e049
 
 
 
 
c9afa60
 
 
 
 
 
 
 
 
 
 
 
 
f49e049
 
c9afa60
f49e049
 
c9afa60
 
f49e049
 
c9afa60
f49e049
 
 
c9afa60
f49e049
 
c9afa60
 
f49e049
 
c9afa60
f49e049
 
c9afa60
f49e049
c9afa60
f49e049
 
c9afa60
f49e049
c9afa60
 
 
 
f49e049
c9afa60
 
 
 
 
 
 
f49e049
 
 
 
 
 
 
 
 
 
c9afa60
 
 
 
f49e049
 
 
c9afa60
 
 
 
 
b2ae6d4
c9afa60
 
3514517
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import streamlit as st
import pandas as pd
import numpy as np
from PIL import Image
from utils import run_sentiment_analysis, preprocess
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification
import os
import time

# the two model trained
dstbt_model_path = "bright1/fine-tuned-distilbert-base-uncased" # distilbert model
rbta_model_path = "bright1/fine-tuned-twitter-Roberta-base-sentiment" # roberta model

# function to load model 
def load_model_components(model_path):
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    config = AutoConfig.from_pretrained(model_path)
    model = AutoModelForSequenceClassification.from_pretrained(model_path)
    return model, tokenizer, config

# configure page
st.set_page_config(
    page_title="Tweet Analyzer",
    page_icon="🤖",
    initial_sidebar_state="expanded",
    menu_items={
        'About': "# This is a Sentiment Analysis App. Call it the Covid Vaccine tweet Analyzer!"
    }
)  

# Define custom CSS style

# Apply custom CSS
# st.markdown("""<style> 
#         [data-testid="stAppViewContainer"] {
#             background-image: url("app\download.png");
#              background-attachment: fixed;
#             background-size: cover
#             }
# </style>""", unsafe_allow_html=True)



# create a sidebar and contents
st.sidebar.markdown("""
## Demo App

This app analyzes your tweets on covid vaccines and classifies them us Neutral, Negative or Positive
""")

# create a three column layout
model_type = st.sidebar.selectbox(label=':red[Select your model]', options=('distilbert', 'roberta'))
st.markdown("""<style> 
        [data-testid="stMarkdownContainer"] {
            font-size: 30px;
            font-weight: 800;
            }
</style>""", unsafe_allow_html=True)

# set a default model path
model_path = dstbt_model_path
if model_type == 'roberta':
    model_path = rbta_model_path


# create app interface 
my_expander  = st.container()

# st.sidebar.selectbox('Menu', ['About', 'Model'])
with my_expander:
    # center text in the container
    st.markdown("""
        <style>
        h1 {
            text-align: center;
        }
        </style>
        """, unsafe_allow_html=True)
    
    #set title for the app
    st.title(':green[Covid-19 Vaccines Tweets Analyzer]')


    # load model components
    model, tokenizer, config = load_model_components(model_path)


    # size columns
    col1, col2, col3 = st.columns((1.6, 1,0.3))
    # col2.markdown("""
    #         <p style= font-color:red>
    #             Results from Analyzer
    #         </p>
    # """,unsafe_allow_html=True)
    st.markdown("""
        <style>
        p {
            font-color: blue;
        }
        </style>
        """, unsafe_allow_html=True)
    
    # set textarea to receive tweet
    tweet = col1.text_area('Tweets to analyze',height=200, max_chars=520, placeholder='Write your Tweets here')

    # divide container into columns
    colA, colb, colc, cold = st.columns(4)
    clear_button = colA.button(label='Clear', type='secondary', use_container_width=True)

    # create a submit button
    submit_button = colb.button(label='Submit', type='primary', use_container_width=True)

    # set an empty container for the results
    empty_container = col2.container() # for progress bars
    empty_container.text("Results from Analyzer")

    empty_container2 = col3.container() # for scores
    empty_container2.text('Scores')
    text = preprocess(tweet)

    # run the analysis on the tweet
    results = run_sentiment_analysis(text=text, model=model, tokenizer=tokenizer)
    
    # when the tweet is submitted
    if submit_button:
        # print a success message 
        success_message = st.success('Success', icon="✅")
        time.sleep(3)
        success_message.empty()
        
        # create am expander to contain the results
        with empty_container:
            neutral = st.progress(value=results['Neutral'], text='Neutral',)
            negative = st.progress(value=results['Negative'], text='Negative')
            positive = st.progress(value=results['Positive'], text='Positive')

        with empty_container2:
            st.markdown(
                """
            <style>
            [data-testid="stMetricValue"] {
                font-size: 20px;
            }
            .st-ed {
                background-color: #FF4B4B;  
            
            }
            .st-ee {
                background-color: #1B9C85;
            }
            .st-eb {
                background-color: #FFD95A;
            }
            </style>
            """,
                unsafe_allow_html=True,
            )

            # class=""
            # dispay the scores with metric widget 
            neutral_score = st.metric(label='Score', value=round(results['Neutral'], 4), label_visibility='collapsed')
            negative_score = st.metric(label='Score', value=round(results['Negative'], 4), label_visibility='collapsed')
            positive_score = st.metric(label='Score', value=round(results['Positive'], 4), label_visibility='collapsed')
            time.sleep(5)
            success_message.empty()
    interpret_button = col2.button(label='Interpret',type='secondary', use_container_width=True)