-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
41 lines (35 loc) · 1.36 KB
/
cli.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
import sys
import time
import warnings
from transformers import RobertaForSequenceClassification, RobertaTokenizerFast
import torch
warnings.filterwarnings('ignore')
if torch.cuda.is_available():
DEVICE = torch.device("cuda")
else:
DEVICE = torch.device("cpu")
model = RobertaForSequenceClassification.from_pretrained("Elluran/Hate_speech_detector")
tokenizer = RobertaTokenizerFast.from_pretrained("Elluran/Hate_speech_detector")
THRESHOLD = 0.810126582278481
def clean_string(s):
s = s.encode('ASCII', 'ignore').decode()
s = s.lower()
s = ''.join([ch for ch in s if ch.isalpha() or ch == ' '])
s = " ".join(s.split())
if len(s) > 0 and s[-1] == ' ':
s = s[:-1]
if len(s) > 0 and s[0] == ' ':
s = s[1:]
return s
if __name__ == "__main__":
start_time = time.time()
input_string = clean_string(sys.argv[1])
tokens = tokenizer(input_string, padding=True, truncation=True, return_tensors="pt")
output = model(**tokens)
result = "hate speech" if torch.sigmoid(output[0]) > THRESHOLD else "regular tweet"
if torch.sigmoid(output[0]) > THRESHOLD:
confidence = torch.sigmoid(output[0]).item()
else:
confidence = 1 - torch.sigmoid(output[0]).item()
print(f"answer is: {result} with confidence: {'{:.2f}'.format(confidence)}")
print(time.time() - start_time, "time spent for tweet")