{"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/jiwer.tgz\n!pip install ./jiwer/jiwer-2.3.0-py3-none-any.whl -f ./ --no-index\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/numpy-1.21.6-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.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\n\n!cp -r /kaggle/input/pypi-kenlm/pypi-kenlm-0.1.20220713 ./\n!pip install ./pypi-kenlm-0.1.20220713 --no-index --no-deps","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-14T19:33:38.586896Z","iopub.execute_input":"2023-10-14T19:33:38.587541Z","iopub.status.idle":"2023-10-14T19:34:44.802694Z","shell.execute_reply.started":"2023-10-14T19:33:38.587509Z","shell.execute_reply":"2023-10-14T19:34:44.801460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r python-packages2 jiwer normalizer pyctcdecode pypikenlm\n!rm -r pypi-kenlm-0.1.20220713","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:35:14.275021Z","iopub.execute_input":"2023-10-14T19:35:14.275449Z","iopub.status.idle":"2023-10-14T19:35:16.184010Z","shell.execute_reply.started":"2023-10-14T19:35:14.275407Z","shell.execute_reply":"2023-10-14T19:35:16.182726Z"},"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 Wav2Vec2ForCTC, Wav2Vec2Processor # , Wav2Vec2ProcessorWithLM\nfrom bnunicodenormalizer import Normalizer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-14T19:35:22.131545Z","iopub.execute_input":"2023-10-14T19:35:22.131887Z","iopub.status.idle":"2023-10-14T19:35:34.512932Z","shell.execute_reply.started":"2023-10-14T19:35:22.131861Z","shell.execute_reply":"2023-10-14T19:35:34.512025Z"},"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 = \"/kaggle/input/facebook-1b-finetuned/best-hf-facebook-1b-funtune-stage3\"\nBEAM_WIDTH = 1024\n\n\n\n\n# LM_PATH = Path(\"/kaggle/input/indiccorp-v1-v2-mix-lm-model\")  # second best for now\nLM_PATH = INPUT / \"kenlm-custum-lm-model/mix_7gram_lm_model\"  # best for now\n# LM_PATH = Path(\"/kaggle/input/multi-corpus-6gram-lm-model\")  # third best for now\nUNIGRAMS = LM_PATH/ \"unigrams.txt\"\nBINARY = LM_PATH/ \"7gram.bin\"\n# CUSTUM_MODEL = \"/kaggle/input/facebook-1b-finetuned/latest_hf_facebook_1b_fintune_stage_3_0921_dropout_0_8_noise_prob\"\nCUSTUM_MODEL = \"/kaggle/input/facebook-1b-stage4\"","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:35:39.052698Z","iopub.execute_input":"2023-10-14T19:35:39.053491Z","iopub.status.idle":"2023-10-14T19:35:39.058882Z","shell.execute_reply.started":"2023-10-14T19:35:39.053453Z","shell.execute_reply":"2023-10-14T19:35:39.057837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(str(UNIGRAMS)) as f:\n    unigrams = f.readlines()\n\nunigrams = [_.strip() for _ in unigrams if _.strip()]","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:35:45.951543Z","iopub.execute_input":"2023-10-14T19:35:45.951877Z","iopub.status.idle":"2023-10-14T19:35:50.656471Z","shell.execute_reply.started":"2023-10-14T19:35:45.951853Z","shell.execute_reply":"2023-10-14T19:35:50.655542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(unigrams)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:35:57.503177Z","iopub.execute_input":"2023-10-14T19:35:57.503535Z","iopub.status.idle":"2023-10-14T19:35:57.511425Z","shell.execute_reply.started":"2023-10-14T19:35:57.503508Z","shell.execute_reply":"2023-10-14T19:35:57.510286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### load model, processor, decoder","metadata":{}},{"cell_type":"code","source":"model = Wav2Vec2ForCTC.from_pretrained(CUSTUM_MODEL, torch_dtype=torch.float16)\nprocessor = Wav2Vec2Processor.from_pretrained(MODEL_PATH)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:36:01.865156Z","iopub.execute_input":"2023-10-14T19:36:01.865503Z","iopub.status.idle":"2023-10-14T19:36:58.033610Z","shell.execute_reply.started":"2023-10-14T19:36:01.865475Z","shell.execute_reply":"2023-10-14T19:36:58.032655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_dict = processor.tokenizer.get_vocab()\nvocab_dict = vocab_dict[\"ben\"]\nvocab_dict['<s>'] = 64\nvocab_dict['</s>'] =  65\nsorted_vocab_dict = {k: v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}\n\ndecoder = pyctcdecode.build_ctcdecoder(\n    list(sorted_vocab_dict.keys()),\n    str(BINARY),\n    unigrams=unigrams,\n    alpha=0.375,\n    beta=0.0625\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:37:32.237505Z","iopub.execute_input":"2023-10-14T19:37:32.237852Z","iopub.status.idle":"2023-10-14T19:41:57.197802Z","shell.execute_reply.started":"2023-10-14T19:37:32.237827Z","shell.execute_reply":"2023-10-14T19:41:57.196731Z"},"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)[0]\n#         w = np.trim_zeros(w, \"fb\")\n        return w","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:43:14.366242Z","iopub.execute_input":"2023-10-14T19:43:14.366613Z","iopub.status.idle":"2023-10-14T19:43:14.373114Z","shell.execute_reply.started":"2023-10-14T19:43:14.366586Z","shell.execute_reply":"2023-10-14T19:43:14.371968Z"},"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-14T19:43:17.802024Z","iopub.execute_input":"2023-10-14T19:43:17.802359Z","iopub.status.idle":"2023-10-14T19:43:17.866782Z","shell.execute_reply.started":"2023-10-14T19:43:17.802331Z","shell.execute_reply":"2023-10-14T19:43:17.865821Z"},"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-14T19:43:21.262901Z","iopub.execute_input":"2023-10-14T19:43:21.263333Z","iopub.status.idle":"2023-10-14T19:43:21.268898Z","shell.execute_reply.started":"2023-10-14T19:43:21.263297Z","shell.execute_reply":"2023-10-14T19:43:21.267650Z"},"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.feature_extractor,\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=1, shuffle=False,\n    num_workers=1, collate_fn=collate_func, drop_last=False,\n    pin_memory=True,\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:43:24.741120Z","iopub.execute_input":"2023-10-14T19:43:24.741457Z","iopub.status.idle":"2023-10-14T19:43:24.752255Z","shell.execute_reply.started":"2023-10-14T19:43:24.741429Z","shell.execute_reply":"2023-10-14T19:43:24.751206Z"},"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-14T19:43:27.973919Z","iopub.execute_input":"2023-10-14T19:43:27.974941Z","iopub.status.idle":"2023-10-14T19:43:28.230238Z","shell.execute_reply.started":"2023-10-14T19:43:27.974905Z","shell.execute_reply":"2023-10-14T19:43:28.229216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device)\nmodel = model.eval()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:43:34.278839Z","iopub.execute_input":"2023-10-14T19:43:34.279184Z","iopub.status.idle":"2023-10-14T19:43:41.751936Z","shell.execute_reply.started":"2023-10-14T19:43:34.279155Z","shell.execute_reply":"2023-10-14T19:43:41.749993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch in tqdm(test_loader):\n    x = batch[\"input_values\"]\n    x = x.to(device).half()\n    attn_mask = batch[\"attention_mask\"]\n    attn_mask = attn_mask.to(device)\n    with torch.no_grad():\n        y = model(input_values=x, attention_mask=attn_mask).logits\n    y = y.detach().cpu().numpy()\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:43:45.636368Z","iopub.execute_input":"2023-10-14T19:43:45.636736Z","iopub.status.idle":"2023-10-14T19:44:00.790835Z","shell.execute_reply.started":"2023-10-14T19:43:45.636709Z","shell.execute_reply":"2023-10-14T19:44:00.789482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_sentence_list = []\n\nfor batch in tqdm(test_loader):\n    x = batch[\"input_values\"]\n    x = x.to(device).half()\n    attn_mask = batch[\"attention_mask\"]\n    attn_mask = attn_mask.to(device)\n    with torch.no_grad():\n        y = model(input_values=x, attention_mask=attn_mask).logits\n    y = y.detach().cpu().numpy()\n    attn_mask = attn_mask.sum(dim=1).cpu().numpy()\n    for l, n in zip(y, attn_mask):\n        valid_nums = int(n)\n        beam = decoder.decode_beams(l[:valid_nums], beam_width=BEAM_WIDTH)\n        s = beam[0][0]\n        pred_sentence_list.append(s)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:44:03.811292Z","iopub.execute_input":"2023-10-14T19:44:03.812046Z","iopub.status.idle":"2023-10-14T19:44:08.983792Z","shell.execute_reply.started":"2023-10-14T19:44:03.812012Z","shell.execute_reply":"2023-10-14T19:44:08.982457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Submission","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    try:\n        if sentence[-1] not in period_set:\n            sentence+=\"।\"\n    except:\n        # print(sentence)\n        sentence = \"।\"\n    return sentence","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:44:13.468970Z","iopub.execute_input":"2023-10-14T19:44:13.469333Z","iopub.status.idle":"2023-10-14T19:44:13.476273Z","shell.execute_reply.started":"2023-10-14T19:44:13.469302Z","shell.execute_reply":"2023-10-14T19:44:13.475211Z"},"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-14T19:44:16.821471Z","iopub.execute_input":"2023-10-14T19:44:16.821824Z","iopub.status.idle":"2023-10-14T19:44:16.855215Z","shell.execute_reply.started":"2023-10-14T19:44:16.821798Z","shell.execute_reply":"2023-10-14T19:44:16.854348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"sentence\"] = pp_pred_sentence_list\n\ntest.to_csv(\"submission.csv\", index=False)\n\nprint(test.head())","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:44:19.900995Z","iopub.execute_input":"2023-10-14T19:44:19.902149Z","iopub.status.idle":"2023-10-14T19:44:19.926575Z","shell.execute_reply.started":"2023-10-14T19:44:19.902100Z","shell.execute_reply":"2023-10-14T19:44:19.925247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EOF","metadata":{}},{"cell_type":"code","source":"print(pp_pred_sentence_list)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:44:23.218303Z","iopub.execute_input":"2023-10-14T19:44:23.218827Z","iopub.status.idle":"2023-10-14T19:44:23.223935Z","shell.execute_reply.started":"2023-10-14T19:44:23.218787Z","shell.execute_reply":"2023-10-14T19:44:23.222740Z"},"trusted":true},"execution_count":null,"outputs":[]}]}