{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "6160ca56",
"metadata": {},
"outputs": [],
"source": [
"# download from https://magichub.com/datasets/malay-conversational-speech-corpus/\n",
"# !unzip Malay_Conversational_Speech_Corpus.zip -d malay-speech"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db1d7259",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AUDIOINFO.txt README.txt SPKINFO.txt\tTXT WAV\r\n"
]
}
],
"source": [
"!ls malay-speech"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c28c25c0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A0004_S008_0_G0369.wav\tA0008_S003_0_G0598.wav\tA0009_S008_0_G0474.wav\r\n",
"A0004_S008_0_G0489.wav\tA0008_S004_0_G0397.wav\tA0009_S008_0_G0591.wav\r\n",
"A0006_S001_0_G0216.wav\tA0008_S004_0_G0598.wav\tA0010_S001_0_G0521.wav\r\n",
"A0006_S001_0_G0712.wav\tA0008_S005_0_G0397.wav\tA0010_S001_0_G0626.wav\r\n",
"A0006_S002_0_G0216.wav\tA0008_S005_0_G0598.wav\tA0010_S002_0_G0521.wav\r\n",
"A0006_S002_0_G0712.wav\tA0009_S004_0_G0474.wav\tA0010_S002_0_G0626.wav\r\n",
"A0008_S003_0_G0397.wav\tA0009_S004_0_G0591.wav\r\n"
]
}
],
"source": [
"!ls malay-speech/WAV"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7e94aed2",
"metadata": {},
"outputs": [],
"source": [
"!mkdir audio-malay-speech"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "b7562ac7",
"metadata": {},
"outputs": [],
"source": [
"import soundfile as sf\n",
"from glob import glob\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f630adef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A0010_S001_0_G0521'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"os.path.split(f)[1].replace('.txt', '')"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "e3480688",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 191/191 [00:02<00:00, 73.04it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 109/109 [00:01<00:00, 74.40it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 156/156 [00:03<00:00, 44.06it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 214/214 [00:03<00:00, 62.35it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 155/155 [00:02<00:00, 69.24it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 224/224 [00:04<00:00, 53.28it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 175/175 [00:02<00:00, 85.67it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 140/140 [00:02<00:00, 59.89it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 225/225 [00:03<00:00, 73.55it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 152/152 [00:02<00:00, 53.09it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 188/188 [00:02<00:00, 72.32it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 192/192 [00:03<00:00, 54.64it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 166/166 [00:03<00:00, 51.48it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 175/175 [00:02<00:00, 77.67it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 143/143 [00:02<00:00, 65.63it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 144/144 [00:02<00:00, 64.00it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 126/126 [00:02<00:00, 42.88it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 119/119 [00:02<00:00, 49.52it/s]\n",
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 55/55 [00:00<00:00, 61.18it/s]\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 212/212 [00:03<00:00, 64.23it/s]\n"
]
}
],
"source": [
"from tqdm import tqdm\n",
"\n",
"files = glob('malay-speech/TXT/*.txt')\n",
"data = []\n",
"for f in files:\n",
" new_f = os.path.split(f)[1].replace('.txt', '')\n",
" audio = f.replace('/TXT/', '/WAV/').replace('.txt', '.wav')\n",
" y, sr = sf.read(audio)\n",
" with open(f) as fopen:\n",
" texts = fopen.read().split('\\n')\n",
" for no in tqdm(range(len(texts))):\n",
" t = texts[no]\n",
" if not len(t):\n",
" continue\n",
" splitted = t.split('\\t')\n",
" start, end = splitted[0].replace('[', '').replace(']', '').split(',')\n",
" start = int(float(start) * sr)\n",
" end = int(float(end) * sr)\n",
" y_ = y[start:end]\n",
" new_f_ = f'audio-malay-speech/{new_f}-{no}.mp3'\n",
" sf.write(new_f_, y_, sr)\n",
" data.append({\n",
" 'filename': new_f_,\n",
" 'Y': splitted[-1],\n",
" 'gender': splitted[-2],\n",
" 'id': splitted[-3]\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "274ac002",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'filename': 'audio-malay-speech/A0010_S001_0_G0521-1.mp3',\n",
" 'Y': 'ah makanan bagi aku macam struggle kan',\n",
" 'gender': 'female,Malaysia',\n",
" 'id': 'G0521'}"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[1]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "3a9a57b6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import IPython.display as ipd\n",
"ipd.Audio(data[1]['filename'])"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "12e41983",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3241"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(data)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "c495bc09",
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, Audio\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "181ee061",
"metadata": {},
"outputs": [],
"source": [
"with open('malay-speech.json', 'w') as fopen:\n",
" json.dump(data, fopen)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "b14e9b4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading and preparing dataset json/default to /home/husein/.cache/huggingface/datasets/json/default-0bb280d8793bbee0/0.0.0/e347ab1c932092252e717ff3f949105a4dd28b27e842dd53157d2f72e276c2e4...\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7b2c25161eaa4f3b9febf9ca68da069f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading data files: 0%| | 0/1 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3caeaea4399241f4b6bb7caa97b3837b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Extracting data files: 0%| | 0/1 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating train split: 0 examples [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset json downloaded and prepared to /home/husein/.cache/huggingface/datasets/json/default-0bb280d8793bbee0/0.0.0/e347ab1c932092252e717ff3f949105a4dd28b27e842dd53157d2f72e276c2e4. Subsequent calls will reuse this data.\n"
]
}
],
"source": [
"dataset = load_dataset('json', data_files = 'malay-speech.json', keep_in_memory = False, split = 'train')"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "bdf06b7d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Y': 'hai weh',\n",
" 'id': 'G0521',\n",
" 'gender': 'female,Malaysia',\n",
" 'filename': 'audio-malay-speech/A0010_S001_0_G0521-0.mp3'}"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset[0]"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "36432ba1",
"metadata": {},
"outputs": [],
"source": [
"dataset = dataset.cast_column('filename', Audio(sampling_rate = 16000))"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "d01099f1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.00693709, 0.01167124, -0.00172221, ..., 0.00830214,\n",
" 0.00946393, 0.00854981])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset[100]['filename']['array']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc3f2a3f",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/3241 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d1082320fe004f7699ab7f3521fd1942",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "23b7ffea61524e12a9ce61d1782106ff",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Creating parquet from Arrow format: 0%| | 0/33 [00:00, ?ba/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dataset.push_to_hub('malaysia-ai/malay-conversational-speech-corpus')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}