Notebooks
G
Google Gemini
[PaliGemma 1]Using With Mesop

[PaliGemma 1]Using With Mesop

recurrentgemmagemmapaligemmagemma-cookbookcodegemma
Copyright 2024 Google LLC.
[1]

Integrating PaliGemma with Mesop

This notebook demonstrates how to use PaliGemma models with Mesop to create a simple GUI application.

Setup

Select the Colab runtime

To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the Gemma model. In this case, you can use a T4 GPU:

  1. In the upper-right of the Colab window, select ▾ (Additional connection options).
  2. Select Change runtime type.
  3. Under Hardware accelerator, select T4 GPU.

Gemma setup

To complete this tutorial, you'll first need to complete the setup instructions at Gemma setup. The Gemma setup instructions show you how to do the following:

  • Get access to Gemma on kaggle.com.
  • Select a Colab runtime with sufficient resources to run the Gemma 2B model.
  • Generate and configure a Kaggle username and an API key as Colab secrets.

After you've completed the Gemma setup, move on to the next section, where you'll set environment variables for your Colab environment.

Configure your credentials

Add your your Kaggle credentials to the Colab Secrets manager to securely store it.

  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. Create new secrets: KAGGLE_USERNAME and KAGGLE_KEY
  3. Copy/paste your username into KAGGLE_USERNAME
  4. Copy/paste your key into KAGGLE_KEY
  5. Toggle the buttons on the left to allow notebook access to the secrets.
[2]

Install dependencies

Run the cell below to install all the required dependencies.

[3]
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 5.9 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 571.8/571.8 kB 37.7 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 347.7/347.7 kB 26.2 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.2/5.2 MB 46.9 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 590.6/590.6 MB 2.5 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.3/5.3 MB 69.6 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2/2.2 MB 73.2 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.5/5.5 MB 69.8 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.
tf-keras 2.15.1 requires tensorflow<2.16,>=2.15, but you have tensorflow 2.16.2 which is incompatible.
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 77.9/77.9 kB 1.5 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.2/43.2 kB 3.5 MB/s eta 0:00:00
  Building wheel for ml_collections (setup.py) ... done

Exploring prompting capabilities

PaliGemma

PaliGemma is a lightweight open vision-language model (VLM) inspired by PaLI-3, and based on open components like the SigLIP vision model and the Gemma language model. PaliGemma takes both images and text as inputs and can answer questions about images with detail and context, meaning that PaliGemma can perform deeper analysis of images and provide useful insights, such as captioning for images and short videos, object detection, and reading text embedded within images.

Prompting:

  • cap {lang}\n: Very raw short caption (from WebLI-alt)
  • caption {lang}\n: Nice, COCO-like short captions
  • describe {lang}\n: Somewhat longer, more descriptive captions
  • ocr: Optical character recognition
  • answer en {question}\n: Question answering about the image contents
  • question {lang} {answer}\n: Question generation for a given answer
  • detect {object} ; {object}\n: Count objects in a scene and return the bounding boxes for the objects
  • segment {object}\n: Do image segmentation of the object in the scene
[4]
[5]
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors.index.json...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/metadata.json...
100%|██████████| 143/143 [00:00<00:00, 112kB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/task.json...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/config.json...
100%|██████████| 861/861 [00:00<00:00, 1.19MB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors.index.json...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.weights.h5...
100%|██████████| 5.45G/5.45G [01:15<00:00, 77.7MB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/model.safetensors.index.json...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/preprocessor.json...
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/tokenizer.json...
100%|██████████| 410/410 [00:00<00:00, 471kB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/paligemma/keras/pali_gemma_3b_mix_224/1/download/assets/tokenizer/vocabulary.spm...
100%|██████████| 4.07M/4.07M [00:00<00:00, 15.8MB/s]
[6]
[7]

Prompting example: Visual Question Answering

[8]
[9]
In this image we can see a bird on the ground. In the background there is water.

Integration with Mesop

We will create a simple GUI application that allows users to upload images, specify prompt styles, and other arguments, such as the question or type of object, that are necessary to generate the answer.

The application will be able to provide both images and text as the output.

[10]
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.0/5.0 MB 18.8 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.6/76.6 kB 12.2 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.0/83.0 kB 13.5 MB/s eta 0:00:00
[11]

Running server on: http://localhost:32123
 * Serving Flask app 'mesop.server.server'
 * Debug mode: off
[12]
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (::)
 * Running on http://[::1]:32123
 * Running on http://[::1]:32123
INFO:werkzeug:Press CTRL+C to quit
[13]
<IPython.core.display.Javascript object>