{ "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": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nena_speech_1_0_test 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 }