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
}
|