import os import pandas as pd from datasets import DatasetBuilder, GeneratorBasedBuilder, Split, Value, Features, ClassLabel class MidiDataset(GeneratorBasedBuilder): """Hugging Face Dataset for MIDI files and their labels""" def _info(self): return DatasetBuilder.info( features=Features({ "file_path": Value("string"), # MIDI file path "label": ClassLabel(names=["1", "2", "3", "4"]), # 4Q labels as class labels "annotator": Value("string") # Annotator information }) ) def _split_generators(self, dl_manager): """Split the dataset. Assumes all files are local.""" # Set paths to midis/ and label.csv midi_path = "midis" label_path = "label.csv" return [ Split( name=Split.TRAIN, gen_kwargs={ "midi_dir": midi_path, "label_file": label_path } ) ] def _generate_examples(self, midi_dir, label_file): """Yield examples from MIDI files and the label.csv""" # Read the label.csv into a pandas DataFrame df = pd.read_csv(label_file) for index, row in df.iterrows(): midi_file = os.path.join(midi_dir, f"{row['ID']}.mid") if os.path.exists(midi_file): yield index, { "file_path": midi_file, "label": str(row["4Q"]), # Convert label to string for ClassLabel compatibility "annotator": row["annotator"] } # Usage Example: # You can now load the dataset using this script as follows: # from datasets import load_dataset # dataset = load_dataset("path/to/your/script", split="train")