Read data is extremely slow
Is it my problem that the speed is particularly slow when reading data, and how can I solve it?
Here is my code
from datasets import load_dataset
import soundfile as sf
import os
from tqdm import tqdm
import os.path as osp
import json
data = json.load(open("/mnt/workspace/haonan/data/genshin/simon3000/genshin-voice/result.json", "r"))
print(f"number of all samples: {len(data)}")
name_list = []
for k, v in tqdm(data.items()):
if v['language'] == "English(US)" and v['speaker'] != '':
name_list.append(v['speaker'])
name_list = list(set(name_list))
print(f"number of zh and en roles: {len(name_list)}")
Load the dataset
dataset = load_dataset('/mnt/workspace/haonan/data/genshin/simon3000/genshin-voice', split='train', streaming=True)
for name in tqdm(name_list):
Filter the dataset for Chinese voices of name with transcriptions
chinese_name = dataset.filter(lambda voice: voice['language'] == 'English(US)' and voice['speaker'] == name and voice['transcription'] != '')
Create a folder to store the audio and transcription files
name_folder = name.lower()
name_folder = osp.join("/mnt/workspace/haonan/data/genshin/simon3000/genshin-voice/data_en", name_folder)
os.makedirs(name_folder, exist_ok=True)
Process each voice in the filtered dataset
for i, voice in tqdm(enumerate(chinese_name)):
audio_path = os.path.join(name_folder, f'{i}_audio.wav') # Path to save the audio file
transcription_path = os.path.join(name_folder, f'{i}_transcription.txt') # Path to save the transcription file
# Save the audio file
sf.write(audio_path, voice['audio']['array'], voice['audio']['sampling_rate'])
# Save the transcription file
with open(transcription_path, 'w') as transcription_file:
transcription_file.write(voice['transcription'])
Solved?
Done. Change the streaming=True
to False
.