| import json |
| import hashlib |
| import random |
|
|
|
|
|
|
| PATH = "/work/fast_data_yinghao/MTG/mtg-jamendo-dataset/data" |
| |
|
|
| number_to_letter = { |
| 0: "A", |
| 1: "B", |
| 2: "C", |
| 3: "D" |
| } |
|
|
| def get_genre(_="test"): |
| data_samples = [] |
| for split in [_]: |
| input_file_path = f'{PATH}/splits/split-0/autotagging_genre-{split}.tsv' |
|
|
| with open(input_file_path, "r") as f: |
| for idx, line in enumerate(f): |
| if idx > 0: |
| tmp = line.strip().split("\t") |
| genres = tmp[5:] |
| audio_path = tmp[3] |
| audioid = "66_MTG-Jamendo_" + audio_path.split("/")[-1] |
| if "low" not in audioid: |
| audioid = audioid[:-4] + ".low.mp3" |
|
|
| data_sample = { |
| "instruction": "Please provide the genre of given audio.", |
| "input": f"<|SOA|>f'{audio_path}'<|EOA|>", |
| "output": ", ".join(sorted([genre.split("-")[-1] for genre in genres])), |
| "uuid": audio_path, |
| "split": [split if split != "validation" else "dev"], |
| "task_type": {"major": ["global_MIR"], "minor": ["genre_classification"]}, |
| "domain": "music", |
| "audioid": audio_path, |
| "source": "MTG", |
| "other": {"tag":"null"} |
| } |
| data_samples.append(data_sample) |
| |
| |
| f.close() |
| |
|
|
| existed_uuid_list = set() |
| all_genres = set(genre for data_sample in data_samples for genre in data_sample["output"].split(", ")) |
| for data_sample in data_samples: |
| |
| data_sample["instruction"] = data_sample["instruction"] + " If you can find multiple genres, please output in alphabeta order. Use ', ' to split multiple tags." |
| |
| |
| uuid_string = f"{data_sample['instruction']}#{data_sample['input']}#{data_sample['output']}" |
| unique_id = hashlib.md5(uuid_string.encode()).hexdigest()[:16] |
|
|
| if unique_id in existed_uuid_list: |
| sha1_hash = hashlib.sha1(uuid_string.encode()).hexdigest()[:16] |
| unique_id = hashlib.md5((unique_id + sha1_hash).encode()).hexdigest()[:16] |
|
|
| existed_uuid_list.add(unique_id) |
| data_sample["uuid"] = f"{unique_id}" |
| |
| return data_samples |
|
|
| if __name__ == "__main__": |
| print("start") |
| for split in ["test", "train", "validation"]: |
| data_samples = get_genre(split) |
| |
| output_file_path = f'genre_{split}.jsonl' |
| |
| with open(output_file_path, 'w') as outfile: |
| for sample in data_samples: |
| json.dump(sample, outfile) |
|
|
| outfile.write('\n') |
| outfile.close() |
|
|