Update code_to_download_datasets_in_wav_or_mp3.ipynb
Browse files
code_to_download_datasets_in_wav_or_mp3.ipynb
CHANGED
|
@@ -1,47 +1,34 @@
|
|
| 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 |
-
" transcription_file.write(str(\",\"+str(sample['audio_length']))) # Save audio length, remove if not needed\n",
|
| 36 |
-
" transcription_file.write('\\n') "
|
| 37 |
-
]
|
| 38 |
-
}
|
| 39 |
-
],
|
| 40 |
-
"metadata": {
|
| 41 |
-
"language_info": {
|
| 42 |
-
"name": "python"
|
| 43 |
-
}
|
| 44 |
-
},
|
| 45 |
-
"nbformat": 4,
|
| 46 |
-
"nbformat_minor": 2
|
| 47 |
-
}
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
import soundfile as sf, os, pandas as pd, re
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
|
| 5 |
+
dataset = load_dataset("mozilla-foundation/common_voice_17_0", "ja", split="other", trust_remote_code=True, streaming=True, token="your_token")
|
| 6 |
+
|
| 7 |
+
name = "other"
|
| 8 |
+
ouput_dir = "./datasets/CV17/"
|
| 9 |
+
output_file = 'metadata.csv'
|
| 10 |
+
os.makedirs(ouput_dir + name, exist_ok=True)
|
| 11 |
+
folder_path = ouput_dir + name # Create a folder to store the audio and transcription files
|
| 12 |
+
|
| 13 |
+
char = '[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890]'
|
| 14 |
+
special_characters = '[♬「」?!“%‘”~♪…?!゛#$%&()*+:;〈=〉@^_{|}~"█♩♫』『.;:<>_()*&^$#@`, ]'
|
| 15 |
+
|
| 16 |
+
dsa = (dataset
|
| 17 |
+
.filter(lambda sample: bool(sample["sentence"]))
|
| 18 |
+
.filter(lambda sample: not re.search(char, sample["sentence"]))
|
| 19 |
+
.filter(lambda sample: sample["down_votes"] == 0)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
for i, sample in tqdm(enumerate(dsa)): # Process each sample in the filtered dataset
|
| 23 |
+
audio_sample = name + f'_{i}.wav' # or wav
|
| 24 |
+
audio_path = os.path.join(folder_path, audio_sample)
|
| 25 |
+
transcription_path = os.path.join(folder_path, output_file) # Path to save transcription file
|
| 26 |
+
if not os.path.exists(audio_path):
|
| 27 |
+
sf.write(audio_path, sample['audio']['array'], sample['audio']['sampling_rate'])
|
| 28 |
+
sample["audio_length"] = len(sample["audio"]["array"]) / sample["audio"]["sampling_rate"] # Get audio length, remove if not needed
|
| 29 |
+
with open(transcription_path, 'a', encoding='utf-8') as transcription_file: # Save transcription file
|
| 30 |
+
transcription_file.write(audio_sample+",") # Save transcription file name
|
| 31 |
+
sample["sentence"] = re.sub(special_characters,'', sample["sentence"])
|
| 32 |
+
transcription_file.write(sample['sentence']) # Save transcription
|
| 33 |
+
transcription_file.write(str(","+str(sample['audio_length']))) # Save audio length, remove if not needed
|
| 34 |
+
transcription_file.write('\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|