Datasets:

Languages:
English
ArXiv:
License:
polinaeterna HF staff commited on
Commit
949d4d7
1 Parent(s): f9dc33b

add splits support

Browse files
Files changed (1) hide show
  1. peoples_speech.py +57 -17
peoples_speech.py CHANGED
@@ -61,14 +61,16 @@ _LICENSE = [
61
  "cc-by-sa-3.0", "cc-by-sa-4.0"
62
  ]
63
 
 
 
64
  # relative path to data inside dataset's repo
65
- _DATA_URL = "train/{config}/{config}_{archive_id:06d}.tar"
66
 
67
  # relative path to file containing number of audio archives inside dataset's repo
68
- _N_FILES_URL = "train/{config}/n_files.txt"
69
 
70
  # relative path to metadata inside dataset's repo
71
- _MANIFEST_URL = "train/{config}.json"
72
 
73
 
74
  class PeoplesSpeech(datasets.GeneratorBasedBuilder):
@@ -103,21 +105,39 @@ class PeoplesSpeech(datasets.GeneratorBasedBuilder):
103
  citation=_CITATION,
104
  )
105
 
 
 
 
 
 
 
 
106
  def _split_generators(self, dl_manager):
107
- n_files_url = _N_FILES_URL.format(config=self.config.name)
108
- n_files_path = dl_manager.download_and_extract(n_files_url)
109
- with open(n_files_path, encoding="utf-8") as f:
110
- n_files = int(f.read().strip())
111
 
112
- urls = [_DATA_URL.format(config=self.config.name, archive_id=i) for i in range(n_files)]
113
- archive_paths = [dl_manager.download(url) for url in urls]
 
 
 
 
114
 
115
  # In non-streaming mode, we extract the archives to have the data locally:
116
- local_extracted_archive_paths = [dl_manager.extract(path) for path in archive_paths] \
117
- if not dl_manager.is_streaming else [None] * len(archive_paths)
118
-
119
- manifest_url = _MANIFEST_URL.format(config=self.config.name)
120
- manifest_path = dl_manager.download_and_extract(manifest_url)
 
 
 
 
 
 
 
 
121
 
122
  # To access the audio data from the TAR archives using the download manager,
123
  # we have to use the dl_manager.iter_archive method
@@ -133,10 +153,28 @@ class PeoplesSpeech(datasets.GeneratorBasedBuilder):
133
  datasets.SplitGenerator(
134
  name=datasets.Split.TRAIN,
135
  gen_kwargs={
136
- "local_extracted_archive_paths": local_extracted_archive_paths,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  # use iter_archive here to access the files in the TAR archives:
138
- "archives": [dl_manager.iter_archive(path) for path in archive_paths],
139
- "manifest_path": manifest_path,
140
  },
141
  ),
142
  ]
@@ -151,6 +189,7 @@ class PeoplesSpeech(datasets.GeneratorBasedBuilder):
151
  audio_filenames = sample_meta["training_data"]["name"]
152
  durations = sample_meta["training_data"]["duration_ms"]
153
  for audio_filename, text, duration in zip(audio_filenames, texts, durations):
 
154
  meta[audio_filename] = {
155
  "audio_document_id": _id,
156
  "text": text,
@@ -160,6 +199,7 @@ class PeoplesSpeech(datasets.GeneratorBasedBuilder):
160
  for local_extracted_archive_path, archive in zip(local_extracted_archive_paths, archives):
161
  # Here we iterate over all the files within the TAR archive:
162
  for audio_filename, audio_file in archive:
 
163
  # if an audio file exists locally (i.e. in default, non-streaming mode) set the full path to it
164
  # joining path to directory that the archive was extracted to and audio filename.
165
  path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path \
 
61
  "cc-by-sa-3.0", "cc-by-sa-4.0"
62
  ]
63
 
64
+ # _BASE_URL = "https://huggingface.co/datasets/MLCommons/peoples_speech/resolve/main/"
65
+
66
  # relative path to data inside dataset's repo
67
+ _DATA_URL = "{split}/{config}/{config}_{archive_id:06d}.tar"
68
 
69
  # relative path to file containing number of audio archives inside dataset's repo
70
+ _N_FILES_URL = "{split}/{config}/n_files.txt"
71
 
72
  # relative path to metadata inside dataset's repo
73
+ _MANIFEST_URL = "{split}/{config}.json"
74
 
75
 
76
  class PeoplesSpeech(datasets.GeneratorBasedBuilder):
 
105
  citation=_CITATION,
106
  )
107
 
108
+ def _get_n_files(self, dl_manager, split, config):
109
+ n_files_url = _N_FILES_URL.format(split=split, config=config)
110
+ n_files_paths = dl_manager.download_and_extract(n_files_url)
111
+
112
+ with open(n_files_paths, encoding="utf-8") as f:
113
+ return int(f.read().strip())
114
+
115
  def _split_generators(self, dl_manager):
116
+ n_files_train = self._get_n_files(dl_manager, split="train", config=self.config.name)
117
+ n_files_dev = self._get_n_files(dl_manager, split="dev", config="dev")
118
+ n_files_test = self._get_n_files(dl_manager, split="test", config="test")
 
119
 
120
+ urls = {
121
+ "train": [_DATA_URL.format(split="train", config=self.config.name, archive_id=i) for i in range(n_files_train)],
122
+ "dev": [_DATA_URL.format(split="dev", config="dev", archive_id=i) for i in range(n_files_dev)],
123
+ "test": [_DATA_URL.format(split="test", config="test", archive_id=i) for i in range(n_files_test)],
124
+ }
125
+ archive_paths = dl_manager.download(urls)
126
 
127
  # In non-streaming mode, we extract the archives to have the data locally:
128
+ local_extracted_archive_paths = dl_manager.extract(archive_paths) if not dl_manager.is_streaming else \
129
+ {
130
+ "train": [None] * len(archive_paths),
131
+ "dev": [None] * len(archive_paths),
132
+ "test": [None] * len(archive_paths),
133
+ }
134
+
135
+ manifest_urls = {
136
+ "train": _MANIFEST_URL.format(split="train", config=self.config.name),
137
+ "dev": _MANIFEST_URL.format(split="dev", config="dev"),
138
+ "test": _MANIFEST_URL.format(split="test", config="test"),
139
+ }
140
+ manifest_paths = dl_manager.download_and_extract(manifest_urls)
141
 
142
  # To access the audio data from the TAR archives using the download manager,
143
  # we have to use the dl_manager.iter_archive method
 
153
  datasets.SplitGenerator(
154
  name=datasets.Split.TRAIN,
155
  gen_kwargs={
156
+ "local_extracted_archive_paths": local_extracted_archive_paths["train"],
157
+ # use iter_archive here to access the files in the TAR archives:
158
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths["train"]],
159
+ "manifest_path": manifest_paths["train"],
160
+ },
161
+ ),
162
+ datasets.SplitGenerator(
163
+ name=datasets.Split.VALIDATION,
164
+ gen_kwargs={
165
+ "local_extracted_archive_paths": local_extracted_archive_paths["dev"],
166
+ # use iter_archive here to access the files in the TAR archives:
167
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths["dev"]],
168
+ "manifest_path": manifest_paths["dev"],
169
+ },
170
+ ),
171
+ datasets.SplitGenerator(
172
+ name=datasets.Split.TEST,
173
+ gen_kwargs={
174
+ "local_extracted_archive_paths": local_extracted_archive_paths["dev"],
175
  # use iter_archive here to access the files in the TAR archives:
176
+ "archives": [dl_manager.iter_archive(path) for path in archive_paths["test"]],
177
+ "manifest_path": manifest_paths["test"],
178
  },
179
  ),
180
  ]
 
189
  audio_filenames = sample_meta["training_data"]["name"]
190
  durations = sample_meta["training_data"]["duration_ms"]
191
  for audio_filename, text, duration in zip(audio_filenames, texts, durations):
192
+ audio_filename = audio_filename.lstrip("./")
193
  meta[audio_filename] = {
194
  "audio_document_id": _id,
195
  "text": text,
 
199
  for local_extracted_archive_path, archive in zip(local_extracted_archive_paths, archives):
200
  # Here we iterate over all the files within the TAR archive:
201
  for audio_filename, audio_file in archive:
202
+ audio_filename = audio_filename.lstrip("./")
203
  # if an audio file exists locally (i.e. in default, non-streaming mode) set the full path to it
204
  # joining path to directory that the archive was extracted to and audio filename.
205
  path = os.path.join(local_extracted_archive_path, audio_filename) if local_extracted_archive_path \