[Gemma 3n]Multimodal Understanding With HF
Copyright 2025 Google LLC.
Multimodal understanding with Hugging Face Transformers
Gemma 3n is a generative AI model optimized for use in everyday devices, such as phones, laptops, and tablets.. This tutorial shows you how to get started running Gemma 3n using Hugging Face Transformers using Image + Audio input to generate text content. The Transformers Python library provides a API for accessing pre-trained generative AI models, including Gemma. For more information, see the Transformers documentation.
Setup
Before starting this tutorial, complete the following steps:
- Get access to Gemma by logging into Hugging Face and selecting Acknowledge license for a Gemma model.
- Select a Colab runtime with sufficient resources to run the Gemma model size you want to run. Learn more.
- Generate a Hugging Face Access Token and use it to login from Colab.
This notebook will run on an NVIDIA T4 GPU using Gemma 3n E2B.
But if you want to use Gemma 3n E4B, select L4 or A100.
Install Python packages
Install the Hugging Face libraries required for running the Gemma model and making requests.
Define formatting helper functions
Create a chat helper to manage and display the conversations.
Load Model
processor_config.json: 0%| | 0.00/98.0 [00:00<?, ?B/s]
chat_template.jinja: 0%| | 0.00/1.63k [00:00<?, ?B/s]
preprocessor_config.json: 0%| | 0.00/1.12k [00:00<?, ?B/s]
tokenizer_config.json: 0%| | 0.00/1.20M [00:00<?, ?B/s]
tokenizer.model: 0%| | 0.00/4.70M [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/33.4M [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/769 [00:00<?, ?B/s]
config.json: 0%| | 0.00/3.96k [00:00<?, ?B/s]
model.safetensors.index.json: 0%| | 0.00/159k [00:00<?, ?B/s]
Fetching 3 files: 0%| | 0/3 [00:00<?, ?it/s]
model-00001-of-00003.safetensors: 0%| | 0.00/3.08G [00:00<?, ?B/s]
model-00003-of-00003.safetensors: 0%| | 0.00/2.82G [00:00<?, ?B/s]
model-00002-of-00003.safetensors: 0%| | 0.00/4.98G [00:00<?, ?B/s]
Loading checkpoint shards: 0%| | 0/3 [00:00<?, ?it/s]
generation_config.json: 0%| | 0.00/215 [00:00<?, ?B/s]
Device: cuda:0 DType: torch.bfloat16
Image-in: Captioning
🙋♂️
describe the image in detail
🤖
The image is a top-down, slightly tilted illustration depicting a bright and cozy kitchen scene. The perspective is from above, looking down onto a wooden countertop laden with various kitchen utensils and cookware.The countertop is a warm, light brown wood with a slightly rustic texture. It's positioned slightly off-center, angled towards the right side of the frame. A light green wooden base supports the countertop, and it features a simple, round handle on the right.
On the countertop, a colorful assortment of items is arranged. There are several pots and pans in different colors: a light blue pot, a reddish-brown pot, a yellow pot, and a light pink pot. Alongside these, there's a collection of wooden spoons, whisks, spatulas, and other cooking utensils of various sizes and shapes. These are neatly organized in small, colorful holders – light blue, yellow, and beige.
The kitchen itself is softly rendered. A light green cabinet with two doors is visible on the left side, featuring simple, round knobs. Above the cabinet, a pale green wall extends towards the top of the frame. A white tiled backsplash runs along the wall behind the countertop.
A tall, pale pink and white kitchen light fixture hangs from the ceiling on the left side, casting a soft glow on the scene. A large window is visible on the far left, letting in bright natural light that illuminates the kitchen.
In the background, partially visible, are other kitchen elements – possibly a stove or oven, also rendered in a muted, pastel style. A small potted plant with green leaves sits near the edge of the countertop, adding a touch of freshness to the scene.
The overall style of the illustration is whimsical and charming, with soft colors and gentle lines, suggesting a warm and inviting atmosphere.
Audio + Image : Tour Guide (image in + audio question)
This section demonstrates how to leverage Gemma 3n's multimodal input capability to assist as a Tour Guide. Take a picture of a famous landmark and ask any questions. Gemma 3n will act as your helpful tour guide.
🤖
The image shows the Golden Gate Bridge in San Francisco, California.It's a suspension bridge known for its iconic orange color, which helps to make it visible in the fog. The bridge connects San Francisco to Marin County.
Try this by yourself
Press the circle button and start speaking. Click the circle button again when you are finshed. The widget will immediately begin to play back what it captured.
Convert webm file to wav format that PyTorch can understand.
Next steps
Build and explore more with Gemma models:
Run in Google Colab