Spaces:
Sleeping

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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -278
app.py CHANGED
@@ -1,288 +1,91 @@
 
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))])
 
1
+ # Import necessary libraries
2
  import streamlit as st
3
  import pandas as pd
4
  import numpy as np
5
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, BertForSequenceClassification, DistilBertModel
6
  import torch
7
+ from torch import cuda
 
8
  from torch.utils.data import Dataset, DataLoader
9
+ import finetuning
10
+ from finetuning import CustomDistilBertClass
11
+
12
+ # Define the pretrained models to be used
13
+ model_map = {
14
+ 'BERT': 'bert-base-uncased',
15
+ 'RoBERTa': 'roberta-base',
16
+ 'DistilBERT': 'distilbert-base-uncased'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
18
 
19
+ # Load dropdown options for model selection
20
+ model_options = list(model_map.keys())
21
+
22
+ # Load the training dataset
23
+ train_df = pd.read_csv('train.csv')
24
+ train_df = train_df.sample(n=256)
25
+ label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
26
+
27
+ # Define a function to load the pretrained BERT model
28
+ @st.cache_resource
29
+ def load_model(model_name):
30
+ """Load pretrained BERT model."""
31
+ path = "finetuned_model.pt"
32
+ model = torch.load(path)
33
+ tokenizer = AutoTokenizer.from_pretrained(model_map[model_name])
34
+ return model, tokenizer
35
+
36
+ # Define a function to classify text using the pretrained BERT model
37
+ def classify_text(model, tokenizer, text):
38
+ """Classify text using pretrained BERT model."""
39
+ inputs = tokenizer.encode_plus(
 
 
 
 
 
 
 
 
 
40
  text,
 
41
  add_special_tokens=True,
42
+ max_length=512,
43
  padding='max_length',
44
+ return_tensors='pt',
45
+ truncation=True
46
+ )
47
+ print(inputs)
48
+ with torch.no_grad():
49
+ logits = model(inputs['input_ids'],inputs['attention_mask'])[0]
50
+ probabilities = torch.softmax(logits, dim=1)[0]
51
+ pred_class = torch.argmax(probabilities, dim=0)
52
+ print(f"predicted class: {pred_class}")
53
+ print(probabilities[0].tolist())
54
+ return label_cols[pred_class], round(probabilities[0].tolist(),2)
55
+
56
+ # Set up Streamlit app
57
+ st.title('Toxic Comment Classifier')
58
+ model_name = st.sidebar.selectbox('Select a model', model_options)
59
+ st.sidebar.write('Selected:', model_name)
60
+ model, tokenizer = load_model(model_name)
61
+ print(type(model))
62
+
63
+ # Define input text area for user input
64
+ st.subheader('Enter comment below:')
65
+ text_input = st.text_area(label='', height=100, max_chars=500)
66
+
67
+ # Make prediction when user clicks 'Classify' button
68
+ if st.button('Classify Toxicity'):
69
+ if not text_input:
70
+ st.write('Please enter comment')
71
+ else:
72
+ class_label, class_prob = classify_text(model, tokenizer, text_input)
73
+ st.subheader('Results')
74
+ st.write('Comment:', text_input)
75
+ st.write('Highest Toxicity Class:', class_label)
76
+ st.write('Probability:', class_prob)
77
+
78
+ # Display table of results
79
+ st.subheader('Toxic Classification Results')
80
+ if 'classification_results' not in st.session_state:
81
+ st.session_state.classification_results = pd.DataFrame(columns=['comment', 'toxicity_class', 'probability'])
82
+ if st.button('Add to Results'):
83
+ if not text_input:
84
+ st.write('Please enter comment')
85
+ else:
86
+ class_label, class_prob = classify_text(model, tokenizer, text_input)
87
+ st.subheader('Results')
88
+ st.write('Comment:', text_input)
89
+ st.write('Highest Toxicity Class:', class_label)
90
+ st.write('Probability:', class_prob)
91
+ st.session_state.classification_results = st.session_state.classification