music_classification / music_classification.py
lewtun's picture
lewtun HF Staff
Add script
30ec6a9
# 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.
"""The Kaggle Music Genre Prediction Challenge."""
import os
from pathlib import Path
import datasets
import pandas as pd
_CITATION = """
"""
_DESCRIPTION = """\
"""
_HOMEPAGE = ""
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
_URL = ""
genres_df = pd.read_csv("data/genres.csv")
GENRES = genres_df["genre"].tolist()
class MusicClassification(datasets.GeneratorBasedBuilder):
"""The Kaggle Music Genre Prediction Challenge"""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"song_id": datasets.Value("int32"),
"file": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=22_050),
"genre_id": datasets.ClassLabel(names=GENRES),
"genre": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
train_path = dl_manager.download_and_extract("data/train.zip")
test_path = dl_manager.download_and_extract("data/test.zip")
metadata_train = Path("data/train.csv")
metadata_test = Path("data/test.csv")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"audio_path": train_path, "metadata_path": metadata_train, "split": "train"},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"audio_path": test_path, "metadata_path": metadata_test, "split": "test"},
),
]
def _generate_examples(self, audio_path, metadata_path, split):
print(audio_path)
print(metadata_path)
print(split)
metadata_df = pd.read_csv(metadata_path)
if split == "train":
for idx_, row in metadata_df.iterrows():
yield idx_, {
"song_id": row["song_id"],
"file": os.path.join(audio_path, row["filepath"]),
"audio": os.path.join(audio_path, row["filepath"]),
"genre_id": row["genre_id"],
"genre": row["genre"],
}
else:
for idx_, row in metadata_df.iterrows():
yield idx_, {
"song_id": row["song_id"],
"file": os.path.join(audio_path, row["filepath"]),
"audio": os.path.join(audio_path, row["filepath"]),
"genre_id": -1,
"genre": "NA",
}