File size: 12,772 Bytes
7f3926e |
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "347417aa",
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "131dee3d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Reusing dataset common_voice (/workspace/cache/hf/datasets/mozilla-foundation___common_voice/ga-IE/7.0.0/fe20cac47c166e25b1f096ab661832e3da7cf298ed4a91dcaa1343ad972d175b)\n"
]
},
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['client_id', 'path', 'audio', 'sentence', 'up_votes', 'down_votes', 'age', 'gender', 'accent', 'locale', 'segment'],\n",
" num_rows: 529\n",
"})"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset = load_dataset(\"mozilla-foundation/common_voice_7_0\", 'ga-IE', split=\"train\", use_auth_token = True)\n",
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "05c3ae92",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'An bhfuil do pheannsa sa bhaile, a Shíle'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"audio_sample = dataset[2]\n",
"audio_sample['sentence']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c2edcf22",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'client_id': '7a622ad3217ecf8c2fc6656077a33059732504874d096bd0fc1d239f6e5f39a5861c5faad9e119d588ea80c048c3ab02bfae41fbde22232e89272ac3d5ecc7a4',\n",
" 'path': 'cv-corpus-7.0-2021-07-21/ga-IE/clips/common_voice_ga-IE_17410230.mp3',\n",
" 'audio': {'path': 'cv-corpus-7.0-2021-07-21/ga-IE/clips/common_voice_ga-IE_17410230.mp3',\n",
" 'array': array([ 0. , 0. , 0. , ..., -0.00993466,\n",
" -0.00979847, -0.00967056], dtype=float32),\n",
" 'sampling_rate': 48000},\n",
" 'sentence': 'An bhfuil do pheannsa sa bhaile, a Shíle',\n",
" 'up_votes': 2,\n",
" 'down_votes': 0,\n",
" 'age': '',\n",
" 'gender': '',\n",
" 'accent': '',\n",
" 'locale': 'ga-IE',\n",
" 'segment': ''}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"audio_sample"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f3d9bb64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Git LFS: (2 of 2 files) 3.59 GB / 3.59 GB \n"
]
}
],
"source": [
"!git lfs pull"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "936da5cb",
"metadata": {},
"outputs": [],
"source": [
"from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC\n",
"\n",
"processor = Wav2Vec2Processor.from_pretrained(\"./\")\n",
"model = Wav2Vec2ForCTC.from_pretrained(\"./\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "28659e97",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'client_id': '7a622ad3217ecf8c2fc6656077a33059732504874d096bd0fc1d239f6e5f39a5861c5faad9e119d588ea80c048c3ab02bfae41fbde22232e89272ac3d5ecc7a4',\n",
" 'path': 'cv-corpus-7.0-2021-07-21/ga-IE/clips/common_voice_ga-IE_17410230.mp3',\n",
" 'audio': {'path': 'cv-corpus-7.0-2021-07-21/ga-IE/clips/common_voice_ga-IE_17410230.mp3',\n",
" 'array': array([ 0. , 0. , 0. , ..., -0.00993466,\n",
" -0.00979847, -0.00967056], dtype=float32),\n",
" 'sampling_rate': 48000},\n",
" 'sentence': 'An bhfuil do pheannsa sa bhaile, a Shíle',\n",
" 'up_votes': 2,\n",
" 'down_votes': 0,\n",
" 'age': '',\n",
" 'gender': '',\n",
" 'accent': '',\n",
" 'locale': 'ga-IE',\n",
" 'segment': ''}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"audio_sample"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "e24cacdc",
"metadata": {},
"outputs": [],
"source": [
"inputs = processor(audio_sample[\"audio\"][\"array\"], sampling_rate=16000, return_tensors=\"pt\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "d9972307",
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"\n",
"with torch.no_grad():\n",
" logits = model(**inputs).logits"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "b78e3ea6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([1, 611, 34])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logits.shape"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "b1692f0a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'anrúir car nria cn nn non mneanaénnni'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predicted_ids = torch.argmax(logits, dim=-1)\n",
"transcription = processor.batch_decode(predicted_ids)\n",
"\n",
"transcription[0].lower()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "ecf01625",
"metadata": {},
"outputs": [],
"source": [
"with open(\"text.txt\", \"w\") as file:\n",
" file.write(\" \".join(dataset['sentence']))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "40067117",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== 1/5 Counting and sorting n-grams ===\n",
"Reading /workspace/wav2vec2-xls-r-1b-ir/text.txt\n",
"----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
"****************************************************************************************************\n",
"Unigram tokens 3960 types 1431\n",
"=== 2/5 Calculating and sorting adjusted counts ===\n",
"Chain sizes: 1:17172 2:14475761664 3:27142055936 4:43427287040 5:63331463168\n",
"Statistics:\n",
"1 1430 D1=0.788367 D2=1.34216 D3+=0.97277\n",
"2 3029 D1=0.885256 D2=1.28784 D3+=1.81966\n",
"3 3538 D1=0.95385 D2=1.62076 D3+=1.26573\n",
"4 3709 D1=0.979641 D2=1.38128 D3+=2.02036\n",
"5 3789 D1=0.942754 D2=1.59232 D3+=2.05725\n",
"Memory estimate for binary LM:\n",
"type kB\n",
"probing 343 assuming -p 1.5\n",
"probing 409 assuming -r models -p 1.5\n",
"trie 160 without quantization\n",
"trie 98 assuming -q 8 -b 8 quantization \n",
"trie 154 assuming -a 22 array pointer compression\n",
"trie 92 assuming -a 22 -q 8 -b 8 array pointer compression and quantization\n",
"=== 3/5 Calculating and sorting initial probabilities ===\n",
"Chain sizes: 1:17160 2:48464 3:70760 4:89016 5:106092\n",
"----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
"####################################################################################################\n",
"=== 4/5 Calculating and writing order-interpolated probabilities ===\n",
"Chain sizes: 1:17160 2:48464 3:70760 4:89016 5:106092\n",
"----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
"####################################################################################################\n",
"=== 5/5 Writing ARPA model ===\n",
"----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
"****************************************************************************************************\n",
"Name:lmplz\tVmPeak:145097716 kB\tVmRSS:6968 kB\tRSSMax:25576636 kB\tuser:2.61395\tsys:13.3051\tCPU:15.9192\treal:15.8981\n"
]
}
],
"source": [
"!../kenlm/build/bin/lmplz -o 5 <\"text.txt\" > \"5gram.arpa\""
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "ab7fc7d0",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoProcessor\n",
"\n",
"processor = AutoProcessor.from_pretrained('./')"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "4d994ae5",
"metadata": {},
"outputs": [],
"source": [
"vocab_dict = processor.tokenizer.get_vocab()\n",
"sorted_vocab_dict = {k.lower(): v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "3dd24709",
"metadata": {},
"outputs": [],
"source": [
"with open(\"5gram.arpa\", \"r\") as read_file, open(\"5gram_correct.arpa\", \"w\") as write_file:\n",
" has_added_eos = False\n",
" for line in read_file:\n",
" if not has_added_eos and \"ngram 1=\" in line:\n",
" count=line.strip().split(\"=\")[-1]\n",
" write_file.write(line.replace(f\"{count}\", f\"{int(count)+1}\"))\n",
" elif not has_added_eos and \"<s>\" in line:\n",
" write_file.write(line)\n",
" write_file.write(line.replace(\"<s>\", \"</s>\"))\n",
" has_added_eos = True\n",
" else:\n",
" write_file.write(line)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65607d2d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 48,
"id": "9f2c0244",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Found entries of length > 1 in alphabet. This is unusual unless style is BPE, but the alphabet was not recognized as BPE type. Is this correct?\n",
"Unigrams and labels don't seem to agree.\n"
]
}
],
"source": [
"from pyctcdecode import build_ctcdecoder\n",
"\n",
"decoder = build_ctcdecoder(\n",
" labels=list(sorted_vocab_dict.keys()),\n",
" kenlm_model_path=\"5gram_correct.arpa\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "017e8d2c",
"metadata": {},
"outputs": [],
"source": [
"from transformers import Wav2Vec2ProcessorWithLM\n",
"\n",
"processor_with_lm = Wav2Vec2ProcessorWithLM(\n",
" feature_extractor=processor.feature_extractor,\n",
" tokenizer=processor.tokenizer,\n",
" decoder=decoder\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "3b4c2228",
"metadata": {},
"outputs": [],
"source": [
"processor_with_lm.save_pretrained(\"xls-r-1b-ir\")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "786587f0",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'repo' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-55-b8e08bd9412e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrepo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpush_to_hub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcommit_message\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Upload lm-boosted decoder\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'repo' is not defined"
]
}
],
"source": [
"repo.push_to_hub(commit_message=\"Upload lm-boosted decoder\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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
}
|