Hands-on Gemma Open Models (Generative AI): A Practical Approach
Welcome to the world of Gemma Open Models, a family of lightweight and generative AI models that empower you to create innovative applications. In this hands-on tutorial, I’ll guide you through a practical step-by-step process to harness the power of Gemma and explore its capabilities.
We will create: An Email Generator, Translator, Travel Assistant, and a Code Assistant.
Step 1: Request Gemma Access
Gain access to Gemma models through Kaggle, Hugging Face, or Vertex AI. For this tutorial, we’ll use Kaggle. Visit https://www.kaggle.com/models/google/gemma and accept the terms and conditions.
Step 2: Generate Kaggle Credentials
To authenticate with Gemma from our scripts, generate a Kaggle API token. Head to https://www.kaggle.com/settings and navigate to the API section. Create a new token and save the generated JSON file containing your API credentials.
Step 3: Create a Google Colab Notebook
Open a new Colab notebook at https://colab.research.google.com/ and change the runtime to T4 GPU.
Step 4: Set Up the Notebook
Install the necessary Keras libraries using the following commands:
# Install the following Keras required libraries
!pip install -q --upgrade keras-nlp
!pip install -q --upgrade keras>=3
Import required libraries:
import os # For configuring Kaggle credentials
from google.colab import userdata # (Optional) To retrieve Kaggle credentials from Colab secrets
import keras
import keras_nlp
from IPython.display import Markdown # To display model results in a friendly format
Set Kaggle credentials within the script:
- Copy credentials from step2
- Or use Secrets from Colab in order to store the Kaggle credentials.
# Retrieve Kaggle credentials from the environment
# You can also directly copy the Kaggle credentials from Step 2
os.environ["KAGGLE_USERNAME"] = userdata.get("KAGGLE_USERNAME")
os.environ["KAGGLE_KEY"] = userdata.get("KAGGLE_KEY")
# Use Jax from Keras
os.environ["KERAS_BACKEND"] = "jax" # "jax" Or "tensorflow" or "torch"
os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "0.9"
Step 5: Select the Gemma Model
Gemma offers models in two sizes:
- 2 Billion Parameters: Ideal for resource-constrained devices like mobile phones or laptops.
- 7 Billion Parameters: Designed for desktops and servers.
Each model comes in two versions:
- Pretrained: Requires fine-tuning with developer-specific data.
- Instruction Tuned: Ready-to-use version for interacting with prompts.
Available Gemma models variants:
- Gemma: Generic model.
- CodeGemma: Optimized for programming language contexts and code generation.
- PaliGemma: Geared towards generating visualizations.
- RecurrentGemma: Leverages recurrent neural network techniques for improved memory usage.
For testing generic prompts, we’ll use: gemma_1.1_instruct_2b_en
# Define and instantiate the Gemma model
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset("gemma_1.1_instruct_2b_en")
Step 6: Interact with Gemma and Explore Its Magic
To interact with Gemma, simply use the following instruction:
response = gemma_lm.generate("Prompt text", max_length=128)
print(response)
Unleash Your Creativity with Gemma Applications:
Email Generator:
Since we are going to try different prompts, the template we will use will be the following: for example, to create an email generator we will use:
# @title Email creator { form-width: "40%" }
# Specify the task to perform
email_purpose = "Request PTO for next Friday. May 31" # @param {type:"string"}
# Make the prompt dynamic to generate emails for different purposes without code changes
prompt_parts = "Create an email with the following purpose: " + email_purpose
# Debug
# Print the complete prompt to be used
print("Prompt to use: " + prompt_parts)
# Call gemma model
response = gemma_lm.generate(prompt_parts, max_length=128)
# Print Response
Markdown(response)
The previous code will generate a form in Colab to make the email_purpose part dynamic and receive different values within an input text box.
Phrase Translator
Now we will try the following prompt in spanish.
Prompt to use: Traduce la siguiente frase a francés: [phrase]
Travel Assistant
Prompt to use: List the 3 main tourist attractions of: [place]
Code Generator
Prompt to use: Generate code in Python with the following purpose: Identify Palindrome words.
In the scenario of generating code, the best option is to use the code-gemma variant. You will have better results :)
You can find the code used in the following repository: