{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1b285579", "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForCTC, Wav2Vec2Processor\n", "from datasets import load_dataset, load_metric, Audio\n", "import numpy as np\n", "import torch" ] }, { "cell_type": "code", "execution_count": 20, "id": "1f3354ce", "metadata": {}, "outputs": [], "source": [ "model = AutoModelForCTC.from_pretrained(\".\")\n", "processor = Wav2Vec2Processor.from_pretrained(\".\")\n", "\n", "# model = AutoModelForCTC.from_pretrained(\"vitouphy/wav2vec2-xls-r-1b-km\")\n", "# processor = Wav2Vec2Processor.from_pretrained(\"vitouphy/wav2vec2-xls-r-1b-km\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "bdcb886d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using custom data configuration default-36119ec2a15afb82\n", "Reusing dataset csv (/workspace/.cache/huggingface/datasets/csv/default-36119ec2a15afb82/0.0.0/6b9057d9e23d9d8a2f05b985917a0da84d70c5dae3d22ddd8a3f22fb01c69d9e)\n" ] } ], "source": [ "common_voice_test = (load_dataset('csv', data_files='/workspace/xls-r-300m-km/km_kh_male/line_index_test.csv', split = 'train')\n", " .remove_columns([\"Unnamed: 0\", \"drop\"])\n", " .rename_column('text', 'sentence')\n", " .cast_column(\"path\", Audio(sampling_rate=16_000)).rename_column('path', 'audio'))" ] }, { "cell_type": "code", "execution_count": 7, "id": "e6631b64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'audio': {'path': '/workspace/xls-r-300m-km/km_kh_male/wavs/khm_3154_2555595821.wav',\n", " 'array': array([ 0.00014737, 0.00016698, 0.00013704, ..., -0.00011244,\n", " -0.0001059 , -0.00011476], dtype=float32),\n", " 'sampling_rate': 16000},\n", " 'sentence': 'ការ ធ្វើ អាជីវកម្ម រ៉ែ ដំបូង នៅ កម្ពុជា'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_voice_test[0]" ] }, { "cell_type": "code", "execution_count": 8, "id": "6357f917", "metadata": {}, "outputs": [], "source": [ "def prepare_dataset(batch):\n", " audio = batch[\"audio\"]\n", " \n", " # batched output is \"un-batched\"\n", " batch[\"input_values\"] = processor(np.array(audio[\"array\"]), sampling_rate=audio[\"sampling_rate\"]).input_values[0]\n", " batch[\"input_length\"] = len(batch[\"input_values\"])\n", " \n", " with processor.as_target_processor():\n", " batch[\"labels\"] = processor(batch[\"sentence\"]).input_ids\n", " return batch" ] }, { "cell_type": "code", "execution_count": 9, "id": "86b15f84", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d422eded8090415fa2af118843279369", "version_major": 2, "version_minor": 0 }, "text/plain": [ "0ex [00:00, ?ex/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "common_voice_test = common_voice_test.map(prepare_dataset, remove_columns=common_voice_test.column_names)" ] }, { "cell_type": "code", "execution_count": 21, "id": "d29cda20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "processor.tokenizer.pad_token_id" ] }, { "cell_type": "code", "execution_count": 22, "id": "2b38bde0", "metadata": {}, "outputs": [], "source": [ "i = 25" ] }, { "cell_type": "code", "execution_count": 23, "id": "7e22806f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "It is strongly recommended to pass the ``sampling_rate`` argument to this function. Failing to do so can result in silent errors that might be hard to debug.\n" ] } ], "source": [ "input_dict = processor(common_voice_test[i][\"input_values\"], return_tensors=\"pt\", padding=True)\n", "logits = model(input_dict.input_values).logits\n", "pred_ids = torch.argmax(logits, dim=-1)[0]" ] }, { "cell_type": "code", "execution_count": 24, "id": "c804d1e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Prediction:\n", "កញ្ញាទេ បូព្រឹក សម្ដែង នៅ តន្ត្រី ស្រាប៊ៀរ កម្ពុជា\n", "\n", "Reference:\n", "កញ្ញា ទេព បូព្រឹក្ស សម្ដែង នៅ តន្ត្រី ស្រាបៀរ កម្ពុជា\n" ] } ], "source": [ "print(\"Prediction:\")\n", "pred_ids = pred_ids[pred_ids != processor.tokenizer.pad_token_id]\n", "print(processor.decode(pred_ids))\n", "\n", "print(\"\\nReference:\")\n", "print(processor.decode(common_voice_test['labels'][i]))\n", "# print(common_voice_test_transcription[0][\"sentence\"].lower())" ] }, { "cell_type": "code", "execution_count": null, "id": "7c576143", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5745b14f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }