-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (78 loc) · 2.65 KB
/
main.py
File metadata and controls
89 lines (78 loc) · 2.65 KB
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
import os
import json
import argparse
from tqdm import tqdm
from copy import copy
import logging
from data import StrategyQA, WikiMultiHopQA, HotpotQA, IIRC, BIOASQ, PubmedQA
from generate import *
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config_path", type=str, required=True)
args = parser.parse_args()
config_path = args.config_path
with open(config_path, "r") as f:
args = json.load(f)
args = argparse.Namespace(**args)
args.config_path = config_path
if "shuffle" not in args:
args.shuffle = False
if "use_counter" not in args:
args.use_counter = True
return args
def main():
args = get_args()
logger.info(f"{args}")
if os.path.exists(args.output_dir) is False:
os.makedirs(args.output_dir)
dir_name = os.listdir(args.output_dir)
for i in range(10000):
if str(i) not in dir_name:
args.output_dir = os.path.join(args.output_dir, str(i))
os.makedirs(args.output_dir)
break
logger.info(f"output dir: {args.output_dir}")
with open(os.path.join(args.output_dir, "config.json"), "w") as f:
json.dump(args.__dict__, f, indent=4)
output_file = open(os.path.join(args.output_dir, "output.txt"), "w")
# load data
if args.dataset == "strategyqa":
data = StrategyQA(args.data_path)
elif args.dataset == "2wikimultihopqa":
data = WikiMultiHopQA(args.data_path)
elif args.dataset == "hotpotqa":
data = HotpotQA(args.data_path)
elif args.dataset == "iirc":
data = IIRC(args.data_path)
elif args.dataset == "bioasq_7b_yesno":
data = BIOASQ(args.data_path)
elif args.dataset == "pubmedQA":
data = PubmedQA(args.data_path)
else:
raise NotImplementedError
data.format(fewshot=args.fewshot)
data = data.dataset
if args.shuffle:
data = data.shuffle()
if args.sample != -1:
samples = min(len(data), args.sample)
data = data.select(range(samples))
model = ETC(args)
logger.info("start inference")
for i in tqdm(range(len(data))):
last_counter = copy(model.counter)
entry = data[i]
pred = model.inference(entry["question"], entry["demo"], entry["case"])
pred = pred.strip()
ret = {
"qid": entry["qid"],
"prediction": pred,
}
if args.use_counter:
ret.update(model.counter.calc(last_counter))
output_file.write(json.dumps(ret)+"\n")
if __name__ == "__main__":
with torch.no_grad():
main()