-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
54 lines (49 loc) · 2.05 KB
/
model.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
import argparse
import os
import torch
import random
import numpy as np
from transformers import DistilBertTokenizerFast
from transformers import DistilBertForQuestionAnswering
def init_args(context, question):
if context is None or question is None:
context = "Stephen Silvagni (born 31 May 1967) is a former Australian rules footballer for the Carlton Football Club."
question = "What was the name of Stephen Silvagni's team?"
args = {
'context': context,
'question': question,
'seed': 42,
'save_dir': 'save/baseline-01',
}
return args
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def predict(context, question):
args = init_args(context, question)
set_seed(args['seed'])
checkpoint_path = os.path.join(args['save_dir'], 'checkpoint')
model = DistilBertForQuestionAnswering.from_pretrained(checkpoint_path)
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
args['device'] = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
os.environ["TOKENIZERS_PARALLELISM"] = "false"
context = args['context']
question = args['question']
inputs = tokenizer(question, context, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs, )
start_index = outputs.start_logits.argmax()
end_index = outputs.end_logits.argmax()
start_idx = int(start_index.numpy())
end_idx = int(end_index.numpy())
predict_tokens = inputs.input_ids[0,start_index:end_index+1]
predict_answer = tokenizer.decode(predict_tokens)
result = {'context': context, 'question': question, 'start_idx': start_idx, 'end_idx': end_idx, 'answer': predict_answer}
print(result)
return result
if __name__ == '__main__':
context = "Stephen Silvagni (born 31 May 1967) is a former Australian rules footballer for the Carlton Football Club."
question = "What was the name of Stephen Silvagni's team?"
predict(context, question)