-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_dataset.py
56 lines (44 loc) · 1.83 KB
/
split_dataset.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
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2023/12/3 15:58
# @Author : Roger
# @Version : V 0.1
# @Email : [email protected]
# @File : split_dataset.py
import json
import os
def merge_jsonl(input_files, output_file):
with open(output_file, 'w', encoding='utf-8') as output:
for input_file in input_files:
with open(input_file, 'r', encoding='utf-8') as f:
output.write(f.read())
def split_jsonl(input_file, output_prefix, max_file_size):
with open(input_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
current_file_size = 0
current_file_index = 1
current_output_file = f"{output_prefix}_{current_file_index}.jsonl"
with open(current_output_file, 'w', encoding='utf-8') as f:
for line in lines:
line_size = len(line.encode('utf-8'))
if current_file_size + line_size > max_file_size:
current_file_index += 1
current_file_size = 0
current_output_file = f"{output_prefix}_{current_file_index}.jsonl"
f.close()
f = open(current_output_file, 'w', encoding='utf-8')
f.write(line)
current_file_size += line_size
if __name__ == '__main__':
root = 'data/sample'
# 输入的JSONL文件列表
input_files = [
os.path.join(root, 'moss-002-sft-data.json'),
os.path.join(root, 'moss-003-sft-data.json'),
os.path.join(root, 'moss-003-sft-data-plugin.json')
]
merged_file = os.path.join(root, 'merged.jsonl') # 合并后的JSONL文件
output_prefix = os.path.join(root, "moss_sft") # 输出文件的前缀
max_file_size = 500 * 1024 * 1024 # 指定的最大文件大小,这里设置为1MB
merge_jsonl(input_files, merged_file)
split_jsonl(merged_file, output_prefix, max_file_size)