Notebooks
G
Google Gemini
[Gemma 2]Synthetic Data Generation

[Gemma 2]Synthetic Data Generation

recurrentgemmagemmapaligemmagemma-cookbookcodegemma
Copyright 2025 Google LLC.
[ ]

Synthetic data generation with Gemma 2

Setup

Runtime Environment

  1. Click Open in Colab.
  2. In the menu, go to Runtime > Change runtime type.
  3. Under Hardware accelerator, select T4 GPU.

Hugging Face Hub Access Token

Before diving into the tutorial, let's set up Gemma:

  1. Create a Hugging Face Account: If you don't have one, you can sign up for a free account here.
  2. Access the Gemma Model: Visit the Gemma model page and accept the usage terms.
  3. Generate a Hugging Face Token: Go to your Hugging Face settings page and generate a new access token (preferably with write permissions). You'll need this token later in the tutorial.

Once you've completed these steps, you're ready to move on to the next section, where you'll set up environment variables in your Colab environment.

Configure Your Credentials

To access private models and datasets, you need to log in to the Hugging Face (HF) ecosystem.

If you're using Colab, you can securely store your Hugging Face token (HF_TOKEN) using the Colab Secrets Manager:

  1. Open your Google Colab notebook and click on the πŸ”‘ Secrets tab in the left panel. The Secrets tab is found on the left panel.
  2. Add Hugging Face Token:
  • Create a new secret with the name HF_TOKEN.
  • Copy and paste your token key into the Value input box for HF_TOKEN.
  • Toggle the button on the left to allow notebook access to the secret

This code retrieves your secrets and sets them as environment variables for use later in the tutorial.

[ ]

Synthetic data generation with Gemma 2

What's distilabel?

Distilabel is a framework designed for generating synthetic data and AI feedback, tailored for engineers working on scalable, reliable pipelines based on verified research. It supports a variety of use cases, including traditional NLP tasks like classification and extraction, as well as generative scenarios like instruction following and dialogue generation. With its programmatic approach, Distilabel accelerates AI development by enabling the rapid creation of high-quality, diverse datasets.

Check out distilabel's home page to learn more!

Install dependencies

We only need distilabel, which provides all the required modules.

[ ]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 442.2/442.2 kB 10.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 480.6/480.6 kB 26.6 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.8/134.8 kB 7.9 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 50.1/50.1 kB 2.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 116.3/116.3 kB 6.7 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 179.3/179.3 kB 8.3 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 7.2 MB/s eta 0:00:00
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
gcsfs 2024.10.0 requires fsspec==2024.10.0, but you have fsspec 2024.9.0 which is incompatible.

Initializing Gemma 2 model

In this example, we don't need to initialize Gemma ourselves; this is handled by distilabel within the pipeline. The good news is that distilabel is fully compatible with Hugging Face Model Hub, so it will retrieve the model directly! There's no need to set up the model or tokenizer manually.

[ ]

Basic example

In this example, you'll see how to use Gemma on a very small dataset used by distilabel for testing. It contains only 10 rows of data, as shown below. The dataset can be explored here.

[ ]

The following defines the execution flow within the pipeline. In our case, it's quite simple:

  1. First, we need to load the dataset from the Hugging Face repository.
  2. Then, we define the task and specify which LLM will be used to generate new data.
  3. Finally, you need to define how the steps are connected together."
[ ]

Let's run our simple pipeline:

[ ]
README.md:   0%|          | 0.00/656 [00:00<?, ?B/s]
The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.
0it [00:00, ?it/s]
config.json:   0%|          | 0.00/838 [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/24.2k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.99G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/241M [00:00<?, ?B/s]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/187 [00:00<?, ?B/s]
tokenizer_config.json:   0%|          | 0.00/47.0k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.24M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/636 [00:00<?, ?B/s]
Device set to use cuda:0
The 'batch_size' attribute of HybridCache is deprecated and will be removed in v4.49. Use the more precisely named 'self.max_batch_size' attribute instead.
Generating train split: 0 examples [00:00, ? examples/s]
[ ]

As you can see, a new column, generation, has been added to the dataset. This column contains the newly generated data.

Create your own dataset and pipeline

It’s always better to apply the tool to real-life examples. In the following cells, we will explore how to generate facts about various items. These items will be provided as a list.

[ ]
[{'instruction': 'Generate 3 fun facts about dogs'},
, {'instruction': 'Generate 3 fun facts about cats'},
, {'instruction': 'Generate 3 fun facts about birds'}]

Here, we will define our pipeline. It won’t be much different from the previous one, but we will use our dictionary data as the input and won’t use any input parameters when calling the run(...) method, allowing for more output tokens.

[ ]
Device set to use cuda:0
The 'batch_size' attribute of HybridCache is deprecated and will be removed in v4.49. Use the more precisely named 'self.max_batch_size' attribute instead.
Generating train split: 0 examples [00:00, ? examples/s]

Let's see the results:

[ ]

What's Next?

That's it! If you're wondering how to make your chatbot even better, check out the following resources:

  • Explore the Gemma family models: Visit Gemma Open Models to learn about the latest updates regarding the Gemma family models, new capabilities, versions, and more.
  • Check out Distilabel's Components Gallery: The components gallery showcases various modules that can be used within a pipeline to achieve the desired results.