File size: 6,011 Bytes
3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 da15453 3e93b32 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"id": "04c8de09",
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1eae750a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Reusing dataset common_voice (/workspace/.cache/huggingface/datasets/mozilla-foundation___common_voice/fr/7.0.0/fe20cac47c166e25b1f096ab661832e3da7cf298ed4a91dcaa1343ad972d175b)\n"
]
}
],
"source": [
"dataset = load_dataset(\"mozilla-foundation/common_voice_7_0\", \"fr\", split=\"train\", use_auth_token=True)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "da1cfcaa",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c110c54654c045b9a2cbc6cad43fa685",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"0ex [00:00, ?ex/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"chars_to_ignore_regex = '[^a-zàâäçéèêëîïôöùûüÿ\\'’ ]'\n",
"\n",
"def extract_text(batch):\n",
" batch[\"text\"] = re.sub(chars_to_ignore_regex, \"\", batch[\"sentence\"].lower()).replace('’', \"'\")\n",
" return batch\n",
"\n",
"dataset = dataset.map(extract_text, remove_columns=[\"sentence\"])"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "bb306916",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d21bc14560b747f49105f598a2ffe2ff",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Pushing dataset shards to the dataset hub: 0%| | 0/29 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dataset.push_to_hub(f\"common_voice_7_0_fr_processed\", split=\"train\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "312d9c63",
"metadata": {},
"outputs": [],
"source": [
"with open(\"text_for_lm.txt\", \"w\") as file:\n",
" file.write(\" \".join(dataset[\"text\"]))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "6d82daed",
"metadata": {},
"outputs": [],
"source": [
"with open(\"5gram.arpa\", \"r\") as read_file, open(\"5gram_correct.arpa\", \"w\") as write_file:\n",
" has_added_eos = False\n",
" for line in read_file:\n",
" if not has_added_eos and \"ngram 1=\" in line:\n",
" count=line.strip().split(\"=\")[-1]\n",
" write_file.write(line.replace(f\"{count}\", f\"{int(count)+1}\"))\n",
" elif not has_added_eos and \"<s>\" in line:\n",
" write_file.write(line)\n",
" write_file.write(line.replace(\"<s>\", \"</s>\"))\n",
" has_added_eos = True\n",
" else:\n",
" write_file.write(line)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "07ff4067",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoProcessor"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e75ab227",
"metadata": {},
"outputs": [],
"source": [
"processor = AutoProcessor.from_pretrained(\"./\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "604776b7",
"metadata": {},
"outputs": [],
"source": [
"vocab_dict = processor.tokenizer.get_vocab()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ef4dd957",
"metadata": {},
"outputs": [],
"source": [
"sorted_vocab_dict = {k.lower(): v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9a14839d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading the LM will be faster if you build a binary file.\n",
"Reading /home/pascal/kenlm/build/bin/xls-r-300m-lm-fr/language_model/5gram_correct.arpa\n",
"----5---10---15---20---25---30---35---40---45---50---55---60---65---70---75---80---85---90---95--100\n",
"****************************************************************************************************\n"
]
}
],
"source": [
"from pyctcdecode import build_ctcdecoder\n",
"\n",
"decoder = build_ctcdecoder(\n",
" labels=list(sorted_vocab_dict.keys()),\n",
" kenlm_model_path=\"./language_model/5gram_correct.arpa\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "656979ca",
"metadata": {},
"outputs": [],
"source": [
"from transformers import Wav2Vec2ProcessorWithLM\n",
"\n",
"processor_with_lm = Wav2Vec2ProcessorWithLM(\n",
" feature_extractor=processor.feature_extractor,\n",
" tokenizer=processor.tokenizer,\n",
" decoder=decoder\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d2dd8891",
"metadata": {},
"outputs": [],
"source": [
"processor_with_lm.save_pretrained(\"xls-r-300m-lm-fr\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85908c6d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|