Winnie-Kay commited on
Commit
027c54f
·
1 Parent(s): d3f4b27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -25
app.py CHANGED
@@ -1,32 +1,27 @@
1
- import streamlit as st
2
 
3
- import transformers
4
 
5
- import torch
6
 
7
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
8
 
9
 
 
10
 
 
11
 
12
- # Define the paths of the pre-trained models
13
-
14
- model1_path = "saisi/finetuned-Sentiment-classfication-ROBERTA-Base-model"
15
-
16
- model2_path = "saisi/finetuned-Sentiment-classfication-DISTILBERT-model"
17
-
18
-
19
 
20
 
21
  # Initialize the tokenizer and models for sentiment analysis
22
 
23
- tokenizer1 = AutoTokenizer.from_pretrained(model1_path)
24
 
25
- model1 = AutoModelForSequenceClassification.from_pretrained(model1_path)
26
 
27
- tokenizer2 = AutoTokenizer.from_pretrained(model2_path)
28
 
29
- model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
30
 
31
 
32
 
@@ -34,67 +29,151 @@ model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
34
  # Define a function to preprocess the text data
35
 
36
  def preprocess(text):
 
37
  new_text = []
 
 
 
38
  for t in text.split(" "):
 
39
  t = '@user' if t.startswith('@') and len(t) > 1 else t
40
- # Replace links with 'http'
 
 
41
  t = 'http' if t.startswith('http') else t
42
- new_text.append(t)
43
- # Join the preprocessed text
44
 
45
- return " ".join(new_text)
46
 
 
47
 
 
48
 
49
 
50
  # Define a function to perform sentiment analysis on the input text using model 1
51
 
52
  def sentiment_analysis_model1(text):
 
 
 
53
  text = preprocess(text)
 
 
54
  # Tokenize the input text using the pre-trained tokenizer
 
55
  encoded_input = tokenizer1(text, return_tensors='pt')
 
 
56
  # Feed the tokenized input to the pre-trained model and obtain output
 
57
  output = model1(**encoded_input)
 
 
58
  # Obtain the prediction scores for the output
 
59
  scores_ = output[0][0].detach().numpy()
 
 
60
  # Apply softmax activation function to obtain probability distribution over the labels
 
61
  scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
 
 
62
  # Format the output dictionary with the predicted scores
 
63
  labels = ['Negative', 'Positive']
 
64
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
65
- # Return the scores
66
- return scores
67
 
68
 
 
 
 
69
 
70
 
71
  # Define a function to perform sentiment analysis on the input text using model 2
72
 
73
  def sentiment_analysis_model2(text):
 
 
 
74
  text = preprocess(text)
 
 
75
  # Tokenize the input text using the pre-trained tokenizer
76
- encoded_input = tokenizer1(text, return_tensors='pt')
 
 
 
77
  # Feed the tokenized input to the pre-trained model and obtain output
78
- output = model1(**encoded_input)
 
 
 
79
  # Obtain the prediction scores for the output
 
80
  scores_ = output[0][0].detach().numpy()
 
 
81
  # Apply softmax activation function to obtain probability distribution over the labels
 
82
  scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
 
 
83
  # Format the output dictionary with the predicted scores
84
- labels = ['Negative', 'Positive']
 
 
85
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
86
- # Return the scores
 
 
 
87
  return scores
88
 
89
 
90
  # Define the Streamlit app
 
91
  def app():
 
92
  # Define the app title
 
93
  st.title("Sentiment Analysis")
 
94
  # Define the input field
 
95
  text_input = st.text_input("Enter text:")
 
 
96
  # Define the model selection dropdown
 
97
  model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
 
 
98
  # Perform sentiment analysis when the submit button is clicked
 
99
  if st.button("Submit"):
100
- if text_input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ import streamlit as st
3
 
4
+ from PIL import Image
5
 
6
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
 
8
 
9
+ # Define the model names or identifiers
10
 
11
+ model1_name = "Winnie-Kay/Sentiment-Analysis-Roberta-bases"
12
 
13
+ model2_name = "Winnie-Kay/Finetuned_BertModel_SentimentAnalysis"
 
 
 
 
 
 
14
 
15
 
16
  # Initialize the tokenizer and models for sentiment analysis
17
 
18
+ tokenizer1 = AutoTokenizer.from_pretrained(model1_name)
19
 
20
+ model1 = AutoModelForSequenceClassification.from_pretrained(model1_name)
21
 
22
+ tokenizer2 = AutoTokenizer.from_pretrained(model2_name)
23
 
24
+ model2 = AutoModelForSequenceClassification.from_pretrained(model2_name)
25
 
26
 
27
 
 
29
  # Define a function to preprocess the text data
30
 
31
  def preprocess(text):
32
+
33
  new_text = []
34
+
35
+ # Replace user mentions with '@user'
36
+
37
  for t in text.split(" "):
38
+
39
  t = '@user' if t.startswith('@') and len(t) > 1 else t
40
+
41
+ # Replace links with 'http'
42
+
43
  t = 'http' if t.startswith('http') else t
 
 
44
 
45
+ new_text.append(t)
46
 
47
+ # Join the preprocessed text
48
 
49
+ return " ".join(new_text)
50
 
51
 
52
  # Define a function to perform sentiment analysis on the input text using model 1
53
 
54
  def sentiment_analysis_model1(text):
55
+
56
+ # Preprocess the input text
57
+
58
  text = preprocess(text)
59
+
60
+
61
  # Tokenize the input text using the pre-trained tokenizer
62
+
63
  encoded_input = tokenizer1(text, return_tensors='pt')
64
+
65
+
66
  # Feed the tokenized input to the pre-trained model and obtain output
67
+
68
  output = model1(**encoded_input)
69
+
70
+
71
  # Obtain the prediction scores for the output
72
+
73
  scores_ = output[0][0].detach().numpy()
74
+
75
+
76
  # Apply softmax activation function to obtain probability distribution over the labels
77
+
78
  scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
79
+
80
+
81
  # Format the output dictionary with the predicted scores
82
+
83
  labels = ['Negative', 'Positive']
84
+
85
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
 
 
86
 
87
 
88
+ # Return the scores
89
+
90
+ return scores
91
 
92
 
93
  # Define a function to perform sentiment analysis on the input text using model 2
94
 
95
  def sentiment_analysis_model2(text):
96
+
97
+ # Preprocess the input text
98
+
99
  text = preprocess(text)
100
+
101
+
102
  # Tokenize the input text using the pre-trained tokenizer
103
+
104
+ encoded_input = tokenizer2(text, return_tensors='pt')
105
+
106
+
107
  # Feed the tokenized input to the pre-trained model and obtain output
108
+
109
+ output = model2(**encoded_input)
110
+
111
+
112
  # Obtain the prediction scores for the output
113
+
114
  scores_ = output[0][0].detach().numpy()
115
+
116
+
117
  # Apply softmax activation function to obtain probability distribution over the labels
118
+
119
  scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
120
+
121
+
122
  # Format the output dictionary with the predicted scores
123
+
124
+ labels = ['Negative', 'Neutral', 'Positive']
125
+
126
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
127
+
128
+
129
+ # Return the scores
130
+
131
  return scores
132
 
133
 
134
  # Define the Streamlit app
135
+
136
  def app():
137
+
138
  # Define the app title
139
+
140
  st.title("Sentiment Analysis")
141
+
142
  # Define the input field
143
+
144
  text_input = st.text_input("Enter text:")
145
+
146
+
147
  # Define the model selection dropdown
148
+
149
  model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
150
+
151
+
152
  # Perform sentiment analysis when the submit button is clicked
153
+
154
  if st.button("Submit"):
155
+
156
+ if text_input:
157
+
158
+ if model_selection == "Model 1":
159
+
160
+ # Perform sentiment analysis using model 1
161
+
162
+ scores = sentiment_analysis_model1(text_input)
163
+
164
+ st.write(f"Model 1 predicted scores: {scores}")
165
+
166
+ else:
167
+
168
+ # Perform sentiment analysis using model 2
169
+
170
+ scores = sentiment_analysis_model2(text_input)
171
+
172
+ st.write(f"Model 2 predicted scores: {scores}")
173
+
174
+
175
+
176
+
177
+
178
+
179
+