kiptoozeff commited on
Commit
f17f407
1 Parent(s): a67640c

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +88 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #import the model
11
+ tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
12
+
13
+ model_path = f"penscola/tweet_sentiments_analysis_distilbert"
14
+ config = AutoConfig.from_pretrained(model_path)
15
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
16
+ #Set the page configs
17
+ st.set_page_config(page_title='Sentiments Analysis',page_icon='😎',layout='wide')
18
+
19
+ #welcome Animation
20
+ com.iframe("https://embed.lottiefiles.com/animation/149093")
21
+ st.markdown('<h1> Tweet Sentiments </h1>',unsafe_allow_html=True)
22
+
23
+ #Create a form to take user inputs
24
+ with st.form(key='tweet',clear_on_submit=True):
25
+ 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')
26
+ submit=st.form_submit_button('submit')
27
+
28
+ #create columns to show outputs
29
+ col1,col2,col3=st.columns(3)
30
+ col1.title('Sentiment Emoji')
31
+ col2.title('How this user feels about the vaccine')
32
+ col3.title('Confidence of this prediction')
33
+
34
+ if submit:
35
+ print('submitted')
36
+ #pass text to preprocessor
37
+ def preprocess(text):
38
+ #initiate an empty list
39
+ new_text = []
40
+ #split text by space
41
+ for t in text.split(" "):
42
+ #set username to @user
43
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
44
+ #set tweet source to http
45
+ t = 'http' if t.startswith('http') else t
46
+ #store text in the list
47
+ new_text.append(t)
48
+ #change text from list back to string
49
+ return " ".join(new_text)
50
+
51
+
52
+ #pass text to model
53
+
54
+ #change label id
55
+ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
56
+
57
+ text = preprocess(text)
58
+
59
+ # PyTorch-based models
60
+ encoded_input = tokenizer(text, return_tensors='pt')
61
+ output = model(**encoded_input)
62
+ scores = output[0][0].detach().numpy()
63
+ scores = softmax(scores)
64
+
65
+ #Process scores
66
+ ranking = np.argsort(scores)
67
+ ranking = ranking[::-1]
68
+ l = config.id2label[ranking[0]]
69
+ s = scores[ranking[0]]
70
+
71
+ #output
72
+ if l=='NEGATIVE':
73
+ with col1:
74
+ com.iframe("https://embed.lottiefiles.com/animation/125694")
75
+ col2.write('Negative')
76
+ col3.write(f'{s}%')
77
+ elif l=='POSITIVE':
78
+ with col1:
79
+ com.iframe("https://embed.lottiefiles.com/animation/148485")
80
+ col2.write('Positive')
81
+ col3.write(f'{s}%')
82
+ else:
83
+ with col1:
84
+ com.iframe("https://embed.lottiefiles.com/animation/136052")
85
+ col2.write('Neutral')
86
+ col3.write(f'{s}%')
87
+
88
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ transformers[torch]
4
+ Scipy