-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.py
59 lines (44 loc) · 1.64 KB
/
interactive.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
__version__ = '1.0.0-rc.1'
__author__ = 'Lorenzo Menghini, Martino Pulici, Alessandro Stockman, Luca Zucchini'
import argparse
import pandas as pd
import torch
from rate_severity_of_toxic_comments.model import create_model
from rate_severity_of_toxic_comments.utilities import parse_config, process_config
DEFAULT_CONFIG_FILE_PATH = 'config/default.json'
LOCAL_CONFIG_FILE_PATH = 'config/local.json'
BEST_MODELS_FILE_PATH = 'config/best_models.json'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_file')
args = parser.parse_args()
CONFIG = parse_config(DEFAULT_CONFIG_FILE_PATH, LOCAL_CONFIG_FILE_PATH)
support_bag = process_config(pd.DataFrame(), CONFIG)
run_mode = CONFIG['options']['run_mode']
device = torch.device('cuda' if torch.cuda.is_available()
and CONFIG['options']['use_gpu'] else 'cpu')
model = create_model(
run_mode,
CONFIG['training'],
CONFIG[run_mode],
support_bag)
model.load_state_dict(torch.load(args.model_file))
model.to(device)
query = True
while query:
query = input('Type comment:')
inputs = support_bag['tokenizer'](
query,
truncation=True,
add_special_tokens=True,
max_length=128,
padding='max_length'
)
ids = inputs['input_ids']
mask = inputs['attention_mask']
score = model(
ids.unsqueeze(
dim=0).to(device), mask.unsqueeze(
dim=0).to(device), torch.tensor(
[0]).to(device))
print('Score:', score.item())