{"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":"code","source":"\nimport os\nimport numpy as np\nimport pandas as pd\nfrom transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration\nfrom transformers import pipeline\nimport soundfile as sf","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:24:46.404127Z","iopub.execute_input":"2023-08-20T10:24:46.404490Z","iopub.status.idle":"2023-08-20T10:24:46.410163Z","shell.execute_reply.started":"2023-08-20T10:24:46.404455Z","shell.execute_reply":"2023-08-20T10:24:46.409043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_path = \"/kaggle/input/audio2txt/model\"\nsave_path_processor = \"/kaggle/input/audio2txt/processor\"\nsave_path_pipe = \"/kaggle/input/audio2txt/pipe\"","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:24:48.138034Z","iopub.execute_input":"2023-08-20T10:24:48.138389Z","iopub.status.idle":"2023-08-20T10:24:48.143396Z","shell.execute_reply.started":"2023-08-20T10:24:48.138361Z","shell.execute_reply":"2023-08-20T10:24:48.142348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Speech2TextForConditionalGeneration.from_pretrained(save_path, return_dict=True)\nprocessor = Speech2TextProcessor.from_pretrained(save_path_processor, return_dict=True)\npipe= pipeline('text2text-generation', model=save_path_pipe)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:24:48.437702Z","iopub.execute_input":"2023-08-20T10:24:48.438019Z","iopub.status.idle":"2023-08-20T10:24:59.436787Z","shell.execute_reply.started":"2023-08-20T10:24:48.437993Z","shell.execute_reply":"2023-08-20T10:24:59.435777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mp3toarray(path):\n    return sf.read(path)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:24:59.438779Z","iopub.execute_input":"2023-08-20T10:24:59.439146Z","iopub.status.idle":"2023-08-20T10:24:59.444581Z","shell.execute_reply.started":"2023-08-20T10:24:59.439114Z","shell.execute_reply":"2023-08-20T10:24:59.443566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nspeech = []\nfor i in (os.listdir('/kaggle/input/bengaliai-speech/test_mp3s')):\n    ids.append(i.split('.')[0])\n    speech.append(mp3toarray('/kaggle/input/bengaliai-speech/test_mp3s/'+i))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:24:59.446142Z","iopub.execute_input":"2023-08-20T10:24:59.447004Z","iopub.status.idle":"2023-08-20T10:24:59.519676Z","shell.execute_reply.started":"2023-08-20T10:24:59.446971Z","shell.execute_reply":"2023-08-20T10:24:59.518758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def translate_to_bengali(text):\n    tln=pipe(text, forced_bos_token_id=pipe.tokenizer.get_lang_id('bn'))[0]\n    for item in tln:\n        result=tln[item]\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:26:50.022511Z","iopub.execute_input":"2023-08-20T10:26:50.023678Z","iopub.status.idle":"2023-08-20T10:26:50.029988Z","shell.execute_reply.started":"2023-08-20T10:26:50.023642Z","shell.execute_reply":"2023-08-20T10:26:50.028962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def speech2text(array, sample_rate):\n    inputs = processor(array, sampling_rate=sample_rate, return_tensors=\"pt\")\n    generted_id = model.generate(inputs['input_features'], attention_mask=inputs['attention_mask'])\n    translate = translate_to_bengali(processor.batch_decode(generted_id, skip_special_token=True)[0].split('</s>')[1])\n    return translate","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:26:50.381209Z","iopub.execute_input":"2023-08-20T10:26:50.382479Z","iopub.status.idle":"2023-08-20T10:26:50.388545Z","shell.execute_reply.started":"2023-08-20T10:26:50.382435Z","shell.execute_reply":"2023-08-20T10:26:50.387529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sentence = []\nfor i in speech:\n    array = i[0]\n    sppe= speech2text(array, 16000)\n    del array\n    sentence.append(sppe)\n    del sppe","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:26:52.035280Z","iopub.execute_input":"2023-08-20T10:26:52.036009Z","iopub.status.idle":"2023-08-20T10:27:25.030741Z","shell.execute_reply.started":"2023-08-20T10:26:52.035967Z","shell.execute_reply":"2023-08-20T10:27:25.029590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model\ndel processor\ndel pipe\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:28:01.776910Z","iopub.execute_input":"2023-08-20T10:28:01.777277Z","iopub.status.idle":"2023-08-20T10:28:02.015217Z","shell.execute_reply.started":"2023-08-20T10:28:01.777249Z","shell.execute_reply":"2023-08-20T10:28:02.013946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({\n    'id': ids,\n    'sentence': sentence\n})","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:28:03.553970Z","iopub.execute_input":"2023-08-20T10:28:03.554348Z","iopub.status.idle":"2023-08-20T10:28:03.566023Z","shell.execute_reply.started":"2023-08-20T10:28:03.554319Z","shell.execute_reply":"2023-08-20T10:28:03.564852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T10:28:03.917096Z","iopub.execute_input":"2023-08-20T10:28:03.917494Z","iopub.status.idle":"2023-08-20T10:28:03.928083Z","shell.execute_reply.started":"2023-08-20T10:28:03.917449Z","shell.execute_reply":"2023-08-20T10:28:03.926941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}