Jacob Jaroya commited on
Commit
5900ee7
β€’
1 Parent(s): 7da0cb3

commit app update

Browse files
Files changed (1) hide show
  1. app.py +43 -58
app.py CHANGED
@@ -5,85 +5,70 @@ from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoC
5
  import numpy as np
6
  #convert logits to probabilities
7
  from scipy.special import softmax
 
8
 
9
 
10
-
11
-
12
- #import the model
13
- tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
14
-
15
- model_path = f"UholoDala/tweet_sentiments_analysis"
16
- config = AutoConfig.from_pretrained(model_path)
17
- model = AutoModelForSequenceClassification.from_pretrained(model_path)
18
  #Set the page configs
19
  st.set_page_config(page_title='Sentiments Analysis',page_icon='😎',layout='wide')
20
 
21
  #welcome Animation
22
  com.iframe("https://embed.lottiefiles.com/animation/149093")
23
- st.markdown('<h1> Tweet Sentiments </h1>',unsafe_allow_html=True)
 
24
 
25
  #Create a form to take user inputs
26
  with st.form(key='tweet',clear_on_submit=True):
 
27
  text=st.text_area('Copy and paste a tweet or type one',placeholder='I find it quite amusing how people ignore the effects of not taking the vaccine')
28
- submit=st.form_submit_button('submit')
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  #create columns to show outputs
31
  col1,col2,col3=st.columns(3)
32
- col1.write('<h2 style="font-size: 24px;">Sentiment Emoji</h2>', unsafe_allow_html=True)
33
- col2.write('<h2 style="font-size: 24px;">How this user feels about the vaccine</h2>', unsafe_allow_html=True)
34
- col3.write('<h2 style="font-size: 24px;">Confidence of this prediction</h2>', unsafe_allow_html=True)
35
-
36
 
37
  if submit:
38
- print('submitted')
39
- #pass text to preprocessor
40
- def preprocess(text):
41
- #initiate an empty list
42
- new_text = []
43
- #split text by space
44
- for t in text.split(" "):
45
- #set username to @user
46
- t = '@user' if t.startswith('@') and len(t) > 1 else t
47
- #set tweet source to http
48
- t = 'http' if t.startswith('http') else t
49
- #store text in the list
50
- new_text.append(t)
51
- #change text from list back to string
52
- return " ".join(new_text)
53
 
54
-
55
- #pass text to model
56
-
57
- #change label id
58
- config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
59
-
60
- text = preprocess(text)
61
-
62
- # PyTorch-based models
63
- encoded_input = tokenizer(text, return_tensors='pt')
64
- output = model(**encoded_input)
65
- scores = output[0][0].detach().numpy()
66
- scores = softmax(scores)
67
-
68
- #Process scores
69
- ranking = np.argsort(scores)
70
- ranking = ranking[::-1]
71
- l = config.id2label[ranking[0]]
72
- s = scores[ranking[0]]
73
-
74
- #output
75
- if l=='NEGATIVE':
76
  with col1:
77
  com.iframe("https://embed.lottiefiles.com/animation/125694")
78
- col2.write('Negative')
79
- col3.write(f'{s:.2%}')
80
- elif l=='POSITIVE':
81
  with col1:
82
  com.iframe("https://embed.lottiefiles.com/animation/148485")
83
- col2.write('Positive')
84
- col3.write(f'{s:.2%}')
85
  else:
86
  with col1:
87
  com.iframe("https://embed.lottiefiles.com/animation/136052")
88
- col2.write('Neutral')
89
- col3.write(f'{s:.2%}')
 
5
  import numpy as np
6
  #convert logits to probabilities
7
  from scipy.special import softmax
8
+ from transformers import pipeline
9
 
10
 
 
 
 
 
 
 
 
 
11
  #Set the page configs
12
  st.set_page_config(page_title='Sentiments Analysis',page_icon='😎',layout='wide')
13
 
14
  #welcome Animation
15
  com.iframe("https://embed.lottiefiles.com/animation/149093")
16
+ st.markdown("<h1 style='text-align: center'> Covid Vaccine Tweet Sentiments </h1>",unsafe_allow_html=True)
17
+ st.write("<h2 style='font-size: 24px;'> These models were trained to detect how a user feels about the covid vaccines based on their tweets(text) </h2>",unsafe_allow_html=True)
18
 
19
  #Create a form to take user inputs
20
  with st.form(key='tweet',clear_on_submit=True):
21
+ #input text
22
  text=st.text_area('Copy and paste a tweet or type one',placeholder='I find it quite amusing how people ignore the effects of not taking the vaccine')
23
+ #Set examples
24
+ alt_text=st.selectbox("Can't Type? Select an Example below",('I hate the vaccines','Vaccines made from dead human tissues','Take the vaccines or regret the consequences','Covid is a Hoax','Making the vaccines is a huge step forward for humanity. Just take them'))
25
+ #Select a model
26
+ models={'Bert':'UholoDala/tweet_sentiments_analysis_bert',
27
+ 'Distilbert':'UholoDala/tweet_sentiments_analysis_distilbert',
28
+ 'Roberta':'UholoDala/tweet_sentiments_analysis_roberta'}
29
+ model=st.selectbox('Which model would you want to Use?',('Bert','Distilbert','Roberta'))
30
+ #Submit
31
+ submit=st.form_submit_button('Predict','Continue processing input')
32
+
33
+ selected_model=models[model]
34
+
35
 
36
  #create columns to show outputs
37
  col1,col2,col3=st.columns(3)
38
+ col1.write('<h2 style="font-size: 24px;"> Sentiment Emoji </h2>',unsafe_allow_html=True)
39
+ col2.write('<h2 style="font-size: 24px;"> How this user feels about the vaccine </h2>',unsafe_allow_html=True)
40
+ col3.write('<h2 style="font-size: 24px;"> Confidence of this prediction </h2>',unsafe_allow_html=True)
 
41
 
42
  if submit:
43
+ #Check text
44
+ if text=="":
45
+ text=alt_text
46
+ st.success(f"input text is set to '{text}'")
47
+ else:
48
+ st.success('Text received',icon='βœ…')
49
+
50
+ #import the model
51
+ pipe=pipeline(model=selected_model)
52
+
53
+ #pass text to model
54
+ output=pipe(text)
55
+ output_dict=output[0]
56
+ lable=output_dict['label']
57
+ score=output_dict['score']
58
 
59
+ #output
60
+ if lable=='NEGATIVE' or lable=='LABEL_0':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  with col1:
62
  com.iframe("https://embed.lottiefiles.com/animation/125694")
63
+ col2.write('NEGATIVE')
64
+ col3.write(f'{score:.2%}')
65
+ elif lable=='POSITIVE'or lable=='LABEL_2':
66
  with col1:
67
  com.iframe("https://embed.lottiefiles.com/animation/148485")
68
+ col2.write('POSITIVE')
69
+ col3.write(f'{score:.2%}')
70
  else:
71
  with col1:
72
  com.iframe("https://embed.lottiefiles.com/animation/136052")
73
+ col2.write('NEUTRAL')
74
+ col3.write(f'{score:.2%}')