File size: 6,090 Bytes
d067682 8434d7c d067682 8434d7c d067682 3428b9a d067682 3428b9a d067682 3428b9a 8434d7c 3428b9a 8434d7c 3428b9a 8434d7c 3428b9a d067682 8434d7c d067682 3428b9a d067682 3428b9a d067682 3428b9a d067682 3428b9a d067682 3428b9a d067682 3428b9a 8434d7c 3428b9a 8434d7c 3428b9a d067682 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating the NENA Speech Dataset"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Repo card metadata block was not found. Setting CardData to empty.\n"
]
},
{
"data": {
"text/plain": [
"<nena_speech_1_0.NENASpeech at 0x15ae1c0d0>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from nena_speech_1_0 import NENASpeech\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download validated examples from Pocketbase"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pocketbase import PocketBase\n",
"\n",
"def get_examples():\n",
" pb = PocketBase('https://pocketbase.nenadb.dev/')\n",
"\n",
" examples = pb.collection(\"examples\").get_full_list(query_params={\n",
" \"expand\": \"dialect\",\n",
" \"filter\": \"validated=true\",\n",
" })\n",
"\n",
" return examples"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"examples = get_examples()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bucket examples into subsets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def split_examples(examples, test_split=0.10, dev_split=0.10):\n",
" subsets = {}\n",
"\n",
" for example in examples:\n",
" dialect = example.expand['dialect'].name.lower()\n",
" if not subsets.get(dialect):\n",
" subsets[dialect] = { 'all': [] }\n",
" subsets[dialect]['all'].append(example)\n",
"\n",
" for subset in subsets.values():\n",
" for i, example in enumerate(subset['all']):\n",
" prog = i / len(subset['all'])\n",
"\n",
" if prog < test_split:\n",
" split = 'test'\n",
" elif prog < dev_split + test_split:\n",
" split = 'dev'\n",
" else:\n",
" split = 'train'\n",
"\n",
" if not subset.get(split):\n",
" subset[split] = []\n",
" subset[split].append(example)\n",
" \n",
" del subset['all']\n",
"\n",
" return subsets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"subsets = split_examples(examples)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create shards"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pydub import AudioSegment\n",
"import requests\n",
"import tempfile\n",
"import tarfile\n",
"import shutil\n",
"import os\n",
"import csv\n",
"\n",
"def save_data(subsets):\n",
" for dialect, subset in subsets.items():\n",
" for split, examples in subset.items():\n",
" audio_dir_path = os.path.join(\"audio\", dialect, split)\n",
" os.makedirs(audio_dir_path, exist_ok=True)\n",
"\n",
" transcripts = []\n",
" transcript_dir_path = os.path.join(\"transcript\", dialect)\n",
" os.makedirs(transcript_dir_path, exist_ok=True)\n",
" \n",
" for example in examples:\n",
" pb = PocketBase('https://pocketbase.nenadb.dev/')\n",
" audio_url = pb.get_file_url(example, example.speech, {})\n",
" response = requests.get(audio_url)\n",
" with tempfile.NamedTemporaryFile() as f:\n",
" f.write(response.content)\n",
" f.flush()\n",
" audio = AudioSegment.from_file(f.name)\n",
" audio = audio.set_frame_rate(48000)\n",
" audio_file_name = f\"nena_speech_{example.id}.mp3\"\n",
" audio_file_path = os.path.join(audio_dir_path, audio_file_name)\n",
" audio.export(audio_file_path, format=\"mp3\")\n",
" \n",
" transcripts.append({\n",
" 'age': example.age,\n",
" 'transcription': example.transcription,\n",
" 'translation': example.translation,\n",
" 'path': audio_file_name,\n",
" })\n",
"\n",
" audio_tar_path = f\"{audio_dir_path}.tar\"\n",
" with tarfile.open(audio_tar_path, 'w') as tar:\n",
" tar.add(audio_dir_path, arcname=os.path.basename(audio_dir_path))\n",
"\n",
" with open(os.path.join(transcript_dir_path, f\"{split}.tsv\"), 'w', newline='') as f:\n",
" writer = csv.DictWriter(f, fieldnames=transcripts[0].keys(), delimiter='\\t')\n",
" writer.writeheader()\n",
" writer.writerows(transcripts)\n",
"\n",
" shutil.rmtree(audio_dir_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"save_data(subsets)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|