{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":73047,"databundleVersionId":8149390,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nimport pandas as pd\n\nimport librosa\nimport librosa.display\n\nimport numpy as np\n\nimport IPython.display as ipd\n\nimport matplotlib.pyplot as plt\n\nimport random\n\nfrom collections import Counter\n\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torchaudio\n\nfrom dataclasses import dataclass\nfrom typing import Any, Dict, List, Union\nfrom datasets import DatasetDict\nfrom datasets import Dataset as DS\n\nfrom transformers import (\n    WhisperFeatureExtractor,\n    WhisperTokenizer,\n    WhisperProcessor,\n    WhisperForConditionalGeneration,\n    Seq2SeqTrainingArguments,\n    Seq2SeqTrainer,\n    TrainerCallback,\n    TrainingArguments,\n    TrainerState,\n    TrainerControl,\n    EarlyStoppingCallback,\n    pipeline\n)\n\nfrom torchmetrics.text import WordErrorRate, CharErrorRate","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-23T09:26:16.453133Z","iopub.execute_input":"2024-04-23T09:26:16.453595Z","iopub.status.idle":"2024-04-23T09:26:41.167224Z","shell.execute_reply.started":"2024-04-23T09:26:16.453557Z","shell.execute_reply":"2024-04-23T09:26:41.166400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/ben10/ben10'\ntrain_data_dir = f\"{BASE_DIR}/16_kHz_train_audio/\"\ntest_data_dir = f\"{BASE_DIR}/16_kHz_valid_audio/\"\ndata_path = f\"{BASE_DIR}/train.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:26:41.169039Z","iopub.execute_input":"2024-04-23T09:26:41.169703Z","iopub.status.idle":"2024-04-23T09:26:41.174993Z","shell.execute_reply.started":"2024-04-23T09:26:41.169667Z","shell.execute_reply":"2024-04-23T09:26:41.174051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split2path = {\n    \"train\": train_data_dir,\n    \"test\": test_data_dir,\n}","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:26:41.176694Z","iopub.execute_input":"2024-04-23T09:26:41.177102Z","iopub.status.idle":"2024-04-23T09:26:41.182823Z","shell.execute_reply.started":"2024-04-23T09:26:41.177068Z","shell.execute_reply":"2024-04-23T09:26:41.181924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(data_path)\ndata.sample(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:26:41.184170Z","iopub.execute_input":"2024-04-23T09:26:41.184500Z","iopub.status.idle":"2024-04-23T09:26:41.408542Z","shell.execute_reply.started":"2024-04-23T09:26:41.184473Z","shell.execute_reply":"2024-04-23T09:26:41.407619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_split(filename):\n    filename_ = filename.split(\"_\")\n    split = filename_[0]\n    return split\n\ndef extract_district(filename):\n    filename_ = filename.split(\" \")[0]\n    district = filename_.split(\"_\")[1]\n    return district\n\ndef beautify_dataset(data):\n    splits = []\n    districts = []\n    newpaths = []\n    transcripts = []\n    \n    for i in range(len(data)):\n        filename, transcript = data.iloc[i]\n        split = extract_split(filename)\n        district = extract_district(filename)\n        dir_path = split2path[split]\n        composed_path = f\"{dir_path}{filename}\"\n        \n        if os.path.exists(composed_path) == False:\n            print(f\"{composed_path} does not exist.\")\n            continue\n        \n        # replace any newline characters\n        transcript = transcript.replace(\"\\n\", \" \")\n        transcript = \" \".join(transcript.split())\n        \n        splits.append(split)\n        districts.append(district)\n        newpaths.append(composed_path)\n        transcripts.append(transcript)\n    \n    data['file_path'] = newpaths\n    data['district'] = districts\n    data['split'] = splits\n    data['transcripts'] = transcripts\n    \n#     data.drop(columns=['file_name'], inplace=True)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:26:41.411698Z","iopub.execute_input":"2024-04-23T09:26:41.411982Z","iopub.status.idle":"2024-04-23T09:26:41.422139Z","shell.execute_reply.started":"2024-04-23T09:26:41.411957Z","shell.execute_reply":"2024-04-23T09:26:41.421110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = beautify_dataset(data)\ndata.sample(20)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:26:41.423363Z","iopub.execute_input":"2024-04-23T09:26:41.423680Z","iopub.status.idle":"2024-04-23T09:27:06.513473Z","shell.execute_reply.started":"2024-04-23T09:26:41.423653Z","shell.execute_reply":"2024-04-23T09:27:06.512254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"words = data['transcripts']\nwords","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:06.515167Z","iopub.execute_input":"2024-04-23T09:27:06.515671Z","iopub.status.idle":"2024-04-23T09:27:06.525359Z","shell.execute_reply.started":"2024-04-23T09:27:06.515626Z","shell.execute_reply":"2024-04-23T09:27:06.524018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w = {}\n\n\nfor word in words:\n    for ch in word:\n      \n        if ch in w:\n            w[ch]+=1\n        else:\n            w[ch]=1\n    \nw","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:06.526815Z","iopub.execute_input":"2024-04-23T09:27:06.527216Z","iopub.status.idle":"2024-04-23T09:27:07.627741Z","shell.execute_reply.started":"2024-04-23T09:27:06.527156Z","shell.execute_reply":"2024-04-23T09:27:07.626718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install bnunicodenormalizer\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:07.629230Z","iopub.execute_input":"2024-04-23T09:27:07.629680Z","iopub.status.idle":"2024-04-23T09:27:25.921276Z","shell.execute_reply.started":"2024-04-23T09:27:07.629636Z","shell.execute_reply":"2024-04-23T09:27:25.919927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nimport unicodedata\n\n\ndef normalize_bengali_text(text):\n    # Unicode normalization (NFC form)\n    text = unicodedata.normalize('NFC', text)\n    \n  \n    \n    # Remove diacritics\n    text = ''.join(c for c in text if not unicodedata.combining(c))\n    \n    # Normalize whitespace\n    text = re.sub(r'\\s+', ' ', text)\n    \n    #remove punctuation\n    \n    text = re.sub(r'[^\\w\\s]', '', text)\n    #remove english aplhabet\n    text = re.sub(r'[a-zA-Z]', '', text)\n    \n\n  \n    # Remove numbers\n    text = re.sub(r'\\b\\d+\\b', '', text)\n    \n    text = re.sub(r'[৫]', '', text)\n    \n    text = re.sub(r'[০]', '', text)\n    \n    text = re.sub(r'[৷]', '', text)\n    \n    \n    \n    return text\n\n\n\n# Apply normalization function to each element of the Series\ndata['transcripts'] = data['transcripts'].apply(normalize_bengali_text)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:25.922678Z","iopub.execute_input":"2024-04-23T09:27:25.923002Z","iopub.status.idle":"2024-04-23T09:27:28.025388Z","shell.execute_reply.started":"2024-04-23T09:27:25.922974Z","shell.execute_reply":"2024-04-23T09:27:28.024356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = []\nw = {}\nwords = data['transcripts']\nfor word in words:\n    for ch in word:\n        s.append(ch)\n        if ch in w:\n            w[ch]+=1\n        else:\n            w[ch]=1\n    \n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:28.026736Z","iopub.execute_input":"2024-04-23T09:27:28.027103Z","iopub.status.idle":"2024-04-23T09:27:28.929616Z","shell.execute_reply.started":"2024-04-23T09:27:28.027072Z","shell.execute_reply":"2024-04-23T09:27:28.928781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(s)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:28.930812Z","iopub.execute_input":"2024-04-23T09:27:28.931420Z","iopub.status.idle":"2024-04-23T09:27:28.990948Z","shell.execute_reply.started":"2024-04-23T09:27:28.931384Z","shell.execute_reply":"2024-04-23T09:27:28.990010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:28.992211Z","iopub.execute_input":"2024-04-23T09:27:28.992550Z","iopub.status.idle":"2024-04-23T09:27:29.003920Z","shell.execute_reply.started":"2024-04-23T09:27:28.992511Z","shell.execute_reply":"2024-04-23T09:27:29.002900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**NOTE:** Think of how you want use the existing models/your finetuned model to replace these examples.... For now let's just handle them.","metadata":{}},{"cell_type":"code","source":"data[\"transcripts\"] = data[\"transcripts\"].str.strip()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:29.010523Z","iopub.execute_input":"2024-04-23T09:27:29.010906Z","iopub.status.idle":"2024-04-23T09:27:29.022769Z","shell.execute_reply.started":"2024-04-23T09:27:29.010880Z","shell.execute_reply":"2024-04-23T09:27:29.021810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TASK = \"transcribe\"\nMODEL_NAME = \"openai/whisper-small\"","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:29.024146Z","iopub.execute_input":"2024-04-23T09:27:29.024785Z","iopub.status.idle":"2024-04-23T09:27:29.030685Z","shell.execute_reply.started":"2024-04-23T09:27:29.024745Z","shell.execute_reply":"2024-04-23T09:27:29.029831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_extractor = WhisperFeatureExtractor.from_pretrained(MODEL_NAME)\ntokenizer = WhisperTokenizer.from_pretrained(MODEL_NAME, language='bn', task=TASK)\nprocessor = WhisperProcessor.from_pretrained(MODEL_NAME, language='bn', task=TASK)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:29.032008Z","iopub.execute_input":"2024-04-23T09:27:29.032610Z","iopub.status.idle":"2024-04-23T09:27:32.103733Z","shell.execute_reply.started":"2024-04-23T09:27:29.032581Z","shell.execute_reply":"2024-04-23T09:27:32.102658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = tokenizer.encode(\"\")\nids","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.105125Z","iopub.execute_input":"2024-04-23T09:27:32.105540Z","iopub.status.idle":"2024-04-23T09:27:32.113166Z","shell.execute_reply.started":"2024-04-23T09:27:32.105504Z","shell.execute_reply":"2024-04-23T09:27:32.112113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.decode(ids)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.114744Z","iopub.execute_input":"2024-04-23T09:27:32.115173Z","iopub.status.idle":"2024-04-23T09:27:32.132868Z","shell.execute_reply.started":"2024-04-23T09:27:32.115140Z","shell.execute_reply":"2024-04-23T09:27:32.131597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@dataclass\nclass DataCollatorSpeechSeq2SeqWithPadding:\n    processor: Any\n\n    def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]:\n        # split inputs and labels since they have to be of different lengths and need different padding methods\n        # first treat the audio inputs by simply returning torch tensors\n        input_features = [{\"input_features\": feature[\"input_features\"]} for feature in features]\n        batch = self.processor.feature_extractor.pad(input_features, return_tensors=\"pt\")\n\n        # get the tokenized label sequences\n        label_features = [{\"input_ids\": feature[\"labels\"]} for feature in features]\n        # pad the labels to max length\n        labels_batch = self.processor.tokenizer.pad(label_features, return_tensors=\"pt\")\n\n        # replace padding with -100 to ignore loss correctly\n        labels = labels_batch[\"input_ids\"].masked_fill(labels_batch.attention_mask.ne(1), -100)\n\n        # if bos token is appended in previous tokenization step,\n        # cut bos token here as it's append later anyways\n        if (labels[:, 0] == self.processor.tokenizer.bos_token_id).all().cpu().item():\n            labels = labels[:, 1:]\n\n        batch[\"labels\"] = labels\n        \n        torch.cuda.empty_cache()\n\n        return batch","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.134386Z","iopub.execute_input":"2024-04-23T09:27:32.134729Z","iopub.status.idle":"2024-04-23T09:27:32.145762Z","shell.execute_reply.started":"2024-04-23T09:27:32.134698Z","shell.execute_reply":"2024-04-23T09:27:32.144669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_collator = DataCollatorSpeechSeq2SeqWithPadding(processor=processor)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.147047Z","iopub.execute_input":"2024-04-23T09:27:32.147402Z","iopub.status.idle":"2024-04-23T09:27:32.157226Z","shell.execute_reply.started":"2024-04-23T09:27:32.147370Z","shell.execute_reply":"2024-04-23T09:27:32.155940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_dataset(example):\n    audio_path = example[\"file_path\"]\n    \n    # load the audio using librosa or torch audio (as you wish)\n    audio, sr = librosa.load(audio_path, sr=16_000)\n    \n    example[\"input_features\"] = feature_extractor(audio, sampling_rate=sr).input_features[0]\n    \n    example[\"labels\"] = tokenizer(f\"{example['transcripts']}\", max_length=448, padding=True, truncation=True).input_ids\n    \n    return example\n\n\ndef filter_inputs(input_audio):\n    \"\"\"filter inputs with zero input length\"\"\"\n    return 0 < len(input_audio)\n\n\ndef filter_labels(input_labels):\n    \"\"\"filter empty label sequences\"\"\"\n    return 0 < len(input_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.159264Z","iopub.execute_input":"2024-04-23T09:27:32.159767Z","iopub.status.idle":"2024-04-23T09:27:32.170938Z","shell.execute_reply.started":"2024-04-23T09:27:32.159724Z","shell.execute_reply":"2024-04-23T09:27:32.169672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = data[data[\"split\"] == \"train\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.172696Z","iopub.execute_input":"2024-04-23T09:27:32.173141Z","iopub.status.idle":"2024-04-23T09:27:32.197226Z","shell.execute_reply.started":"2024-04-23T09:27:32.173094Z","shell.execute_reply":"2024-04-23T09:27:32.196079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n    adjust test size accordingly.\n\"\"\"\ntrain_df, eval_df = train_test_split(train_df, test_size=0.02, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.198573Z","iopub.execute_input":"2024-04-23T09:27:32.198888Z","iopub.status.idle":"2024-04-23T09:27:32.209652Z","shell.execute_reply.started":"2024-04-23T09:27:32.198860Z","shell.execute_reply":"2024-04-23T09:27:32.208450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df), len(eval_df)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.211029Z","iopub.execute_input":"2024-04-23T09:27:32.211424Z","iopub.status.idle":"2024-04-23T09:27:32.220196Z","shell.execute_reply.started":"2024-04-23T09:27:32.211391Z","shell.execute_reply":"2024-04-23T09:27:32.219052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ben_reg_voice_ds = DatasetDict()\n\ntrain_split = DS.from_pandas(train_df)\neval_split = DS.from_pandas(eval_df)\n\nds_splits = DatasetDict({\n    'train': train_split,\n    'eval': eval_split\n})","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.221648Z","iopub.execute_input":"2024-04-23T09:27:32.222369Z","iopub.status.idle":"2024-04-23T09:27:32.275440Z","shell.execute_reply.started":"2024-04-23T09:27:32.222337Z","shell.execute_reply":"2024-04-23T09:27:32.274596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_splits = ds_splits.remove_columns([\"split\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.276854Z","iopub.execute_input":"2024-04-23T09:27:32.277756Z","iopub.status.idle":"2024-04-23T09:27:32.285427Z","shell.execute_reply.started":"2024-04-23T09:27:32.277712Z","shell.execute_reply":"2024-04-23T09:27:32.284425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(ds_splits)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.286770Z","iopub.execute_input":"2024-04-23T09:27:32.287425Z","iopub.status.idle":"2024-04-23T09:27:32.294394Z","shell.execute_reply.started":"2024-04-23T09:27:32.287384Z","shell.execute_reply":"2024-04-23T09:27:32.293475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.object = object","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.295489Z","iopub.execute_input":"2024-04-23T09:27:32.295850Z","iopub.status.idle":"2024-04-23T09:27:32.301736Z","shell.execute_reply.started":"2024-04-23T09:27:32.295813Z","shell.execute_reply":"2024-04-23T09:27:32.300877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_splits = ds_splits.map(prepare_dataset, remove_columns=ds_splits.column_names[\"train\"],\n                          num_proc=2 # open for multithreadding\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:27:32.304039Z","iopub.execute_input":"2024-04-23T09:27:32.304993Z","iopub.status.idle":"2024-04-23T09:34:08.591394Z","shell.execute_reply.started":"2024-04-23T09:27:32.304964Z","shell.execute_reply":"2024-04-23T09:34:08.590219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ds_splits = ds_splits.filter(filter_inputs, input_columns=[\"input_features\"])\n# ds_splits = ds_splits.filter(filter_labels, input_columns=[\"labels\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:08.593086Z","iopub.execute_input":"2024-04-23T09:34:08.594104Z","iopub.status.idle":"2024-04-23T09:34:08.598022Z","shell.execute_reply.started":"2024-04-23T09:34:08.594070Z","shell.execute_reply":"2024-04-23T09:34:08.597152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ds_splits[\"train\"]), len(ds_splits[\"eval\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:08.599149Z","iopub.execute_input":"2024-04-23T09:34:08.599500Z","iopub.status.idle":"2024-04-23T09:34:08.612696Z","shell.execute_reply.started":"2024-04-23T09:34:08.599473Z","shell.execute_reply":"2024-04-23T09:34:08.611694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cer = CharErrorRate()\nwer = WordErrorRate()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:08.613983Z","iopub.execute_input":"2024-04-23T09:34:08.614304Z","iopub.status.idle":"2024-04-23T09:34:08.626279Z","shell.execute_reply.started":"2024-04-23T09:34:08.614276Z","shell.execute_reply":"2024-04-23T09:34:08.625431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compute_metrics(pred):\n    pred_ids = pred.predictions\n    label_ids = pred.label_ids\n\n    label_ids[label_ids == -100] = tokenizer.pad_token_id\n\n    pred_str = tokenizer.batch_decode(pred_ids, skip_special_tokens=True)\n    label_str = tokenizer.batch_decode(label_ids, skip_special_tokens=True)\n\n    wer_res = wer(pred_str, label_str)\n    cer_res = cer(pred_str, label_str)\n    \n    \"\"\"\n        uncomment the next 3 lines if you want to see how the examples look like during eval \n    \"\"\"\n    print(\"WER:\",wer_res,\"| CER:\", cer_res) # to show up during running logs\n    print(\"Pred:\",pred_str[0])\n    print(\"Label:\",label_str[0])\n    \n    return {\"wer\": wer_res, \"cer\": cer_res}","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:08.627486Z","iopub.execute_input":"2024-04-23T09:34:08.627739Z","iopub.status.idle":"2024-04-23T09:34:08.635609Z","shell.execute_reply.started":"2024-04-23T09:34:08.627716Z","shell.execute_reply":"2024-04-23T09:34:08.634722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = WhisperForConditionalGeneration.from_pretrained(MODEL_NAME, device_map=\"auto\")","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:08.636716Z","iopub.execute_input":"2024-04-23T09:34:08.637016Z","iopub.status.idle":"2024-04-23T09:34:16.500948Z","shell.execute_reply.started":"2024-04-23T09:34:08.636988Z","shell.execute_reply":"2024-04-23T09:34:16.499925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_id = \"whisper-reg-ben\"","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:16.502225Z","iopub.execute_input":"2024-04-23T09:34:16.502547Z","iopub.status.idle":"2024-04-23T09:34:16.507252Z","shell.execute_reply.started":"2024-04-23T09:34:16.502519Z","shell.execute_reply":"2024-04-23T09:34:16.506201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_args = Seq2SeqTrainingArguments(\n    output_dir=model_id,\n    per_device_train_batch_size=32,\n    per_device_eval_batch_size=16,\n    gradient_accumulation_steps=1,\n    gradient_checkpointing=True,\n    fp16=True,\n    learning_rate=2e-4,\n    weight_decay=1e-2,\n    warmup_steps=100,\n    num_train_epochs=4,\n    evaluation_strategy= \"steps\",\n    predict_with_generate=True,\n#     generation_max_length=448,\n    save_steps=1000,\n    eval_steps=1000,\n    logging_steps=1000,\n    save_total_limit=1,\n    load_best_model_at_end=True,\n    metric_for_best_model=\"wer\",\n    greater_is_better=False,\n    push_to_hub=False,\n    report_to=\"none\",\n    remove_unused_columns=False,\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:39:11.122120Z","iopub.execute_input":"2024-04-23T09:39:11.122551Z","iopub.status.idle":"2024-04-23T09:39:11.132329Z","shell.execute_reply.started":"2024-04-23T09:39:11.122516Z","shell.execute_reply":"2024-04-23T09:39:11.131238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.generation_config.language = \"bn\"\nmodel.generation_config.task = \"transcribe\"\n\nmodel.generation_config.forced_decoder_ids = None\nmodel.config.suppress_tokens = [] # added later","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:39:15.898498Z","iopub.execute_input":"2024-04-23T09:39:15.898930Z","iopub.status.idle":"2024-04-23T09:39:15.904406Z","shell.execute_reply.started":"2024-04-23T09:39:15.898895Z","shell.execute_reply":"2024-04-23T09:39:15.903350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer = Seq2SeqTrainer(\n    args=training_args,\n    model=model,\n    train_dataset=ds_splits[\"train\"],\n    eval_dataset=ds_splits[\"eval\"],\n    data_collator=data_collator,\n    tokenizer=processor.feature_extractor,\n    compute_metrics=compute_metrics,\n#     callbacks=[EarlyStoppingCallback(2, 1.0)]\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:39:20.211697Z","iopub.execute_input":"2024-04-23T09:39:20.212102Z","iopub.status.idle":"2024-04-23T09:39:20.230741Z","shell.execute_reply.started":"2024-04-23T09:39:20.212067Z","shell.execute_reply":"2024-04-23T09:39:20.229768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.train()\n\n# to use the high-level pipeline, ensure both the processor outputs and model outputs exist in the same dir\ntrainer.save_model(training_args.output_dir)\nprocessor.save_pretrained(training_args.output_dir)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:39:29.934122Z","iopub.execute_input":"2024-04-23T09:39:29.934533Z","iopub.status.idle":"2024-04-23T09:46:43.950035Z","shell.execute_reply.started":"2024-04-23T09:39:29.934505Z","shell.execute_reply":"2024-04-23T09:46:43.948415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_logs = pd.DataFrame(trainer.state.log_history)\nout_logs.to_csv(\"logs.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.017998Z","iopub.status.idle":"2024-04-23T09:34:18.018420Z","shell.execute_reply.started":"2024-04-23T09:34:18.018218Z","shell.execute_reply":"2024-04-23T09:34:18.018237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n\ndel ds_splits\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.019982Z","iopub.status.idle":"2024-04-23T09:34:18.020521Z","shell.execute_reply.started":"2024-04-23T09:34:18.020243Z","shell.execute_reply":"2024-04-23T09:34:18.020266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.021924Z","iopub.status.idle":"2024-04-23T09:34:18.022357Z","shell.execute_reply.started":"2024-04-23T09:34:18.022123Z","shell.execute_reply":"2024-04-23T09:34:18.022140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe = pipeline(\n    \"automatic-speech-recognition\",\n    model=model_id,\n    chunk_length_s=30,\n    device=0,\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.023810Z","iopub.status.idle":"2024-04-23T09:34:18.024227Z","shell.execute_reply.started":"2024-04-23T09:34:18.024008Z","shell.execute_reply":"2024-04-23T09:34:18.024024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pretty_sort(filename):\n    name, number_str = filename.split(\" (\")\n    number = int(number_str.split(\")\")[0])\n    return name, number","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.025488Z","iopub.status.idle":"2024-04-23T09:34:18.025894Z","shell.execute_reply.started":"2024-04-23T09:34:18.025696Z","shell.execute_reply":"2024-04-23T09:34:18.025712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\npreds = []","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.027332Z","iopub.status.idle":"2024-04-23T09:34:18.027726Z","shell.execute_reply.started":"2024-04-23T09:34:18.027537Z","shell.execute_reply":"2024-04-23T09:34:18.027554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for root, dirs, files in os.walk(\"/kaggle/input/ben10/ben10/16_kHz_valid_audio\"):\n    files = sorted(files, key=pretty_sort)\n    \n#     print(files.index(\"valid_sandwip (1).wav\"))\n#     print(files.index(\"valid_sandwip (132).wav\"))\n    \n#     put swandip first\n    shift = files[1070 : 1202]\n    \n    files = shift + files[:1070] + files[1202:]\n    ids = files.copy()\n    \n    for file in files:\n        composed_path = f\"{test_data_dir}{file}\"\n        audio, sr = librosa.load(composed_path, sr=16_000)\n        text = pipe(audio)[\"text\"]\n        preds.append(text)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.029171Z","iopub.status.idle":"2024-04-23T09:34:18.029571Z","shell.execute_reply.started":"2024-04-23T09:34:18.029391Z","shell.execute_reply":"2024-04-23T09:34:18.029408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.030762Z","iopub.status.idle":"2024-04-23T09:34:18.031150Z","shell.execute_reply.started":"2024-04-23T09:34:18.030965Z","shell.execute_reply":"2024-04-23T09:34:18.030981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df[\"id\"] = ids\nsub_df[\"sentence\"] = preds\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.033601Z","iopub.status.idle":"2024-04-23T09:34:18.034004Z","shell.execute_reply.started":"2024-04-23T09:34:18.033818Z","shell.execute_reply":"2024-04-23T09:34:18.033834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom bnunicodenormalizer import Normalizer \n\n# Create a normalizer object\nbnorm = Normalizer()\n\ndef normalize(sen):\n    # Normalize each word in the sentence and join them back\n    _words = [bnorm.normalize(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        # Ensure the sentence ends with '।' (Bengali full stop)\n        if sentence[-1] != \"।\":\n            sentence += \"।\"\n    except:\n        print(sentence)\n    return sentence\n\nsub_df[\"sentence\"]= sub_df[\"sentence\"].apply(normalize)\nsub_df[\"sentence\"] =sub_df[\"sentence\"].apply(dari)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.035710Z","iopub.status.idle":"2024-04-23T09:34:18.036117Z","shell.execute_reply.started":"2024-04-23T09:34:18.035926Z","shell.execute_reply":"2024-04-23T09:34:18.035943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.037560Z","iopub.status.idle":"2024-04-23T09:34:18.037971Z","shell.execute_reply.started":"2024-04-23T09:34:18.037768Z","shell.execute_reply":"2024-04-23T09:34:18.037786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T09:34:18.039328Z","iopub.status.idle":"2024-04-23T09:34:18.039696Z","shell.execute_reply.started":"2024-04-23T09:34:18.039519Z","shell.execute_reply":"2024-04-23T09:34:18.039535Z"},"trusted":true},"execution_count":null,"outputs":[]}]}