Gikubu
commited on
Commit
β’
5a61203
1
Parent(s):
e0ea527
model
Browse files
app.py
CHANGED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import streamlit.components.v1 as com
|
3 |
+
#import libraries
|
4 |
+
from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoConfig
|
5 |
+
import numpy as np
|
6 |
+
#convert logits to probabilities
|
7 |
+
from scipy.special import softmax
|
8 |
+
from transformers import pipeline
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
#Set the page configs
|
14 |
+
st.set_page_config(page_title='TWEET SENTIMENT ANALYSIS',page_icon='π€',layout='wide')
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
#welcome Animation
|
19 |
+
com.iframe("https://lottie.host/?file=8c9ae0c8-e9fc-4fc7-954e-16f922db889b/0BlrGUjJxw.json")
|
20 |
+
st.markdown("<h1 style='text-align: center'> TWEET SENTIMENT FOR COVID VACCINATION </h1>",unsafe_allow_html=True)
|
21 |
+
st.write("<h2 style='font-size: 24px;'> Text Classification Models developed to ascertain public perception of covid vaccines </h2>",unsafe_allow_html=True)
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
#Create a form to take user inputs
|
26 |
+
with st.form(key='tweet',clear_on_submit=True):
|
27 |
+
#input text
|
28 |
+
text=st.text_area('Please enter tweet of vaccine perception')
|
29 |
+
#Set examples
|
30 |
+
alt_text=st.selectbox("Choose any of the sample tweets",('-select-', 'Vaccines have been good so far', 'Had a bad experience with the vaccine', 'Covid is human made. The vaccines are deadly', 'Unqualified people administered vaccine', 'Vaccine is dangerous to women', 'Vaccine can kill people with anaemia', 'Vaccine protects us from the deadly virus'))
|
31 |
+
#Select a model
|
32 |
+
models={'Bert':'Gikubu/Gikubu_bert_base',
|
33 |
+
'Roberta': 'Gikubu/joe_roberta'}
|
34 |
+
model=st.selectbox('Select preferred model',('Bert','Roberta'))
|
35 |
+
#Submit
|
36 |
+
submit=st.form_submit_button('Predict','Continue processing input')
|
37 |
+
|
38 |
+
selected_model=models[model]
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
#create columns to show outputs
|
43 |
+
col1,col2,col3=st.columns(3)
|
44 |
+
col1.write('<h2 style="font-size: 24px;"> Sentiment Emoji </h2>', unsafe_allow_html=True)
|
45 |
+
col2.write('<h2 style="font-size: 24px;"> Vaccine Perception of User </h2>', unsafe_allow_html=True)
|
46 |
+
col3.write('<h2 style="font-size: 24px;"> Model Prediction Confidence </h2>', unsafe_allow_html=True)
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
if submit:
|
51 |
+
#Check text
|
52 |
+
if text=="":
|
53 |
+
text=alt_text
|
54 |
+
st.success(f"input text is set to '{text}'")
|
55 |
+
else:
|
56 |
+
st.success('Hey, tweet received', icon='ππΌ')
|
57 |
+
|
58 |
+
#import the model
|
59 |
+
pipe=pipeline(model=selected_model)
|
60 |
+
|
61 |
+
|
62 |
+
#pass text to model
|
63 |
+
output=pipe(text)
|
64 |
+
output_dict=output[0]
|
65 |
+
lable=output_dict['label']
|
66 |
+
score=output_dict['score']
|
67 |
+
|
68 |
+
#output
|
69 |
+
if lable=='NEGATIVE' or lable=='LABEL_0':
|
70 |
+
with col1:
|
71 |
+
com.iframe("https://lottie.host/?file=c8010531-31de-4dc8-8952-1aa854314455/NQNXZWPduv.json")
|
72 |
+
col2.write('NEGATIVE')
|
73 |
+
col3.write(f'{score*100:.2f}%')
|
74 |
+
elif lable=='POSITIVE'or lable=='LABEL_2':
|
75 |
+
with col1:
|
76 |
+
com.iframe("https://lottie.host/?file=51ba274f-064a-4d67-877b-159f4490a944/pBBe4CCH8e.json")
|
77 |
+
col2.write('POSITIVE')
|
78 |
+
col3.write(f'{score*100:.2f}%')
|
79 |
+
else:
|
80 |
+
with col1:
|
81 |
+
com.iframe("https://lottie.host/?file=4e8f4b09-bafb-4ff8-9749-2470c459dce1/v5FATJ9QVm.json")
|
82 |
+
col2.write('NEUTRAL')
|
83 |
+
col3.write(f'{score*100:.2f}%')
|