-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
195 lines (150 loc) · 6.04 KB
/
predict.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import argparse
import json
from pathlib import Path
from typing import List
import torch
from transformers import BertTokenizer, BertConfig
from configs import Config
from models import JointBert
class Predictor(object):
args: Config
model: JointBert
tokenizer: BertTokenizer
_intent_list:List[str]
_slot_list:List[str]
def __init__(self, args:Config):
configuration = BertConfig()
self.model = JointBert(config=configuration, args=args)
model_path = output_dir / 'checkpoint.pth'
self.model.soft_load_from_pretrained(model_path=str(model_path))
self.tokenizer = BertTokenizer.from_pretrained(args.bert_model_name, model_max_length=args.max_sentence_length)
self._intent_list = args.intents_list
self._slot_list = args.tags_list
def intent_prediction_to_label(self, predictions: torch.tensor):
intents = []
for i, x in enumerate(predictions):
intents.append(self._intent_list[x.item()])
return intents
def slot_prediction_to_label(self, predictions: torch.tensor):
sentences = []
for i, s in enumerate(predictions):
sentence = []
for j, w in enumerate(s):
slot_idx = w.item()
if slot_idx != 0:
sentence.append(self._slot_list[slot_idx])
sentences.append(sentence)
return sentences
def clean_output_tokens(self, slot_token: str):
slot_token = slot_token.replace(" ##", "")
return slot_token
def _convert_sentence_to_output_format(self, sentence: str, intent: str, slots: List[str]):
tokens = self.tokenizer.tokenize(sentence)
assert len(tokens) == len(slots)
slot_val = list()
slot_key = list()
slot_token = ""
current_slot = ""
for i, slot in enumerate(slots):
if slot != 'O':
label = slot[2:]
if label == current_slot:
slot_token += " " + tokens[i]
else:
if slot_token != "" and current_slot != "":
slot_val.append(slot_token)
slot_key.append(current_slot)
slot_token = tokens[i]
current_slot = label
elif slot_token != "" and current_slot != "":
slot_val.append(slot_token)
slot_key.append(current_slot)
slot_token = ""
current_slot = ""
if slot_token != "" and current_slot != "":
slot_val.append(slot_token)
slot_key.append(current_slot)
slot_dict = dict()
for s in range(len(slot_val)):
slot_dict[slot_key[s].lower()] = self.clean_output_tokens(slot_val[s])
return {
"intent": intent,
"text": sentence,
"slots": slot_dict,
}
@torch.no_grad()
def predict(self, sentences: List[str]):
self.model.eval()
inputs = self.tokenizer(
sentences,
padding="max_length",
truncation=True,
return_token_type_ids=True,
return_attention_mask=True,
add_special_tokens=True,
return_tensors="pt",
)
inputs.update({
"intent_label_ids": None,
"slot_labels_ids": None,
})
outputs = self.model(**inputs)
losses, (intent_logits, slot_logits) = outputs
intent_preds = torch.argmax(intent_logits.detach(), dim=1)
intents = self.intent_prediction_to_label(intent_preds)
slot_preds = torch.argmax(slot_logits.detach(), dim=2)
slots = self.slot_prediction_to_label(slot_preds)
output = []
for i, s in enumerate(sentences):
formatted = self._convert_sentence_to_output_format(s, intents[i], slots[i])
output.append(formatted)
return output
def predict_from_file(self, input_file:str, output_file:str):
with open(input_file, "r") as read_file:
input_data = json.load(read_file)
batch_size = self.args.batch_size
keys = list(input_data.keys())
key_chunks = [keys[x:x+batch_size] for x in range(0, len(keys), batch_size)]
outputs = dict()
for chunk in key_chunks:
sentences = []
for key in chunk:
sentences.append(input_data[key]['text'])
chunk_out = self.predict(sentences)
for i in range(len(chunk_out)):
outputs[chunk[i]] = chunk_out[i]
json_out = json.dumps(outputs, indent=4)
with open(output_file, 'w') as f:
f.write(json_out)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='JointBERT predictor',
description='Predict Intent and slots from input file sentences using Pretrained JointBERT model',
epilog='developed by Rajesh Baidya')
parser.add_argument('-m', '--model', help="Pretrained Model Directory", required=True)
parser.add_argument('-i', '--input', help="Input file for prediction. (See code for input file format)")
parser.add_argument('-o', '--output', help="Output file for the prediction.")
args = parser.parse_args()
samples = [
"I'm looking for a local cafeteria that has wifi accesss for a party of 4",
"book for one in Indiana at a restaurant",
"Add As I Was Going to St Ives to the fantasía playlist."
]
output_dir = Path(args.model)
config = Config.from_pretrained(str(output_dir))
config.device = 'cpu'
config.output_dir = output_dir
# print(args.__dict__)
predictor = Predictor(config)
if args.input == None:
output = predictor.predict(samples)
print(output)
else:
assert os.path.isfile(args.input)
input_file = Path(args.input)
if args.output == None:
output_file = input_file.parent / 'output.json'
else:
output_file = Path(args.output)
predictor.predict_from_file(str(input_file), str(output_file))