{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# JAV to MS HuggingFace" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [Malaya/example/jav-ms-translation-huggingface](https://github.com/huseinzol05/Malaya/tree/master/example/jav-ms-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 4.1 s, sys: 3.32 s, total: 7.42 s\n", "Wall time: 3.35 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.jav_ms:tested on FLORES200 JAV-MS (jav_Latn-zsm_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.79762858.2/31.1/18.1/10.8 (BP = 0.977 ratio = 0.977 ...50.65512
mesolitica/finetune-translation-austronesian-t5-small-standard-bahasa-cased24225.24437757.7/31.9/19.0/11.6 (BP = 1.000 ratio = 1.022 ...52.58512
mesolitica/finetune-translation-austronesian-t5-base-standard-bahasa-cased89225.77289758.9/32.6/19.6/12.1 (BP = 0.992 ratio = 0.992 ...52.21512
\n", "
" ], "text/plain": [ " Size (MB) BLEU \\\n", "mesolitica/finetune-translation-austronesian-t5... 139 23.797628 \n", "mesolitica/finetune-translation-austronesian-t5... 242 25.244377 \n", "mesolitica/finetune-translation-austronesian-t5... 892 25.772897 \n", "\n", " SacreBLEU Verbose \\\n", "mesolitica/finetune-translation-austronesian-t5... 58.2/31.1/18.1/10.8 (BP = 0.977 ratio = 0.977 ... \n", "mesolitica/finetune-translation-austronesian-t5... 57.7/31.9/19.0/11.6 (BP = 1.000 ratio = 1.022 ... \n", "mesolitica/finetune-translation-austronesian-t5... 58.9/32.6/19.6/12.1 (BP = 0.992 ratio = 0.992 ... \n", "\n", " SacreBLEU-chrF++-FLORES200 \\\n", "mesolitica/finetune-translation-austronesian-t5... 50.65 \n", "mesolitica/finetune-translation-austronesian-t5... 52.58 \n", "mesolitica/finetune-translation-austronesian-t5... 52.21 \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.jav_ms.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 JAV-to-MS.\n", "\n", " Parameters\n", " ----------\n", " model: str, optional (default='mesolitica/finetune-translation-t5-small-standard-bahasa-cased')\n", " Check available models at `malaya.translation.jav_ms.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.jav_ms.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 = 'Aku ora seneng lele lan pitik goreng'" ] }, { "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": [ "['Saya tidak suka lele dan ayam goreng']\n", "CPU times: user 1.08 s, sys: 0 ns, total: 1.08 s\n", "Wall time: 95.1 ms\n" ] } ], "source": [ "%%time\n", "\n", "pprint(transformer_huggingface.generate([text],\n", " max_length = 1000))" ] } ], "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 }