Upload audio-kw-in-context.py
Browse files- audio-kw-in-context.py +146 -144
audio-kw-in-context.py
CHANGED
@@ -1,145 +1,147 @@
|
|
1 |
-
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
-
#
|
3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
-
# you may not use this file except in compliance with the License.
|
5 |
-
# You may obtain a copy of the License at
|
6 |
-
#
|
7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
-
#
|
9 |
-
# Unless required by applicable law or agreed to in writing, software
|
10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
-
# See the License for the specific language governing permissions and
|
13 |
-
# limitations under the License.
|
14 |
-
"""sil-ai/audio-kw-in-context is a subset of MLCommons/ml_spoken_words focusing on keywords found in the Bible'"""
|
15 |
-
|
16 |
-
import json
|
17 |
-
import os
|
18 |
-
|
19 |
-
import datasets
|
20 |
-
|
21 |
-
_CITATION = """\
|
22 |
-
@InProceedings{huggingface:audio-kw-in-context,
|
23 |
-
title = {audio-kw-in-context},
|
24 |
-
author={Joshua Nemecek
|
25 |
-
},
|
26 |
-
year={2022}
|
27 |
-
}
|
28 |
-
"""
|
29 |
-
_DESCRIPTION = 'sil-ai/audio-kw-in-context is a subset of MLCommons/ml_spoken_words focusing on keywords found in the Bible'
|
30 |
-
_LANGUAGES = ['eng', 'ind', 'spa']
|
31 |
-
_LANG_ISO_DICT = {'en':'eng','es':'spa','id':'ind'}
|
32 |
-
_HOMEPAGE = 'https://ai.sil.org'
|
33 |
-
_URLS = {"metadata": "bible-keyword-context.json",
|
34 |
-
"files": {lang: f'https://audio-keyword-spotting.s3.amazonaws.com/HF-context-v2/{lang}-kw-archive.tar.gz' for lang in _LANGUAGES},
|
35 |
-
}
|
36 |
-
_LICENSE = 'CC-BY 4.0'
|
37 |
-
_GENDERS = ["MALE", "FEMALE", "OTHER", "NAN"]
|
38 |
-
|
39 |
-
class AudioKwInContextConfig(datasets.BuilderConfig):
|
40 |
-
"""BuilderConfig for Audio-Kw-In-Context"""
|
41 |
-
def __init__(self, language='', **kwargs):
|
42 |
-
super(AudioKwInContextConfig, self).__init__(**kwargs)
|
43 |
-
self.language = _LANG_ISO_DICT.get(language, language)
|
44 |
-
|
45 |
-
class AudioKwInContext(datasets.GeneratorBasedBuilder):
|
46 |
-
"""Audio-Kw-In-Context class"""
|
47 |
-
BUILDER_CONFIGS = [AudioKwInContextConfig(name=x, description=f'Audio keyword spotting for language code {x}', language=x) for x in _LANGUAGES]
|
48 |
-
|
49 |
-
DEFAULT_CONFIG_NAME = ''
|
50 |
-
|
51 |
-
BUILDER_CONFIG_CLASS = AudioKwInContextConfig
|
52 |
-
|
53 |
-
VERSION = datasets.Version("0.0.2")
|
54 |
-
|
55 |
-
def _info(self):
|
56 |
-
features = datasets.Features(
|
57 |
-
{
|
58 |
-
"file": datasets.Value("string"),
|
59 |
-
"language": datasets.Value("string"),
|
60 |
-
"speaker_id": datasets.Value("string"),
|
61 |
-
"sentence": datasets.Value("string"),
|
62 |
-
"keywords": datasets.Sequence(datasets.Value("string")),
|
63 |
-
"audio": datasets.Audio(sampling_rate=16_000),
|
64 |
-
"start_times": datasets.Sequence(datasets.Value("float32")),
|
65 |
-
"end_times": datasets.Sequence(datasets.Value("float32")),
|
66 |
-
"confidence": datasets.Sequence(datasets.Value("float32")),
|
67 |
-
}
|
68 |
-
)
|
69 |
-
|
70 |
-
return datasets.DatasetInfo(
|
71 |
-
description=_DESCRIPTION,
|
72 |
-
features=features,
|
73 |
-
homepage=_HOMEPAGE,
|
74 |
-
license=_LICENSE,
|
75 |
-
citation=_CITATION,
|
76 |
-
)
|
77 |
-
|
78 |
-
def _split_generators(self, dl_manager):
|
79 |
-
|
80 |
-
if self.config.language == '':
|
81 |
-
raise ValueError('Please specify a language.')
|
82 |
-
elif self.config.language not in _LANGUAGES:
|
83 |
-
raise ValueError(f'{self.config.language} does not appear in the list of languages: {_LANGUAGES}')
|
84 |
-
|
85 |
-
data_dir = dl_manager.download(_URLS['metadata'])
|
86 |
-
with open(data_dir, 'r') as f:
|
87 |
-
filemeta = json.load(f)
|
88 |
-
|
89 |
-
audio_dir = dl_manager.download_and_extract(_URLS['files'][self.config.name])
|
90 |
-
|
91 |
-
langmeta = filemeta[self.config.language]
|
92 |
-
|
93 |
-
return [
|
94 |
-
datasets.SplitGenerator(
|
95 |
-
name=datasets.Split.TRAIN,
|
96 |
-
# These kwargs will be passed to _generate_examples
|
97 |
-
gen_kwargs={
|
98 |
-
"audio_dir": audio_dir,
|
99 |
-
"data": langmeta,
|
100 |
-
"split": "train",
|
101 |
-
},
|
102 |
-
),
|
103 |
-
datasets.SplitGenerator(
|
104 |
-
name=datasets.Split.VALIDATION,
|
105 |
-
# These kwargs will be passed to _generate_examples
|
106 |
-
gen_kwargs={
|
107 |
-
"audio_dir": audio_dir,
|
108 |
-
"data": langmeta,
|
109 |
-
"split": "dev",
|
110 |
-
},
|
111 |
-
),
|
112 |
-
datasets.SplitGenerator(
|
113 |
-
name=datasets.Split.TEST,
|
114 |
-
# These kwargs will be passed to _generate_examples
|
115 |
-
gen_kwargs={
|
116 |
-
"audio_dir": audio_dir,
|
117 |
-
"data": langmeta,
|
118 |
-
"split": "test",
|
119 |
-
},
|
120 |
-
),
|
121 |
-
]
|
122 |
-
|
123 |
-
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
124 |
-
def _generate_examples(self, audio_dir, data, split):
|
125 |
-
for key, row in enumerate(data[split]):
|
126 |
-
try:
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
"
|
135 |
-
"
|
136 |
-
"
|
137 |
-
"
|
138 |
-
"
|
139 |
-
"
|
140 |
-
"
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
145 |
pass
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
"""sil-ai/audio-kw-in-context is a subset of MLCommons/ml_spoken_words focusing on keywords found in the Bible'"""
|
15 |
+
|
16 |
+
import json
|
17 |
+
import os
|
18 |
+
|
19 |
+
import datasets
|
20 |
+
|
21 |
+
_CITATION = """\
|
22 |
+
@InProceedings{huggingface:audio-kw-in-context,
|
23 |
+
title = {audio-kw-in-context},
|
24 |
+
author={Joshua Nemecek
|
25 |
+
},
|
26 |
+
year={2022}
|
27 |
+
}
|
28 |
+
"""
|
29 |
+
_DESCRIPTION = 'sil-ai/audio-kw-in-context is a subset of MLCommons/ml_spoken_words focusing on keywords found in the Bible'
|
30 |
+
_LANGUAGES = ['eng', 'ind', 'spa']
|
31 |
+
_LANG_ISO_DICT = {'en':'eng','es':'spa','id':'ind'}
|
32 |
+
_HOMEPAGE = 'https://ai.sil.org'
|
33 |
+
_URLS = {"metadata": "bible-keyword-context.json",
|
34 |
+
"files": {lang: f'https://audio-keyword-spotting.s3.amazonaws.com/HF-context-v2/{lang}-kw-archive.tar.gz' for lang in _LANGUAGES},
|
35 |
+
}
|
36 |
+
_LICENSE = 'CC-BY 4.0'
|
37 |
+
_GENDERS = ["MALE", "FEMALE", "OTHER", "NAN"]
|
38 |
+
|
39 |
+
class AudioKwInContextConfig(datasets.BuilderConfig):
|
40 |
+
"""BuilderConfig for Audio-Kw-In-Context"""
|
41 |
+
def __init__(self, language='', **kwargs):
|
42 |
+
super(AudioKwInContextConfig, self).__init__(**kwargs)
|
43 |
+
self.language = _LANG_ISO_DICT.get(language, language)
|
44 |
+
|
45 |
+
class AudioKwInContext(datasets.GeneratorBasedBuilder):
|
46 |
+
"""Audio-Kw-In-Context class"""
|
47 |
+
BUILDER_CONFIGS = [AudioKwInContextConfig(name=x, description=f'Audio keyword spotting for language code {x}', language=x) for x in _LANGUAGES]
|
48 |
+
|
49 |
+
DEFAULT_CONFIG_NAME = ''
|
50 |
+
|
51 |
+
BUILDER_CONFIG_CLASS = AudioKwInContextConfig
|
52 |
+
|
53 |
+
VERSION = datasets.Version("0.0.2")
|
54 |
+
|
55 |
+
def _info(self):
|
56 |
+
features = datasets.Features(
|
57 |
+
{
|
58 |
+
"file": datasets.Value("string"),
|
59 |
+
"language": datasets.Value("string"),
|
60 |
+
"speaker_id": datasets.Value("string"),
|
61 |
+
"sentence": datasets.Value("string"),
|
62 |
+
"keywords": datasets.Sequence(datasets.Value("string")),
|
63 |
+
"audio": datasets.Audio(sampling_rate=16_000),
|
64 |
+
"start_times": datasets.Sequence(datasets.Value("float32")),
|
65 |
+
"end_times": datasets.Sequence(datasets.Value("float32")),
|
66 |
+
"confidence": datasets.Sequence(datasets.Value("float32")),
|
67 |
+
}
|
68 |
+
)
|
69 |
+
|
70 |
+
return datasets.DatasetInfo(
|
71 |
+
description=_DESCRIPTION,
|
72 |
+
features=features,
|
73 |
+
homepage=_HOMEPAGE,
|
74 |
+
license=_LICENSE,
|
75 |
+
citation=_CITATION,
|
76 |
+
)
|
77 |
+
|
78 |
+
def _split_generators(self, dl_manager):
|
79 |
+
|
80 |
+
if self.config.language == '':
|
81 |
+
raise ValueError('Please specify a language.')
|
82 |
+
elif self.config.language not in _LANGUAGES:
|
83 |
+
raise ValueError(f'{self.config.language} does not appear in the list of languages: {_LANGUAGES}')
|
84 |
+
|
85 |
+
data_dir = dl_manager.download(_URLS['metadata'])
|
86 |
+
with open(data_dir, 'r') as f:
|
87 |
+
filemeta = json.load(f)
|
88 |
+
|
89 |
+
audio_dir = dl_manager.download_and_extract(_URLS['files'][self.config.name])
|
90 |
+
|
91 |
+
langmeta = filemeta[self.config.language]
|
92 |
+
|
93 |
+
return [
|
94 |
+
datasets.SplitGenerator(
|
95 |
+
name=datasets.Split.TRAIN,
|
96 |
+
# These kwargs will be passed to _generate_examples
|
97 |
+
gen_kwargs={
|
98 |
+
"audio_dir": audio_dir,
|
99 |
+
"data": langmeta,
|
100 |
+
"split": "train",
|
101 |
+
},
|
102 |
+
),
|
103 |
+
datasets.SplitGenerator(
|
104 |
+
name=datasets.Split.VALIDATION,
|
105 |
+
# These kwargs will be passed to _generate_examples
|
106 |
+
gen_kwargs={
|
107 |
+
"audio_dir": audio_dir,
|
108 |
+
"data": langmeta,
|
109 |
+
"split": "dev",
|
110 |
+
},
|
111 |
+
),
|
112 |
+
datasets.SplitGenerator(
|
113 |
+
name=datasets.Split.TEST,
|
114 |
+
# These kwargs will be passed to _generate_examples
|
115 |
+
gen_kwargs={
|
116 |
+
"audio_dir": audio_dir,
|
117 |
+
"data": langmeta,
|
118 |
+
"split": "test",
|
119 |
+
},
|
120 |
+
),
|
121 |
+
]
|
122 |
+
|
123 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
124 |
+
def _generate_examples(self, audio_dir, data, split):
|
125 |
+
for key, row in enumerate(data[split]):
|
126 |
+
try:
|
127 |
+
trows = row['file'].split('/')
|
128 |
+
trow = '/'.join([trows[0], 'HF-context-v2',trows[1:]])
|
129 |
+
tfile = os.path.join(audio_dir, trow)
|
130 |
+
if not tfile.endswith('.mp3'):
|
131 |
+
os.rename(tfile, tfile + '.mp3')
|
132 |
+
tfile += '.mp3'
|
133 |
+
yield key, {
|
134 |
+
"file": tfile,
|
135 |
+
"sentence": row.get('sentence'),
|
136 |
+
"language": self.config.language,
|
137 |
+
"speaker_id": row.get('speaker_id',row.get('client_id')),
|
138 |
+
"keywords": row['keywords'],
|
139 |
+
"audio": tfile,
|
140 |
+
"start_times": row.get('start_times'),
|
141 |
+
"end_times": row.get('end_times'),
|
142 |
+
"confidence": row.get('confidence'),
|
143 |
+
}
|
144 |
+
except Exception as e:
|
145 |
+
print(e)
|
146 |
+
print(f'In split {split}: {row["file"]} failed to load. Data may be missing.')
|
147 |
pass
|