File size: 5,217 Bytes
1a48339
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
23c8c88
 
1a48339
 
 
 
 
23c8c88
1a48339
23c8c88
1a48339
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
23c8c88
1a48339
23c8c88
1a48339
 
 
 
 
 
 
 
 
 
 
 
23c8c88
 
1a48339
 
 
 
 
 
 
 
 
 
 
 
23c8c88
 
1a48339
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
 
23c8c88
1a48339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57176d39",
   "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": "dbc1f98a",
   "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'\n",
    "KENLM_MODEL_LOC = '/workspace/xls-r-300m-km/data/kmwiki_5gram.binary'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54d76e5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "processor = AutoProcessor.from_pretrained(\"vitouphy/xls-r-300m-km\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c76a5c8e",
   "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": "8b640127",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Unigrams not provided and cannot be automatically determined from LM file (only arpa format). Decoding accuracy might be reduced.\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",
      "No known unigrams provided, decoding results might be a lot worse.\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": 6,
   "id": "2560c32d",
   "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": 7,
   "id": "badc19a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "processor_with_lm.save_pretrained(\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89e517c8",
   "metadata": {},
   "source": [
    "## Save Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed9535c8",
   "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": "758b6f9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_pretrained('.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3166a19b",
   "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
}