Junr-syl commited on
Commit
0aaefc8
Β·
1 Parent(s): 55973dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -40
app.py CHANGED
@@ -5,16 +5,18 @@ 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('Junr-syl/sentiments_analysis_Roberta')
 
14
 
15
- model_path = f"Junr-syl/sentiments_analysis_Roberta"
16
- config = AutoConfig.from_pretrained(model_path)
17
- model = AutoModelForSequenceClassification.from_pretrained(model_path)
18
 
19
  #Set the page configs
20
  st.set_page_config(page_title='Sentiments Analysis',page_icon='😎',layout='centered')
@@ -34,58 +36,61 @@ col1.title('Sentiment Emoji')
34
  col2.title('How this user feels about the vaccine')
35
  col3.title('Confidence of this prediction')
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*100:.2f}%')
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*100:.2f}%')
85
  else:
86
  with col1:
87
  com.iframe("https://embed.lottiefiles.com/animation/136052")
88
  col2.write('NEUTRAL')
89
- col3.write(f'{s*100:.2f}%')
90
 
91
 
 
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
  #import the model
14
+ pipe=pipeline(model="Junr-syl/sentiments_analysis_DISTILBERT")
15
+ # tokenizer = AutoTokenizer.from_pretrained('Junr-syl/sentiments_analysis_DISTILBERT')
16
 
17
+ # model_path = f"Junr-syl/sentiments_analysis_DISTILBERT"
18
+ # config = AutoConfig.from_pretrained(model_path)
19
+ # model = AutoModelForSequenceClassification.from_pretrained(model_path)
20
 
21
  #Set the page configs
22
  st.set_page_config(page_title='Sentiments Analysis',page_icon='😎',layout='centered')
 
36
  col2.title('How this user feels about the vaccine')
37
  col3.title('Confidence of this prediction')
38
 
39
+ # if submit:
40
+ # print('submitted')
41
+ # #pass text to preprocessor
42
+ # def preprocess(text):
43
+ # #initiate an empty list
44
+ # new_text = []
45
+ # #split text by space
46
+ # for t in text.split(" "):
47
+ # #set username to @user
48
+ # t = '@user' if t.startswith('@') and len(t) > 1 else t
49
+ # #set tweet source to http
50
+ # t = 'http' if t.startswith('http') else t
51
+ # #store text in the list
52
+ # new_text.append(t)
53
+ # #change text from list back to string
54
+ # return " ".join(new_text)
55
 
56
 
57
+ # #pass text to model
58
+ output=pipe(text)
59
+ output_dict=output[0]
60
+ lable=output_dict['label']
61
+ score=output_dict['score']
62
+ # #change label id
63
+ # #config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
64
 
65
+ # text = preprocess(text)
 
66
 
67
+ # # PyTorch-based models
68
+ # encoded_input = tokenizer(text, return_tensors='pt')
69
+ # output = model(**encoded_input)
70
+ # scores = output[0][0].detach().numpy()
71
+ # scores = softmax(scores)
72
 
73
+ # #Process scores
74
+ # ranking = np.argsort(scores)
75
+ # ranking = ranking[::-1]
76
+ # l = config.id2label[ranking[0]]
77
+ # s = scores[ranking[0]]
 
 
 
 
 
 
78
 
79
  #output
80
+ if lable=='NEGATIVE':
81
  with col1:
82
  com.iframe("https://embed.lottiefiles.com/animation/125694")
83
  col2.write('NEGATIVE')
84
+ col3.write(f'{score*100:.2f}%')
85
+ elif lable=='POSITIVE':
86
  with col1:
87
  com.iframe("https://embed.lottiefiles.com/animation/148485")
88
  col2.write('POSITIVE')
89
+ col3.write(f'{score*100:.2f}%')
90
  else:
91
  with col1:
92
  com.iframe("https://embed.lottiefiles.com/animation/136052")
93
  col2.write('NEUTRAL')
94
+ col3.write(f'{score*100:.2f}%')
95
 
96