diff --git a/Gemma/Gemma_2_for_Japan_using_Transformers_and_PyTorch.ipynb b/Gemma/Gemma_2_for_Japan_using_Transformers_and_PyTorch.ipynb new file mode 100644 index 0000000..5a04412 --- /dev/null +++ b/Gemma/Gemma_2_for_Japan_using_Transformers_and_PyTorch.ipynb @@ -0,0 +1,1991 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "b-FX_tt-s8sU" + }, + "source": [ + "##### Copyright 2024 Google LLC." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "JA485iBMs7ub" + }, + "outputs": [], + "source": [ + "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "igbYjdwhvioG" + }, + "source": [ + "This notebook provides a practical guide to working with Gemma 2 for Japan, the latest variant of Google's open language models.\n", + "\n", + "The model itself is available on both Kaggle and Hugging Face.\n", + "\n", + "As the model is currently only available in a 2B size, no special hardware is required.\n", + "\n", + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "y5ZzHdAb9sNW" + }, + "source": [ + "### Downloading prerequisites\n", + "The **KaggleHub** library will be used to retrieve the model from Kaggle, and the **Transformers** framework from Hugging Face will be used with **PyTorch** for inference.\n", + "\n", + "Note: the `%%capture` magic keyword suppresses output of a given cell." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "YYPGnh709SJR" + }, + "outputs": [], + "source": [ + "%%capture\n", + "!pip install kagglehub --upgrade\n", + "!pip install transformers --upgrade\n", + "!pip install torch --upgrade" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mEjYLz9l9eeg" + }, + "source": [ + "### Authenticating with Kaggle\n", + "\n", + "Next, Kaggle will need to be authenticated with.\n", + "\n", + "Note: the call to `login()` can be skipped if running in a Kaggle notebook." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "8aucYnW5_f1S" + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b2f1c08705234b9fad50a04c03229ade", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HTML(value='
str:\n", + " return f\"user {instruction}model\"\n", + "\n", + "def unwrap_gemma_response(query: str, response: str) -> str:\n", + " end_sequence = ''\n", + "\n", + " start_idx = 0\n", + " query_idx = response.find(query)\n", + "\n", + " if query_idx >= 0:\n", + " start_idx = query_idx + len(query)\n", + "\n", + " trim = response[start_idx:]\n", + "\n", + " end_idx = len(trim) - 1\n", + " endseq_idx = trim.find(end_sequence)\n", + "\n", + " if endseq_idx >= 0:\n", + " end_idx = endseq_idx\n", + "\n", + " return trim[:end_idx].strip()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a7rqMiZ4vlnm" + }, + "source": [ + "First, you will send a prompt in Japanese to the model, asking it to write us a poem in Japanese." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "s2CFT7fOFaMy" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "The 'max_batch_size' argument of HybridCache is deprecated and will be removed in v4.46. Use the more precisely named 'batch_size' argument instead.\n", + "Starting from v4.46, the `logits` model output will have the same type as the model (except at train time, where it will always be FP32)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "機械学習の波、広がる未来の光、\n", + "データの海、複雑な知識を導く。\n", + "複雑なパターン、隠された法則を見つける、\n", + "予測と改善、未来を形作る。\n", + "\n", + "ニューラルネットワーク、複雑な脳の像、\n", + "学習と進化、無限の可能性を秘める。\n", + "教師あり学習、教師なし学習、\n", + "データの力、人間の知恵を融合する。\n", + "\n", + "機械学習の力、無限の可能性を秘める、\n", + "複雑な問題を解き明かす、未来を拓く。\n", + "AIの進化、人類の未来を左右する、\n", + "新たな時代を迎えよう。\n" + ] + } + ], + "source": [ + "# Our prompt will be, \"Write me a poem about machine learning.\" in Japanese.\n", + "input_text = \"マシーンラーニングについての詩を書いてください。\"\n", + "\n", + "input_formatted = format_gemma_instruction(input_text)\n", + "\n", + "input_ids = tokenizer(input_formatted, return_tensors=\"pt\").to(device)\n", + "\n", + "outputs = model.generate(**input_ids, max_new_tokens=1024)\n", + "formatted = unwrap_gemma_response(input_formatted, tokenizer.decode(outputs[0]))\n", + "\n", + "print(formatted)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8H1FS_A7vuDI" + }, + "source": [ + "Next, you can feed that poem back into the model, asking it to translate it into English." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "DDK4sYMaBSb8" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here's the translation of the Japanese poem:\n", + "\n", + "**The wave of machine learning, spreading the light of the future,\n", + "A vast ocean of data, guiding complex knowledge.\n", + "Finding complex patterns, uncovering hidden laws,\n", + "Prediction and improvement, shaping the future.\n", + "\n", + "A neural network, a complex image of the brain,\n", + "Learning and evolution, holding infinite possibilities.\n", + "Supervised and unsupervised learning,\n", + "The power of data, fusing human wisdom.\n", + "\n", + "The power of machine learning, holding infinite possibilities,\n", + "Unraveling complex problems, opening up the future.\n", + "The evolution of AI, shaping the future of humanity,\n", + "We are ushering in a new era.**\n", + "\n", + "\n", + "\n", + "**Explanation of the Poem's Themes:**\n", + "\n", + "* **Machine Learning's Impact:** The poem highlights the growing influence of machine learning, emphasizing its ability to shape the future.\n", + "* **Data as the Foundation:** The poem emphasizes the role of data as the foundation for machine learning, suggesting that it is the key to unlocking its potential.\n", + "* **Complexity and Pattern Recognition:** The poem focuses on the ability of machine learning to identify complex patterns and uncover hidden laws within data.\n", + "* **Prediction and Improvement:** The poem highlights the role of prediction and improvement in shaping the future, suggesting that machine learning can be used to make better decisions and create a better world.\n", + "* **AI's Role in the Future:** The poem suggests that AI, powered by machine learning, will play a crucial role in shaping the future of humanity.\n", + "\n", + "\n", + "\n", + "Let me know if you'd like any further clarification on the poem's meaning or symbolism.\n" + ] + } + ], + "source": [ + "translation_input_text = \"Translate the following poem from Japanese to English. \\n\\n\" + formatted\n", + "translation_input_formatted = format_gemma_instruction(translation_input_text)\n", + "\n", + "translation_input_ids = tokenizer(translation_input_formatted, return_tensors=\"pt\").to(device)\n", + "\n", + "translation_output = model.generate(**translation_input_ids, max_new_tokens=2048)\n", + "translation_formatted = unwrap_gemma_response(translation_input_formatted, tokenizer.decode(translation_output[0]))\n", + "\n", + "print(translation_formatted)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r2uMH5c4v5e3" + }, + "source": [ + "And we're done!" + ] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "name": "Gemma_2_for_Japan_using_Transformers_and_PyTorch.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}