ynp3 commited on
Commit
4d4af75
·
1 Parent(s): c547ed9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +288 -0
app.py CHANGED
@@ -0,0 +1,288 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+ from torch.utils.data import Dataset, DataLoader
8
+ from transformers import AutoTokenizer,AutoModel
9
+ import random
10
+ from bs4 import BeautifulSoup
11
+ import re
12
+ from nltk.corpus import stopwords
13
+ from nltk.stem import WordNetLemmatizer
14
+ import nltk.data
15
+ import nltk
16
+
17
+ from transformers import AutoModelForSequenceClassification
18
+ import pytorch_lightning as pl
19
+
20
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
21
+
22
+ train_path = "train.csv"
23
+ test_path = "test.csv"
24
+ test_labels_paths = "test_labels.csv"
25
+ test_df = pd.read_csv(test_path)
26
+ test_labels_df = pd.read_csv(test_labels_paths)
27
+ test_df = pd.concat([test_df.iloc[:, 1], test_labels_df.iloc[:, 1:]], axis = 1)
28
+ test_df.to_csv("test-dataset.csv")
29
+ test_dataset_path = "test-dataset.csv"
30
+
31
+ #Lets make a new column labeled "healthy"
32
+
33
+ def healthy_filter(df):
34
+ if (df["toxic"]==0) and (df["severe_toxic"]==0) and (df["obscene"]==0) and (df["threat"]==0) and (df["insult"]==0) and (df["identity_hate"]==0):
35
+ return 1
36
+ else:
37
+ return 0
38
+
39
+ attributes = ['toxic', 'severe_toxic', 'obscene', 'threat',
40
+ 'insult', 'identity_hate', 'healthy']
41
+
42
+ class Comments_Dataset(Dataset):
43
+ def __init__(self, data_path, tokenizer, attributes, max_token_len=128, sample=5000):
44
+ self.data_path = data_path
45
+ self.tokenizer = tokenizer
46
+ self.attributes = attributes
47
+ self.max_token_len = max_token_len
48
+ self.sample = sample
49
+ self._prepare_data()
50
+
51
+ def _prepare_data(self):
52
+ data = pd.read_csv(self.data_path)
53
+ data["healthy"] = data.apply(healthy_filter, axis=1)
54
+ data["unhealthy"] = np.where(data['healthy'] == 1, 0, 1)
55
+ if self.sample is not None:
56
+ unhealthy = data.loc[data["healthy"] == 0]
57
+ healthy = data.loc[data["healthy"] == 1]
58
+ self.data = pd.concat([unhealthy, healthy.sample(self.sample, random_state=42)])
59
+ else:
60
+ self.data = data
61
+
62
+ def __len__(self):
63
+ return len(self.data)
64
+
65
+ def __getitem__(self, index):
66
+ item = self.data.iloc[index]
67
+ comment = str(item.comment_text)
68
+ attributes = torch.FloatTensor(item[self.attributes])
69
+ tokens = self.tokenizer.encode_plus(comment,
70
+ add_special_tokens=True,
71
+ return_tensors='pt',
72
+ truncation=True,
73
+ padding='max_length',
74
+ max_length=self.max_token_len,
75
+ return_attention_mask=True)
76
+ return {'input_ids': tokens.input_ids.flatten(), 'attention_mask': tokens.attention_mask.flatten(),
77
+ 'labels': attributes}
78
+
79
+ class Comments_Data_Module(pl.LightningDataModule):
80
+
81
+ def __init__(self, train_path, val_path, attributes, batch_size: int = 16, max_token_length: int = 128, model_name='roberta-base'):
82
+ super().__init__()
83
+ self.train_path = train_path
84
+ self.val_path = val_path
85
+ self.attributes = attributes
86
+ self.batch_size = batch_size
87
+ self.max_token_length = max_token_length
88
+ self.model_name = model_name
89
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
90
+
91
+ def setup(self, stage = None):
92
+ if stage in (None, "fit"):
93
+ self.train_dataset = Comments_Dataset(self.train_path, attributes=self.attributes, tokenizer=self.tokenizer)
94
+ self.val_dataset = Comments_Dataset(self.val_path, attributes=self.attributes, tokenizer=self.tokenizer, sample=None)
95
+ if stage == 'predict':
96
+ self.val_dataset = Comments_Dataset(self.val_path, attributes=self.attributes, tokenizer=self.tokenizer, sample=None)
97
+
98
+ def train_dataloader(self):
99
+ return DataLoader(self.train_dataset, batch_size = self.batch_size, num_workers=4, shuffle=True)
100
+
101
+ def val_dataloader(self):
102
+ return DataLoader(self.val_dataset, batch_size = self.batch_size, num_workers=4, shuffle=False)
103
+
104
+ def predict_dataloader(self):
105
+ return DataLoader(self.val_dataset, batch_size = self.batch_size, num_workers=4, shuffle=False)
106
+
107
+ comments_data_module = Comments_Data_Module(train_path, test_dataset_path, attributes=attributes)
108
+ comments_data_module.setup()
109
+ comments_data_module.train_dataloader()
110
+
111
+ class Comment_Classifier(pl.LightningModule):
112
+ #the config dict has the hugginface parameters in it
113
+ def __init__(self, config: dict):
114
+ super().__init__()
115
+ self.config = config
116
+ self.pretrained_model = AutoModel.from_pretrained(config['model_name'], return_dict = True)
117
+ self.hidden = torch.nn.Linear(self.pretrained_model.config.hidden_size, self.pretrained_model.config.hidden_size)
118
+ self.classifier = torch.nn.Linear(self.pretrained_model.config.hidden_size, self.config['n_labels'])
119
+ torch.nn.init.xavier_uniform_(self.classifier.weight)
120
+ self.loss_func = nn.CrossEntropyLoss()
121
+ self.dropout = nn.Dropout()
122
+
123
+ def forward(self, input_ids, attention_mask, labels=None):
124
+ # roberta layer
125
+ output = self.pretrained_model(input_ids=input_ids, attention_mask=attention_mask)
126
+ pooled_output = torch.mean(output.last_hidden_state, 1)
127
+ # final logits / classification layers
128
+ pooled_output = self.dropout(pooled_output)
129
+ pooled_output = self.hidden(pooled_output)
130
+ pooled_output = F.relu(pooled_output)
131
+ pooled_output = self.dropout(pooled_output)
132
+ logits = self.classifier(pooled_output)
133
+ # calculate loss
134
+ loss = 0
135
+ if labels is not None:
136
+ loss = self.loss_func(logits.view(-1, self.config['n_labels']), labels.view(-1, self.config['n_labels']))
137
+ return loss, logits
138
+
139
+ def training_step(self, batch, batch_index):
140
+ loss, outputs = self(**batch)
141
+ self.log("train loss ", loss, prog_bar = True, logger=True)
142
+ return {"loss":loss, "predictions":outputs, "labels": batch["labels"]}
143
+
144
+ def validation_step(self, batch, batch_index):
145
+ loss, outputs = self(**batch)
146
+ self.log("validation loss ", loss, prog_bar = True, logger=True)
147
+ return {"val_loss": loss, "predictions":outputs, "labels": batch["labels"]}
148
+
149
+ def predict_step(self, batch, batch_index):
150
+ loss, outputs = self(**batch)
151
+ return outputs
152
+
153
+ def configure_optimizers(self):
154
+ optimizer = AdamW(self.parameters(), lr=self.config['lr'], weight_decay=self.config['weight_decay'])
155
+ total_steps = self.config['train_size']/self.config['batch_size']
156
+ warmup_steps = math.floor(total_steps * self.config['warmup'])
157
+ scheduler = get_cosine_schedule_with_warmup(optimizer, warmup_steps, total_steps)
158
+ return [optimizer],[scheduler]
159
+
160
+
161
+ # def validation_epoch_end(self, outputs):
162
+ # losses = []
163
+ # for output in outputs:
164
+ # loss = output['val_loss'].detach().cpu()
165
+ # losses.append(loss)
166
+ # avg_loss = torch.mean(torch.stack(losses))
167
+ # self.log("avg_val_loss", avg_loss)
168
+
169
+ config = {
170
+ 'model_name': 'distilroberta-base',
171
+ 'n_labels': len(attributes),
172
+ 'batch_size': 128,
173
+ 'lr': 1.5e-6,
174
+ 'warmup': 0.2,
175
+ 'train_size': len(comments_data_module.train_dataloader()),
176
+ 'weight_decay': 0.001,
177
+ 'n_epochs': 100
178
+ }
179
+
180
+ ##tokenizer
181
+ model_name = 'distilroberta-base'
182
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
183
+
184
+ model = Comment_Classifier(config=config)
185
+ model.load_state_dict(torch.load("model_state_dict.pt"))
186
+ model.eval()
187
+
188
+ # lemmatizing tool
189
+ lemmatizer = WordNetLemmatizer()
190
+
191
+ def prepare_tokenized_review(raw_review):
192
+ # Remove HTML tags with BS
193
+ review_text = BeautifulSoup(raw_review).get_text()
194
+ # Removing non-letters using a regular expression
195
+ review_text = re.sub("[^a-zA-Z!?]"," ", review_text)
196
+ # Convert words to lower case and split them
197
+ words = review_text.lower().split()
198
+ # Remove stop-words
199
+ # stops = set(stopwords.words("english"))
200
+ # words = [w for w in words if not w in stops]
201
+ # Lemmatize the word list
202
+ lemmatized = []
203
+ for word in words:
204
+ lemmatized.append(lemmatizer.lemmatize(word))
205
+ return " ".join(lemmatized)
206
+
207
+ def get_encodings(text):
208
+ MAX_LEN=256
209
+ encodings = tokenizer.encode_plus(
210
+ text,
211
+ None,
212
+ add_special_tokens=True,
213
+ max_length=MAX_LEN,
214
+ padding='max_length',
215
+ truncation=True,
216
+ return_attention_mask=True,
217
+ return_tensors='pt')
218
+ return encodings
219
+
220
+ def run_inference(encoding):
221
+ with torch.no_grad():
222
+ input_ids = encoding['input_ids'].to(device, dtype=torch.long)
223
+ attention_mask = encoding['attention_mask'].to(device, dtype=torch.long)
224
+ output = model(input_ids, attention_mask)
225
+ final_output = torch.softmax(output[1][0],dim=0).cpu()
226
+ print(final_output.numpy().tolist())
227
+ return final_output.numpy().tolist()
228
+
229
+ # test_encoding = get_encodings("I think you are wonderful and deserve the best!")
230
+ # test_result = run_inference(test_encoding)
231
+
232
+ test_tweets = test_df["comment_text"].values
233
+ #streamlit section
234
+ models = ["distilroberta-base"]
235
+ model_pointers = ["default: distilroberta-base"]
236
+
237
+ # current_random_tweet = test_tweets[random.randint(0,len(test_tweets))]
238
+ # current_random_tweet = prepare_tokenized_review(current_random_tweet)
239
+ st.write("1. Hit the button to view and see the analyis of a random tweet")
240
+
241
+
242
+ # current_random_tweet = test_tweets[random.randint(0,len(test_tweets))]
243
+ # current_random_tweet = prepare_tokenized_review(current_random_tweet)
244
+ # st.write(current_random_tweet)
245
+ # if st.button("New Tweet"):
246
+ # st.experimental_rerun()
247
+
248
+
249
+
250
+ #######################################
251
+
252
+ # st.write(device)
253
+ # st.write(test_result)
254
+ # st.write(attributes[test_result.index(max(test_result))])
255
+ with st.form(key="init_form"):
256
+ current_random_tweet = test_tweets[random.randint(0,len(test_tweets))]
257
+ current_random_tweet = prepare_tokenized_review(current_random_tweet)
258
+ # st.write(current_random_tweet)
259
+
260
+
261
+ choice = st.selectbox("Choose Model", model_pointers)
262
+
263
+ # The index of choice in model_pointers will access the models list
264
+ # and select the Hugging Face model path at index.
265
+ user_picked_model = models[model_pointers.index(choice)]
266
+ with st.spinner("Analyzing..."):
267
+ text_encoding = get_encodings(current_random_tweet)
268
+ result = run_inference(text_encoding)
269
+ df = pd.DataFrame({"Tweet":current_random_tweet}, index=[0])
270
+ df["Highest Toxicity Class"] = attributes[result.index(max(result))]
271
+ df["Sentiment Score"] = max(result)
272
+ st.table(df)
273
+ # st.write(input_text)
274
+ # st.write(text_encoding)
275
+ # st.write(result)
276
+ # st.write(attributes[result.index(max(result))])
277
+
278
+ next_tweet = st.form_submit_button("Next Tweet")
279
+
280
+ if next_tweet:
281
+ with st.spinner("Analyzing..."):
282
+ st.write("")
283
+ # text_encoding = get_encodings(current_random_tweet)
284
+ # result = run_inference(text_encoding)
285
+ # # st.write(input_text)
286
+ # # st.write(text_encoding)
287
+ # st.write(result)
288
+ # st.write(attributes[result.index(max(result))])