{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MS to JAV HuggingFace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [Malaya/example/ms-jav-translation-huggingface](https://github.com/huseinzol05/Malaya/tree/master/example/ms-jav-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.94 s, sys: 3.32 s, total: 7.26 s\n", "Wall time: 3.1 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_jav:tested on FLORES200 MS-JAV (zsm_Latn-jav_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-cased13923.79649959.2/31.6/18.2/10.8 (BP = 0.966 ratio = 0.967 ...51.21512
mesolitica/finetune-translation-austronesian-t5-small-standard-bahasa-cased24224.59998958.3/31.6/18.5/11.2 (BP = 0.990 ratio = 0.990 ...51.65512
mesolitica/finetune-translation-austronesian-t5-base-standard-bahasa-cased89224.64236360.1/32.7/19.1/11.5 (BP = 0.961 ratio = 0.961 ...51.91512
\n", "
" ], "text/plain": [ " Size (MB) BLEU \\\n", "mesolitica/finetune-translation-austronesian-t5... 139 23.796499 \n", "mesolitica/finetune-translation-austronesian-t5... 242 24.599989 \n", "mesolitica/finetune-translation-austronesian-t5... 892 24.642363 \n", "\n", " SacreBLEU Verbose \\\n", "mesolitica/finetune-translation-austronesian-t5... 59.2/31.6/18.2/10.8 (BP = 0.966 ratio = 0.967 ... \n", "mesolitica/finetune-translation-austronesian-t5... 58.3/31.6/18.5/11.2 (BP = 0.990 ratio = 0.990 ... \n", "mesolitica/finetune-translation-austronesian-t5... 60.1/32.7/19.1/11.5 (BP = 0.961 ratio = 0.961 ... \n", "\n", " SacreBLEU-chrF++-FLORES200 \\\n", "mesolitica/finetune-translation-austronesian-t5... 51.21 \n", "mesolitica/finetune-translation-austronesian-t5... 51.65 \n", "mesolitica/finetune-translation-austronesian-t5... 51.91 \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_jav.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-JAV.\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_jav.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_jav.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": 6, "metadata": {}, "outputs": [], "source": [ "text = 'saya tak suka ayam goreng dan itik'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "You're using a T5TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['Aku ora seneng karo pitik goreng lan bebek']\n", "CPU times: user 2.29 s, sys: 0 ns, total: 2.29 s\n", "Wall time: 198 ms\n" ] } ], "source": [ "%%time\n", "\n", "pprint(transformer_huggingface.generate([text],\n", " max_length = 1000))" ] }, { "cell_type": "code", "execution_count": null, "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.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 }