|
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"), |
|
"label": ClassLabel(names=["1", "2", "3", "4"]), |
|
"annotator": Value("string") |
|
}) |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Split the dataset. Assumes all files are local.""" |
|
|
|
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""" |
|
|
|
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"]), |
|
"annotator": row["annotator"] |
|
} |
|
|
|
|
|
|
|
|
|
|