File size: 5,840 Bytes
bb8c2ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5393aa33",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, AutoModelForCTC, Wav2Vec2Processor, AutoProcessor, Wav2Vec2ProcessorWithLM\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\n",
    "from tqdm.auto import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2d34d3b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# KENLM_MODEL_LOC = '/workspace/xls-r-300m-km/data/km_text_word_unigram.arpa'\n",
    "KENLM_MODEL_LOC = '/workspace/xls-r-300m-km/data/km_wiki_ngram.arpa'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f0354cb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading the LM will be faster if you build a binary file.\n",
      "Reading /workspace/xls-r-300m-km/vitouphy/xls-r-300m-km/language_model/km_text.arpa\n",
      "----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
      "Only 81 unigrams passed as vocabulary. Is this small or artificial data?\n",
      "****************************************************************************************************\n"
     ]
    }
   ],
   "source": [
    "processor = AutoProcessor.from_pretrained(\"vitouphy/xls-r-300m-km\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "109f28e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'|': 0, 'แž€': 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, '[unk]': 71, '[pad]': 72, '<s>': 73, '</s>': 74}\n"
     ]
    }
   ],
   "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])}\n",
    "print(sorted_vocab_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "300cec39",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading the LM will be faster if you build a binary file.\n",
      "Reading /workspace/xls-r-300m-km/data/km_wiki_ngram.arpa\n",
      "----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
      "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",
      "****************************************************************************************************\n"
     ]
    }
   ],
   "source": [
    "decoder = build_ctcdecoder(\n",
    "    labels=list(sorted_vocab_dict.keys()),\n",
    "    kenlm_model_path=KENLM_MODEL_LOC,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "27dd8427",
   "metadata": {},
   "outputs": [],
   "source": [
    "processor_with_lm = Wav2Vec2ProcessorWithLM(\n",
    "    feature_extractor=processor.feature_extractor,\n",
    "    tokenizer=processor.tokenizer,\n",
    "    decoder=decoder\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "94eb248e",
   "metadata": {},
   "outputs": [],
   "source": [
    "processor_with_lm.save_pretrained(\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f9b3dcc",
   "metadata": {},
   "source": [
    "## Save Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8b584690",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "bc5bf68946064e97b869d44b02e7af19",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading:   0%|          | 0.00/1.18G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model = AutoModelForCTC.from_pretrained(\"vitouphy/xls-r-300m-km\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3712c030",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_pretrained('.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5d8de20",
   "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
}