{"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":"This notebook works only with **GPU** accelerator and requires **Internet** connection.","metadata":{}},{"cell_type":"markdown","source":"# Loading The model","metadata":{}},{"cell_type":"code","source":"!pip install transformers==4.21.1\n!pip install -q git+https://github.com/mnansary/gsoc-wav2vec2.git ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:10:30.138441Z","iopub.execute_input":"2023-03-10T08:10:30.140950Z","iopub.status.idle":"2023-03-10T08:11:00.229771Z","shell.execute_reply.started":"2023-03-10T08:10:30.140907Z","shell.execute_reply":"2023-03-10T08:11:00.228512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q git+https://github.com/mnansary/gsoc-wav2vec2.git ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:11:00.234468Z","iopub.execute_input":"2023-03-10T08:11:00.234834Z","iopub.status.idle":"2023-03-10T08:11:11.501679Z","shell.execute_reply.started":"2023-03-10T08:11:00.234797Z","shell.execute_reply":"2023-03-10T08:11:11.500548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \nimport random\nimport tensorflow as tf\nimport transformers\nimport matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd\nfrom tqdm.auto import tqdm\nfrom IPython.display import display,Audio\nfrom wav2vec2 import RobustWav2Vec2Config,Wav2Vec2,CTCLoss\ntqdm.pandas()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:32:56.111312Z","iopub.execute_input":"2023-03-10T08:32:56.111775Z","iopub.status.idle":"2023-03-10T08:33:03.173019Z","shell.execute_reply.started":"2023-03-10T08:32:56.111675Z","shell.execute_reply":"2023-03-10T08:33:03.171987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VOCAB   =[' ', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', \n          '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', \n          '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', \n          '<empty>', '<empty>', '<empty>', '<empty>', '<empty>', '।', 'ঁ', 'ং', 'ঃ', 'অ', 'আ', 'ই', \n          'ঈ', 'উ', 'ঊ', 'ঋ', 'এ', 'ঐ', 'ও', 'ঔ', 'ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ', \n          'ট', 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন', 'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল',\n          'শ', 'ষ', 'স', 'হ', '<empty>', 'া', 'ি', 'ী', 'ু', 'ূ', 'ৃ', 'ে', 'ৈ', 'ো', 'ৌ', '্', 'ৎ', \n          '<empty>', 'ড়', 'ঢ়', 'য়', '০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '<empty>', '<empty>', \n          '\\u200d', '<empty>', '<empty>', '', '<s>', '</s>']\nconfig = RobustWav2Vec2Config()\nconfig.pad_id=VOCAB.index(\"\")\nprint(\"Vocab Len:\",len(VOCAB))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:03.174908Z","iopub.execute_input":"2023-03-10T08:33:03.175605Z","iopub.status.idle":"2023-03-10T08:33:03.186753Z","shell.execute_reply.started":"2023-03-10T08:33:03.175568Z","shell.execute_reply":"2023-03-10T08:33:03.185648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class cfg:\n    audio_shape      =  (246000,)                   # this is actually fixed for the pretrained weights we are using -- highets audio length=15 secs\n    label_shape      =  (250,)                      # this is actually fixed for the pretrained weights we are using \n    sample_rate      =  16000\n    shuffle_buffer   =  1024\n    batch_size       =  128\n    vocab_len        =  len(VOCAB)                \n    embed_dim        =  1024\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:03.188270Z","iopub.execute_input":"2023-03-10T08:33:03.189351Z","iopub.status.idle":"2023-03-10T08:33:03.203273Z","shell.execute_reply.started":"2023-03-10T08:33:03.189312Z","shell.execute_reply":"2023-03-10T08:33:03.202311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SUFFIX = \":0\"\nMAPPING = (\n    (\"layer_norm.weight\", \"layer_norm/gamma\"),\n    (\"layer_norm.bias\", \"layer_norm.beta\"),\n    (\"weight\", \"kernel\"),\n    (\".\", \"/\"),\n)\n\nSPECIAL_MAPPING_WITH_HEAD = {\n    \"wav2vec2.encoder.pos_conv_embed.conv.weight_g\": \"wav2vec2/encoder/pos_conv_embed/conv/weight_g:0\",\n    \"wav2vec2.encoder.pos_conv_embed.conv.weight_v\": \"wav2vec2/encoder/pos_conv_embed/conv/weight_v:0\",\n}\n\n\n\ndef replace(k):\n    \"\"\"\n    Converts PyTorch state_dict keys to TensorFlow varible name.\n    \"\"\"\n    for hf_v, tf_v in MAPPING:\n        k = k.replace(hf_v, tf_v)\n    return k + SUFFIX\n\n\ndef get_tf_pretrained_model(hf_model_id,tf_model):\n    \"\"\"\n    Converts HuggingFace PyTorch weights to TensorFlow compatible weights.\n    \"\"\"\n    hf_model = transformers.Wav2Vec2ForCTC.from_pretrained(hf_model_id)\n\n    hf_state_dict = hf_model.state_dict()\n\n    tf_variables = tf_model.variables\n    tf_variables_dict = {}\n    for v in tf_variables:\n        tf_variables_dict[v.name] = v\n\n    tf_weights = []\n    extra_keys = []\n    for k in tqdm(hf_state_dict):\n        if k in SPECIAL_MAPPING_WITH_HEAD:\n            new_k = (SPECIAL_MAPPING_WITH_HEAD[k])\n        else:\n            new_k = replace(k)\n        if new_k not in tf_variables_dict.keys():\n            extra_keys.append(k)\n            print(f\"SKIPPING {k}\")\n            continue\n\n        \n        array = hf_state_dict[k].numpy()\n\n        if k in SPECIAL_MAPPING_WITH_HEAD:\n            array = np.transpose(array, axes=(2, 1, 0))\n        elif \"kernel\" in new_k:\n            array = np.transpose(array)\n\n        tf_weights.append((tf_variables_dict[new_k], array))\n\n    print(\"EXTRA KEYS:\\n\", extra_keys)\n\n    tf.keras.backend.batch_set_value(tf_weights)\n    return tf_model","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:03.206138Z","iopub.execute_input":"2023-03-10T08:33:03.206771Z","iopub.status.idle":"2023-03-10T08:33:03.218965Z","shell.execute_reply.started":"2023-03-10T08:33:03.206736Z","shell.execute_reply":"2023-03-10T08:33:03.217966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(cfg):\n    inputs = tf.keras.Input(shape=cfg.audio_shape)\n    # avoid using spec augmentation\n    config.apply_spec_augment=False\n    # freeze feature extractor\n    states = Wav2Vec2(config)(inputs)\n    logits= tf.keras.layers.Dense(cfg.vocab_len,name=\"lm_head\")(states)\n    model = tf.keras.Model(inputs=inputs, outputs=logits)\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:03.220639Z","iopub.execute_input":"2023-03-10T08:33:03.220993Z","iopub.status.idle":"2023-03-10T08:33:03.233235Z","shell.execute_reply.started":"2023-03-10T08:33:03.220958Z","shell.execute_reply":"2023-03-10T08:33:03.232315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=get_tf_pretrained_model(\"../input/banglaasr-old-account/checkpoint-2670\",create_model(cfg))\n    # freeze feature extractor\nmodel.layers[1].freeze_feature_extractor()\n#model.load_weights(\"../input/wave2vec2-tpu-transfer-learning-dl-sprint/model.h5\")\nmodel.load_weights(\"../input/tf-training-model-loc/model.h5\")\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:03.234536Z","iopub.execute_input":"2023-03-10T08:33:03.235141Z","iopub.status.idle":"2023-03-10T08:33:44.679190Z","shell.execute_reply.started":"2023-03-10T08:33:03.235105Z","shell.execute_reply":"2023-03-10T08:33:44.677760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Language Model","metadata":{}},{"cell_type":"code","source":"!pip install pyctcdecode \n!python -m pip install pypi-kenlm \n!pip install https://github.com/kpu/kenlm/archive/master.zip ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:33:44.680945Z","iopub.execute_input":"2023-03-10T08:33:44.682163Z","iopub.status.idle":"2023-03-10T08:34:27.822004Z","shell.execute_reply.started":"2023-03-10T08:33:44.682119Z","shell.execute_reply":"2023-03-10T08:34:27.820783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\"Please Select **Version 1** of **ngramlm** if the following cell shows error\"","metadata":{}},{"cell_type":"code","source":"import soundfile as sf\nfrom transformers import Wav2Vec2Processor\n\nfrom transformers import Wav2Vec2ForCTC\nfrom pyctcdecode import build_ctcdecoder\nfrom transformers import Wav2Vec2ProcessorWithLM\n\nprocessor = Wav2Vec2Processor.from_pretrained(\"../input/download-huggingface-models/wav2vec2_bangla\")\n#model = Wav2Vec2ForCTC.from_pretrained(\"../input/banglaasr-old-account/MyBanglaAsr\")\n\nvocab_dict = processor.tokenizer.get_vocab()\nsorted_vocab_dict = {k.lower(): v for k, v in sorted(vocab_dict.items(), key=lambda item: item[1])}\n\n\ndecoderx = build_ctcdecoder(\n    labels=list(sorted_vocab_dict.keys()),\n    #kenlm_model_path=\"../input/ngramlm/6gram_correct.arpa\",\n    kenlm_model_path=\"../input/ngramlm/6gram_correct.arpa\",\n    alpha=0.68,  # tuned on a val set \n    beta=1.45,  # tuned on a val set \n)\n\n\n\nprocessor = Wav2Vec2ProcessorWithLM(\n    feature_extractor=processor.feature_extractor,\n    tokenizer=processor.tokenizer,\n    decoder=decoderx\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:34:27.825774Z","iopub.execute_input":"2023-03-10T08:34:27.826173Z","iopub.status.idle":"2023-03-10T08:37:34.097158Z","shell.execute_reply.started":"2023-03-10T08:34:27.826137Z","shell.execute_reply":"2023-03-10T08:37:34.095905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Audio","metadata":{}},{"cell_type":"code","source":"import librosa","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:34.098689Z","iopub.execute_input":"2023-03-10T08:37:34.099122Z","iopub.status.idle":"2023-03-10T08:37:46.096028Z","shell.execute_reply.started":"2023-03-10T08:37:34.099080Z","shell.execute_reply":"2023-03-10T08:37:46.095061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_audio2(path):\n    wave,_= librosa.load(path, sr=16000, mono=True)\n    wave=processor(wave, sampling_rate=16000,padding='max_length',max_length=246000,truncation=True).input_values[0]\n    wave=np.expand_dims(wave,axis=0)\n    return wave","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.101751Z","iopub.execute_input":"2023-03-10T08:37:46.102236Z","iopub.status.idle":"2023-03-10T08:37:46.107942Z","shell.execute_reply.started":"2023-03-10T08:37:46.102201Z","shell.execute_reply":"2023-03-10T08:37:46.106999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom scipy.signal import resample\n\ndef resample_audio(audio, old_sample_rate=48000, new_sample_rate=16000):\n    \"\"\"\n    Resample audio from old_sample_rate to new_sample_rate using scipy.signal.resample.\n    \n    Args:\n        audio (np.ndarray): Input audio signal as a 1D numpy array.\n        old_sample_rate (int): Sampling rate of the input audio. Default is 48000.\n        new_sample_rate (int): Desired sampling rate of the output audio. Default is 16000.\n    \n    Returns:\n        np.ndarray: Output audio signal as a 1D numpy array with the desired sampling rate.\n    \"\"\"\n    # Calculate the resampling ratio\n    resample_ratio = new_sample_rate / old_sample_rate\n    \n    # Compute the new length of the resampled audio array\n    new_length = int(np.ceil(audio.shape[0] * resample_ratio))\n    \n    # Resample the audio using scipy.signal.resample\n    resampled_audio = resample(audio, new_length)\n    \n    return resampled_audio","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.109641Z","iopub.execute_input":"2023-03-10T08:37:46.110683Z","iopub.status.idle":"2023-03-10T08:37:46.123692Z","shell.execute_reply.started":"2023-03-10T08:37:46.110608Z","shell.execute_reply":"2023-03-10T08:37:46.122666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_audio(audio):\n    wave=resample_audio(audio)\n    wave=processor(wave, sampling_rate=16000,padding='max_length',max_length=246000,truncation=True).input_values[0]\n    wave=np.expand_dims(wave,axis=0)\n\n    return wave","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.125195Z","iopub.execute_input":"2023-03-10T08:37:46.125678Z","iopub.status.idle":"2023-03-10T08:37:46.134556Z","shell.execute_reply.started":"2023-03-10T08:37:46.125640Z","shell.execute_reply":"2023-03-10T08:37:46.133598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # Inference code","metadata":{}},{"cell_type":"markdown","source":" Update: The inference will be done on .wav files","metadata":{}},{"cell_type":"code","source":"import os\nfrom glob import glob\nfrom tqdm import tqdm\nimport pandas as pd\ntqdm.pandas()\n\n# CHANGE ACCORDINGLY\nBATCH_SIZE = 32\nTEST_DIRECTORY = '../input/test-wav-files-dl-sprint/test_files_wav'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-10T08:37:46.136005Z","iopub.execute_input":"2023-03-10T08:37:46.136643Z","iopub.status.idle":"2023-03-10T08:37:46.147069Z","shell.execute_reply.started":"2023-03-10T08:37:46.136606Z","shell.execute_reply":"2023-03-10T08:37:46.145693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Infer on a single data","metadata":{}},{"cell_type":"code","source":"def infer(audio_path):\n    audio=load_audio(audio_path)\n    tf_out = model.predict(audio)\n    transcription = processor.batch_decode(tf_out)\n    return transcription['text'][0]\n    '''\n    infers on a signle audio\n    args:\n      audio_path  : the path to audio file <string>\n    returns:\n      bangla predicted text <string>\n  '''\n    \n  # your code goes here","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.149102Z","iopub.execute_input":"2023-03-10T08:37:46.149856Z","iopub.status.idle":"2023-03-10T08:37:46.159211Z","shell.execute_reply.started":"2023-03-10T08:37:46.149788Z","shell.execute_reply":"2023-03-10T08:37:46.158183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(infer(\"../input/test-wav-files-dl-sprint/test_files_wav/common_voice_bn_30707481.wav\"))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.161132Z","iopub.execute_input":"2023-03-10T08:37:46.161894Z","iopub.status.idle":"2023-03-10T08:37:46.169033Z","shell.execute_reply.started":"2023-03-10T08:37:46.161855Z","shell.execute_reply":"2023-03-10T08:37:46.167901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Infer on a batch of data - MOST IMPORTANT","metadata":{}},{"cell_type":"code","source":"def batch_infer(audio_paths, batch_size=BATCH_SIZE):\n    audio_list=None\n    for path in audio_paths:\n        audio=load_audio(path)\n        try:\n            audio_list=np.concatenate((audio_list,audio),axis =0)\n        except:\n            audio_list=audio\n        \n    tf_out = model.predict(audio_list)\n    transcription = processor.batch_decode(tf_out)\n    sentences=transcription['text']\n    return sentences\n\n    '''\n    infers on a batch of audio\n    args:\n      audio_paths  : list of path to audio files <list of string>\n    returns:\n      bangla predicted texts <list of string>\n    '''\n    # your code goes her\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.170590Z","iopub.execute_input":"2023-03-10T08:37:46.170956Z","iopub.status.idle":"2023-03-10T08:37:46.183788Z","shell.execute_reply.started":"2023-03-10T08:37:46.170922Z","shell.execute_reply":"2023-03-10T08:37:46.182811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE=32","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.185347Z","iopub.execute_input":"2023-03-10T08:37:46.185758Z","iopub.status.idle":"2023-03-10T08:37:46.193747Z","shell.execute_reply.started":"2023-03-10T08:37:46.185721Z","shell.execute_reply":"2023-03-10T08:37:46.192790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_infer2(ds, batch_size=BATCH_SIZE):\n    audio_list=None\n    for data in ds['audio']:\n        #print(data)\n        audio=load_audio(data['array'])\n        try:\n            audio_list=np.concatenate((audio_list,audio),axis =0)\n        except:\n            audio_list=audio\n        \n    tf_out = model.predict(audio_list)\n    transcription = processor.batch_decode(tf_out)\n    sentences=transcription['text']\n    return sentences","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.196307Z","iopub.execute_input":"2023-03-10T08:37:46.196995Z","iopub.status.idle":"2023-03-10T08:37:46.204434Z","shell.execute_reply.started":"2023-03-10T08:37:46.196959Z","shell.execute_reply":"2023-03-10T08:37:46.203447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Infer on a directory","metadata":{}},{"cell_type":"code","source":"def directory_infer(audio_dir):\n    '''\n    infers on a directory that contains audio files\n    args:\n      audio_dir  : directory that contains some audio files <string>\n    returns:\n      a dataframe that contains 2 columns:\n        * path <string>\n        * sentence <string>\n    '''\n    # list all audio files\n\n    audio_paths=[audio_path for audio_path in tqdm(glob(os.path.join(audio_dir,\"*.*\")))]\n    sentences=[]\n    for idx in tqdm(range(0,len(audio_paths),BATCH_SIZE)):\n        batch_paths=audio_paths[idx:min(idx+BATCH_SIZE,len(audio_paths))]\n        sentences+=batch_infer(batch_paths)\n    df=pd.DataFrame({\"path\":audio_paths,\"sentence\":sentences})\n    return df ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.205910Z","iopub.execute_input":"2023-03-10T08:37:46.206298Z","iopub.status.idle":"2023-03-10T08:37:46.215822Z","shell.execute_reply.started":"2023-03-10T08:37:46.206262Z","shell.execute_reply":"2023-03-10T08:37:46.214885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dataset_infer(audio_dataset):\n    '''\n    infers on a directory that contains audio files\n    args:\n      audio_dir  : directory that contains some audio files <string>\n    returns:\n      a dataframe that contains 2 columns:\n        * path <string>\n        * sentence <string>\n    '''\n    # list all audio files\n\n    #audio_paths=[audio_path for audio_path in tqdm(glob(os.path.join(audio_dir,\"*.*\")))]\n    sentences=[]\n    for idx in tqdm(range(0,len(audio_dataset),BATCH_SIZE)):\n        batch_ds=audio_dataset[idx:min(idx+BATCH_SIZE,len(audio_dataset))]\n        sentences+=batch_infer2(batch_ds)\n    df=pd.DataFrame({\"Expected\":audio_dataset['sentence'],\"Predicted\":sentences})\n    return df ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.217159Z","iopub.execute_input":"2023-03-10T08:37:46.217669Z","iopub.status.idle":"2023-03-10T08:37:46.227316Z","shell.execute_reply.started":"2023-03-10T08:37:46.217633Z","shell.execute_reply":"2023-03-10T08:37:46.226345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.230512Z","iopub.execute_input":"2023-03-10T08:37:46.230859Z","iopub.status.idle":"2023-03-10T08:37:46.241220Z","shell.execute_reply.started":"2023-03-10T08:37:46.230833Z","shell.execute_reply":"2023-03-10T08:37:46.240230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install datasets==4.13.3\n!pip install torchaudio\n!pip install jiwer","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:37:46.242547Z","iopub.execute_input":"2023-03-10T08:37:46.243115Z","iopub.status.idle":"2023-03-10T08:38:06.432318Z","shell.execute_reply.started":"2023-03-10T08:37:46.243075Z","shell.execute_reply":"2023-03-10T08:38:06.430958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import load_dataset\ndataset = load_dataset(\"bengaliAI/cvbn\", \"bn\",split='test')","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:38:06.434475Z","iopub.execute_input":"2023-03-10T08:38:06.434887Z","iopub.status.idle":"2023-03-10T08:38:08.614332Z","shell.execute_reply.started":"2023-03-10T08:38:06.434846Z","shell.execute_reply":"2023-03-10T08:38:08.613337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:38:22.433545Z","iopub.execute_input":"2023-03-10T08:38:22.433970Z","iopub.status.idle":"2023-03-10T08:38:27.199934Z","shell.execute_reply.started":"2023-03-10T08:38:22.433931Z","shell.execute_reply":"2023-03-10T08:38:27.198896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print()","metadata":{"execution":{"iopub.status.busy":"2023-03-10T08:31:57.612366Z","iopub.execute_input":"2023-03-10T08:31:57.613131Z","iopub.status.idle":"2023-03-10T08:31:57.618509Z","shell.execute_reply.started":"2023-03-10T08:31:57.613092Z","shell.execute_reply":"2023-03-10T08:31:57.617335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dataset[:10])","metadata":{"execution":{"iopub.status.busy":"2023-03-10T07:05:04.141579Z","iopub.execute_input":"2023-03-10T07:05:04.141957Z","iopub.status.idle":"2023-03-10T07:05:04.249678Z","shell.execute_reply.started":"2023-03-10T07:05:04.141924Z","shell.execute_reply":"2023-03-10T07:05:04.248637Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dataset[:35]['sentence'])","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:01:53.997812Z","iopub.execute_input":"2023-03-10T09:01:53.998815Z","iopub.status.idle":"2023-03-10T09:01:54.573177Z","shell.execute_reply.started":"2023-03-10T09:01:53.998768Z","shell.execute_reply":"2023-03-10T09:01:54.572116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(batch_infer2(dataset[:35]))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:01:56.586663Z","iopub.execute_input":"2023-03-10T09:01:56.587031Z","iopub.status.idle":"2023-03-10T09:02:03.009234Z","shell.execute_reply.started":"2023-03-10T09:01:56.587001Z","shell.execute_reply":"2023-03-10T09:02:03.006939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=dataset_infer(dataset)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:16:33.958644Z","iopub.execute_input":"2023-03-10T09:16:33.959112Z","iopub.status.idle":"2023-03-10T09:43:40.765708Z","shell.execute_reply.started":"2023-03-10T09:16:33.959072Z","shell.execute_reply":"2023-03-10T09:43:40.763793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Post Processing","metadata":{}},{"cell_type":"code","source":"from pandarallel import pandarallel\npandarallel.initialize(progress_bar=True,nb_workers=8)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:48:08.713243Z","iopub.execute_input":"2023-03-10T09:48:08.714356Z","iopub.status.idle":"2023-03-10T09:48:08.751392Z","shell.execute_reply.started":"2023-03-10T09:48:08.714315Z","shell.execute_reply":"2023-03-10T09:48:08.750429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install bnunicodenormalizer","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:48:08.928090Z","iopub.execute_input":"2023-03-10T09:48:08.928489Z","iopub.status.idle":"2023-03-10T09:48:20.839408Z","shell.execute_reply.started":"2023-03-10T09:48:08.928456Z","shell.execute_reply":"2023-03-10T09:48:20.838007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sentences)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:49:01.943309Z","iopub.execute_input":"2023-03-10T09:49:01.943742Z","iopub.status.idle":"2023-03-10T09:49:02.385585Z","shell.execute_reply.started":"2023-03-10T09:49:01.943701Z","shell.execute_reply":"2023-03-10T09:49:02.383971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bnunicodenormalizer import Normalizer \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]) ","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:48:20.842166Z","iopub.execute_input":"2023-03-10T09:48:20.842936Z","iopub.status.idle":"2023-03-10T09:48:20.856877Z","shell.execute_reply.started":"2023-03-10T09:48:20.842889Z","shell.execute_reply":"2023-03-10T09:48:20.855797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.rename(columns = {'Expected:':'Expected'}, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:50:34.715763Z","iopub.execute_input":"2023-03-10T09:50:34.716213Z","iopub.status.idle":"2023-03-10T09:50:34.728163Z","shell.execute_reply.started":"2023-03-10T09:50:34.716175Z","shell.execute_reply":"2023-03-10T09:50:34.727006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.Expected=submission.Expected.parallel_apply(lambda x:normalize(x))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:53:52.093756Z","iopub.execute_input":"2023-03-10T09:53:52.094249Z","iopub.status.idle":"2023-03-10T09:54:17.691382Z","shell.execute_reply.started":"2023-03-10T09:53:52.094211Z","shell.execute_reply":"2023-03-10T09:54:17.690206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(submission)):\n    try:\n        \n        if(submission.Predicted[i][-1] == '।'):\n            submission.Predicted[i] = submission.Predicted[i]\n        else:\n            submission.Predicted[i] = submission.Predicted[i]+'।'\n    except:\n        submission.Predicted[i] == '।'\nsubmission.Predicted=submission.Predicted.parallel_apply(lambda x:normalize(x))\nsubmission.Expected=submissin.Expected.parallel_apply(lambda x:normalize(x))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:50:51.570141Z","iopub.execute_input":"2023-03-10T09:50:51.570542Z","iopub.status.idle":"2023-03-10T09:51:19.887881Z","shell.execute_reply.started":"2023-03-10T09:50:51.570512Z","shell.execute_reply":"2023-03-10T09:51:19.885618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission.to_csv(\"BN wav2vec2 Prediction.csv\", index=False)\nsubmission.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-10T09:54:17.694179Z","iopub.execute_input":"2023-03-10T09:54:17.694579Z","iopub.status.idle":"2023-03-10T09:54:17.741933Z","shell.execute_reply.started":"2023-03-10T09:54:17.694537Z","shell.execute_reply":"2023-03-10T09:54:17.741087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Your code must output a submission.csv file in the end with predictions on the test_files","metadata":{}},{"cell_type":"code","source":"from datasets import load_metric\n\ncer = load_metric(\"cer\")\ncer_score = cer.compute(predictions=submission.Predicted, references=submission.Expected)\nwer = load_metric(\"wer\")\nwer_score = wer.compute(predictions=submission.Predicted, references=submission.Expected)\nprint(\"Character Error Rate:{}%\".format(cer_score*100))\nprint(\"Word Error Rate:{}%\".format(wer_score*100))","metadata":{"execution":{"iopub.status.busy":"2023-03-10T10:00:36.966317Z","iopub.execute_input":"2023-03-10T10:00:36.966706Z","iopub.status.idle":"2023-03-10T10:00:38.622295Z","shell.execute_reply.started":"2023-03-10T10:00:36.966675Z","shell.execute_reply":"2023-03-10T10:00:38.621238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}