File size: 2,143 Bytes
a5bde6b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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()
|