{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"gpu","dataSources":[{"sourceId":73047,"databundleVersionId":8149390,"sourceType":"competition"},{"sourceId":8215535,"sourceType":"datasetVersion","datasetId":4869422},{"sourceId":8215554,"sourceType":"datasetVersion","datasetId":4869435}],"dockerImageVersionId":30703,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q git+https://github.com/m-bain/whisperx.git\n","metadata":{"execution":{"iopub.status.busy":"2024-04-24T12:05:47.777399Z","iopub.execute_input":"2024-04-24T12:05:47.778082Z","iopub.status.idle":"2024-04-24T12:06:21.452495Z","shell.execute_reply.started":"2024-04-24T12:05:47.778046Z","shell.execute_reply":"2024-04-24T12:06:21.451444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndebug = False\n\ndata_source = '/kaggle/input/ben10/sample_submission.csv'\nroot_path = '/kaggle/input/ben10/ben10/16_kHz_valid_audio'\nroot_valid_path = '/kaggle/input/ben10/ben10/16_kHz_valid_audio'\nroot_train_path = '/kaggle/input/ben10/ben10/16_kHz_train_audio'\nacoustic_model = '/kaggle/input/whisper-medium-dialect'\nalignment_model = '/kaggle/input/bn-wav2vec2'\nstupid_symbol_threshold = 1.5\n\ndata = pd.read_csv(data_source)\n\nif debug:\n    data = data[100:105]\n    \nimport os\nfrom tqdm import tqdm\ntqdm.pandas()\n\ndef get_path_name(name):\n    if name.startswith('valid'):\n        return os.path.join(root_valid_path, name)\n    elif name.startswith('train'):\n        return os.path.join(root_valid_path, name.replace('train','valid'))\n\ndata['path'] = data.id.progress_apply(lambda x: get_path_name(x))\nfrom datasets import Dataset\ndataset = Dataset.from_pandas(data)\ndataset\n\nimport whisperx\n\nret_time = True\nmodel = whisperx.load_model(acoustic_model, device='cuda', compute_type='float32')\nif ret_time:\n    model_a, metadata = whisperx.load_align_model(language_code='bn', device='cuda', model_name = alignment_model)\n\ndef get_transcription(audio_path, return_timestamps = ret_time):\n    audio = whisperx.load_audio(audio_path, sr = 16000)\n    result = model.transcribe(audio)\n    transcription = ' '.join([text['text'] for text in result['segments']])\n    if return_timestamps:\n        result = whisperx.align(result[\"segments\"], model_a, metadata, audio, device='cuda', return_char_alignments=False)\n        duration_list = []\n        try:\n            inf_words = pd.DataFrame.from_records(result['segments'][0]['words']).word.tolist()\n            start_time_list = pd.DataFrame.from_records(result['segments'][0]['words']).start.tolist()\n            end_time_list = pd.DataFrame.from_records(result['segments'][0]['words']).end.tolist()\n            \n            durations_of_unknwn = []\n            if '<>' in inf_words:\n                prev_time_stamp = 0\n                next_time_stamp = 0\n                flag = 0\n                for inf_w, start_t, end_t in zip(inf_words, start_time_list, end_time_list):\n                    if inf_w != '<>' and flag == 0:\n                        prev_time_stamp = end_t\n                    elif inf_w == '<>' or inf_w == '<> ' or inf_w == ' <>' or inf_w == ' <> ':\n                        flag = 1\n                        continue\n                    if flag == 1:\n                        next_time_stamp = start_t\n                        durations_of_unknwn.append(next_time_stamp - prev_time_stamp)\n                        flag = 0\n            duration_list = durations_of_unknwn\n        except:\n            duration_list =  [int(0)]\n        \n        return {'sentence': transcription.strip(), 'duration_list': duration_list}\n    else:\n        return {'sentence': transcription.strip()}","metadata":{"execution":{"iopub.status.busy":"2024-04-24T12:09:18.306121Z","iopub.execute_input":"2024-04-24T12:09:18.306511Z","iopub.status.idle":"2024-04-24T12:09:36.328883Z","shell.execute_reply.started":"2024-04-24T12:09:18.306481Z","shell.execute_reply":"2024-04-24T12:09:36.327840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset.map(lambda x: get_transcription(x['path']))\n# dataset.save_to_disk('RAW_TRANSCRIPTIONS')","metadata":{"execution":{"iopub.status.busy":"2024-04-24T12:09:48.180085Z","iopub.execute_input":"2024-04-24T12:09:48.180458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from datasets import load_from_disk\n# dataset = load_from_disk('RAW_TRANSCRIPTIONS')\nimport re, os\nimport pandas as pd\n\ndef process_symbols(sentence, durations):\n    def replace_func(match):\n        nonlocal index, removed_count\n        keep = durations[index] >= 0.25\n        index += 1\n        if not keep:\n            removed_count += 1\n        return match.group() if keep else ''\n\n    index = 0\n    removed_count = 0\n    result_sentence = re.sub(r'<>', replace_func, sentence)\n    return result_sentence\n\ndef fix_repetition(text, max_count):\n    uniq_word_counter = {}\n    words = text.split()\n    for word in text.split():\n        if word not in uniq_word_counter:\n            uniq_word_counter[word] = 1\n        else:\n            uniq_word_counter[word] += 1\n\n    for word, count in uniq_word_counter.items():\n        if count > max_count:\n            words = [w for w in words if w != word]\n    text = \" \".join(words)\n    return text\n\ndef post_process(row):\n    text = row['sentence']\n    id = os.path.split(row['path'])[-1]\n    if text is None:\n        return '।'\n    else:\n        text = fix_repetition(text, max_count = 8)\n        try:\n            text = process_symbols(text, row['duration_list'])\n        except:\n            pass\n    if text == '':\n        text = '।'\n    return {'sentence': text.strip(), 'id':id}\n\ndataset = dataset.map(post_process,num_proc=16)\nsubs = pd.DataFrame(dataset)\nsubs.drop(columns=['path','duration_list'],inplace=True)\nsubs.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}