{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import","metadata":{}},{"cell_type":"code","source":"!cp -r ../input/python-packages2 ./\n\n!tar xvfz ./python-packages2/normalizer.tgz\n!pip install ./normalizer/bnunicodenormalizer-0.0.24.tar.gz -f ./ --no-index\n!tar xvfz ./python-packages2/pyctcdecode.tgz\n!pip install ./pyctcdecode/attrs-22.1.0-py2.py3-none-any.whl -f ./ --no-index --no-deps\n!pip install ./pyctcdecode/exceptiongroup-1.0.0rc9-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install ./pyctcdecode/hypothesis-6.54.4-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install ./pyctcdecode/pygtrie-2.5.0.tar.gz -f ./ --no-index --no-deps\n!pip install ./pyctcdecode/sortedcontainers-2.4.0-py2.py3-none-any.whl -f ./ --no-index --no-deps\n!pip install ./pyctcdecode/pyctcdecode-0.4.0-py2.py3-none-any.whl -f ./ --no-index --no-deps\n\n!tar xvfz ./python-packages2/pypikenlm.tgz\n!pip install ./pypikenlm/pypi-kenlm-0.1.20220713.tar.gz -f ./ --no-index --no-deps","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-17T12:40:36.605540Z","iopub.execute_input":"2023-10-17T12:40:36.605915Z","iopub.status.idle":"2023-10-17T12:41:37.153822Z","shell.execute_reply.started":"2023-10-17T12:40:36.605891Z","shell.execute_reply":"2023-10-17T12:41:37.152675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -r python-packages2 normalizer pyctcdecode pypikenlm","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:41:37.156442Z","iopub.execute_input":"2023-10-17T12:41:37.156787Z","iopub.status.idle":"2023-10-17T12:41:38.108489Z","shell.execute_reply.started":"2023-10-17T12:41:37.156751Z","shell.execute_reply":"2023-10-17T12:41:38.107195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import typing as tp\nfrom pathlib import Path\nfrom functools import partial\nfrom dataclasses import dataclass, field\n\nimport pandas as pd\nimport pyctcdecode\nimport numpy as np\nfrom tqdm.notebook import tqdm\n\nimport librosa\n\nimport pyctcdecode\nimport kenlm\nimport torch\nfrom transformers import Wav2Vec2Processor, Wav2Vec2ProcessorWithLM, Wav2Vec2ForCTC\nfrom bnunicodenormalizer import Normalizer\n\nimport cloudpickle as cpkl","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-17T12:41:38.110515Z","iopub.execute_input":"2023-10-17T12:41:38.110899Z","iopub.status.idle":"2023-10-17T12:41:48.893140Z","shell.execute_reply.started":"2023-10-17T12:41:38.110860Z","shell.execute_reply":"2023-10-17T12:41:48.892242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT = Path.cwd().parent\nINPUT = ROOT / \"input\"\nDATA = INPUT / \"bengaliai-speech\"\nTRAIN = DATA / \"train_mp3s\"\nTEST = DATA / \"test_mp3s\"\n\nSAMPLING_RATE = 16_000\nMODEL_PATH = INPUT / \"wav114-2epoch/\"\nLM_PATH = INPUT / \"wav2vec2-xls-r-300m-bengali\"\n\n#BEAM_WIDTH = 512\nBEAM_WIDTH = 1500","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:41:48.895589Z","iopub.execute_input":"2023-10-17T12:41:48.895916Z","iopub.status.idle":"2023-10-17T12:41:48.901109Z","shell.execute_reply.started":"2023-10-17T12:41:48.895886Z","shell.execute_reply":"2023-10-17T12:41:48.899757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### load model, processor, decoder","metadata":{}},{"cell_type":"code","source":"model = Wav2Vec2ForCTC.from_pretrained(MODEL_PATH)\nprocessor = Wav2Vec2Processor.from_pretrained(MODEL_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:41:48.902339Z","iopub.execute_input":"2023-10-17T12:41:48.903390Z","iopub.status.idle":"2023-10-17T12:41:59.203136Z","shell.execute_reply.started":"2023-10-17T12:41:48.903358Z","shell.execute_reply":"2023-10-17T12:41:59.202162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_dict = processor.tokenizer.get_vocab()\nsorted_vocab_dict = {k: v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}\n\nwith open(LM_PATH / \"unigrams.txt\", encoding=\"utf-8\") as f:\n    unigram_list = [t.lower() for t in f.read().strip().split(\"\\n\")]\n\ndecoder = pyctcdecode.build_ctcdecoder(\n    list(sorted_vocab_dict.keys()),\n    str(LM_PATH / \"5gram.bin\"),\n    unigram_list,\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:41:59.204613Z","iopub.execute_input":"2023-10-17T12:41:59.204941Z","iopub.status.idle":"2023-10-17T12:43:07.894197Z","shell.execute_reply.started":"2023-10-17T12:41:59.204910Z","shell.execute_reply":"2023-10-17T12:43:07.893249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processor_with_lm = Wav2Vec2ProcessorWithLM(\n    feature_extractor=processor.feature_extractor,\n    tokenizer=processor.tokenizer,\n    decoder=decoder\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.895425Z","iopub.execute_input":"2023-10-17T12:43:07.895759Z","iopub.status.idle":"2023-10-17T12:43:07.900918Z","shell.execute_reply.started":"2023-10-17T12:43:07.895728Z","shell.execute_reply":"2023-10-17T12:43:07.899972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## prepare dataloader","metadata":{}},{"cell_type":"code","source":"class BengaliSRTestDataset(torch.utils.data.Dataset):\n    \n    def __init__(\n        self,\n        audio_paths: list[str],\n        sampling_rate: int\n    ):\n        self.audio_paths = audio_paths\n        self.sampling_rate = sampling_rate\n        \n    def __len__(self,):\n        return len(self.audio_paths)\n    \n    def __getitem__(self, index: int):\n        audio_path = self.audio_paths[index]\n        sr = self.sampling_rate\n        w = librosa.load(audio_path, sr=sr, mono=False)[0]\n        \n        return w","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.902358Z","iopub.execute_input":"2023-10-17T12:43:07.902971Z","iopub.status.idle":"2023-10-17T12:43:07.912999Z","shell.execute_reply.started":"2023-10-17T12:43:07.902905Z","shell.execute_reply":"2023-10-17T12:43:07.912067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(DATA / \"sample_submission.csv\", dtype={\"id\": str})\nprint(test.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.914400Z","iopub.execute_input":"2023-10-17T12:43:07.915009Z","iopub.status.idle":"2023-10-17T12:43:07.934998Z","shell.execute_reply.started":"2023-10-17T12:43:07.914979Z","shell.execute_reply":"2023-10-17T12:43:07.934100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_audio_paths = [str(TEST / f\"{aid}.mp3\") for aid in test[\"id\"].values]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.938380Z","iopub.execute_input":"2023-10-17T12:43:07.939216Z","iopub.status.idle":"2023-10-17T12:43:07.943856Z","shell.execute_reply.started":"2023-10-17T12:43:07.939185Z","shell.execute_reply":"2023-10-17T12:43:07.942647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = BengaliSRTestDataset(\n    test_audio_paths, SAMPLING_RATE\n)\n\ncollate_func = partial(\n    processor_with_lm.feature_extractor,\n    return_tensors=\"pt\", sampling_rate=SAMPLING_RATE,\n    padding=True,\n)\n\ntest_loader = torch.utils.data.DataLoader(\n    test_dataset, batch_size=8, shuffle=False,\n    num_workers=2, collate_fn=collate_func, drop_last=False,\n    pin_memory=True,\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.945274Z","iopub.execute_input":"2023-10-17T12:43:07.945883Z","iopub.status.idle":"2023-10-17T12:43:07.952997Z","shell.execute_reply.started":"2023-10-17T12:43:07.945853Z","shell.execute_reply":"2023-10-17T12:43:07.952010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"if not torch.cuda.is_available():\n    device = torch.device(\"cpu\")\nelse:\n    device = torch.device(\"cuda\")\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.954357Z","iopub.execute_input":"2023-10-17T12:43:07.954672Z","iopub.status.idle":"2023-10-17T12:43:07.986945Z","shell.execute_reply.started":"2023-10-17T12:43:07.954644Z","shell.execute_reply":"2023-10-17T12:43:07.985975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device)\nmodel = model.eval()\nmodel = model.half()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:07.988279Z","iopub.execute_input":"2023-10-17T12:43:07.988601Z","iopub.status.idle":"2023-10-17T12:43:12.982006Z","shell.execute_reply.started":"2023-10-17T12:43:07.988571Z","shell.execute_reply":"2023-10-17T12:43:12.981056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_sentence_list = []\n\nwith torch.no_grad():\n    for batch in tqdm(test_loader):\n        x = batch[\"input_values\"]\n        x = x.to(device, non_blocking=True)\n        with torch.cuda.amp.autocast(True):\n            y = model(x).logits\n        y = y.detach().cpu().numpy()\n        \n        for l in y:  \n            sentence = processor_with_lm.decode(l, beam_width=BEAM_WIDTH).text\n            pred_sentence_list.append(sentence)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:12.983195Z","iopub.execute_input":"2023-10-17T12:43:12.983521Z","iopub.status.idle":"2023-10-17T12:43:24.899330Z","shell.execute_reply.started":"2023-10-17T12:43:12.983491Z","shell.execute_reply":"2023-10-17T12:43:24.898185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Submission","metadata":{}},{"cell_type":"markdown","source":"### Post Process","metadata":{}},{"cell_type":"code","source":"bnorm = Normalizer()\n\ndef postprocess(sentence):\n    period_set = set([\".\", \"?\", \"!\", \"।\"])\n    _words = [bnorm(word)['normalized']  for word in sentence.split()]\n    sentence = \" \".join([word for word in _words if word is not None])\n    \n    try:\n        if sentence[-1] not in period_set:\n            sentence += \"।\"\n    except:\n        sentence = \"।\"\n\n    return sentence","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:24.900946Z","iopub.execute_input":"2023-10-17T12:43:24.901961Z","iopub.status.idle":"2023-10-17T12:43:24.908435Z","shell.execute_reply.started":"2023-10-17T12:43:24.901906Z","shell.execute_reply":"2023-10-17T12:43:24.907502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp_pred_sentence_list = [\n    postprocess(s) for s in tqdm(pred_sentence_list)]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:24.909801Z","iopub.execute_input":"2023-10-17T12:43:24.910496Z","iopub.status.idle":"2023-10-17T12:43:24.946589Z","shell.execute_reply.started":"2023-10-17T12:43:24.910451Z","shell.execute_reply":"2023-10-17T12:43:24.945756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"sentence\"] = pp_pred_sentence_list\n\n#test.to_csv(\"submission.csv\", index=False)\ntest.to_csv(\"tmp_submission.csv\", index=False)\n\nprint(test.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:24.947656Z","iopub.execute_input":"2023-10-17T12:43:24.948421Z","iopub.status.idle":"2023-10-17T12:43:24.962113Z","shell.execute_reply.started":"2023-10-17T12:43:24.948390Z","shell.execute_reply":"2023-10-17T12:43:24.961004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### punctuation restore","metadata":{}},{"cell_type":"code","source":"!mkdir -p /root/.cache/torch/transformers\n\n!cp -r /kaggle/input/punctuation-cache/5ac6d3984e5ca7c5227e4821c65d341900125db538c5f09a1ead14f380def4a7.aa59609b4f56f82fa7699f0d47997566ccc4cf07e484f3a7bc883bd7c5a34488 /root/.cache/torch/transformers\n!cp -r /kaggle/input/punctuation-cache/5ac6d3984e5ca7c5227e4821c65d341900125db538c5f09a1ead14f380def4a7.aa59609b4f56f82fa7699f0d47997566ccc4cf07e484f3a7bc883bd7c5a34488.json /root/.cache/torch/transformers\n!cp -r /kaggle/input/punctuation-cache/a89d1c4637c1ea5ecd460c2a7c06a03acc9a961fc8c59aa2dd76d8a7f1e94536.2f41fe28a80f2730715b795242a01fc3dda846a85e7903adb3907dc5c5a498bf /root/.cache/torch/transformers\n!cp -r /kaggle/input/punctuation-cache/a89d1c4637c1ea5ecd460c2a7c06a03acc9a961fc8c59aa2dd76d8a7f1e94536.2f41fe28a80f2730715b795242a01fc3dda846a85e7903adb3907dc5c5a498bf.json /root/.cache/torch/transformers\n!cp -r /kaggle/input/punctuation-cache/f7e58cf8eef122765ff522a4c7c0805d2fe8871ec58dcb13d0c2764ea3e4a0f3.309f0c29486cffc28e1e40a2ab0ac8f500c203fe080b95f820aa9cb58e5b84ed /root/.cache/torch/transformers\n!cp -r /kaggle/input/punctuation-cache/f7e58cf8eef122765ff522a4c7c0805d2fe8871ec58dcb13d0c2764ea3e4a0f3.309f0c29486cffc28e1e40a2ab0ac8f500c203fe080b95f820aa9cb58e5b84ed.json /root/.cache/torch/transformers","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:24.963544Z","iopub.execute_input":"2023-10-17T12:43:24.964182Z","iopub.status.idle":"2023-10-17T12:43:47.426022Z","shell.execute_reply.started":"2023-10-17T12:43:24.964149Z","shell.execute_reply":"2023-10-17T12:43:47.424435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/punctuation-package/pytorch_crf-0.7.2-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install /kaggle/input/punctuation-package/sacremoses-0.0.53-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install /kaggle/input/punctuation-package/transformers-2.11.0-py3-none-any.whl -f ./ --no-index --no-deps","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:47.428225Z","iopub.execute_input":"2023-10-17T12:43:47.428984Z","iopub.status.idle":"2023-10-17T12:43:57.415857Z","shell.execute_reply.started":"2023-10-17T12:43:47.428930Z","shell.execute_reply":"2023-10-17T12:43:57.414637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/punctuation-package/inference.py ./\n!cp /kaggle/input/punctuation-package/config.py ./\n!cp /kaggle/input/punctuation-package/model.py ./","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:43:57.417601Z","iopub.execute_input":"2023-10-17T12:43:57.418367Z","iopub.status.idle":"2023-10-17T12:44:00.708956Z","shell.execute_reply.started":"2023-10-17T12:43:57.418328Z","shell.execute_reply":"2023-10-17T12:44:00.707594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\n# get time when the program stated\nstart_time = time.time()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:44:00.710422Z","iopub.execute_input":"2023-10-17T12:44:00.710800Z","iopub.status.idle":"2023-10-17T12:44:00.722820Z","shell.execute_reply.started":"2023-10-17T12:44:00.710756Z","shell.execute_reply":"2023-10-17T12:44:00.722130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/inference.py --pretrained-model=xlm-roberta-large --weight-path=/kaggle/input/xlm-roberta-large-bn/xlm-roberta-large-bn.pt --language=bn --in-file=/kaggle/working/tmp_submission.csv --out-file=/kaggle/working/submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:44:00.725110Z","iopub.execute_input":"2023-10-17T12:44:00.729278Z","iopub.status.idle":"2023-10-17T12:48:50.527506Z","shell.execute_reply.started":"2023-10-17T12:44:00.729249Z","shell.execute_reply":"2023-10-17T12:48:50.526265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"elapsed_time = int(time.time() - start_time)\n\n# convert second to hour, minute and seconds\nelapsed_hour = elapsed_time // 3600\nelapsed_minute = (elapsed_time % 3600) // 60\nelapsed_second = (elapsed_time % 3600 % 60)\n\n# print as 00:00:00\nprint(str(elapsed_hour).zfill(2) + \":\" + str(elapsed_minute).zfill(2) + \":\" + str(elapsed_second).zfill(2))","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:48:50.530327Z","iopub.execute_input":"2023-10-17T12:48:50.530916Z","iopub.status.idle":"2023-10-17T12:48:50.538023Z","shell.execute_reply.started":"2023-10-17T12:48:50.530877Z","shell.execute_reply":"2023-10-17T12:48:50.536950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -r model.py config.py inference.py tmp_submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-10-17T12:48:50.539819Z","iopub.execute_input":"2023-10-17T12:48:50.540523Z","iopub.status.idle":"2023-10-17T12:48:51.657402Z","shell.execute_reply.started":"2023-10-17T12:48:50.540492Z","shell.execute_reply":"2023-10-17T12:48:51.655997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EOF","metadata":{}}]}