-
Notifications
You must be signed in to change notification settings - Fork 5
/
random_instruction.py
47 lines (37 loc) · 1.64 KB
/
random_instruction.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
import argparse
import json
from tqdm import tqdm
import random
def format_example(example: dict) -> dict:
instructions = ["将这句话改写成鲁迅风格的语言", "用鲁迅的风格改写", "你是一个非常熟悉鲁迅风格的作家,请用鲁迅的风格改写这句话", "用鲁迅的风格改写这句话"]
instruction = random.choice(instructions)
context = f"Instruction: {instruction}\n"
if example.get("Input"):
context += f"Input: {example['Input']}\n"
context += "Answer: "
target = example["Output"]
return {"context": context, "target": target}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str, default="example_data/selected_aug.jsonl")
parser.add_argument("--save_path", type=str, default="example_data/luxun_data.jsonl")
args = parser.parse_args()
print("#> Formatting dataset...")
print("#> Input path: {}".format(args.data_path))
print("#> Output path: {}".format(args.save_path))
random.seed(42)
if args.data_path.endswith(".json"):
with open(args.data_path) as f:
examples = json.load(f)
elif args.data_path.endswith(".jsonl"):
examples = []
with open(args.data_path) as f:
for line in f:
examples.append(json.loads(line))
with open(args.save_path, 'a+') as f:
for example in tqdm(examples, desc="formatting.."):
datapoint = format_example(example)
f.write(json.dumps(datapoint, ensure_ascii=False) + '\n')
print("#> Formatting finished!", "Total examples:", len(examples))
if __name__ == "__main__":
main()