{"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":"# Inference code for YellowKing's model from  DL Sprint 2022\nhttps://www.kaggle.com/code/sameen53/yellowking-dlsprint-inference","metadata":{}},{"cell_type":"code","source":"!cp -r ../input/python-packages2 ./","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:52:15.635854Z","iopub.execute_input":"2023-07-18T06:52:15.636244Z","iopub.status.idle":"2023-07-18T06:52:16.898344Z","shell.execute_reply.started":"2023-07-18T06:52:15.636129Z","shell.execute_reply":"2023-07-18T06:52:16.896805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!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","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:52:16.905689Z","iopub.execute_input":"2023-07-18T06:52:16.908384Z","iopub.status.idle":"2023-07-18T06:53:58.122905Z","shell.execute_reply.started":"2023-07-18T06:52:16.908338Z","shell.execute_reply":"2023-07-18T06:53:58.121393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom tqdm.auto import tqdm\nfrom glob import glob\nfrom transformers import AutoFeatureExtractor, pipeline\nimport pandas as pd\nimport librosa\nimport IPython\nfrom datasets import load_metric\nfrom tqdm.auto import tqdm\nfrom torch.utils.data import Dataset, DataLoader\nimport torch\nimport gc\nimport wave\nfrom scipy.io import wavfile\nimport scipy.signal as sps\nimport pyctcdecode\n\ntqdm.pandas()\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:53:58.124827Z","iopub.execute_input":"2023-07-18T06:53:58.125322Z","iopub.status.idle":"2023-07-18T06:54:08.893100Z","shell.execute_reply.started":"2023-07-18T06:53:58.125290Z","shell.execute_reply":"2023-07-18T06:54:08.891902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CHANGE ACCORDINGLY\nBATCH_SIZE = 16\nTEST_DIRECTORY = '/kaggle/input/bengaliai-speech/test_mp3s'\npaths = glob(os.path.join(TEST_DIRECTORY,'*.mp3'))\nprint(paths[:2])","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:14:45.484168Z","iopub.execute_input":"2023-07-18T07:14:45.484553Z","iopub.status.idle":"2023-07-18T07:14:45.493293Z","shell.execute_reply.started":"2023-07-18T07:14:45.484521Z","shell.execute_reply":"2023-07-18T07:14:45.491871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass CFG:\n    my_model_name = '../input/yellowking-dlsprint-model/YellowKing_model'\n    processor_name = '../input/yellowking-dlsprint-model/YellowKing_processor'","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:54:08.909265Z","iopub.execute_input":"2023-07-18T06:54:08.909908Z","iopub.status.idle":"2023-07-18T06:54:08.923912Z","shell.execute_reply.started":"2023-07-18T06:54:08.909868Z","shell.execute_reply":"2023-07-18T06:54:08.922803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import Wav2Vec2ProcessorWithLM\n\nprocessor = Wav2Vec2ProcessorWithLM.from_pretrained(CFG.processor_name)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:54:08.925590Z","iopub.execute_input":"2023-07-18T06:54:08.925948Z","iopub.status.idle":"2023-07-18T06:55:42.607866Z","shell.execute_reply.started":"2023-07-18T06:54:08.925912Z","shell.execute_reply":"2023-07-18T06:55:42.606792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_asrLM = pipeline(\"automatic-speech-recognition\", model=CFG.my_model_name ,feature_extractor =processor.feature_extractor, tokenizer= processor.tokenizer,decoder=processor.decoder ,device=0)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:55:42.609565Z","iopub.execute_input":"2023-07-18T06:55:42.609981Z","iopub.status.idle":"2023-07-18T06:56:02.015371Z","shell.execute_reply.started":"2023-07-18T06:55:42.609941Z","shell.execute_reply":"2023-07-18T06:56:02.014235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"speech, sr = librosa.load('/kaggle/input/bengaliai-speech/test_mp3s/0f3dac00655e.mp3', sr=processor.feature_extractor.sampling_rate)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:04:28.633930Z","iopub.execute_input":"2023-07-18T07:04:28.634898Z","iopub.status.idle":"2023-07-18T07:04:29.564260Z","shell.execute_reply.started":"2023-07-18T07:04:28.634850Z","shell.execute_reply":"2023-07-18T07:04:29.562916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_asrLM([speech]*2, chunk_length_s=112, stride_length_s=None)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:09:59.885566Z","iopub.execute_input":"2023-07-18T07:09:59.885940Z","iopub.status.idle":"2023-07-18T07:09:59.999392Z","shell.execute_reply.started":"2023-07-18T07:09:59.885907Z","shell.execute_reply":"2023-07-18T07:09:59.998225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_asrLM","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:05:49.164400Z","iopub.execute_input":"2023-07-18T07:05:49.164780Z","iopub.status.idle":"2023-07-18T07:05:49.171626Z","shell.execute_reply.started":"2023-07-18T07:05:49.164746Z","shell.execute_reply":"2023-07-18T07:05:49.170407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Following Sample Submission:**","metadata":{}},{"cell_type":"code","source":"class AudioDataset(Dataset):\n    def __init__(self, paths):\n        self.paths = paths\n    def __len__(self):\n        return len(self.paths)\n    def __getitem__(self,idx):\n        speech, sr = librosa.load(self.paths[idx], sr=processor.feature_extractor.sampling_rate) \n#         print(speech.shape)\n        return speech","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:29:43.229113Z","iopub.execute_input":"2023-07-18T07:29:43.229518Z","iopub.status.idle":"2023-07-18T07:29:43.236941Z","shell.execute_reply.started":"2023-07-18T07:29:43.229485Z","shell.execute_reply":"2023-07-18T07:29:43.235773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = AudioDataset(paths)\ndataset[0]","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:29:43.374210Z","iopub.execute_input":"2023-07-18T07:29:43.374619Z","iopub.status.idle":"2023-07-18T07:29:44.454621Z","shell.execute_reply.started":"2023-07-18T07:29:43.374584Z","shell.execute_reply":"2023-07-18T07:29:44.453112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda:0'","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:29:44.457898Z","iopub.execute_input":"2023-07-18T07:29:44.458893Z","iopub.status.idle":"2023-07-18T07:29:44.465909Z","shell.execute_reply.started":"2023-07-18T07:29:44.458858Z","shell.execute_reply":"2023-07-18T07:29:44.464900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def collate_fn_padd(batch):\n    '''\n    Padds batch of variable length\n\n    note: it converts things ToTensor manually here since the ToTensor transform\n    assume it takes in images rather than arbitrary tensors.\n    '''\n    ## get sequence lengths\n    lengths = torch.tensor([ t.shape[0] for t in batch ])\n    ## padd\n    batch = [ torch.Tensor(t) for t in batch ]\n    batch = torch.nn.utils.rnn.pad_sequence(batch)\n    ## compute mask\n    mask = (batch != 0)\n    return batch, lengths, mask\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:30:07.510934Z","iopub.execute_input":"2023-07-18T07:30:07.511922Z","iopub.status.idle":"2023-07-18T07:30:07.519264Z","shell.execute_reply.started":"2023-07-18T07:30:07.511885Z","shell.execute_reply":"2023-07-18T07:30:07.518105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloader = DataLoader(dataset, batch_size=32, shuffle=False, num_workers=8, collate_fn=collate_fn_padd)","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:32:01.139659Z","iopub.execute_input":"2023-07-18T07:32:01.140058Z","iopub.status.idle":"2023-07-18T07:32:01.145966Z","shell.execute_reply.started":"2023-07-18T07:32:01.139999Z","shell.execute_reply":"2023-07-18T07:32:01.144609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_all = []\nfor batch, lengths, mask in dataloader:\n    preds = my_asrLM(list(batch.numpy().transpose()))\n    preds_all+=preds","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:32:09.169255Z","iopub.execute_input":"2023-07-18T07:32:09.169647Z","iopub.status.idle":"2023-07-18T07:32:12.599598Z","shell.execute_reply.started":"2023-07-18T07:32:09.169615Z","shell.execute_reply":"2023-07-18T07:32:12.598291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bnunicodenormalizer import Normalizer \n\n\nbnorm = Normalizer()\ndef normalize(sen):\n    _words = [bnorm(word)['normalized']  for word in sen.split()]\n    return \" \".join([word for word in _words if word is not None])\n\ndef dari(sentence):\n    try:\n        if sentence[-1]!=\"।\":\n            sentence+=\"।\"\n    except:\n        print(sentence)\n    return sentence","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:56:02.043685Z","iopub.execute_input":"2023-07-18T06:56:02.044136Z","iopub.status.idle":"2023-07-18T06:56:02.059816Z","shell.execute_reply.started":"2023-07-18T06:56:02.044099Z","shell.execute_reply":"2023-07-18T06:56:02.058811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.DataFrame(\n    {\n        \"id\":[p.split(os.sep)[-1].replace('.mp3','') for p in paths],\n        \"sentence\":[p['text']for p in preds_all]\n    }\n)\ndf.sentence= df.sentence.apply(lambda x:normalize(x))\ndf.sentence= df.sentence.apply(lambda x:dari(x))","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:36:57.734967Z","iopub.execute_input":"2023-07-18T07:36:57.736003Z","iopub.status.idle":"2023-07-18T07:36:57.762247Z","shell.execute_reply.started":"2023-07-18T07:36:57.735961Z","shell.execute_reply":"2023-07-18T07:36:57.761262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-07-18T07:37:13.860291Z","iopub.execute_input":"2023-07-18T07:37:13.860672Z","iopub.status.idle":"2023-07-18T07:37:13.873844Z","shell.execute_reply.started":"2023-07-18T07:37:13.860639Z","shell.execute_reply":"2023-07-18T07:37:13.872740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-18T06:56:53.404791Z","iopub.execute_input":"2023-07-18T06:56:53.405605Z","iopub.status.idle":"2023-07-18T06:56:53.415661Z","shell.execute_reply.started":"2023-07-18T06:56:53.405566Z","shell.execute_reply":"2023-07-18T06:56:53.414451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}