Jedida commited on
Commit
f267475
1 Parent(s): 6dd62b5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +90 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+
10
+
11
+
12
+ #import the model
13
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
14
+
15
+ model_path = f"Junr-syl/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.title('Sentiment Emoji')
33
+ col2.title('How this user feels about the vaccine')
34
+ col3.title('Confidence of this prediction')
35
+
36
+ if submit:
37
+ print('submitted')
38
+ #pass text to preprocessor
39
+ def preprocess(text):
40
+ #initiate an empty list
41
+ new_text = []
42
+ #split text by space
43
+ for t in text.split(" "):
44
+ #set username to @user
45
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
46
+ #set tweet source to http
47
+ t = 'http' if t.startswith('http') else t
48
+ #store text in the list
49
+ new_text.append(t)
50
+ #change text from list back to string
51
+ return " ".join(new_text)
52
+
53
+
54
+ #pass text to model
55
+
56
+ #change label id
57
+ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
58
+
59
+ text = preprocess(text)
60
+
61
+ # PyTorch-based models
62
+ encoded_input = tokenizer(text, return_tensors='pt')
63
+ output = model(**encoded_input)
64
+ scores = output[0][0].detach().numpy()
65
+ scores = softmax(scores)
66
+
67
+ #Process scores
68
+ ranking = np.argsort(scores)
69
+ ranking = ranking[::-1]
70
+ l = config.id2label[ranking[0]]
71
+ s = scores[ranking[0]]
72
+
73
+ #output
74
+ if l=='NEGATIVE':
75
+ with col1:
76
+ com.iframe("https://embed.lottiefiles.com/animation/125694")
77
+ col2.write('Negative')
78
+ col3.write(f'{s}%')
79
+ elif l=='POSITIVE':
80
+ with col1:
81
+ com.iframe("https://embed.lottiefiles.com/animation/148485")
82
+ col2.write('Positive')
83
+ col3.write(f'{s}%')
84
+ else:
85
+ with col1:
86
+ com.iframe("https://embed.lottiefiles.com/animation/136052")
87
+ col2.write('Neutral')
88
+ col3.write(f'{s}%')
89
+
90
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ transformers[torch]
4
+ Scipy