Datasets:

ArXiv:
License:
xinjianl commited on
Commit
a533b56
1 Parent(s): f3843b9

Upload yodas.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. yodas.py +150 -0
yodas.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from collections import OrderedDict
3
+ from pathlib import Path
4
+ import datasets
5
+ import os
6
+ from .meta import lang2shard_cnt
7
+
8
+
9
+ class YodasConfig(datasets.BuilderConfig):
10
+ """BuilderConfig for Yodas."""
11
+
12
+ def __init__(self, lang, version, **kwargs):
13
+ self.language = lang
14
+ self.base_data_path = f"data/{lang}"
15
+
16
+ description = (
17
+ f"Youtube speech to text dataset in {self.language}."
18
+ )
19
+ super(YodasConfig, self).__init__(
20
+ name=lang,
21
+ version=datasets.Version(version),
22
+ description=description,
23
+ **kwargs,
24
+ )
25
+
26
+
27
+ DEFAULT_CONFIG_NAME = "all"
28
+ LANGS = list(lang2shard_cnt.keys())
29
+ VERSION = "1.0.0"
30
+
31
+ class Yodas(datasets.GeneratorBasedBuilder):
32
+ """Yodas dataset."""
33
+
34
+ BUILDER_CONFIGS = [
35
+ YodasConfig(lang, version=VERSION) for lang in LANGS
36
+ ]
37
+
38
+ VERSION = datasets.Version("1.0.0")
39
+
40
+ def _info(self):
41
+ return datasets.DatasetInfo(
42
+ description="Yodas",
43
+ features=datasets.Features(
44
+ OrderedDict(
45
+ [
46
+ ("id", datasets.Value("string")),
47
+ ("utt_id", datasets.Value("string")),
48
+ ("audio", datasets.Audio(sampling_rate=16_000)),
49
+ ("text", datasets.Value("string")),
50
+ ]
51
+ )
52
+ ),
53
+ supervised_keys=None,
54
+ homepage="", # TODO
55
+ citation="", # TODO
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ """Returns SplitGenerators."""
60
+ # TODO
61
+
62
+ total_cnt = lang2shard_cnt[self.config.name]
63
+
64
+ idx_lst = [f"{i:08d}" for i in range(total_cnt)]
65
+ audio_tar_files = dl_manager.download([f"{self.config.base_data_path}/audio/{i:08d}.tar.gz" for i in range(total_cnt)])
66
+ text_files = dl_manager.download([f"{self.config.base_data_path}/text/{i:08d}.txt" for i in range(total_cnt)])
67
+ #duration_files = dl_manager.download([f"{self.config.base_data_path}/duration/{i:08d}.txt" for i in range(total_cnt)])
68
+
69
+ if dl_manager.is_streaming:
70
+ audio_archives = [dl_manager.iter_archive(audio_tar_file) for audio_tar_file in audio_tar_files]
71
+ text_archives = [dl_manager.extract(text_file) for text_file in text_files]
72
+
73
+ else:
74
+ print("extracting audio ...")
75
+ extracted_audio_archives = dl_manager.extract(audio_tar_files)
76
+
77
+ audio_archives = []
78
+ text_archives = []
79
+ for idx, audio_tar_file, extracted_dir, text_file in zip(idx_lst, audio_tar_files, extracted_audio_archives, text_files):
80
+ audio_archives.append(str(extracted_dir)+'/'+idx)
81
+ text_archives.append(text_file)
82
+
83
+
84
+ return [
85
+ datasets.SplitGenerator(
86
+ name=datasets.Split.TRAIN,
87
+ gen_kwargs={
88
+ "is_streaming": dl_manager.is_streaming,
89
+ "audio_archives": audio_archives,
90
+ 'text_archives': text_archives,
91
+ },
92
+ ),
93
+ ]
94
+
95
+ def _generate_examples(self, is_streaming, audio_archives, text_archives):
96
+ """Yields examples."""
97
+
98
+ id_ = 0
99
+
100
+ if is_streaming:
101
+ for tar_file, text_file in zip(audio_archives, text_archives):
102
+
103
+ utt2text = {}
104
+
105
+ with open(text_file) as f:
106
+ for id_, row in enumerate(f):
107
+ row = row.strip().split(maxsplit=1)
108
+ utt2text[row[0]] = row[1]
109
+
110
+ for path, audio_f in tar_file:
111
+
112
+ path = Path(path)
113
+ utt_id = path.stem
114
+
115
+ if utt_id in utt2text:
116
+
117
+ result = {
118
+ 'id': id_,
119
+ 'utt_id': utt_id,
120
+ 'audio': {"path": None, "bytes": audio_f.read()},
121
+ 'text': utt2text[utt_id]
122
+ }
123
+
124
+ yield id_, result
125
+ id_ += 1
126
+ else:
127
+ for extracted_dir, text_file in zip(audio_archives, text_archives):
128
+
129
+ utt2text = {}
130
+ print(extracted_dir)
131
+
132
+ with open(text_file) as f:
133
+ for _, row in enumerate(f):
134
+ row = row.strip().split(maxsplit=1)
135
+ utt2text[row[0]] = row[1]
136
+
137
+ for audio_file in list(Path(extracted_dir).glob('*')):
138
+
139
+ utt_id = audio_file.stem
140
+ if utt_id in utt2text:
141
+
142
+ result = {
143
+ 'id': id_,
144
+ 'utt_id': utt_id,
145
+ 'audio': {"path": str(audio_file.absolute()), "bytes": open(audio_file, 'rb').read()},
146
+ 'text': utt2text[utt_id]
147
+ }
148
+
149
+ yield id_, result
150
+ id_ += 1