-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_ans.py
134 lines (107 loc) · 4.02 KB
/
predict_ans.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
from transformers import pipeline
import tensorflow as tf
from logging import Formatter
from absl import app, logging, flags
import os
import json
from tqdm.autonotebook import tqdm
from text_gan.utils import SQuADReader
from text_gan import cfg, cfg_from_file
FLAGS = flags.FLAGS
flags.DEFINE_string("cfg", None, "Config YAML filepath")
flags.DEFINE_string("set", None, "train/dev set")
flags.DEFINE_string("predicted", None, "json file with predicted questions")
flags.DEFINE_string("out", None, "Output files' prefix")
def main(_):
if FLAGS.cfg is not None:
cfg_from_file(FLAGS.cfg)
if FLAGS.log_dir is not None and FLAGS.log_dir != "":
if not os.path.exists(FLAGS.log_dir):
os.makedirs(FLAGS.log_dir)
if not os.path.isdir(FLAGS.log_dir):
raise ValueError(f"{FLAGS.log_dir} should be a directory!")
logging.get_absl_handler().use_absl_log_file()
logging.get_absl_handler().setFormatter(
Formatter(fmt="%(levelname)s:%(message)s"))
if FLAGS.set is None or FLAGS.set not in ['train', 'dev']:
raise ValueError("Choose a set, train/ dev")
FLAGS.set = (
cfg.RAW_TRAIN_SAVE if FLAGS.set == 'train' else cfg.RAW_DEV_SAVE)
if FLAGS.out is None:
raise ValueError("Give an output filename prefix")
reader = SQuADReader()
data = reader.flatten_parsed(reader.parse(FLAGS.set, qids=True), qids=True)
with open(FLAGS.predicted, "r") as fp:
preds = json.load(fp)
nlp = pipeline("question-answering")
pred_ans = {}
orig_ans = {}
batch_size = 256
batch_context = []
batch_pred_question = []
batch_orig_question = []
batch_qid = []
for sample in tqdm(data):
pred_question = preds.get(sample['qid'], None)
if not pred_question:
continue
pred_question = pred_question[0]
pred_question = pred_question[:pred_question.find("EOS")]
batch_context.append(sample['context'])
batch_pred_question.append(pred_question)
batch_orig_question.append(sample['question'])
batch_qid.append(sample['qid'])
if len(batch_context) == batch_size:
try:
answers = nlp(
context=batch_context,
question=batch_pred_question)
for answer, qid in zip(answers, batch_qid):
pred_ans[qid] = answer['answer']
except Exception as e: # noqa
print("pred", e)
pass
try:
answers = nlp(
context=batch_context,
question=batch_orig_question)
for answer, qid in zip(answers, batch_qid):
orig_ans[qid] = answer['answer']
except Exception as e: # noqa
print("orig", e)
pass
batch_context = []
batch_pred_question = []
batch_orig_question = []
batch_qid = []
if len(batch_context) > 0:
try:
answers = nlp(
context=batch_context,
question=batch_pred_question)
for answer, qid in zip(answers, batch_qid):
pred_ans[qid] = answer['answer']
except Exception as e: # noqa
print("pred", e)
pass
try:
answers = nlp(
context=batch_context,
question=batch_orig_question)
for answer, qid in zip(answers, batch_qid):
orig_ans[qid] = answer['answer']
except Exception as e: # noqa
print("orig", e)
pass
with open(f"{FLAGS.out}-pred-ans.json", "w") as fp:
json.dump(pred_ans, fp)
with open(f"{FLAGS.out}-orig-ans.json", "w") as fp:
json.dump(orig_ans, fp)
if __name__ == "__main__":
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
tf.config.experimental.set_memory_growth(gpus[0], True)
except RuntimeError as e:
print(e)
app.run(main)