VBVLSP / VBVLSP.py
Le Viet Hoang
fix dataset #4
ae4e3b0
# coding=utf-8
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Common Voice Dataset"""
import csv
import os
import json
import datasets
from datasets.utils.py_utils import size_str
from tqdm import tqdm
# TODO: change "streaming" to "main" after merge!
_BASE_URL = "https://huggingface.co/datasets/leviethoang/VBVLSP/resolve/main/"
_AUDIO_URL = {
"train": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/EYhNns0j8GJEgZvb-G2aRS4Bt7AEdQMrGxYtyO2xjc6Img?e=3PkypA&download=1",
"test": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/Ea0uw5DdlxRKpjay1pm6LIoBI6cU4cxHbpTmhWCCRtvMXw?e=yfN5NR&download=1",
"validation": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/EerG7YTpS8dNgpG5vsnpsm0BBKZYYifqcW4kRX3VzHHO5w?e=uvo7Is&download=1"
}
_TRANSCRIPT_URL = _BASE_URL + "transcript/{split}.tsv"
class CommonVoice(datasets.GeneratorBasedBuilder):
DEFAULT_WRITER_BATCH_SIZE = 1000
def _info(self):
description = ("""
"""
)
features = datasets.Features(
{
"file_path": datasets.Value("string"),
"audio": datasets.features.Audio(sampling_rate=48_000),
"script": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=description,
features=features,
supervised_keys=None,
version=self.config.version,
)
def _split_generators(self, dl_manager):
splits = ("train", "test", "validation")
archive_paths = dl_manager.download(_AUDIO_URL)
local_extracted_archive_paths = dl_manager.extract(archive_paths)
meta_urls = {split: _TRANSCRIPT_URL.format(split=split) for split in splits}
meta_paths = dl_manager.download_and_extract(meta_urls)
split_generators = []
split_names = {
"train": datasets.Split.TRAIN,
"dev": datasets.Split.VALIDATION,
"test": datasets.Split.TEST,
}
for split in splits:
split_generators.append(
datasets.SplitGenerator(
name=split_names.get(split, split),
gen_kwargs={
"local_extracted_archive_path": local_extracted_archive_paths.get(split),
"archive": dl_manager.iter_archive(archive_paths.get(split)),
"meta_path": meta_paths[split],
},
),
)
return split_generators
def _generate_examples(self, local_extracted_archive_path, archive, meta_path):
data_fields = list(self._info().features.keys())
metadata = {}
with open(meta_path, encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in tqdm(reader, desc="Reading metadata..."):
# if data is incomplete, fill with empty values
for field in data_fields:
if field not in row:
row[field] = ""
metadata[row["file_path"]] = row
for filename, file in archive:
_, filename = os.path.split(filename)
if filename in metadata:
result = dict(metadata[filename])
# set the audio feature and the path to the extracted file
path = os.path.join(local_extracted_archive_path, filename) if local_extracted_archive_path else filename
result["audio"] = {"file_path": path, "bytes": file.read()}
# set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
result["file_path"] = path if local_extracted_archive_path else filename
yield path, result