forked from nshepperd/gpt-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_bpe.py
41 lines (28 loc) · 1.1 KB
/
preprocess_bpe.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
#!/usr/bin/env python
# coding: utf-8
"""
Preprocess the given dataset using the BPE encoder from a pretrained model.
"""
import argparse
from collections import Counter
import glob
import json
import os
from tqdm import tqdm
from encoder import get_encoder
SPECIAL_TOKENS = ["<|endoftext|>"]
parser = argparse.ArgumentParser(
description="Preprocess the given dataset using the BPE encoder from a pretrained model.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--model_name", type=str, required=True)
parser.add_argument("--dataset", metavar="PATH", type=str, required=True)
parser.add_argument("-o", "--output", metavar="PATH", type=str, required=True,
help="Path to which to save preprocessed dataset.")
def main(args):
encoder = get_encoder(args.model_name)
with open(args.dataset, "r", encoding="utf-8") as f, \
open(args.output, "w", encoding="utf-8") as outf:
for line in f:
outf.write(" ".join(encoder.encode_to_strings(line)) + "\n")
if __name__ == "__main__":
main(parser.parse_args())