{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":8823072,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport librosa\nfrom tqdm import tqdm\nimport numpy as np\nimport random\nfrom pydub import AudioSegment\nimport librosa\nimport matplotlib.pyplot as plt\nimport os\nimport librosa\nfrom multiprocessing import Pool\nimport time\nimport re","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:13:28.61525Z","iopub.execute_input":"2024-12-04T06:13:28.616428Z","iopub.status.idle":"2024-12-04T06:13:28.622349Z","shell.execute_reply.started":"2024-12-04T06:13:28.616388Z","shell.execute_reply":"2024-12-04T06:13:28.621199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/ben10/ben10/16_kHz_train_audio/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/ben10/sample_submission.csv\")\nprint(\"Train dataframe : \")\ndisplay(df.head())\nprint(\"Test dataframe : \")\ndisplay(test.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:10:53.700041Z","iopub.execute_input":"2024-12-04T06:10:53.701065Z","iopub.status.idle":"2024-12-04T06:11:00.726309Z","shell.execute_reply.started":"2024-12-04T06:10:53.701026Z","shell.execute_reply":"2024-12-04T06:11:00.725326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Total samples in Train set: \",df.shape[0])\nprint(\"Total samples in test set: \",len(os.listdir(\"/kaggle/input/ben10/ben10/16_kHz_valid_audio\")))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:00.727976Z","iopub.execute_input":"2024-12-04T06:11:00.728304Z","iopub.status.idle":"2024-12-04T06:11:00.74867Z","shell.execute_reply.started":"2024-12-04T06:11:00.728248Z","shell.execute_reply":"2024-12-04T06:11:00.747627Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### total second to hour","metadata":{}},{"cell_type":"code","source":"def seconds_to_hours(seconds):\n    hours = seconds / 3600\n    return hours","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:00.749873Z","iopub.execute_input":"2024-12-04T06:11:00.75017Z","iopub.status.idle":"2024-12-04T06:11:00.754648Z","shell.execute_reply.started":"2024-12-04T06:11:00.750142Z","shell.execute_reply":"2024-12-04T06:11:00.753626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport librosa\n\ndef get_total_audio_duration(folder_path):\n    total_duration = 0\n    for filename in os.listdir(folder_path):\n        if filename.endswith(('.wav')):\n            file_path = os.path.join(folder_path, filename)\n            try:\n                audio_duration = librosa.get_duration(path=file_path)\n                total_duration += audio_duration\n            except Exception as e:\n                print(f\"Error processing {filename}: {e}\")\n    return total_duration\n\nfolder_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\ntotal_duration = get_total_audio_duration(folder_path)\nhours = seconds_to_hours(total_duration)\nprint(f\"Total duration of Train audio files: {total_duration} seconds which is {hours} Hours\")\n\nTest_dataset = \"/kaggle/input/ben10/ben10/16_kHz_valid_audio\"\ntest_total_duration = get_total_audio_duration(Test_dataset)\ntest_hours = seconds_to_hours(test_total_duration)\nprint(f\"Total duration of Test audio files: {total_duration} seconds which is {test_hours} Hours\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:00.756615Z","iopub.execute_input":"2024-12-04T06:11:00.756938Z","iopub.status.idle":"2024-12-04T06:11:46.475555Z","shell.execute_reply.started":"2024-12-04T06:11:00.756909Z","shell.execute_reply":"2024-12-04T06:11:46.473968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport librosa\nimport re\ndef get_total_audio_duration(folder_path):\n    total_duration = 0\n    pattern = re.compile(r'barisal', re.IGNORECASE)  # Regex to match \"barisal\" (case-insensitive)\n    \n    for filename in os.listdir(folder_path):\n        if filename.endswith('.wav') and re.search(r\"barishal\",filename):\n            file_path = os.path.join(folder_path, filename)\n            #print(file_path)\n\n            try:\n                audio_duration = librosa.get_duration(path=file_path)\n                total_duration += audio_duration\n            except Exception as e:\n                print(f\"Error processing {filename}: {e}\")\n    return total_duration\n\nfolder_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\ntotal_duration = get_total_audio_duration(folder_path)\nhours = seconds_to_hours(total_duration)\nprint(f\"Total duration of Train audio files: {total_duration} seconds which is {hours} Hours\")\n\nTest_dataset = \"/kaggle/input/ben10/ben10/16_kHz_valid_audio\"\ntest_total_duration = get_total_audio_duration(Test_dataset)\ntest_hours = seconds_to_hours(test_total_duration)\nprint(f\"Total duration of Test audio files: {total_duration} seconds which is {test_hours} Hours\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:09:20.360924Z","iopub.execute_input":"2024-12-04T06:09:20.361334Z","iopub.status.idle":"2024-12-04T06:09:22.137639Z","shell.execute_reply.started":"2024-12-04T06:09:20.361305Z","shell.execute_reply":"2024-12-04T06:09:22.136728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:53.03116Z","iopub.execute_input":"2024-12-04T06:11:53.031524Z","iopub.status.idle":"2024-12-04T06:11:53.042963Z","shell.execute_reply.started":"2024-12-04T06:11:53.031493Z","shell.execute_reply":"2024-12-04T06:11:53.041856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\n\nfor idx in df.sample(10).index:\n    \n    file_path = os.path.join(root_path, df['file_name'].iloc[idx])\n    text = df['transcriptions'].iloc[idx]\n    region = df['district'].iloc[idx]\n    print(f'Region: {str.capitalize(region)}')\n    display(AudioSegment.from_file(file_path))\n    print(f\"Original transcription : {text}\\n\\n\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:54.144934Z","iopub.execute_input":"2024-12-04T06:11:54.145324Z","iopub.status.idle":"2024-12-04T06:11:57.455901Z","shell.execute_reply.started":"2024-12-04T06:11:54.145261Z","shell.execute_reply":"2024-12-04T06:11:57.454901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\n\ndef plot_sound_characteristics(region_df):\n    for idx, row in region_df.iterrows():\n        file_path = os.path.join(root_path, row['file_name'])\n        text = row['transcriptions']\n        region = row['district']\n        \n        print(f'Region: {region.capitalize()}')\n        audio = AudioSegment.from_file(file_path)\n        \n        samples, sample_rate = librosa.load(file_path, sr=None)\n        \n        time = np.arange(0, len(samples)) / sample_rate\n        \n        plt.figure(figsize=(12, 6))\n        plt.subplot(2, 1, 1)\n        plt.plot(time, samples)\n        plt.title('Waveform (Amplitude)')\n        plt.xlabel('Time (s)')\n        plt.ylabel('Amplitude')\n        \n        plt.subplot(2, 1, 2)\n        n_fft = 2048  \n        freqs = np.fft.rfftfreq(n_fft, d=1/sample_rate)\n        spectrum = np.abs(np.fft.rfft(samples, n=n_fft))\n        plt.plot(freqs, spectrum)\n        plt.title('Frequency Spectrum')\n        plt.xlabel('Frequency (Hz)')\n        plt.ylabel('Magnitude')\n        plt.xlim(0, 5000) \n        \n        plt.tight_layout()\n        plt.show()\n        \n        print(f\"Original transcription: {text}\\n\\n\")\n\n\ngrouped_by_region = df.groupby('district')\n\n\nfor region, region_df in grouped_by_region:\n    plot_sound_characteristics(region_df.sample(1))  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:11:59.236907Z","iopub.execute_input":"2024-12-04T06:11:59.237921Z","iopub.status.idle":"2024-12-04T06:12:15.504358Z","shell.execute_reply.started":"2024-12-04T06:11:59.237863Z","shell.execute_reply":"2024-12-04T06:12:15.503334Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Vocabulary List\r\n","metadata":{}},{"cell_type":"code","source":"chars = {}\nfor sen in tqdm(df.transcriptions[df.district==\"barishal\"]):\n    for j in sen:\n        try:\n            chars[j] += 1\n        except:\n            chars[j] = 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:12:15.506198Z","iopub.execute_input":"2024-12-04T06:12:15.506528Z","iopub.status.idle":"2024-12-04T06:12:15.549851Z","shell.execute_reply.started":"2024-12-04T06:12:15.506499Z","shell.execute_reply":"2024-12-04T06:12:15.548824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"list(chars.items())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:12:15.551087Z","iopub.execute_input":"2024-12-04T06:12:15.551444Z","iopub.status.idle":"2024-12-04T06:12:15.560409Z","shell.execute_reply.started":"2024-12-04T06:12:15.551411Z","shell.execute_reply":"2024-12-04T06:12:15.559289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(chars.items())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:12:15.563021Z","iopub.execute_input":"2024-12-04T06:12:15.563457Z","iopub.status.idle":"2024-12-04T06:12:15.57185Z","shell.execute_reply.started":"2024-12-04T06:12:15.563424Z","shell.execute_reply":"2024-12-04T06:12:15.571061Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# reduce noise","metadata":{}},{"cell_type":"code","source":"!pip install noisereduce","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:12:15.573171Z","iopub.execute_input":"2024-12-04T06:12:15.573609Z","iopub.status.idle":"2024-12-04T06:12:26.410483Z","shell.execute_reply.started":"2024-12-04T06:12:15.573564Z","shell.execute_reply":"2024-12-04T06:12:26.409347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport noisereduce as nr\n# Set root path to the directory containing audio files\nroot_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\n\n# Define a function to denoise and plot audio\ndef denoise_and_plot(region_df):\n    for idx, row in region_df.iterrows():\n        file_path = os.path.join(root_path, row['file_name'])\n        text = row['transcriptions']\n        region = row['district']\n        \n        print(f'Region: {region.capitalize()}')\n        \n        # Load audio file using librosa\n        samples, sample_rate = librosa.load(file_path, sr=None)\n        \n        # Perform noise reduction using spectral subtraction\n        reduced_noise = nr.reduce_noise(y=samples, sr=sample_rate)\n        \n        # Compute time array for waveform plot\n        time = np.arange(0, len(samples)) / sample_rate\n        \n        # Plot original and denoised waveform\n        plt.figure(figsize=(12, 6))\n        plt.subplot(2, 1, 1)\n        plt.plot(time, samples, label='Original')\n        plt.title('Original Waveform')\n        plt.xlabel('Time (s)')\n        plt.ylabel('Amplitude')\n        plt.legend()\n        \n        plt.subplot(2, 1, 2)\n        plt.plot(time, reduced_noise, label='Denoised', color='orange')\n        plt.title('Denoised Waveform')\n        plt.xlabel('Time (s)')\n        plt.ylabel('Amplitude')\n        plt.legend()\n        \n        plt.tight_layout()\n        plt.show()\n        \n        print(f\"Original transcription: {text}\\n\\n\")\n\n# Assuming df is your DataFrame containing sample information\n# Assuming 'region' column exists in df\n\n# Group DataFrame by 'region'\ngrouped_by_region = df.groupby('district')\n\n# Iterate over each region and denoise audio\nfor region, region_df in grouped_by_region:\n    denoise_and_plot(region_df.sample(1))  # Sample 1 entry from each region for demonstration","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:12:26.412541Z","iopub.execute_input":"2024-12-04T06:12:26.41303Z","iopub.status.idle":"2024-12-04T06:12:41.705439Z","shell.execute_reply.started":"2024-12-04T06:12:26.412977Z","shell.execute_reply":"2024-12-04T06:12:41.704372Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Remove english words","metadata":{}},{"cell_type":"code","source":"# Set root path to the directory containing audio files\nroot_path = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\n\n# Define a function to check if a transcript contains English letters\ndef contains_english_letters(transcript):\n    # Regular expression pattern to match English letters\n    #pattern = re.compile('[a-zA-Z]')\n    # Search for English letters in the transcript\n    if re.search(r'[a-zA-Z]',transcript):\n        return True\n    return False\n\n# Define a function to remove English letters from a transcript\ndef remove_english_letters(transcript):\n    # Regular expression pattern to match English letters\n    pattern = re.compile('[a-zA-Z]')\n    # Replace English letters with an empty string\n    clean_transcript = pattern.sub('', transcript)\n    return clean_transcript\n\n# Iterate over samples and remove English letters from transcripts\nfor idx, row in df.iterrows():\n    file_path = os.path.join(root_path, row['file_name'])\n    text = row['transcriptions']\n    region = row['district']\n   \n    if contains_english_letters(text):\n        print(f\"Region: {region.capitalize()}\")\n        print(f\"Original Transcription: {text}\")\n        \n        # Remove English letters from transcript\n        cleaned_text = remove_english_letters(text)\n        print(f\"Cleaned Transcription: {cleaned_text}\")\n        \n        display(AudioSegment.from_file(file_path))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:13:38.485522Z","iopub.execute_input":"2024-12-04T06:13:38.485906Z","iopub.status.idle":"2024-12-04T06:13:39.265594Z","shell.execute_reply.started":"2024-12-04T06:13:38.485874Z","shell.execute_reply":"2024-12-04T06:13:39.264553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[df.district==\"barishal\"].nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:13:40.41517Z","iopub.execute_input":"2024-12-04T06:13:40.41557Z","iopub.status.idle":"2024-12-04T06:13:40.42975Z","shell.execute_reply.started":"2024-12-04T06:13:40.41554Z","shell.execute_reply":"2024-12-04T06:13:40.428619Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"markdown","source":"### Seperate train audio based on region","metadata":{}},{"cell_type":"markdown","source":"training  split","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nfrom tqdm import tqdm  # Import tqdm\n\n# Path to the directory containing the audio files\naudio_dir = \"/kaggle/input/ben10/ben10/16_kHz_train_audio\"\n#audio_dir = \"/kaggle/input/ben10/ben10/16_kHz_valid_audio\"\n\n# Directory to save the separated audio files\nsave_dir = \"dataset_based_on_region/train/\"\n#save_dir = \"dataset_based_on_region/valid/\"\n# Seperate train.csv based on region\n# Get the list of audio files\naudio_files = [f for f in os.listdir(audio_dir) if f.endswith(\".wav\")]\n\n# Use tqdm to iterate through each audio file with a progress bar\nfor filename in tqdm(audio_files, desc=\"Copying audio files\"):\n    # Extract the region name from the filename\n    region_name = filename.split(\" (\")[0]  # Split by \" (\" and take the first part\n\n    # Create a directory for the region if it doesn't exist in the save directory\n    region_dir = os.path.join(save_dir, region_name)\n    os.makedirs(region_dir, exist_ok=True)\n\n    # Copy the audio file to the corresponding directory in the save directory\n    source_path = os.path.join(audio_dir, filename)\n    destination_path = os.path.join(region_dir, filename)\n    shutil.copy(source_path, destination_path)  # Copy the file\n\nprint(\"Separation completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:13:54.411734Z","iopub.execute_input":"2024-12-04T06:13:54.412116Z","iopub.status.idle":"2024-12-04T06:15:51.542816Z","shell.execute_reply.started":"2024-12-04T06:13:54.412084Z","shell.execute_reply":"2024-12-04T06:15:51.541738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport shutil\nfrom tqdm import tqdm  # Import tqdm\n\n# Path to the directory containing the audio files\naudio_dir = \"/kaggle/input/ben10/ben10/16_kHz_valid_audio\"\n\n# Directory to save the separated audio files\nsave_dir = \"dataset_based_on_region/valid/\"\n# Seperate train.csv based on region\n# Get the list of audio files\naudio_files = [f for f in os.listdir(audio_dir) if f.endswith(\".wav\")]\n\n# Use tqdm to iterate through each audio file with a progress bar\nfor filename in tqdm(audio_files, desc=\"Copying audio files\"):\n    # Extract the region name from the filename\n    region_name = filename.split(\" (\")[0]  # Split by \" (\" and take the first part\n\n    # Create a directory for the region if it doesn't exist in the save directory\n    region_dir = os.path.join(save_dir, region_name)\n    os.makedirs(region_dir, exist_ok=True)\n\n    # Copy the audio file to the corresponding directory in the save directory\n    source_path = os.path.join(audio_dir, filename)\n    destination_path = os.path.join(region_dir, filename)\n    shutil.copy(source_path, destination_path)  # Copy the file\n\nprint(\"Separation completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:21:20.127374Z","iopub.execute_input":"2024-12-04T06:21:20.127768Z","iopub.status.idle":"2024-12-04T06:21:41.005395Z","shell.execute_reply.started":"2024-12-04T06:21:20.127736Z","shell.execute_reply":"2024-12-04T06:21:41.004423Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Seperate train.csv based on region","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport csv\n\n# Read the CSV file\n#csv_file = \"/kaggle/input/ben10/ben10/train.csv\"\ncsv_file = \"/kaggle/input/ben10/ben10/16_kHz_train_audio/train.csv\"\ndf = pd.read_csv(csv_file)\n\n# Create a directory to save the separated CSV files\noutput_dir = \"dataset_based_on_region/separated_train_csv/\"\nos.makedirs(output_dir, exist_ok=True)\n\n# Iterate through each row in the DataFrame to write headers and data to separate CSV files\nfor index, row in df.iterrows():\n    # Extract file name and transcripts from the current row\n    file_name = row[\"file_name\"]\n    transcripts = row[\"transcriptions\"]\n    \n    # Remove newline characters from transcripts\n    transcripts = transcripts.replace(\"\\n\", \" \")\n    \n    # Enclose transcripts in double quotes and escape any double quotes within the text\n    transcripts = '\"' + transcripts.replace('\"', '\"\"') + '\"'\n    \n    # Extract the region name from the file name\n    region_name = file_name.split(\"_\")[1].split(\" (\")[0]\n    \n    # Create a CSV file for the region if it doesn't exist\n    region_csv_file = os.path.join(output_dir, f\"{region_name}_train.csv\")\n    if not os.path.exists(region_csv_file):\n        with open(region_csv_file, \"w\", newline='', encoding='utf-8') as f:\n            # Initialize CSV writer\n            writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n            # Write header to the CSV file\n            writer.writerow([\"file_name\", \"transcriptions\"])\n    \n    # Append the row data to the CSV file for the region\n    with open(region_csv_file, \"a\", newline='', encoding='utf-8') as f:\n        # Initialize CSV writer\n        writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n        # Write the row data to the CSV file\n        writer.writerow([file_name, transcripts])\n\nprint(\"Separation completed.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:21:41.007486Z","iopub.execute_input":"2024-12-04T06:21:41.007913Z","iopub.status.idle":"2024-12-04T06:21:43.090394Z","shell.execute_reply.started":"2024-12-04T06:21:41.007869Z","shell.execute_reply":"2024-12-04T06:21:43.089343Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## import libraries","metadata":{}},{"cell_type":"code","source":"import torch\ntorch.cuda.is_available()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:21:43.091656Z","iopub.execute_input":"2024-12-04T06:21:43.09195Z","iopub.status.idle":"2024-12-04T06:21:43.191637Z","shell.execute_reply.started":"2024-12-04T06:21:43.091922Z","shell.execute_reply":"2024-12-04T06:21:43.190451Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:21:43.193981Z","iopub.execute_input":"2024-12-04T06:21:43.194424Z","iopub.status.idle":"2024-12-04T06:22:03.817518Z","shell.execute_reply.started":"2024-12-04T06:21:43.194378Z","shell.execute_reply":"2024-12-04T06:22:03.81667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_region_name = 'barishal'\nBASE_DIR = '/kaggle/working/dataset_based_on_region'\ntrain_data_dir = f\"{BASE_DIR}/train/train_{train_region_name}/\"\ntest_data_dir = f\"{BASE_DIR}/valid/valid_{train_region_name}/\"\ndata_path = f\"{BASE_DIR}/separated_train_csv/{train_region_name}_train.csv\"\n\n#data_path = \"dataset/ben10/train.csv\"\nprint(data_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:03.819069Z","iopub.execute_input":"2024-12-04T06:22:03.819902Z","iopub.status.idle":"2024-12-04T06:22:03.825301Z","shell.execute_reply.started":"2024-12-04T06:22:03.819855Z","shell.execute_reply":"2024-12-04T06:22:03.824321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"split2path = {\n    \"train\": train_data_dir,\n    \"test\": test_data_dir,\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:03.826953Z","iopub.execute_input":"2024-12-04T06:22:03.827252Z","iopub.status.idle":"2024-12-04T06:22:03.856494Z","shell.execute_reply.started":"2024-12-04T06:22:03.827224Z","shell.execute_reply":"2024-12-04T06:22:03.855568Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Here using data=pd.concat([data, data]) to enrich the dataset, By doing this we were getting less WER as this helps the model to place words at correct position","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv(data_path)\nprint(len(data))\ndata=pd.concat([data, data])\nprint(len(data))\ndata.sample(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:03.857472Z","iopub.execute_input":"2024-12-04T06:22:03.857783Z","iopub.status.idle":"2024-12-04T06:22:03.885924Z","shell.execute_reply.started":"2024-12-04T06:22:03.857755Z","shell.execute_reply":"2024-12-04T06:22:03.884991Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"This code defines functions to extract information like split and district from filenames, then it processes a dataset by mapping filenames to their corresponding paths, replacing newline characters in transcripts, and adding columns for split, district, file path, and cleaned transcripts","metadata":{}},{"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['transcriptions'] = transcripts\n    \n#     data.drop(columns=['file_name'], inplace=True)\n    \n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:10.900659Z","iopub.execute_input":"2024-12-04T06:22:10.901014Z","iopub.status.idle":"2024-12-04T06:22:10.909194Z","shell.execute_reply.started":"2024-12-04T06:22:10.900985Z","shell.execute_reply":"2024-12-04T06:22:10.90813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = beautify_dataset(data)\ndata.sample(20)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:11.960838Z","iopub.execute_input":"2024-12-04T06:22:11.961224Z","iopub.status.idle":"2024-12-04T06:22:12.046688Z","shell.execute_reply.started":"2024-12-04T06:22:11.961191Z","shell.execute_reply":"2024-12-04T06:22:12.045639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.split.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:13.222091Z","iopub.execute_input":"2024-12-04T06:22:13.223038Z","iopub.status.idle":"2024-12-04T06:22:13.229325Z","shell.execute_reply.started":"2024-12-04T06:22:13.222996Z","shell.execute_reply":"2024-12-04T06:22:13.228361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[data[\"transcriptions\"] == \"<>\"]\ndata[data[\"transcriptions\"] == \"\"]\ndata[data[\"transcriptions\"] == \"..\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:14.499654Z","iopub.execute_input":"2024-12-04T06:22:14.500515Z","iopub.status.idle":"2024-12-04T06:22:14.524377Z","shell.execute_reply.started":"2024-12-04T06:22:14.500445Z","shell.execute_reply":"2024-12-04T06:22:14.523352Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Removing punctuation marks from training set. Here we are not removing '-' '*' '=' '+' ',' ':' ';' '!' as they were attached to the words and if we infer them seperately then we were getting higher WER. so we only remove \"।()[]{}\\,?_~\" these punctuations","metadata":{}},{"cell_type":"code","source":"import string\nimport re \n\n# Bangla punctuation marks to remove\nbangla_punctuation = \"।()[]{}\\,?_~\"\n\n# Function to remove punctuation marks\ndef remove_punctuation(text):\n    translator = str.maketrans('', '', bangla_punctuation)\n    text = text.translate(translator)\n    # Remove single dots not part of a sequence of continuous dots\n    text = re.sub(r'(?<!\\.)\\.(?!\\.)', '', text)\n    return text\n\n# Apply the function to remove punctuation marks\ndata[\"transcriptions\"] = data[\"transcriptions\"].apply(remove_punctuation)\n\ndata.head(20)\ndata['transcriptions'].to_csv(\"dots.csv\",index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:16.257417Z","iopub.execute_input":"2024-12-04T06:22:16.25777Z","iopub.status.idle":"2024-12-04T06:22:16.32222Z","shell.execute_reply.started":"2024-12-04T06:22:16.257739Z","shell.execute_reply":"2024-12-04T06:22:16.321403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"transcriptions\"] = data[\"transcriptions\"].str.strip()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:17.041737Z","iopub.execute_input":"2024-12-04T06:22:17.042105Z","iopub.status.idle":"2024-12-04T06:22:17.048322Z","shell.execute_reply.started":"2024-12-04T06:22:17.042073Z","shell.execute_reply":"2024-12-04T06:22:17.047225Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## normalize text","metadata":{}},{"cell_type":"code","source":"!pip install jiwer\n!pip install bnlp-toolkit\n!pip install banglanum2words\n!pip install git+https://github.com/csebuetnlp/normalizer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:22:18.113712Z","iopub.execute_input":"2024-12-04T06:22:18.114168Z","iopub.status.idle":"2024-12-04T06:23:31.022638Z","shell.execute_reply.started":"2024-12-04T06:22:18.114119Z","shell.execute_reply":"2024-12-04T06:23:31.021463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\nimport unicodedata\nfrom normalizer import normalize\nfrom banglanum2words import num_convert\n\ndef unicode_to_ascii(s):\n    return unicodedata.normalize('NFKC', s)\n\ndef is_english_numeral(numeral):\n    english_numeral_regex = re.compile(\"[0-9]\")\n    return english_numeral_regex.search(numeral) is not None\n\ndef convert_numbers_to_words(text):\n    numerals = re.findall(r'\\d+', text)\n    bangla_numerals = []\n    for ix,numeral in enumerate(numerals):\n        if not is_english_numeral(numeral):\n            bangla_numerals.append(numeral)\n    for numeral in bangla_numerals:\n        text = re.sub(numeral, num_convert.number_to_bangla_words(str(numeral)) ,text)\n    return text\n\n\ndef remove_chars(text,remove_english=True):\n    #chars_to_ignore = '[{(-:;\\'\"¿!\\?\\|)}]'\n    chars_to_ignore = '[{(।,/:;.\\'\"¿!*\\?\\-|)}]'\n    text = re.sub(chars_to_ignore, \" \", text)\n    if remove_english:\n        text = re.sub(r\"[a-zA-Z]+\", \"\", text)\n        text = re.sub(r\"[0-9]+\", \"\", text)\n    text = re.sub(\"\\t\",\"\", text)\n    return text\n\ndef remove_extra_whitespace(sentence):\n    words = sentence.split()\n    return \" \".join(words)\n\ndef preprocess_sentence(s,remove_english=True):\n    s = s.lower().strip()\n    s = remove_chars(s, remove_english)\n    #s = replace_numerals_with_words(s)\n    #s = convert_numbers_to_words(s)\n    s = normalize(s)\n    s = unicode_to_ascii(s)\n    s = remove_extra_whitespace(s)\n    return s","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:23:31.02542Z","iopub.execute_input":"2024-12-04T06:23:31.026558Z","iopub.status.idle":"2024-12-04T06:23:31.185219Z","shell.execute_reply.started":"2024-12-04T06:23:31.026489Z","shell.execute_reply":"2024-12-04T06:23:31.184263Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = data.copy()\ndata[\"transcriptions\"] = data[\"transcriptions\"].apply(preprocess_sentence)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:23:31.186525Z","iopub.execute_input":"2024-12-04T06:23:31.187249Z","iopub.status.idle":"2024-12-04T06:23:31.952229Z","shell.execute_reply.started":"2024-12-04T06:23:31.187204Z","shell.execute_reply":"2024-12-04T06:23:31.951412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[\"transcriptions\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:24:11.815149Z","iopub.execute_input":"2024-12-04T06:24:11.815535Z","iopub.status.idle":"2024-12-04T06:24:11.823532Z","shell.execute_reply.started":"2024-12-04T06:24:11.815503Z","shell.execute_reply":"2024-12-04T06:24:11.822428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TASK = \"transcribe\"\nMODEL_NAME = \"openai/whisper-tiny\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:24:12.905543Z","iopub.execute_input":"2024-12-04T06:24:12.906231Z","iopub.status.idle":"2024-12-04T06:24:12.910682Z","shell.execute_reply.started":"2024-12-04T06:24:12.906194Z","shell.execute_reply":"2024-12-04T06:24:12.9097Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:40.745077Z","iopub.execute_input":"2024-12-04T07:37:40.745954Z","iopub.status.idle":"2024-12-04T07:37:41.762312Z","shell.execute_reply.started":"2024-12-04T07:37:40.745914Z","shell.execute_reply":"2024-12-04T07:37:41.761323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ids = tokenizer.encode(\"\")\nids","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:45.894906Z","iopub.execute_input":"2024-12-04T07:37:45.895335Z","iopub.status.idle":"2024-12-04T07:37:45.901792Z","shell.execute_reply.started":"2024-12-04T07:37:45.895285Z","shell.execute_reply":"2024-12-04T07:37:45.900764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tokenizer.decode(ids)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:47.699761Z","iopub.execute_input":"2024-12-04T07:37:47.700842Z","iopub.status.idle":"2024-12-04T07:37:47.709847Z","shell.execute_reply.started":"2024-12-04T07:37:47.7008Z","shell.execute_reply":"2024-12-04T07:37:47.708868Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## datacollator","metadata":{}},{"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#         print(batch)\n# #         print(batch.shape)\n#         for x in batch[\"input_features\"]: \n#             print(x.shape)\n#         print(batch[\"labels\"].shape)\n\n        return batch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:49.428343Z","iopub.execute_input":"2024-12-04T07:37:49.428727Z","iopub.status.idle":"2024-12-04T07:37:49.437077Z","shell.execute_reply.started":"2024-12-04T07:37:49.428694Z","shell.execute_reply":"2024-12-04T07:37:49.436007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_collator = DataCollatorSpeechSeq2SeqWithPadding(processor=processor)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:52.991529Z","iopub.execute_input":"2024-12-04T07:37:52.991921Z","iopub.status.idle":"2024-12-04T07:37:52.99663Z","shell.execute_reply.started":"2024-12-04T07:37:52.991888Z","shell.execute_reply":"2024-12-04T07:37:52.995531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def prepare_dataset(example):\n    audio_path = example[\"file_path\"]\n    \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['transcriptions']}\", 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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:55.922256Z","iopub.execute_input":"2024-12-04T07:37:55.923255Z","iopub.status.idle":"2024-12-04T07:37:55.929182Z","shell.execute_reply.started":"2024-12-04T07:37:55.923216Z","shell.execute_reply":"2024-12-04T07:37:55.928114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = data[data[\"split\"] == \"train\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:57.079071Z","iopub.execute_input":"2024-12-04T07:37:57.0795Z","iopub.status.idle":"2024-12-04T07:37:57.086975Z","shell.execute_reply.started":"2024-12-04T07:37:57.079462Z","shell.execute_reply":"2024-12-04T07:37:57.085853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:37:57.491483Z","iopub.execute_input":"2024-12-04T07:37:57.49188Z","iopub.status.idle":"2024-12-04T07:37:57.505418Z","shell.execute_reply.started":"2024-12-04T07:37:57.491845Z","shell.execute_reply":"2024-12-04T07:37:57.504309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df, eval_df = train_test_split(train_df, test_size=0.01, shuffle=True, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:01.984936Z","iopub.execute_input":"2024-12-04T07:38:01.985768Z","iopub.status.idle":"2024-12-04T07:38:01.992003Z","shell.execute_reply.started":"2024-12-04T07:38:01.98573Z","shell.execute_reply":"2024-12-04T07:38:01.991074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_df), len(eval_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:02.922798Z","iopub.execute_input":"2024-12-04T07:38:02.923718Z","iopub.status.idle":"2024-12-04T07:38:02.929246Z","shell.execute_reply.started":"2024-12-04T07:38:02.923678Z","shell.execute_reply":"2024-12-04T07:38:02.928355Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:04.524823Z","iopub.execute_input":"2024-12-04T07:38:04.525205Z","iopub.status.idle":"2024-12-04T07:38:04.553088Z","shell.execute_reply.started":"2024-12-04T07:38:04.525172Z","shell.execute_reply":"2024-12-04T07:38:04.552431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds_splits = ds_splits.remove_columns([\"split\"])\nprint(ds_splits)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:05.64636Z","iopub.execute_input":"2024-12-04T07:38:05.647178Z","iopub.status.idle":"2024-12-04T07:38:05.655162Z","shell.execute_reply.started":"2024-12-04T07:38:05.64714Z","shell.execute_reply":"2024-12-04T07:38:05.65401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.object = object","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:07.072574Z","iopub.execute_input":"2024-12-04T07:38:07.072956Z","iopub.status.idle":"2024-12-04T07:38:07.077319Z","shell.execute_reply.started":"2024-12-04T07:38:07.072923Z","shell.execute_reply":"2024-12-04T07:38:07.076452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ds_splits = ds_splits.map(prepare_dataset, remove_columns=ds_splits.column_names[\"train\"],)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:38:19.164838Z","iopub.execute_input":"2024-12-04T07:38:19.16523Z","iopub.status.idle":"2024-12-04T07:38:57.391608Z","shell.execute_reply.started":"2024-12-04T07:38:19.165196Z","shell.execute_reply":"2024-12-04T07:38:57.39056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(ds_splits[\"train\"]), len(ds_splits[\"eval\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:00.268981Z","iopub.execute_input":"2024-12-04T07:39:00.269672Z","iopub.status.idle":"2024-12-04T07:39:00.276347Z","shell.execute_reply.started":"2024-12-04T07:39:00.269631Z","shell.execute_reply":"2024-12-04T07:39:00.275197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cer = CharErrorRate()\nwer = WordErrorRate()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:02.019147Z","iopub.execute_input":"2024-12-04T07:39:02.019539Z","iopub.status.idle":"2024-12-04T07:39:02.026238Z","shell.execute_reply.started":"2024-12-04T07:39:02.019506Z","shell.execute_reply":"2024-12-04T07:39:02.025107Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:04.596774Z","iopub.execute_input":"2024-12-04T07:39:04.597547Z","iopub.status.idle":"2024-12-04T07:39:04.603684Z","shell.execute_reply.started":"2024-12-04T07:39:04.597506Z","shell.execute_reply":"2024-12-04T07:39:04.602591Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Load model","metadata":{}},{"cell_type":"code","source":"model = WhisperForConditionalGeneration.from_pretrained(MODEL_NAME, device_map=\"auto\")\nmodel.config.forced_decoder_ids = tokenizer.get_decoder_prompt_ids(language=\"bn\", task=\"transcribe\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:10.256191Z","iopub.execute_input":"2024-12-04T07:39:10.256627Z","iopub.status.idle":"2024-12-04T07:39:11.297945Z","shell.execute_reply.started":"2024-12-04T07:39:10.256592Z","shell.execute_reply":"2024-12-04T07:39:11.297089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#print(model.generation_config)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nprint(f\"Number of GPUs: {torch.cuda.device_count()}\")\nprint(f\"GPU Names: {[torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:25.2916Z","iopub.execute_input":"2024-12-04T07:39:25.292581Z","iopub.status.idle":"2024-12-04T07:39:25.297767Z","shell.execute_reply.started":"2024-12-04T07:39:25.292545Z","shell.execute_reply":"2024-12-04T07:39:25.296658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_id = f\"whisper-tiny-reg-ben/region_based/train_1_no_pretrained_10_epochs_{train_region_name}\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:39:36.616773Z","iopub.execute_input":"2024-12-04T07:39:36.617143Z","iopub.status.idle":"2024-12-04T07:39:36.62189Z","shell.execute_reply.started":"2024-12-04T07:39:36.617113Z","shell.execute_reply":"2024-12-04T07:39:36.620868Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#import multiprocessing\n#multiprocessing.set_start_method(\"forkserver\", force=True)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import logging\n\nlogging.set_verbosity_info()  # Set verbosity to 'info'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T06:25:59.523915Z","iopub.execute_input":"2024-12-04T06:25:59.524337Z","iopub.status.idle":"2024-12-04T06:25:59.529521Z","shell.execute_reply.started":"2024-12-04T06:25:59.524301Z","shell.execute_reply":"2024-12-04T06:25:59.528539Z"}},"outputs":[],"execution_count":null},{"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=32,\n    gradient_accumulation_steps=12,\n    gradient_checkpointing=True,\n    fp16=True,\n    learning_rate=1e-5, \n    weight_decay=0.0025,\n    warmup_steps=200,\n    num_train_epochs=10,\n    logging_strategy=\"steps\",\n    logging_steps=10,\n    evaluation_strategy=\"steps\", # or \"epochs\"\n    predict_with_generate=True,\n#     generation_max_length=448,\n    save_steps=20,\n    eval_steps=20,\n    save_total_limit=3,\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    #dataloader_num_workers=2,  # Optimize workers\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:42:10.169521Z","iopub.execute_input":"2024-12-04T07:42:10.16993Z","iopub.status.idle":"2024-12-04T07:42:10.211166Z","shell.execute_reply.started":"2024-12-04T07:42:10.169892Z","shell.execute_reply":"2024-12-04T07:42:10.210342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model.generation_config.language = \"bn\"\n#model.generation_config.task = \"transcribe\"\nmodel.generation_config.forced_decoder_ids = None\nmodel.config.suppress_tokens = [] # added later","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:42:13.241626Z","iopub.execute_input":"2024-12-04T07:42:13.242471Z","iopub.status.idle":"2024-12-04T07:42:13.246873Z","shell.execute_reply.started":"2024-12-04T07:42:13.242432Z","shell.execute_reply":"2024-12-04T07:42:13.245786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.config.task_to_id = {\n    \"transcribe\": 50359,\n    \"translate\": 50358\n  }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:42:13.675844Z","iopub.execute_input":"2024-12-04T07:42:13.676649Z","iopub.status.idle":"2024-12-04T07:42:13.681103Z","shell.execute_reply.started":"2024-12-04T07:42:13.676611Z","shell.execute_reply":"2024-12-04T07:42:13.680041Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:42:14.981655Z","iopub.execute_input":"2024-12-04T07:42:14.982051Z","iopub.status.idle":"2024-12-04T07:42:14.994825Z","shell.execute_reply.started":"2024-12-04T07:42:14.982014Z","shell.execute_reply":"2024-12-04T07:42:14.993957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:42:16.501038Z","iopub.execute_input":"2024-12-04T07:42:16.501873Z","iopub.status.idle":"2024-12-04T08:04:58.344828Z","shell.execute_reply.started":"2024-12-04T07:42:16.501833Z","shell.execute_reply":"2024-12-04T08:04:58.343207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:18:11.632938Z","iopub.execute_input":"2024-12-04T07:18:11.634242Z","iopub.status.idle":"2024-12-04T07:18:12.547741Z","shell.execute_reply.started":"2024-12-04T07:18:11.634192Z","shell.execute_reply":"2024-12-04T07:18:12.54675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_logs = pd.DataFrame(trainer.state.log_history)\nout_logs.to_csv(\"logs.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:18:50.558558Z","iopub.execute_input":"2024-12-04T07:18:50.55952Z","iopub.status.idle":"2024-12-04T07:18:50.569318Z","shell.execute_reply.started":"2024-12-04T07:18:50.559479Z","shell.execute_reply":"2024-12-04T07:18:50.568333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# inference","metadata":{}},{"cell_type":"code","source":"pipe = pipeline(task=\"automatic-speech-recognition\",\n                model=model_id,\n                tokenizer=model_id, device=0, )\npipe.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=\"bn\", task=\"transcribe\")\n\nprint(\"model loaded!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T08:06:58.84772Z","iopub.execute_input":"2024-12-04T08:06:58.848425Z","iopub.status.idle":"2024-12-04T08:06:58.950808Z","shell.execute_reply.started":"2024-12-04T08:06:58.848383Z","shell.execute_reply":"2024-12-04T08:06:58.949446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir = \"/kaggle/working/dataset_based_on_region/valid/valid_barishal/\"\ntest_paths = [test_dir+file for file in os.listdir(test_dir)]\ntest_paths[:5]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:24:50.110821Z","iopub.execute_input":"2024-12-04T07:24:50.111242Z","iopub.status.idle":"2024-12-04T07:24:50.118479Z","shell.execute_reply.started":"2024-12-04T07:24:50.111207Z","shell.execute_reply":"2024-12-04T07:24:50.117533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:26:57.190396Z","iopub.execute_input":"2024-12-04T07:26:57.190807Z","iopub.status.idle":"2024-12-04T07:26:57.19522Z","shell.execute_reply.started":"2024-12-04T07:26:57.190773Z","shell.execute_reply":"2024-12-04T07:26:57.194167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ENABLE_BEAM =True\ntest_paths =test_paths[:5]\nif ENABLE_BEAM:\n    texts = pipe(test_paths, generate_kwargs={\"max_length\": 260, \"num_beams\": 4})\nelse:\n    texts = pipe(test_paths)\npreds = []\nfor i in texts:\n    preds.append(i['text'])\nlen(preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:27:01.367587Z","iopub.execute_input":"2024-12-04T07:27:01.367973Z","iopub.status.idle":"2024-12-04T07:27:11.201787Z","shell.execute_reply.started":"2024-12-04T07:27:01.367939Z","shell.execute_reply":"2024-12-04T07:27:11.200686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\nsub = pd.DataFrame({\"id\":test_paths,\"sentence\":preds})\nsub","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T07:27:42.39398Z","iopub.execute_input":"2024-12-04T07:27:42.394996Z","iopub.status.idle":"2024-12-04T07:27:42.406909Z","shell.execute_reply.started":"2024-12-04T07:27:42.394952Z","shell.execute_reply":"2024-12-04T07:27:42.405715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}