import os import csv import datasets logger = datasets.logging.get_logger(__name__) _CITATION = """\ @misc{gtzan2023, title={GTZAN Music Genre Classification Dataset}, author={Your Name}, year={2023}, url={https://example.com}, } """ _DESCRIPTION = """\ The GTZAN dataset is a music genre classification dataset. It consists of 10 genres, each represented by 100 tracks. """ _HOMEPAGE_URL = "https://example.com" _DATA_URL = "data/gtzan.zip" class GTZANConfig(datasets.BuilderConfig): """BuilderConfig for GTZAN""" def __init__(self, **kwargs): super(GTZANConfig, self).__init__(**kwargs) class GTZAN(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ GTZANConfig(name="gtzan", version=datasets.Version("1.0.0"), description="GTZAN Music Genre Classification Dataset") ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("int32"), "audio": datasets.Audio(sampling_rate=44100), "genre": datasets.Value("string"), "title": datasets.Value("string"), "artist": datasets.Value("string"), "tempo": datasets.Value("float"), "keys": datasets.Value("string"), "loudness": datasets.Value("float"), "sorted_pred_genres": datasets.Value("string"), "x_tsne": datasets.Value("float"), "y_tsne": datasets.Value("float"), "z_tsne": datasets.Value("float"), "x_umap": datasets.Value("float"), "y_umap": datasets.Value("float"), "z_umap": datasets.Value("float"), "album_cover_path": datasets.Value("string"), "key": datasets.Value("string"), "filepath": datasets.Value("string"), } ), supervised_keys=("genre", "title"), homepage=_HOMEPAGE_URL, citation=_CITATION, ) def _split_generators(self, dl_manager): archive_path = dl_manager.download_and_extract(_DATA_URL) data_dir = os.path.join(archive_path, "gtzan") # Debugging the paths logger.info(f"Archive path: {archive_path}") logger.info(f"Data directory: {data_dir}") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "metadata_file": os.path.join(data_dir, "metadata.csv"), "data_dir": data_dir, }, ), ] def _generate_examples(self, metadata_file, data_dir): with open(metadata_file, "r", encoding="utf-8") as f: reader = csv.DictReader(f) for id_, row in enumerate(reader): # Correct the file paths by removing the 'data/gtzan/' prefix file_path = os.path.join(data_dir, row["filepath"].replace("data/gtzan/", "")) album_cover_path = os.path.join(data_dir, row["album_cover_path"].replace("data/gtzan/", "")) # Debugging the file paths logger.info(f"Processing file: {file_path}") if not os.path.exists(file_path): logger.error(f"File not found: {file_path}") yield id_, { "id": int(row["id"]), "audio": file_path, # Include the audio path next to the ID "genre": row["genre"], "title": row["title"], "artist": row["artist"], "tempo": float(row["tempo"]), "keys": row["keys"], "loudness": float(row["loudness"]), "sorted_pred_genres": row["sorted_pred_genres"], "x_tsne": float(row["x_tsne"]), "y_tsne": float(row["y_tsne"]), "z_tsne": float(row["z_tsne"]), "x_umap": float(row["x_umap"]), "y_umap": float(row["y_umap"]), "z_umap": float(row["z_umap"]), "album_cover_path": album_cover_path, "key": row["key"], "filepath": file_path, }