|
import hashlib |
|
import json |
|
import tqdm |
|
import os |
|
import pandas as pd |
|
|
|
|
|
|
|
PATH = "/work/fast_data_yinghao/music4all" |
|
|
|
pd_list = pd.read_csv(f"{PATH}/id_genres.csv", sep="\t") |
|
genre_dict = pd_list.set_index('id')['genres'].to_dict() |
|
|
|
|
|
pd_list = pd.read_csv(f"{PATH}/id_lang.csv", sep="\t") |
|
lang_dict = pd_list.set_index('id')['lang'].to_dict() |
|
|
|
|
|
pd_list = pd.read_csv(f"{PATH}/id_tags.csv", sep="\t") |
|
tag_dict = pd_list.set_index('id')['tags'].to_dict() |
|
|
|
|
|
test_jsons = json.load(open(f"{PATH}/SongInterpretation/dataset_test.json","r")) |
|
train_jsons = json.load(open(f"{PATH}/SongInterpretation/dataset_not_negative_256_clean.json","r")) |
|
train_list = [i["music4all_id"] for i in train_jsons] |
|
|
|
test_list = [i["music4all_id"] for i in test_jsons] |
|
|
|
|
|
existed_uuid_list = set() |
|
|
|
def get_sample(id, instruction, output, task, split="train"): |
|
data_sample = { |
|
"instruction": instruction, |
|
"input": f"<|SOA|>{id}.wav<|EOA|>", |
|
"output": output, |
|
"uuid": "", |
|
"audioid": f"{id}.wav", |
|
"split": [split], |
|
"task_type": {"major": ["global_MIR"], "minor": [task]}, |
|
"domain": "music", |
|
"source": "Music4All", |
|
"other": {} |
|
} |
|
|
|
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_sample |
|
|
|
|
|
genre_samples, lang_samples, tag_samples = [], [], [] |
|
comment_train_samples, comment_test_samples = [], [] |
|
count = 0 |
|
for id in tqdm.tqdm(genre_dict.keys()): |
|
if id in test_list: |
|
continue |
|
|
|
audio_path = os.path.join(f"{PATH}", f"audios/{id}.wav") |
|
data_sample = get_sample(id, |
|
"What is the genre of this music?", |
|
genre_dict[id], |
|
"genre_classification") |
|
genre_samples.append(data_sample) |
|
if count < 10000: |
|
data_sample = get_sample(id, |
|
"Which language from the following list is this music? List: ['zh-cn', 'de', 'sw', 'el', 'en', 'cy', 'hu', 'ar', 'so', 'lt', 'ja', 'ru', 'es', 'fr', 'sk', 'bg', 'et', 'th', 'sq', 'INTRUMENTAL', 'lv', 'pa', 'cs', 'no', 'hi', 'ca', 'pt', 'ko', 'nl', 'fa', 'sv', 'tr', 'sl', 'bn', 'pl', 'uk', 'id', 'he', 'af', 'ro', 'hr', 'it', 'vi', 'fi', 'tl', 'da']", |
|
lang_dict[id], |
|
"language_detection") |
|
lang_samples.append(data_sample) |
|
if lang_dict[id] == "en": |
|
count += 1 |
|
data_sample = get_sample(id, |
|
"What are the tags of this music?", |
|
tag_dict[id], |
|
"music_tagging") |
|
tag_samples.append(data_sample) |
|
|
|
|
|
|
|
comment_test_samples = [get_sample(data["music4all_id"], |
|
"You are the user of Spotify, please give a commments on the interpretation of the lyrics.", |
|
data["comment"], |
|
"lyrics_interpretation", |
|
split="test") |
|
for data in test_jsons] |
|
comment_train_samples = [get_sample(data["music4all_id"], |
|
"You are the user of Spotify, please give a commments on the interpretation of the lyrics.", |
|
data["comment"], |
|
"lyrics_interpretation") |
|
for data in train_jsons] |
|
|
|
print("genre_samples:", len(genre_samples)) |
|
print("lang_samples:", len(lang_samples)) |
|
print("tag_samples:", len(tag_samples)) |
|
print("comment_train_samples:", len(comment_train_samples)) |
|
print("comment_test_samples:", len(comment_test_samples)) |
|
|
|
output_file_path = f'{PATH}/Music4all_train.jsonl' |
|
with open(output_file_path, 'w') as outfile: |
|
|
|
json.dump( comment_train_samples, outfile) |
|
|
|
|
|
|
|
outfile.close() |
|
|
|
output_file_path = f'{PATH}/Music4all_test.jsonl' |
|
with open(output_file_path, 'w') as outfile: |
|
json.dump(comment_test_samples, outfile) |
|
outfile.close() |
|
|