-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
146 lines (132 loc) · 6.21 KB
/
convert.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
135
136
137
138
139
140
141
142
143
144
145
146
# booksum_en: chapter, instruction, output
# result_en: chapter, instruction, output
# result_jp: chapter, instruction, output
# baobabu: input, question, answer
"""
{
"context": <context>,
"conversations": [
{"from": "user", "value": <value>},
{"from": "assistant", "value": <value>}
]
}
"""
import json
import random
instruction_list = [
"この文を短くまとめてください。",
"この情報を手短に説明してください。",
"上記の内容について簡潔に教えてください。",
"これの要点だけ教えてもらえますか?",
"このコンテキストを簡単に説明していただけると助かります。",
"これを要約していただけますか?",
"コンパクトにお願いします。",
"この情報についてサクッと教えて",
"上記の文章を短くしてください。",
]
def process_and_write(file_path, context_key, instruction_key=None, output_key=None, random_instruction=False, count=0, first_flags=None):
global train_file, val_file, test_file
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
data = json.loads(line)
if random_instruction:
instruction = random.choice(instruction_list)
elif instruction_key:
instruction = data[instruction_key]
else:
instruction = random.choice(instruction_list)
new_data = {
"context": data[context_key],
"instruction": instruction,
"output": data[output_key]
}
final_data = {
"context": new_data["context"],
"conversations": [
{"from": "user", "value": new_data["instruction"]},
{"from": "assistant", "value": new_data["output"]}
]
}
# データの分割とファイルへの書き込み
if count % 330 == 0:
if not first_flags['test']:
test_file.write(",\n")
else:
first_flags['test'] = False
test_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
elif count % 20 == 0:
if not first_flags['val']:
val_file.write(",\n")
else:
first_flags['val'] = False
val_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
else:
if not first_flags['train']:
train_file.write(",\n")
else:
first_flags['train'] = False
train_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
count += 1
return count, first_flags
# 出力ファイルを開き、'['を書き込む
with open("data/train.json", "w", encoding="utf-8") as train_file, \
open("data/val.json", "w", encoding="utf-8") as val_file, \
open("data/test.json", "w", encoding="utf-8") as test_file:
train_file.write("[\n")
val_file.write("[\n")
test_file.write("[\n")
count = 0
first_flags = {'train': True, 'val': True, 'test': True}
# 各データファイルの処理
count, first_flags = process_and_write("data/booksum_en.jsonl", "chapter", "instruction", "output", count=count, first_flags=first_flags)
count, first_flags = process_and_write("data/result_en.jsonl", "chapter", "instruction", "output", count=count, first_flags=first_flags)
# result_jp.jsonlの処理(エラーハンドリング付き)
with open("data/result_jp.jsonl", "r", encoding="utf-8") as result_jp_file:
for idx, line in enumerate(result_jp_file):
try:
data = json.loads(line)
except json.JSONDecodeError:
print(f"JSONデコードエラーが発生しました。行番号: {idx + 1}")
continue
new_data = {
"context": data["chapter"],
"instruction": data["instruction"],
"output": data["output"]
}
final_data = {
"context": new_data["context"],
"conversations": [
{"from": "user", "value": new_data["instruction"]},
{"from": "assistant", "value": new_data["output"]}
]
}
# データの分割とファイルへの書き込み
if count % 330 == 0:
if not first_flags['test']:
test_file.write(",\n")
else:
first_flags['test'] = False
test_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
elif count % 20 == 0:
if not first_flags['val']:
val_file.write(",\n")
else:
first_flags['val'] = False
val_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
else:
if not first_flags['train']:
train_file.write(",\n")
else:
first_flags['train'] = False
train_file.write(json.dumps(final_data, ensure_ascii=False, indent=4))
count += 1
# baobabu.jsonlの処理
count, first_flags = process_and_write("data/baobabu.jsonl", "input", "question", "answer", count=count, first_flags=first_flags)
count, first_flags = process_and_write("data/aozora_inst.jsonl", "input", "instruction", "response", count=count, first_flags=first_flags)
count, first_flags = process_and_write("data/aozora_summary.jsonl", "input", output_key="generated", random_instruction=True, count=count, first_flags=first_flags)
count, first_flags = process_and_write("data/aozora_summary1.jsonl", "input", output_key="generated", random_instruction=True, count=count, first_flags=first_flags)
# 出力ファイルに']'を書き込む
train_file.write("\n]")
val_file.write("\n]")
test_file.write("\n]")
print(f"処理した合計アイテム数: {count}")