import hashlib import json import tqdm import os import pandas as pd PATH = "/work/fast_data_yinghao/SDD" existed_uuid_list = set() pd_list = pd.read_csv(f"{PATH}/song_describer.csv") caption_dict = pd_list.set_index('caption_id')['caption'].to_dict() path_dict = pd_list.set_index('caption_id')['path'].to_dict() for split in [ "test"]: data_samples = [] for key in tqdm.tqdm(caption_dict.keys()): audio_path = os.path.join(f"{PATH}", f"audio/{path_dict[key]}") if not audio_path.endswith('.wav'): audio_path = audio_path.split(".")[0] + ".wav" data_sample = { "instruction": "Please provide the caption of the given audio.", "input": f"<|SOA|>{path_dict[key]}<|EOA|>", "output": caption_dict[key], "uuid": "", "audioid": f"{audio_path}", "split": [split], "task_type": {"major": ["global_MIR"], "minor": ["music_captioning"]}, "domain": "music", "source": "Youtubet", "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}" # try to load the audio file data_samples.append(data_sample) # print(data_samples) # break # Save to JSONL format output_file_path = f'{PATH}/sdd_{split}.jsonl' # Replace with the desired output path with open(output_file_path, 'w') as outfile: # for sample in data_samples: json.dump(data_samples, outfile) # outfile.write('\n') outfile.close()