import json import tqdm import re import os from pydub import AudioSegment ann_path = "/work/fast_data_yinghao/116_FreesoundLoopDataset/sft_data-116_FreesoundLoopDataset-genre-classification.jsonl" with open(ann_path, "r") as file: tmp1 = file.read() tmp1 = [ eval(tmp) for tmp in tmp1.split("\n")[:-1]] tmp1 = [i for i in tmp1 if i["output"] != "[]" and i["input"] != "[] \n"] for data in tmp1: data["output"] = ",".join(eval(data["output"])) data["input"] = re.sub("\n","",data["input"]) data["audioid"] = data["input"].split("<|SOA|>")[1].split("<|EOA|>")[0][25:] + ".wav" data["split"] = [data["split"]] ann_path = "/work/fast_data_yinghao/116_FreesoundLoopDataset/sft_data-116_FreesoundLoopDataset-key_detection.jsonl" with open(ann_path, "r") as file: tmp2 = file.read() tmp2 = [ eval(tmp) for tmp in tmp2.split("\n")[:-1]] for data in tmp2: if data["output"] == "A": data["output"] = data["input"].split("A:")[1].split("\n")[0][9:] elif data["output"] == "B": data["output"] = data["input"].split("B:")[1].split("\n")[0][9:] data["output"] = re.sub("#","\u266f",data["output"]) data["audioid"] = data["input"].split("<|SOA|>")[1].split("<|EOA|>")[0][25:] + ".wav.wav" file_path = os.path.join(f"/work/fast_data_yinghao/116_FreesoundLoopDataset/audio/wav", data["audioid"]) if not os.path.exists(file_path): data["audioid"] = data["audioid"][:-8] + ".aiff.wav" data["input"] = data["input"].split("option")[0].split("dio:")[1] data["split"] = [data["split"]] data_samples = tmp1 + tmp2 new_data_samples = [] train, valid, test = [], [], [] for data in tqdm.tqdm(data_samples): file_path = os.path.join(f"/work/fast_data_yinghao/116_FreesoundLoopDataset/audio/wav", data["audioid"]) audio = AudioSegment.from_file(file_path) if len(audio) < 5000: continue if data["split"][0] == "train": train.append(data) elif data["split"][0] == "dev": valid.append(data) else: test.append(data) for split in ["train", "valid", "test"]: with open(f"FSLD_{split}.jsonl", 'w') as outfile: for sample in eval(split): json.dump(sample, outfile) outfile.write('\n') outfile.close() # print(f"Data successfully transformed and saved to {output_file_path}")