File size: 8,843 Bytes
75e6bf2 1a48339 75e6bf2 1a48339 75e6bf2 1a48339 e10d376 1a48339 75e6bf2 1a48339 75e6bf2 e256a57 75e6bf2 1a48339 e983d05 75e6bf2 e983d05 1a48339 2a395ac 75e6bf2 e983d05 75e6bf2 1a48339 75e6bf2 e10d376 1a48339 e10d376 75e6bf2 1a48339 e10d376 1a48339 75e6bf2 1a48339 75e6bf2 e10d376 1a48339 e10d376 1a48339 e10d376 1a48339 e10d376 75e6bf2 e10d376 75e6bf2 1a48339 75e6bf2 1a48339 75e6bf2 e983d05 1a48339 e983d05 75e6bf2 1a48339 75e6bf2 e256a57 75e6bf2 1a48339 75e6bf2 e10d376 75e6bf2 1a48339 75e6bf2 1a48339 75e6bf2 1a48339 75e6bf2 e10d376 75e6bf2 1a48339 75e6bf2 1a48339 75e6bf2 |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "33e4a305",
"metadata": {},
"outputs": [],
"source": [
"from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, AutoModelForCTC, Wav2Vec2Processor\n",
"from datasets import load_dataset, load_metric, Audio\n",
"from pyctcdecode import build_ctcdecoder\n",
"from pydub import AudioSegment\n",
"from pydub.playback import play\n",
"\n",
"import numpy as np\n",
"import torch\n",
"import kenlm\n",
"import pandas as pd\n",
"import random\n",
"import soundfile as sf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "328d0662",
"metadata": {},
"outputs": [],
"source": [
"model = AutoModelForCTC.from_pretrained(\".\")\n",
"processor = Wav2Vec2Processor.from_pretrained(\".\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "0fea2518",
"metadata": {},
"outputs": [],
"source": [
"# model = AutoModelForCTC.from_pretrained(\"vitouphy/xls-r-300m-km\").to('cuda')\n",
"# processor = Wav2Vec2Processor.from_pretrained(\"vitouphy/xls-r-300m-km\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9cfef23c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using custom data configuration default-36119ec2a15afb82\n",
"Reusing dataset csv (/workspace/.cache/huggingface/datasets/csv/default-36119ec2a15afb82/0.0.0/6b9057d9e23d9d8a2f05b985917a0da84d70c5dae3d22ddd8a3f22fb01c69d9e)\n"
]
}
],
"source": [
"common_voice_test = (load_dataset('csv', data_files='km_kh_male/line_index_test.csv', split = 'train')\n",
" .remove_columns([\"Unnamed: 0\", \"drop\"])\n",
" .rename_column('text', 'sentence')\n",
" .cast_column(\"path\", Audio(sampling_rate=16_000)).rename_column('path', 'audio'))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "29e6bb1a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'audio': {'path': '/workspace/xls-r-300m-km/km_kh_male/wavs/khm_3154_2555595821.wav',\n",
" 'array': array([ 0.00014737, 0.00016698, 0.00013704, ..., -0.00011244,\n",
" -0.0001059 , -0.00011476], dtype=float32),\n",
" 'sampling_rate': 16000},\n",
" 'sentence': 'ααΆα ααααΎ α’αΆααΈααααα ααα ααααΌα αα
ααααα»ααΆ'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"common_voice_test[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0554b8d8",
"metadata": {},
"outputs": [],
"source": [
"def prepare_dataset(batch):\n",
" audio = batch[\"audio\"]\n",
" \n",
" # batched output is \"un-batched\"\n",
" batch[\"input_values\"] = processor(np.array(audio[\"array\"]), sampling_rate=audio[\"sampling_rate\"]).input_values[0]\n",
" batch[\"input_length\"] = len(batch[\"input_values\"])\n",
" \n",
" with processor.as_target_processor():\n",
" batch[\"labels\"] = processor(batch[\"sentence\"]).input_ids\n",
" return batch"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d26a6659",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading cached processed dataset at /workspace/.cache/huggingface/datasets/csv/default-36119ec2a15afb82/0.0.0/6b9057d9e23d9d8a2f05b985917a0da84d70c5dae3d22ddd8a3f22fb01c69d9e/cache-081703c0621182da.arrow\n"
]
}
],
"source": [
"common_voice_test = common_voice_test.map(prepare_dataset, remove_columns=common_voice_test.column_names)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "04a94f74",
"metadata": {},
"outputs": [],
"source": [
"i = 25"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3993d2c4",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"It is strongly recommended to pass the ``sampling_rate`` argument to this function. Failing to do so can result in silent errors that might be hard to debug.\n"
]
}
],
"source": [
"input_dict = processor(common_voice_test[i][\"input_values\"], return_tensors=\"pt\", padding=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7e3026dc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'input_values': tensor([[ 2.8537e-04, 2.5043e-04, 2.7738e-04, ..., -4.8949e-05,\n",
" -1.1382e-04, 2.7166e-04]]), 'attention_mask': tensor([[1, 1, 1, ..., 1, 1, 1]], dtype=torch.int32)}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_dict"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "adf215c0",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"It is strongly recommended to pass the ``sampling_rate`` argument to this function. Failing to do so can result in silent errors that might be hard to debug.\n"
]
}
],
"source": [
"input_dict = processor(common_voice_test[i][\"input_values\"], return_tensors=\"pt\", padding=True)\n",
"logits = model(input_dict.input_values.to(\"cuda\")).logits\n",
"pred_ids = torch.argmax(logits, dim=-1)[0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e8310629",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([ 1, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,\n",
" 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 10, 70, 70, 70, 10, 72,\n",
" 43, 72, 72, 72, 72, 72, 72, 0, 0, 72, 72, 18, 72, 54, 72, 72, 72, 72,\n",
" 72, 0, 72, 21, 72, 49, 72, 72, 72, 72, 72, 72, 23, 70, 70, 27, 72, 46,\n",
" 72, 72, 72, 1, 72, 0, 0, 30, 72, 72, 72, 72, 25, 70, 70, 72, 72, 11,\n",
" 55, 72, 72, 72, 72, 5, 72, 0, 20, 58, 72, 72, 72, 0, 0, 16, 72, 72,\n",
" 72, 20, 70, 70, 72, 72, 16, 70, 27, 72, 72, 72, 72, 72, 45, 0, 0, 30,\n",
" 30, 70, 70, 27, 72, 43, 72, 72, 72, 72, 72, 72, 21, 72, 53, 72, 72, 72,\n",
" 27, 72, 0, 1, 72, 72, 72, 72, 25, 70, 23, 23, 48, 72, 72, 72, 72, 72,\n",
" 72, 8, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,\n",
" 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,\n",
" 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,\n",
" 72, 72, 72, 72, 72, 72, 72, 72, 43], device='cuda:0')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pred_ids"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5dd986a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prediction:\n",
"αααααΆ αα ααΌααααΉα αααααα αα
αααααααΈ ααααΆααα ααααα»ααΆ\n",
"\n",
"Reference:\n",
"αααααΆ ααα ααΌααααΉααα αααααα αα
αααααααΈ ααααΆααα ααααα»ααΆ\n"
]
}
],
"source": [
"print(\"Prediction:\")\n",
"pred_ids = pred_ids[pred_ids != processor.tokenizer.pad_token_id]\n",
"print(processor.decode(pred_ids))\n",
"\n",
"print(\"\\nReference:\")\n",
"print(processor.decode(common_voice_test['labels'][i]))\n",
"# print(common_voice_test_transcription[0][\"sentence\"].lower())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e39b112",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "562af933",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|