# Copyright 2020 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. from pathlib import Path import pandas as pd import datasets # Find for instance the citation on arxiv or on the dataset repo/website _CITATION = """ @conference {bogdanov2019mtg, author = "Bogdanov, Dmitry and Won, Minz and Tovstogan, Philip and Porter, Alastair and Serra, Xavier", title = "The MTG-Jamendo Dataset for Automatic Music Tagging", booktitle = "Machine Learning for Music Discovery Workshop, International Conference on Machine Learning (ICML 2019)", year = "2019", address = "Long Beach, CA, United States", url = "http://hdl.handle.net/10230/42015" } """ _DESCRIPTION = """ Repackaging of the MTG Jamendo dataset. We present the MTG-Jamendo Dataset, a new open dataset for music auto-tagging. It is built using music available at Jamendo under Creative Commons licenses and tags provided by content creators. The dataset contains over 55,000 full audio tracks with 195 tags from genre, instrument, and mood/theme categories. """ _HOMEPAGE = "https://github.com/MTG/mtg-jamendo-dataset" _LICENSE = "Apache License 2.0" # The HuggingFace Datasets library doesn't host the datasets but only points to the original files. _URLS_DATA = { "train": { i: f"https://huggingface.co/datasets/rkstgr/mtg-jamendo/resolve/main/data/train/{i}.tar" for i in range(200) }, "val": { i: f"https://huggingface.co/datasets/rkstgr/mtg-jamendo/resolve/main/data/val/{i}.tar" for i in range(22) } } _URLS_TRACKS = { "train": "https://huggingface.co/datasets/rkstgr/mtg-jamendo/raw/main/train.tsv", "valid": "https://huggingface.co/datasets/rkstgr/mtg-jamendo/raw/main/valid.tsv" } class MtgJamendo(datasets.GeneratorBasedBuilder): """ Audio dataset containing over 55,000 full audio tracks with 195 tags from genre, instrument, and mood/theme categories """ VERSION = datasets.Version("1.1.0") def _info(self): features = datasets.Features( { "id": datasets.Value("int32"), "artist_id": datasets.Value("int32"), "album_id": datasets.Value("int32"), "durationInSec": datasets.Value("float"), "genres": datasets.Sequence(datasets.Value("string")), "instruments": datasets.Sequence(datasets.Value("string")), "moods": datasets.Sequence(datasets.Value("string")), "audio": datasets.Audio(sampling_rate=22_050), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): archive_path = dl_manager.download(_URLS_DATA) tracks_path = dl_manager.download(_URLS_TRACKS) # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files: local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {} def to_dict(xs: list) -> dict: return {x["id"]: x for x in xs} train_tracks = to_dict( pd.read_csv(tracks_path["train"], sep="\t").to_dict("records") ) valid_tracks = to_dict( pd.read_csv(tracks_path["valid"], sep="\t").to_dict("records") ) train_splits = [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "local_extracted_archive": local_extracted_archive.get("train"), "files": dl_manager.iter_archive(archive_path["train"]), "tracks": train_tracks, }, ) ] val_splits = [ datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={ "local_extracted_archive": local_extracted_archive.get("val"), "files": dl_manager.iter_archive(archive_path["val"]), "tracks": valid_tracks, }, ) ] return train_splits + val_splits # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` def _generate_examples(self, files, local_extracted_archive, tracks): """Generate examples from archive_path.""" audio_paths = {} for _, directory in local_extracted_archive.items(): for f in Path(directory).iterdir(): if f.suffix == ".opus": _id = int(f.stem) audio_paths[_id] = str(f) for _id, audio_path in audio_paths.items(): data = { "id": _id, "artist_id": tracks[_id]["artist_id"], "album_id": tracks[_id]["album_id"], "durationInSec": tracks[_id]["durationInSec"], "genres": eval(tracks[_id]["genres"]), "instruments": eval(tracks[_id]["instruments"]), "moods": eval(tracks[_id]["moods"]), "audio": audio_path } yield _id, data if __name__ == '__main__': mtg = MtgJamendo() mtg.download_and_prepare()