File size: 14,275 Bytes
a53d524 04c27c9 a53d524 04c27c9 a53d524 c0279e6 04c27c9 c0279e6 a53d524 04c27c9 a53d524 04c27c9 a53d524 04c27c9 a53d524 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 a53d524 04c27c9 a53d524 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 a53d524 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 04c27c9 c0279e6 a53d524 c0279e6 04c27c9 a53d524 04c27c9 a53d524 c0279e6 a53d524 c0279e6 a53d524 04c27c9 a53d524 |
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5920c653-448e-43b3-93eb-12d7073ad352",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/espnet/tools/anaconda/envs/espnet/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from espnet2.bin.asr_inference import Speech2Text\n",
"from espnet2.bin.asr_align import CTCSegmentation\n",
"import soundfile\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "83058587-1a8a-4b01-92ff-e9125fbe55a3",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import torch\n",
"torch.set_num_threads(1)"
]
},
{
"cell_type": "markdown",
"id": "32eb58d1-5edd-4cc1-9585-daa7f16efd05",
"metadata": {},
"source": [
"## Load model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5e4670d6-0949-48cf-b6b1-d9cc4cf3ad65",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"speech2text = Speech2Text(\"exp/config.yaml\", \"exp/valid.acc.ave_10best.pth\", quantize_asr_model=True, quantize_lm=True)"
]
},
{
"cell_type": "markdown",
"id": "3192656d-6dce-4973-a649-f7ab0c72c386",
"metadata": {
"tags": []
},
"source": [
"## Load example audiofile to transcribe"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e8120e8e-3718-4a1a-ab7a-46ef98a6bc11",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"speech, rate = soundfile.read(\"example_audio/emt16k.wav\")\n",
"assert rate == 16000"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "eec8d4b2-c27a-4780-aeed-8aa7538f70e5",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.64 s, sys: 6.23 ms, total: 2.65 s\n",
"Wall time: 2.66 s\n"
]
}
],
"source": [
"%time text, *_ = speech2text(speech)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "39f41a8b-94c3-42d6-a989-6c7183a6f94d",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mina tahaksin homme täna ja homme kui saan kolm krampsumas ise müüki panna\n"
]
}
],
"source": [
"print(text[0])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "812060a6-90de-4134-8d1f-9f3d98853bc2",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Input File : 'example_audio/emt16k.wav'\n",
"Channels : 1\n",
"Sample Rate : 16000\n",
"Precision : 16-bit\n",
"Duration : 00:00:12.74 = 203815 samples ~ 955.383 CDDA sectors\n",
"File Size : 408k\n",
"Bit Rate : 256k\n",
"Sample Encoding: 16-bit Signed Integer PCM\n",
"\n"
]
}
],
"source": [
"!soxi example_audio/emt16k.wav"
]
},
{
"cell_type": "markdown",
"id": "7d07e8a4-1dbf-4a79-bdf0-aeaeb160ba19",
"metadata": {},
"source": [
"## Example token level alignment"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e6b7331c-52f1-4162-b564-2e6a08b325b0",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:root:No RNN model detected; memory consumption may be high.\n"
]
}
],
"source": [
"aligner = CTCSegmentation(\"exp/config.yaml\", \"exp/valid.acc.ave_10best.pth\" , kaldi_style_text=False, blank_transition_cost_zero=True)\n",
"segments = aligner(speech, text[0].split())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e6d18b5f-3d2a-4fcf-bf4e-00480e58094a",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"utt_0000 utt 0.36 0.78 -0.0001 mina\n",
"utt_0001 utt 0.78 1.19 -0.0003 tahaksin\n",
"utt_0002 utt 1.19 1.59 -0.0017 homme\n",
"utt_0003 utt 1.67 2.19 -0.0001 täna\n",
"utt_0004 utt 3.24 3.76 -0.0037 ja\n",
"utt_0005 utt 3.76 4.28 -0.0000 homme\n",
"utt_0006 utt 5.61 6.13 -0.0001 kui\n",
"utt_0007 utt 6.17 6.69 -0.0009 saan\n",
"utt_0008 utt 7.98 8.50 -0.2285 kolm\n",
"utt_0009 utt 8.50 9.34 -0.1062 krampsumas\n",
"utt_0010 utt 9.34 9.54 -0.1183 ise\n",
"utt_0011 utt 9.54 10.07 -0.2588 müüki\n",
"utt_0012 utt 10.07 10.31 -0.1041 panna\n",
"\n"
]
}
],
"source": [
"print(segments)"
]
},
{
"cell_type": "markdown",
"id": "77f82a7d-08dc-40cb-88e5-48ef8c36af7d",
"metadata": {
"tags": []
},
"source": [
"## Get timestamps with some correction"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ae9f7e3f-b75d-4bcb-98d1-ae2f037fb4af",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def get_timestamps(aligner, speech, text, time_correction=0.2):\n",
" tokens=text.split()\n",
" segments = aligner(speech, tokens)\n",
" df=pd.DataFrame(segments.segments)\n",
" df.columns=['start', 'end', 'confidence']\n",
" df['start']=df.start+time_correction\n",
" df['end']=df.end+time_correction\n",
" df['words']=tokens\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0215d312-1896-43f1-9782-c92aced787b7",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.96 s, sys: 19 ms, total: 2.98 s\n",
"Wall time: 2.98 s\n"
]
}
],
"source": [
"speech, rate = soundfile.read(\"example_audio/oden_kypsis16k_subset2.wav\")\n",
"assert rate == 16000\n",
"\n",
"%time text, *_ = speech2text(speech)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d31d6840-3a80-411a-969c-05f4a5e3e9a1",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Input File : 'example_audio/oden_kypsis16k_subset2.wav'\n",
"Channels : 1\n",
"Sample Rate : 16000\n",
"Precision : 16-bit\n",
"Duration : 00:00:09.19 = 146983 samples ~ 688.983 CDDA sectors\n",
"File Size : 294k\n",
"Bit Rate : 256k\n",
"Sample Encoding: 16-bit Signed Integer PCM\n",
"\n"
]
}
],
"source": [
"!soxi example_audio/oden_kypsis16k_subset2.wav"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "53f3b63f-9b40-432b-b58c-f5b7223252ed",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 309 ms, sys: 8.51 ms, total: 318 ms\n",
"Wall time: 312 ms\n"
]
}
],
"source": [
"%time df_times=get_timestamps(aligner, speech, text[0])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1b4dd747-4be2-4ace-a301-6207f7dd9a71",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>start</th>\n",
" <th>end</th>\n",
" <th>confidence</th>\n",
" <th>words</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.260173</td>\n",
" <td>0.661328</td>\n",
" <td>-0.049087</td>\n",
" <td>klikid</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.661328</td>\n",
" <td>0.821789</td>\n",
" <td>-0.003573</td>\n",
" <td>neid</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.823233</td>\n",
" <td>1.784560</td>\n",
" <td>-0.001952</td>\n",
" <td>allserva</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.784560</td>\n",
" <td>1.985137</td>\n",
" <td>-0.034099</td>\n",
" <td>tekivad</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2.548197</td>\n",
" <td>3.068255</td>\n",
" <td>-0.000037</td>\n",
" <td>need</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>3.068255</td>\n",
" <td>4.031025</td>\n",
" <td>-0.008919</td>\n",
" <td>lubaküpsiseid</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.754546</td>\n",
" <td>5.274604</td>\n",
" <td>-0.000385</td>\n",
" <td>mis</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.274604</td>\n",
" <td>5.415008</td>\n",
" <td>-0.078755</td>\n",
" <td>on</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>5.415008</td>\n",
" <td>5.555412</td>\n",
" <td>-0.000224</td>\n",
" <td>nagu</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>5.555412</td>\n",
" <td>5.836220</td>\n",
" <td>-0.000488</td>\n",
" <td>ilusti</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>5.836220</td>\n",
" <td>6.117029</td>\n",
" <td>-0.002274</td>\n",
" <td>kohati</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>6.238818</td>\n",
" <td>7.039684</td>\n",
" <td>-0.013956</td>\n",
" <td>tõlgitud</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>7.039684</td>\n",
" <td>7.240261</td>\n",
" <td>-0.002010</td>\n",
" <td>eesti</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>7.240261</td>\n",
" <td>7.681531</td>\n",
" <td>-0.002761</td>\n",
" <td>keelde</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>7.803320</td>\n",
" <td>8.323378</td>\n",
" <td>-0.001533</td>\n",
" <td>see</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>8.323378</td>\n",
" <td>8.644302</td>\n",
" <td>-0.044506</td>\n",
" <td>idee</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>8.644302</td>\n",
" <td>9.326264</td>\n",
" <td>-0.215737</td>\n",
" <td>arusaadavamaks</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" start end confidence words\n",
"0 0.260173 0.661328 -0.049087 klikid\n",
"1 0.661328 0.821789 -0.003573 neid\n",
"2 0.823233 1.784560 -0.001952 allserva\n",
"3 1.784560 1.985137 -0.034099 tekivad\n",
"4 2.548197 3.068255 -0.000037 need\n",
"5 3.068255 4.031025 -0.008919 lubaküpsiseid\n",
"6 4.754546 5.274604 -0.000385 mis\n",
"7 5.274604 5.415008 -0.078755 on\n",
"8 5.415008 5.555412 -0.000224 nagu\n",
"9 5.555412 5.836220 -0.000488 ilusti\n",
"10 5.836220 6.117029 -0.002274 kohati\n",
"11 6.238818 7.039684 -0.013956 tõlgitud\n",
"12 7.039684 7.240261 -0.002010 eesti\n",
"13 7.240261 7.681531 -0.002761 keelde\n",
"14 7.803320 8.323378 -0.001533 see\n",
"15 8.323378 8.644302 -0.044506 idee\n",
"16 8.644302 9.326264 -0.215737 arusaadavamaks"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_times.head(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a4be2b1-5e0f-4558-8097-b37be0b83785",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e9d45ad-c8fc-4bab-9285-b82ff3903702",
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|