Sin2pi commited on
Commit
0efd62a
·
verified ·
1 Parent(s): b51e6a1

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
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "metadata": {},
7
- "outputs": [],
8
- "source": [
9
- "from datasets import load_dataset\n",
10
- "import soundfile as sf, os, pandas as pd, re\n",
11
- "from tqdm import tqdm\n",
12
- "\n",
13
- "dataset = load_dataset(\"Sin2pi/JA_audio_JA_text_180k_samples\", trust_remote_code=True, streaming=True, token=\"your_token\")\n",
14
- "\n",
15
- "name = \"JA_audio_JA_text_180k_samples\"\n",
16
- "output_file = 'metadata.csv'\n",
17
- "os.makedirs(\"./datasets/\" + name, exist_ok=True)\n",
18
- "folder_path = \"./datasets/\" + name # Create a folder to store the audio and transcription files\n",
19
- "\n",
20
- "char = '[ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890]'\n",
21
- "special_characters = '[♬「」 ?!;:“%‘” ~♪… ?!゛#$%&()*+:;〈=〉?@^_‘{|}~\"█♩♫♩ ♩』『.;:<>_()*&^%$#@?!`~, \"]'\n",
22
- "dsa = dataset.filter(lambda batch: bool(batch[\"sentence\"])).filter(lambda sample: not re.search(char, sample[\"sentence\"]))\n",
23
- "\n",
24
- "for i, sample in tqdm(enumerate(dsa)): # Process each sample in the filtered dataset\n",
25
- " audio_sample = name + f'_{i}.mp3' # or wav\n",
26
- " audio_path = os.path.join(folder_path, audio_sample)\n",
27
- " transcription_path = os.path.join(folder_path, output_file) # Path to save transcription file \n",
28
- " if not os.path.exists(audio_path):\n",
29
- " sf.write(audio_path, sample['audio']['array'], sample['audio']['sampling_rate'])\n",
30
- " sample[\"audio_length\"] = len(sample[\"audio\"][\"array\"]) / sample[\"audio\"][\"sampling_rate\"] # Get audio length, remove if not needed\n",
31
- " with open(transcription_path, 'a', encoding='utf-8') as transcription_file: # Save transcription file \n",
32
- " transcription_file.write(audio_sample+\",\") # Save transcription file name \n",
33
- " sample[\"sentence\"] = re.sub(special_characters,'', sample[\"sentence\"])\n",
34
- " transcription_file.write(sample['sentence']) # Save transcription \n",
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')