Notebooks
G
Google Gemini
[Gemma 2]Gradio Chatbot

[Gemma 2]Gradio Chatbot

recurrentgemmagemmapaligemmagemma-cookbookcodegemma
Copyright 2025 Google LLC.
[ ]

Building a Chatbot with Gemma and Gradio

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.

[ ]

Install dependencies

Next, you'll install the required libraries. In this case, we only need gradio for the chat interface and transformers to load the Gemma model from the Hugging Face Hub.

[ ]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.2/57.2 MB 13.3 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 320.4/320.4 kB 23.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 94.8/94.8 kB 7.4 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.3/11.3 MB 34.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 73.2/73.2 kB 3.7 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.3/62.3 kB 4.5 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.1/44.1 kB 3.2 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.0/10.0 MB 63.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 57.1 MB/s eta 0:00:00

Chat with Gemma using Gradio

Initializing Gemma 2 model

Let's create a pipeline that will use the gemma-2-2b-it model to generate text. The transformers library provides an easy way to load the model and tokenizer into memory by simply specifying the model name and some basic parameters.

[ ]
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]
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]

Create a custom chat template

Hugging Face supports chat templates that can define the structure and format for converting conversations into a single tokenizable string, which is the input format expected by the language model. Check the chat templates documentation to learn more about templates and how to create a custom one.

Since Gemma doesn't support system instructions, you will provide system input as user input. This template has been adjusted to be compatible with Gradio's chat interface. To learn more about the format expected by Gemma, check out the Gemma formatting documentation.

[ ]

Handle new messages

Now, you need to define a function that will handle new messages (user inputs).

To make the model context-aware, we need to provide:

  1. System message: The first message of the conversation that guides the behavior of the model during the chat.
  2. Chat history: Messages exchanged between the assistant and the user so far.
  3. New message: A new message sent by the user.

All of this information is converted into a list of messages. Then, apply_chat_template is used to create the actual prompt (a long string with all the special tokens required by Gemma). The prompt is passed to the tokenizer and then to the model to generate the response.

[ ]

Let's Run It!

Now, we will use Gradio's ChatInterface to create an interactive chat interface that will allow you to chat with our Gemma 2 model! In this case, it will create a window inside Google Colab, but if you run it in a standalone file, it will start an HTTP server, and you will be able to access the chat from your browser.

[ ]
Running Gradio in a Colab notebook requires sharing enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).

Colab notebook detected. To show errors in colab notebook, set debug=True in launch()
* Running on public URL: https://29b77c0650271c6a24.gradio.live

This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)
 

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.
  • Gradio Customization: Explore the Gradio documentation to learn about customizing your chat interface, adding new options and features.
  • Share Your Gradio Dashboard: Check out the Sharing your Gradio app page to learn how to safely share your Gradio dashboard with others!