import os from glob import glob from pathlib import Path import datasets from datasets import Features, Value, Audio try: from music_tag import load_file except Exception: load_file = None _DESCRIPTION = """ This dataset contains speech generated for anecdotes from the [baneks dataset](https://huggingface.co/datasets/zeio/baneks) """ _HOMEPAGE = "https://huggingface.co/datasets/zeio/baneks-speech" _LICENSE = "Apache License Version 2.0" _URL = "https://huggingface.co/datasets/zeio/baneks-speech/resolve/main/speech/{first:06d}-{last:06d}.tar.xz" _N_TOTAL = 39_536 _N_BATCH = 1_000 class BaneksSpeech(datasets.GeneratorBasedBuilder): """Speech generated for anecdotes from the baneks dataset""" VERSION = datasets.Version("10.10.2023") def _info(self): return datasets.DatasetInfo( description = _DESCRIPTION, features = Features( { "text": Value("string"), "audio": Audio(sampling_rate = 22_050), "artist": Value("string"), "id": Value("int32"), "source": Value("string") } ), homepage = _HOMEPAGE, license = _LICENSE ) def _split_generators(self, dl_manager): offset = 0 paths = [] while offset < _N_TOTAL: url = _URL.format(first = offset + 1, last = min(offset := offset + _N_BATCH, _N_TOTAL)) paths.append(dl_manager.download_and_extract(url)) # break # uncomment this line to download only first partition of the dataset which weighs approximately 70 Mb return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "paths": paths } ) ] def _generate_examples(self, paths: list[str]): for path in paths: for file in sorted(glob(os.path.join(path, "*"))): components = Path(file).stem.split('.', maxsplit = 1) assert len(components) == 2, f'Incorrect file name: {file}' id_ = int(components[0]) source = components[1] meta = { 'lyrics': 'Here will be lyrics which are loaded from the mp3 file metadata', 'artist': 'Here will be artist which is loaded from the mp3 file metadata' } if load_file is None else load_file(file) yield f'{id_:08d}-{source}', { 'text': meta['lyrics'], 'audio': file, 'artist': meta['artist'], 'id': id_, 'source': source }