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() # genres = set([j for i in genre_dict.values() for j in i.split(",")]) pd_list = pd.read_csv(f"{PATH}/id_lang.csv", sep="\t") lang_dict = pd_list.set_index('id')['lang'].to_dict() # langs = set([j for i in lang_dict.values() for j in i.split(",")]) pd_list = pd.read_csv(f"{PATH}/id_tags.csv", sep="\t") tag_dict = pd_list.set_index('id')['tags'].to_dict() # tags = set([j for i in tag_dict.values() for j in i.split(",")]) 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] # train_dict = {i["music4all_id"]:i["comment"] for i in train_jsons} 1 id may have multiple comments test_list = [i["music4all_id"] for i in test_jsons] # test_dict = {i["music4all_id"]:i["comment"] 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": {} } # change uuid uuid_string = f"{data_sample['instruction']}#{data_sample['input']}#{data_sample['output']}" unique_id = hashlib.md5(uuid_string.encode()).hexdigest()[:16] #只取前16位 if unique_id in existed_uuid_list: sha1_hash = hashlib.sha1(uuid_string.encode()).hexdigest()[:16] # 为了相加的时候位数对应上 # 将 MD5 和 SHA1 结果相加,并计算新的 MD5 作为最终的 UUID 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) # break 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)) # Save to JSONL format output_file_path = f'{PATH}/Music4all_train.jsonl' # Replace with the desired output path with open(output_file_path, 'w') as outfile: # for sample in data_samples: json.dump( comment_train_samples, outfile) # genre_samples + tag_samples + lang_samples + # outfile.write('\n') 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()