File size: 35,547 Bytes
db5855f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Typo Detector with OpenVINO™\n",
    "\n",
    "Typo detection in AI is a process of identifying and correcting typographical errors in text data using machine learning algorithms. The goal of typo detection is to improve the accuracy, readability, and usability of text by identifying and indicating mistakes made during the writing process. To detect typos, AI-based typo detectors use various techniques, such as natural language processing (NLP), machine learning (ML), and deep learning (DL).\n",
    "\n",
    "A typo detector takes a sentence as an input and identify all typographical errors such as misspellings and homophone errors.\n",
    "\n",
    "This tutorial provides how to use the [Typo Detector](https://huggingface.co/m3hrdadfi/typo-detector-distilbert-en) from the [Hugging Face Transformers](https://huggingface.co/docs/transformers/index) library in the OpenVINO environment to perform the above task.\n",
    "\n",
    "The model detects typos in a given text with a high accuracy, performances of which are listed below,\n",
    "- Precision score of 0.9923\n",
    "- Recall score of 0.9859\n",
    "- f1-score of 0.9891\n",
    "\n",
    "[Source for above metrics](https://huggingface.co/m3hrdadfi/typo-detector-distilbert-en)\n",
    "\n",
    "These metrics indicate that the model can correctly identify a high proportion of both correct and incorrect text, minimizing both false positives and false negatives.\n",
    "\n",
    "The model has been pretrained on the [NeuSpell](https://github.com/neuspell/neuspell) dataset.\n",
    "\n",
    "<br />\n",
    "\n",
    "<img src=https://user-images.githubusercontent.com/80534358/224564463-ee686386-f846-4b2b-91af-7163586014b7.png>\n",
    "\n",
    "\n",
    "\n",
    "#### Table of contents:\n",
    "\n",
    "- [Imports](#Imports)\n",
    "- [Methods](#Methods)\n",
    "    - [1. Using the Hugging Face Optimum library](#1.-Using-the-Hugging-Face-Optimum-library)\n",
    "        - [2. Converting the model to OpenVINO IR](#2.-Converting-the-model-to-OpenVINO-IR)\n",
    "    - [Select inference device](#Select-inference-device)\n",
    "    - [1. Hugging Face Optimum Intel library](#1.-Hugging-Face-Optimum-Intel-library)\n",
    "        - [Load the model](#Load-the-model)\n",
    "        - [Load the tokenizer](#Load-the-tokenizer)\n",
    "    - [2. Converting the model to OpenVINO IR](#2.-Converting-the-model-to-OpenVINO-IR)\n",
    "        - [Load the Pytorch model](#Load-the-Pytorch-model)\n",
    "        - [Converting to OpenVINO IR](#Converting-to-OpenVINO-IR)\n",
    "        - [Inference](#Inference)\n",
    "    - [Helper Functions](#Helper-Functions)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -q \"diffusers>=0.17.1\" \"openvino>=2023.1.0\" \"nncf>=2.5.0\" \"gradio>=4.19\" \"onnx>=1.11.0\" \"transformers>=4.39.0\" \"torch>=2.1\" --extra-index-url https://download.pytorch.org/whl/cpu\n",
    "%pip install -q \"git+https://github.com/huggingface/optimum-intel.git\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-09-13 09:19:18.035922: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
      "2023-09-13 09:19:18.070661: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
      "To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2023-09-13 09:19:18.678010: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
     ]
    }
   ],
   "source": [
    "from transformers import (\n",
    "    AutoConfig,\n",
    "    AutoTokenizer,\n",
    "    AutoModelForTokenClassification,\n",
    "    pipeline,\n",
    ")\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "import re\n",
    "from typing import List, Dict\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Methods\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "The notebook provides two methods to run the inference of typo detector with OpenVINO runtime, so that you can experience both calling the API of Optimum with OpenVINO Runtime included, and loading models in other frameworks, converting them to OpenVINO IR format, and running inference with OpenVINO Runtime.\n",
    "\n",
    "##### 1. Using the [Hugging Face Optimum](https://huggingface.co/docs/optimum/index) library\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "The Hugging Face Optimum API is a high-level API that allows us to convert models from the Hugging Face Transformers library to the OpenVINO™ IR format. Compiled models in OpenVINO IR format can be loaded using Optimum. Optimum allows the use of optimization on targeted hardware.\n",
    "\n",
    "##### 2. Converting the model to OpenVINO IR\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "The Pytorch model is converted to [OpenVINO IR format](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html). This method provides much more insight to how to set up a pipeline from model loading to model converting, compiling and running inference with OpenVINO, so that you could conveniently use OpenVINO to optimize and accelerate inference for other deep-learning models. The optimization of targeted hardware is also used here.\n",
    "\n",
    "\n",
    "The following table summarizes the major differences between the two methods\n",
    "\n",
    "</br>\n",
    "\n",
    "| Method 1                                                            | Method 2                                                           |\n",
    "| ------------------------------------------------------------------- | ------------------------------------------------------------------ |\n",
    "| Load models from Optimum, an extension of transformers              | Load model from transformers                                       |\n",
    "| Load the model in OpenVINO IR format on the fly                     | Convert to OpenVINO IR                                             |\n",
    "| Load the compiled model by default                                  | Compile the OpenVINO IR and run inference with OpenVINO Runtime    |\n",
    "| Pipeline is created to run inference with OpenVINO Runtime          | Manually run inference.                                            |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select inference device\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "select device from dropdown list for running inference using OpenVINO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c97c547d095f47d09a012fc772293613",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Device:', index=2, options=('CPU', 'GPU', 'AUTO'), value='AUTO')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import ipywidgets as widgets\n",
    "import openvino as ov\n",
    "\n",
    "core = ov.Core()\n",
    "\n",
    "device = widgets.Dropdown(\n",
    "    options=core.available_devices + [\"AUTO\"],\n",
    "    value=\"AUTO\",\n",
    "    description=\"Device:\",\n",
    "    disabled=False,\n",
    ")\n",
    "\n",
    "device"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Hugging Face Optimum Intel library\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "For this method, we need to install the `Hugging Face Optimum Intel library` accelerated by OpenVINO integration.\n",
    "\n",
    "Optimum Intel can be used to load optimized models from the [Hugging Face Hub](https://huggingface.co/docs/optimum/intel/hf.co/models) and create pipelines to run an inference with OpenVINO Runtime using Hugging Face APIs. The Optimum Inference models are API compatible with Hugging Face Transformers models.  This means we need just replace `AutoModelForXxx` class with the corresponding `OVModelForXxx` class."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import required model class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:nncf:NNCF initialized successfully. Supported frameworks detected: torch, tensorflow, onnx, openvino\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No CUDA runtime is found, using CUDA_HOME='/usr/local/cuda'\n",
      "/home/ea/work/ov_venv/lib/python3.8/site-packages/transformers/deepspeed.py:23: FutureWarning: transformers.deepspeed module is deprecated and will be removed in a future version. Please import deepspeed modules directly from transformers.integrations\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from optimum.intel.openvino import OVModelForTokenClassification"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Load the model\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "From the `OVModelForTokenCLassification` class we will import the relevant pre-trained model. To load a Transformers model and convert it to the OpenVINO format on-the-fly, we set `export=True` when loading your model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling the model...\n",
      "Set CACHE_DIR to optimum_model/model_cache\n"
     ]
    }
   ],
   "source": [
    "# The pretrained model we are using\n",
    "model_id = \"m3hrdadfi/typo-detector-distilbert-en\"\n",
    "\n",
    "model_dir = Path(\"optimum_model\")\n",
    "\n",
    "# Save the model to the path if not existing\n",
    "if model_dir.exists():\n",
    "    model = OVModelForTokenClassification.from_pretrained(model_dir, device=device.value)\n",
    "else:\n",
    "    model = OVModelForTokenClassification.from_pretrained(model_id, export=True, device=device.value)\n",
    "    model.save_pretrained(model_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Load the tokenizer\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "Text Preprocessing cleans the text-based input data so it can be fed into the model. Tokenization splits paragraphs and sentences into smaller units that can be more easily assigned meaning. It involves cleaning the data and assigning tokens or IDs to the words, so they are represented in a vector space where similar words have similar vectors. This helps the model understand the context of a sentence. We're making use of an [AutoTokenizer](https://huggingface.co/docs/transformers/main_classes/tokenizer) from Hugging Face, which is essentially a pretrained tokenizer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(model_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we use the inference pipeline for `token-classification` task. You can find more information about usage Hugging Face inference pipelines in this [tutorial](https://huggingface.co/docs/transformers/pipeline_tutorial)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "nlp = pipeline(\n",
    "    \"token-classification\",\n",
    "    model=model,\n",
    "    tokenizer=tokenizer,\n",
    "    aggregation_strategy=\"average\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Function to find typos in a sentence and write them to the terminal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_typos(sentence: str):\n",
    "    \"\"\"\n",
    "    Detect typos from the given sentence.\n",
    "    Writes both the original input and typo-tagged version to the terminal.\n",
    "\n",
    "    Arguments:\n",
    "    sentence -- Sentence to be evaluated (string)\n",
    "    \"\"\"\n",
    "\n",
    "    typos = [sentence[r[\"start\"] : r[\"end\"]] for r in nlp(sentence)]\n",
    "\n",
    "    detected = sentence\n",
    "    for typo in typos:\n",
    "        detected = detected.replace(typo, f\"<i>{typo}</i>\")\n",
    "\n",
    "    print(\"[Input]: \", sentence)\n",
    "    print(\"[Detected]: \", detected)\n",
    "    print(\"-\" * 130)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's run a demo using the Hugging Face Optimum API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Input]:  He had also stgruggled with addiction during his time in Congress .\n",
      "[Detected]:  He had also <i>stgruggled</i> with addiction during his time in Congress .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  The review thoroughla assessed all aspects of JLENS SuR and CPG esign maturit and confidence .\n",
      "[Detected]:  The review <i>thoroughla</i> assessed all aspects of JLENS SuR and CPG <i>esign maturit</i> and confidence .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  Letterma also apologized two his staff for the satyation .\n",
      "[Detected]:  <i>Letterma</i> also apologized <i>two</i> his staff for the <i>satyation</i> .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  Vincent Jay had earlier won France 's first gold in gthe 10km biathlon sprint .\n",
      "[Detected]:  Vincent Jay had earlier won France 's first gold in <i>gthe</i> 10km biathlon sprint .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  It is left to the directors to figure out hpw to bring the stry across to tye audience .\n",
      "[Detected]:  It is left to the directors to figure out <i>hpw</i> to bring the <i>stry</i> across to <i>tye</i> audience .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  I wnet to the park yestreday to play foorball with my fiends, but it statred to rain very hevaily and we had to stop.\n",
      "[Detected]:  I <i>wnet</i> to the park <i>yestreday</i> to play <i>foorball</i> with my <i>fiends</i>, but it <i>statred</i> to rain very <i>hevaily</i> and we had to stop.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  My faorite restuarant servs the best spahgetti in the town, but they are always so buzy that you have to make a resrvation in advnace.\n",
      "[Detected]:  My <i>faorite restuarant servs</i> the best <i>spahgetti</i> in the town, but they are always so <i>buzy</i> that you have to make a <i>resrvation</i> in <i>advnace</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  I was goig to watch a mvoie on Netflx last night, but the straming was so slow that I decided to cancled my subscrpition.\n",
      "[Detected]:  I was <i>goig</i> to watch a <i>mvoie</i> on <i>Netflx</i> last night, but the <i>straming</i> was so slow that I decided to <i>cancled</i> my <i>subscrpition</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  My freind and I went campign in the forest last weekend and saw a beutiful sunst that was so amzing it took our breth away.\n",
      "[Detected]:  My <i>freind</i> and I went <i>campign</i> in the forest last weekend and saw a <i>beutiful sunst</i> that was so <i>amzing</i> it took our <i>breth</i> away.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "[Input]:  I  have been stuying for my math exam all week, but I'm stil not very confidet that I will pass it, because there are so many formuals to remeber.\n",
      "[Detected]:  I  have been <i>stuying</i> for my math exam all week, but I'm <i>stil</i> not very <i>confidet</i> that I will pass it, because there are so many formuals to <i>remeber</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "Time elapsed: 0.17897582054138184\n"
     ]
    }
   ],
   "source": [
    "sentences = [\n",
    "    \"He had also stgruggled with addiction during his time in Congress .\",\n",
    "    \"The review thoroughla assessed all aspects of JLENS SuR and CPG esign maturit and confidence .\",\n",
    "    \"Letterma also apologized two his staff for the satyation .\",\n",
    "    \"Vincent Jay had earlier won France 's first gold in gthe 10km biathlon sprint .\",\n",
    "    \"It is left to the directors to figure out hpw to bring the stry across to tye audience .\",\n",
    "    \"I wnet to the park yestreday to play foorball with my fiends, but it statred to rain very hevaily and we had to stop.\",\n",
    "    \"My faorite restuarant servs the best spahgetti in the town, but they are always so buzy that you have to make a resrvation in advnace.\",\n",
    "    \"I was goig to watch a mvoie on Netflx last night, but the straming was so slow that I decided to cancled my subscrpition.\",\n",
    "    \"My freind and I went campign in the forest last weekend and saw a beutiful sunst that was so amzing it took our breth away.\",\n",
    "    \"I  have been stuying for my math exam all week, but I'm stil not very confidet that I will pass it, because there are so many formuals to remeber.\",\n",
    "]\n",
    "\n",
    "start = time.time()\n",
    "\n",
    "for sentence in sentences:\n",
    "    show_typos(sentence)\n",
    "\n",
    "print(f\"Time elapsed: {time.time() - start}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Converting the model to OpenVINO IR\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Load the Pytorch model\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "Use the `AutoModelForTokenClassification` class to load the pretrained pytorch model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_id = \"m3hrdadfi/typo-detector-distilbert-en\"\n",
    "model_dir = Path(\"pytorch_model\")\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
    "config = AutoConfig.from_pretrained(model_id)\n",
    "\n",
    "# Save the model to the path if not existing\n",
    "if model_dir.exists():\n",
    "    model = AutoModelForTokenClassification.from_pretrained(model_dir)\n",
    "else:\n",
    "    model = AutoModelForTokenClassification.from_pretrained(model_id, config=config)\n",
    "    model.save_pretrained(model_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Converting to OpenVINO IR\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:Please fix your imports. Module tensorflow.python.training.tracking.base has been moved to tensorflow.python.trackable.base. The old module will be deleted in version 2.11.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARNING ]  Please fix your imports. Module %s has been moved to %s. The old module will be deleted in version %s.\n",
      "/home/ea/work/ov_venv/lib/python3.8/site-packages/nncf/torch/dynamic_graph/wrappers.py:74: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
      "  op1 = operator(*args, **kwargs)\n"
     ]
    }
   ],
   "source": [
    "ov_model_path = Path(model_dir) / \"typo_detect.xml\"\n",
    "\n",
    "dummy_model_input = tokenizer(\"This is a sample\", return_tensors=\"pt\")\n",
    "ov_model = ov.convert_model(model, example_input=dict(dummy_model_input))\n",
    "ov.save_model(ov_model, ov_model_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Inference\n",
    "[back to top ⬆️](#Table-of-contents:)\n",
    "\n",
    "OpenVINO™ Runtime Python API is used to compile the model in OpenVINO IR format. The Core class from the `openvino` module is imported first. This class provides access to the OpenVINO Runtime API. The `core` object, which is an instance of the `Core` class, represents the API and it is used to compile the model. The output layer is extracted from the compiled model as it is needed for inference. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "compiled_model = core.compile_model(ov_model, device.value)\n",
    "output_layer = compiled_model.output(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Helper Functions\n",
    "[back to top ⬆️](#Table-of-contents:)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def token_to_words(tokens: List[str]) -> Dict[str, int]:\n",
    "    \"\"\"\n",
    "    Maps the list of tokens to words in the original text.\n",
    "    Built on the feature that tokens starting with '##' is attached to the previous token as tokens derived from the same word.\n",
    "\n",
    "    Arguments:\n",
    "    tokens -- List of tokens\n",
    "\n",
    "    Returns:\n",
    "    map_to_words -- Dictionary mapping tokens to words in original text\n",
    "    \"\"\"\n",
    "\n",
    "    word_count = -1\n",
    "    map_to_words = {}\n",
    "    for token in tokens:\n",
    "        if token.startswith(\"##\"):\n",
    "            map_to_words[token] = word_count\n",
    "            continue\n",
    "        word_count += 1\n",
    "        map_to_words[token] = word_count\n",
    "    return map_to_words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def infer(input_text: str) -> Dict[np.ndarray, np.ndarray]:\n",
    "    \"\"\"\n",
    "    Creating a generic inference function to read the input and infer the result\n",
    "\n",
    "    Arguments:\n",
    "    input_text -- The text to be infered (String)\n",
    "\n",
    "    Returns:\n",
    "    result -- Resulting list from inference\n",
    "    \"\"\"\n",
    "\n",
    "    tokens = tokenizer(\n",
    "        input_text,\n",
    "        return_tensors=\"np\",\n",
    "    )\n",
    "    inputs = dict(tokens)\n",
    "    result = compiled_model(inputs)[output_layer]\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_typo_indexes(\n",
    "    result: Dict[np.ndarray, np.ndarray],\n",
    "    map_to_words: Dict[str, int],\n",
    "    tokens: List[str],\n",
    ") -> List[int]:\n",
    "    \"\"\"\n",
    "    Given results from the inference and tokens-map-to-words, identifies the indexes of the words with typos.\n",
    "\n",
    "    Arguments:\n",
    "    result -- Result from inference (tensor)\n",
    "    map_to_words -- Dictionary mapping tokens to words (Dictionary)\n",
    "\n",
    "    Results:\n",
    "    wrong_words -- List of indexes of words with typos\n",
    "    \"\"\"\n",
    "\n",
    "    wrong_words = []\n",
    "    c = 0\n",
    "    result_list = result[0][1:-1]\n",
    "    for i in result_list:\n",
    "        prob = np.argmax(i)\n",
    "        if prob == 1:\n",
    "            if map_to_words[tokens[c]] not in wrong_words:\n",
    "                wrong_words.append(map_to_words[tokens[c]])\n",
    "        c += 1\n",
    "    return wrong_words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sentence_split(sentence: str) -> List[str]:\n",
    "    \"\"\"\n",
    "    Split the sentence into words and characters\n",
    "\n",
    "    Arguments:\n",
    "    sentence - Sentence to be split (string)\n",
    "\n",
    "    Returns:\n",
    "    splitted -- List of words and characters\n",
    "    \"\"\"\n",
    "\n",
    "    splitted = re.split(\"([',. ])\", sentence)\n",
    "    splitted = [x for x in splitted if x != \" \" and x != \"\"]\n",
    "    return splitted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_typos(sentence: str):\n",
    "    \"\"\"\n",
    "    Detect typos from the given sentence.\n",
    "    Writes both the original input and typo-tagged version to the terminal.\n",
    "\n",
    "    Arguments:\n",
    "    sentence -- Sentence to be evaluated (string)\n",
    "    \"\"\"\n",
    "\n",
    "    tokens = tokenizer.tokenize(sentence)\n",
    "    map_to_words = token_to_words(tokens)\n",
    "    result = infer(sentence)\n",
    "    typo_indexes = get_typo_indexes(result, map_to_words, tokens)\n",
    "\n",
    "    sentence_words = sentence_split(sentence)\n",
    "\n",
    "    typos = [sentence_words[i] for i in typo_indexes]\n",
    "\n",
    "    detected = sentence\n",
    "    for typo in typos:\n",
    "        detected = detected.replace(typo, f\"<i>{typo}</i>\")\n",
    "\n",
    "    print(\"   [Input]: \", sentence)\n",
    "    print(\"[Detected]: \", detected)\n",
    "    print(\"-\" * 130)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's run a demo using the converted OpenVINO IR model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   [Input]:  He had also stgruggled with addiction during his time in Congress .\n",
      "[Detected]:  He had also <i>stgruggled</i> with addiction during his time in Congress .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  The review thoroughla assessed all aspects of JLENS SuR and CPG esign maturit and confidence .\n",
      "[Detected]:  The review <i>thoroughla</i> assessed all aspects of JLENS SuR and CPG <i>esign</i> <i>maturit</i> and confidence .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  Letterma also apologized two his staff for the satyation .\n",
      "[Detected]:  <i>Letterma</i> also apologized <i>two</i> his staff for the <i>satyation</i> .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  Vincent Jay had earlier won France 's first gold in gthe 10km biathlon sprint .\n",
      "[Detected]:  Vincent Jay had earlier won France 's first gold in <i>gthe</i> 10km biathlon sprint .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  It is left to the directors to figure out hpw to bring the stry across to tye audience .\n",
      "[Detected]:  It is left to the directors to figure out <i>hpw</i> to bring the <i>stry</i> across to <i>tye</i> audience .\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  I wnet to the park yestreday to play foorball with my fiends, but it statred to rain very hevaily and we had to stop.\n",
      "[Detected]:  I <i>wnet</i> to the park <i>yestreday</i> to play <i>foorball</i> with my <i>fiends</i>, but it <i>statred</i> to rain very <i>hevaily</i> and we had to stop.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  My faorite restuarant servs the best spahgetti in the town, but they are always so buzy that you have to make a resrvation in advnace.\n",
      "[Detected]:  My <i>faorite</i> <i>restuarant</i> <i>servs</i> the best <i>spahgetti</i> in the town, but they are always so <i>buzy</i> that you have to make a <i>resrvation</i> in <i>advnace</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  I was goig to watch a mvoie on Netflx last night, but the straming was so slow that I decided to cancled my subscrpition.\n",
      "[Detected]:  I was <i>goig</i> to watch a <i>mvoie</i> on <i>Netflx</i> last night, but the <i>straming</i> was so slow that I decided to <i>cancled</i> my <i>subscrpition</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  My freind and I went campign in the forest last weekend and saw a beutiful sunst that was so amzing it took our breth away.\n",
      "[Detected]:  My <i>freind</i> and I went <i>campign</i> in the forest last weekend and saw a <i>beutiful</i> <i>sunst</i> that was so <i>amzing</i> it took our <i>breth</i> away.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "   [Input]:  I  have been stuying for my math exam all week, but I'm stil not very confidet that I will pass it, because there are so many formuals to remeber.\n",
      "[Detected]:  I  have been <i>stuying</i> for my math exam all week, but I'm <i>stil</i> not very <i>confidet</i> that I will pass it, because there are so many formuals to <i>remeber</i>.\n",
      "----------------------------------------------------------------------------------------------------------------------------------\n",
      "Time elapsed: 0.08928751945495605\n"
     ]
    }
   ],
   "source": [
    "sentences = [\n",
    "    \"He had also stgruggled with addiction during his time in Congress .\",\n",
    "    \"The review thoroughla assessed all aspects of JLENS SuR and CPG esign maturit and confidence .\",\n",
    "    \"Letterma also apologized two his staff for the satyation .\",\n",
    "    \"Vincent Jay had earlier won France 's first gold in gthe 10km biathlon sprint .\",\n",
    "    \"It is left to the directors to figure out hpw to bring the stry across to tye audience .\",\n",
    "    \"I wnet to the park yestreday to play foorball with my fiends, but it statred to rain very hevaily and we had to stop.\",\n",
    "    \"My faorite restuarant servs the best spahgetti in the town, but they are always so buzy that you have to make a resrvation in advnace.\",\n",
    "    \"I was goig to watch a mvoie on Netflx last night, but the straming was so slow that I decided to cancled my subscrpition.\",\n",
    "    \"My freind and I went campign in the forest last weekend and saw a beutiful sunst that was so amzing it took our breth away.\",\n",
    "    \"I  have been stuying for my math exam all week, but I'm stil not very confidet that I will pass it, because there are so many formuals to remeber.\",\n",
    "]\n",
    "\n",
    "start = time.time()\n",
    "\n",
    "for sentence in sentences:\n",
    "    show_typos(sentence)\n",
    "\n",
    "print(f\"Time elapsed: {time.time() - start}\")"
   ]
  }
 ],
 "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"
  },
  "openvino_notebooks": {
   "imageUrl": "https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/typo-detector/typo-detector.png?raw=true",
   "tags": {
    "categories": [
     "Model Demos"
    ],
    "libraries": [],
    "other": [],
    "tasks": [
     "Token Classification"
    ]
   }
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}