-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference_gpt2.py
30 lines (21 loc) · 945 Bytes
/
inference_gpt2.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
import json
import re
from argparse import ArgumentParser
from simpletransformers.language_generation import LanguageGenerationModel
from tqdm import tqdm
def run_inference():
parser = ArgumentParser()
parser.add_argument("--model")
parser.add_argument("--evalpath")
parser.add_argument("--outpath")
args = parser.parse_args()
model = LanguageGenerationModel("gpt2", args.model)
with open(args.evalpath, 'r', encoding='utf8') as infile, open(args.outpath, 'w', encoding='utf8') as out:
for line in tqdm(infile):
example = json.loads(line)
prompt = example['input'] + "<SEP>"
sys_out = model.generate(prompt=prompt, args={"max_length": 128, "stop_token": "<|endoftext|>", "repetition_penalty": 10.0})
example['model_output'] = sys_out[0].replace(prompt, "")
print(json.dumps(example), file=out)
if __name__ == "__main__":
run_inference()