thangylvp commited on
Commit
cdb1d5c
·
verified ·
1 Parent(s): 633902c

Add dataset loading script

Browse files
Files changed (1) hide show
  1. test500.py +105 -0
test500.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import tarfile
3
+ import json
4
+ from collections import defaultdict
5
+ import glob
6
+ import os
7
+
8
+ from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Features, Value
9
+
10
+
11
+ TEXT_EXTS = {".txt", ".json"}
12
+
13
+
14
+ class Test500(GeneratorBasedBuilder):
15
+ VERSION = "1.0.0"
16
+
17
+ def _info(self):
18
+ # Flexible schema: features resolved at runtime
19
+ return DatasetInfo(
20
+ description="Sample-level tar dataset (grouped by channel + sample_id)",
21
+ features=Features(
22
+ {
23
+ "channel": Value("string"),
24
+ "id": Value("string"),
25
+ "raw_audio": Value("binary"),
26
+ "transcribe_assembly": Value("string"),
27
+ "tokenize_vibevoice": Value("binary"),
28
+ }
29
+ ),
30
+ )
31
+
32
+ def _split_generators(self, dl_manager):
33
+ data_dir = self.config.data_dir
34
+ tar_files = sorted(glob.glob(os.path.join(data_dir, "batch_*.tar")))
35
+
36
+ if not tar_files:
37
+ raise FileNotFoundError(f"No tar files found in {data_dir}")
38
+
39
+ return [
40
+ SplitGenerator(
41
+ name="train",
42
+ gen_kwargs={"tar_files": tar_files},
43
+ )
44
+ ]
45
+
46
+ def _generate_examples(self, tar_files):
47
+ idx = 0
48
+
49
+ # buffer for building one sample at a time
50
+ current = None
51
+ current_key = None
52
+
53
+ for tar_path in tar_files:
54
+ with tarfile.open(tar_path, "r:*") as tar:
55
+ for member in tar:
56
+ if not member.isfile():
57
+ continue
58
+
59
+ f = tar.extractfile(member)
60
+ if f is None:
61
+ continue
62
+
63
+ name = member.name
64
+ data = f.read()
65
+
66
+ # -------- PARSE NAME --------
67
+ # format: channel-sample_id-feature.ext
68
+ try:
69
+ channel, sample_id, rest = name.split("-", 2)
70
+ feature, ext = rest.rsplit(".", 1)
71
+ ext = "." + ext
72
+ except ValueError:
73
+ # skip malformed files
74
+ continue
75
+ # print(channel, sample_id, feature, ext)
76
+ key = (channel, sample_id)
77
+
78
+ # -------- FLUSH PREVIOUS SAMPLE --------
79
+ if current_key is not None and key != current_key:
80
+ yield idx, current
81
+ idx += 1
82
+ current = None
83
+
84
+ # -------- INIT SAMPLE --------
85
+ if current is None:
86
+ current_key = key
87
+ current = {
88
+ "channel": channel,
89
+ "id": sample_id,
90
+ }
91
+
92
+ # -------- LOAD FEATURE --------
93
+
94
+ if ext in TEXT_EXTS:
95
+ try:
96
+ current[feature] = data.decode("utf-8")
97
+ except Exception:
98
+ current[feature] = data
99
+ else:
100
+ current[feature] = data
101
+
102
+
103
+ # -------- FINAL SAMPLE --------
104
+ if current is not None:
105
+ yield idx, current