-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
76 lines (50 loc) · 2.21 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from torch import nn, optim
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F
from transformers import AutoTokenizer, BertModel, BertTokenizer, AdamW, get_linear_schedule_with_warmup
from models import CyberbullyingClassifier
from config import *
# DATASET_NAME = "clean_hatespeech_text_label_vote.csv"
# RANDOM_STATE = 42
tokenizer = AutoTokenizer.from_pretrained(PRE_TRAINED_MODEL_NAME)
def get_predictions(model, data_loader):
model = model.eval()
review_texts = []
predictions = []
prediction_probs = []
real_values = []
with torch.no_grad():
for d in data_loader:
texts = d["text"]
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
probs = F.softmax(outputs, dim=1)
review_texts.extend(texts)
predictions.extend(preds)
prediction_probs.extend(probs)
real_values.extend(targets)
predictions = torch.stack(predictions).cpu()
prediction_probs = torch.stack(prediction_probs).cpu()
real_values = torch.stack(real_values).cpu()
return review_texts, predictions, prediction_probs, real_values
if __name__ == "__main__":
best_model = CyberbullyingClassifier(PRE_TRAINED_MODEL_NAME, n_classes=len(CLASS_NAMES))
best_model.load_state_dict(torch.load(SAVED_MODEL_NAME, map_location=torch.device(device)))
best_model = best_model.to(device)
print("\n==========================================================\n")
# """userid Why are there a lot 3rd gen kpop fans so.. fucking stupid? Saying tvxq, bigbang, t-ara, wonder girls"""
review_text = input("Enter a text: ")
encoded_review = tokenizer(review_text, padding=MAX_LEN, truncation=True, return_tensors="pt")
input_ids = encoded_review['input_ids'].to(device)
attention_mask = encoded_review['attention_mask'].to(device)
output = best_model(input_ids, attention_mask)
_, prediction = torch.max(output, dim=1)
print(f'Review text: {review_text}')
print(f'Sentiment : {CLASS_NAMES[prediction]}')