{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "310fea8f", "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": 5, "id": "555c8316", "metadata": {}, "outputs": [], "source": [ "# model = AutoModelForCTC.from_pretrained(\".\").to('cuda')\n", "# processor = Wav2Vec2Processor.from_pretrained(\".\")" ] }, { "cell_type": "code", "execution_count": 20, "id": "24cc91e8", "metadata": {}, "outputs": [], "source": [ "model = AutoModelForCTC.from_pretrained(\"vitouphy/xls-r-300m-km\").to('cuda')\n", "processor = Wav2Vec2Processor.from_pretrained(\"vitouphy/xls-r-300m-km\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "69d79b00", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using custom data configuration default-fbad308ab5a03eb2\n", "Reusing dataset csv (/workspace/.cache/huggingface/datasets/csv/default-fbad308ab5a03eb2/0.0.0/6b9057d9e23d9d8a2f05b985917a0da84d70c5dae3d22ddd8a3f22fb01c69d9e)\n" ] } ], "source": [ "common_voice_test = load_dataset('csv', data_files='km_kh_male/line_index_test.csv', split = 'train')" ] }, { "cell_type": "code", "execution_count": 9, "id": "9c9a59b3", "metadata": {}, "outputs": [], "source": [ "common_voice_test = (common_voice_test\n", " .remove_columns([\"Unnamed: 0\", \"drop\"])\n", " .rename_column('text', 'sentence'))" ] }, { "cell_type": "code", "execution_count": 10, "id": "868afb48", "metadata": {}, "outputs": [], "source": [ "common_voice_test = common_voice_test.cast_column(\"path\", Audio(sampling_rate=16_000)).rename_column('path', 'audio')" ] }, { "cell_type": "code", "execution_count": 11, "id": "f93e7f2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'audio': {'path': '/workspace/xls-r-300m-km/km_kh_male/wavs/khm_1443_3799144408.wav',\n", " 'array': array([-1.0600963e-06, 1.2359066e-06, -1.4001107e-06, ...,\n", " -3.1423504e-05, 4.4914182e-06, 0.0000000e+00], dtype=float32),\n", " 'sampling_rate': 16000},\n", " 'sentence': 'ស៊ី ដាច់ ម៉ូតូ នៅ ពេល ដែល ប្រើ ឱ្យ ឌុប សម្ភារៈ គ្រឿង សង្ហារឹម យក ទៅ ឱ្យ ម៉ូយ នៅ ម្ដុំ វត្ដ សំរោងអណ្ដែត'}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_voice_test[0]" ] }, { "cell_type": "code", "execution_count": 12, "id": "c97bf6c8", "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": 13, "id": "8e6b77e3", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a0dd47d98a4e448c9f786ce464348946", "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": "53b5be56", "metadata": {}, "outputs": [], "source": [ "i = 25" ] }, { "cell_type": "code", "execution_count": 22, "id": "15dda9d3", "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.to(\"cuda\")).logits\n", "pred_ids = torch.argmax(logits, dim=-1)[0]" ] }, { "cell_type": "code", "execution_count": 23, "id": "bc40d9dc", "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": "f755f572", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "16aa56dc", "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 }