{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MS to IND HuggingFace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [Malaya/example/ms-ind-translation-huggingface](https://github.com/huseinzol05/Malaya/tree/master/example/ms-ind-translation-huggingface).\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This module trained on standard language and augmented local language structures, proceed with caution.\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ['CUDA_VISIBLE_DEVICES'] = ''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.81 s, sys: 3.51 s, total: 7.32 s\n", "Wall time: 3.19 s\n" ] } ], "source": [ "%%time\n", "\n", "import malaya\n", "import logging\n", "\n", "logging.basicConfig(level=logging.INFO)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List available HuggingFace models" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:malaya.translation.ms_ind:tested on FLORES200 MS-IND (zsm_Latn-ind_Latn) pair `dev` set, https://github.com/facebookresearch/flores/tree/main/flores200\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Size (MB)BLEUSacreBLEU VerboseSacreBLEU-chrF++-FLORES200Suggested length
mesolitica/finetune-translation-austronesian-t5-tiny-standard-bahasa-cased13933.88207767.7/42.2/28.0/18.9 (BP = 0.966 ratio = 0.966 ...59.46512
mesolitica/finetune-translation-austronesian-t5-small-standard-bahasa-cased24235.95448166.3/42.6/29.1/20.3 (BP = 1.000 ratio = 1.014 ...61.02512
mesolitica/finetune-translation-austronesian-t5-base-standard-bahasa-cased89237.6206870.0/45.8/31.7/22.5 (BP = 0.967 ratio = 0.968 ...62.1512
\n", "
" ], "text/plain": [ " Size (MB) BLEU \\\n", "mesolitica/finetune-translation-austronesian-t5... 139 33.882077 \n", "mesolitica/finetune-translation-austronesian-t5... 242 35.954481 \n", "mesolitica/finetune-translation-austronesian-t5... 892 37.62068 \n", "\n", " SacreBLEU Verbose \\\n", "mesolitica/finetune-translation-austronesian-t5... 67.7/42.2/28.0/18.9 (BP = 0.966 ratio = 0.966 ... \n", "mesolitica/finetune-translation-austronesian-t5... 66.3/42.6/29.1/20.3 (BP = 1.000 ratio = 1.014 ... \n", "mesolitica/finetune-translation-austronesian-t5... 70.0/45.8/31.7/22.5 (BP = 0.967 ratio = 0.968 ... \n", "\n", " SacreBLEU-chrF++-FLORES200 \\\n", "mesolitica/finetune-translation-austronesian-t5... 59.46 \n", "mesolitica/finetune-translation-austronesian-t5... 61.02 \n", "mesolitica/finetune-translation-austronesian-t5... 62.1 \n", "\n", " Suggested length \n", "mesolitica/finetune-translation-austronesian-t5... 512 \n", "mesolitica/finetune-translation-austronesian-t5... 512 \n", "mesolitica/finetune-translation-austronesian-t5... 512 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "malaya.translation.ms_ind.available_huggingface()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load Transformer models\n", "\n", "```python\n", "def huggingface(\n", " model: str = 'mesolitica/finetune-translation-austronesian-t5-small-standard-bahasa-cased',\n", " force_check: bool = True,\n", " **kwargs,\n", "):\n", " \"\"\"\n", " Load HuggingFace model to translate MS-to-IND.\n", "\n", " Parameters\n", " ----------\n", " model: str, optional (default='mesolitica/finetune-translation-t5-small-standard-bahasa-cased')\n", " Check available models at `malaya.translation.ms_ind.available_huggingface()`.\n", "\n", " Returns\n", " -------\n", " result: malaya.torch_model.huggingface.Generator\n", " \"\"\"\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "transformer_huggingface = malaya.translation.ms_ind.huggingface()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Translate\n", "\n", "```python\n", "def generate(self, strings: List[str], **kwargs):\n", " \"\"\"\n", " Generate texts from the input.\n", "\n", " Parameters\n", " ----------\n", " strings : List[str]\n", " **kwargs: vector arguments pass to huggingface `generate` method.\n", " Read more at https://huggingface.co/docs/transformers/main_classes/text_generation\n", "\n", " Returns\n", " -------\n", " result: List[str]\n", " \"\"\"\n", "```\n", "\n", "**For better results, always split by end of sentences**." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from pprint import pprint" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "news = \"SHAH ALAM - Kerajaan harus bersikap 'kejam' terhadap desakan segelintir rakyat berkenaan isu pengeluaran Kumpulan Wang Simpanan Pekerja (KWSP) demi kesejahteraan rakyat di masa akan datang.\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"SHAH ALAM - Pemerintah harus 'kejam' dengan desakan segelintir masyarakat \"\n", " 'terkait isu penarikan Dana Penyediaan Pegawai (EPF) untuk kesejahteraan '\n", " 'masyarakat di masa depan.']\n", "CPU times: user 3.53 s, sys: 0 ns, total: 3.53 s\n", "Wall time: 298 ms\n" ] } ], "source": [ "%%time\n", "\n", "pprint(transformer_huggingface.generate([news],\n", " max_length = 1000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compare with Google translate using googletrans\n", "\n", "Install it by,\n", "\n", "```bash\n", "pip3 install googletrans==4.0.0rc1\n", "```" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from googletrans import Translator\n", "\n", "translator = Translator()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "strings = [news]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shah Alam - Pemerintah harus 'kejam' terhadap desakan beberapa orang tentang masalah produksi Dana Penyedia Karyawan (EPF) untuk masa depan rakyat.\n" ] } ], "source": [ "for t in strings:\n", " r = translator.translate(t, src='ms', dest = 'id')\n", " print(r.text)" ] } ], "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.10" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }