Commit ·
6402aec
1
Parent(s): 331d906
Upload stvi.py
Browse files
stvi.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
import datasets
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
_CITATION = """\
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
_DESCRIPTION = """\
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
_HOMEPAGE = ""
|
| 26 |
+
|
| 27 |
+
_LICENSE = "CC BY-NC-SA 4.0"
|
| 28 |
+
|
| 29 |
+
_DATA_URL = "data/stvi.tar.gz"
|
| 30 |
+
|
| 31 |
+
_PROMPTS_URLS = {
|
| 32 |
+
"train": "data/prompts-train.txt.gz",
|
| 33 |
+
"test": "data/prompts-test.txt.gz",
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class STViDataset(datasets.GeneratorBasedBuilder):
|
| 38 |
+
|
| 39 |
+
VERSION = datasets.Version("1.0.0")
|
| 40 |
+
|
| 41 |
+
# This is an example of a dataset with multiple configurations.
|
| 42 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
| 43 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
| 44 |
+
|
| 45 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
| 46 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
| 47 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
| 48 |
+
|
| 49 |
+
def _info(self):
|
| 50 |
+
return datasets.DatasetInfo(
|
| 51 |
+
# This is the description that will appear on the datasets page.
|
| 52 |
+
description=_DESCRIPTION,
|
| 53 |
+
features=datasets.Features(
|
| 54 |
+
{
|
| 55 |
+
# "speaker_id": datasets.Value("string"),
|
| 56 |
+
"path": datasets.Value("string"),
|
| 57 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
| 58 |
+
"sentence": datasets.Value("string"),
|
| 59 |
+
}
|
| 60 |
+
),
|
| 61 |
+
supervised_keys=None,
|
| 62 |
+
homepage=_HOMEPAGE,
|
| 63 |
+
license=_LICENSE,
|
| 64 |
+
citation=_CITATION,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
def _split_generators(self, dl_manager):
|
| 68 |
+
"""Returns SplitGenerators."""
|
| 69 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 70 |
+
|
| 71 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
| 72 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 73 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 74 |
+
prompts_paths = dl_manager.download_and_extract(_PROMPTS_URLS)
|
| 75 |
+
archive = dl_manager.download(_DATA_URL)
|
| 76 |
+
train_dir = "stvi/train"
|
| 77 |
+
test_dir = "stvi/test"
|
| 78 |
+
|
| 79 |
+
return [
|
| 80 |
+
datasets.SplitGenerator(
|
| 81 |
+
name=datasets.Split.TRAIN,
|
| 82 |
+
# These kwargs will be passed to _generate_examples
|
| 83 |
+
gen_kwargs={
|
| 84 |
+
"prompts_path": prompts_paths["train"],
|
| 85 |
+
"path_to_clips": train_dir + "/waves",
|
| 86 |
+
"audio_files": dl_manager.iter_archive(archive),
|
| 87 |
+
},
|
| 88 |
+
),
|
| 89 |
+
datasets.SplitGenerator(
|
| 90 |
+
name=datasets.Split.TEST,
|
| 91 |
+
# These kwargs will be passed to _generate_examples
|
| 92 |
+
gen_kwargs={
|
| 93 |
+
"prompts_path": prompts_paths["test"],
|
| 94 |
+
"path_to_clips": test_dir + "/waves",
|
| 95 |
+
"audio_files": dl_manager.iter_archive(archive),
|
| 96 |
+
},
|
| 97 |
+
),
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
def _generate_examples(self, prompts_path, path_to_clips, audio_files):
|
| 101 |
+
"""Yields examples as (key, example) tuples."""
|
| 102 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
| 103 |
+
# The `key` is here for legacy reason (tfds) and is not important in itself.
|
| 104 |
+
examples = {}
|
| 105 |
+
with open(prompts_path, encoding="utf-8") as f:
|
| 106 |
+
for row in f:
|
| 107 |
+
data = row.strip().split(" ", 1)
|
| 108 |
+
audio_path = "/".join([path_to_clips, data[0] + ".wav"])
|
| 109 |
+
examples[audio_path] = {
|
| 110 |
+
"path": audio_path,
|
| 111 |
+
"sentence": data[1],
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
inside_clips_dir = False
|
| 115 |
+
id_ = 0
|
| 116 |
+
for path, f in audio_files:
|
| 117 |
+
if path.startswith(path_to_clips):
|
| 118 |
+
inside_clips_dir = True
|
| 119 |
+
if path in examples:
|
| 120 |
+
audio = {"path": path, "bytes": f.read()}
|
| 121 |
+
yield id_, {**examples[path], "audio": audio}
|
| 122 |
+
id_ += 1
|
| 123 |
+
elif inside_clips_dir:
|
| 124 |
+
break
|