### Setup and Installation Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/llm_inference/bundling/llm_bundling.ipynb Sets up the environment by installing the MediaPipe library using pip and importing necessary modules. It displays installation progress and a confirmation message. ```python #@title Setup { display-mode: "form" } import ipywidgets as widgets from IPython.display import display from google.colab import files install_out = widgets.Output() display(install_out) with install_out: !pip install mediapipe from mediapipe.tasks.python.genai import bundler install_out.clear_output() with install_out: print("Setup done.") ``` -------------------------------- ### Install Protobuf and LiteRT Tools Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/gemma3_1b_tflite.ipynb Downloads and installs the protocol buffer compiler and the LiteRT tools library required for model pipeline management. ```python ! wget -q https://github.com/protocolbuffers/protobuf/releases/download/v3.19.0/protoc-3.19.0-linux-x86_64.zip ! unzip -o protoc-3.19.0-linux-x86_64.zip -d /usr/local/ !pip install git+https://github.com/google-ai-edge/ai-edge-apis.git#subdirectory=litert_tools ``` -------------------------------- ### Setup LLM Conversion Environment with Python Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/llm_inference/conversion/llm_conversion.ipynb This code snippet sets up the necessary environment for LLM conversion using Python. It installs required libraries like mediapipe, torch, and huggingface_hub, and imports necessary modules for model conversion. The setup process is displayed using ipywidgets for user feedback. ```python # @title Setup { display-mode: "form" } # @markdown import ipywidgets\ # @markdown import IPython.display\ # @markdown import os\ # @markdown import huggingface downloader\ # @markdown import mediapipe genai converter import ipywidgets as widgets from IPython.display import display install_out = widgets.Output() display(install_out) with install_out: !pip install mediapipe !pip install torch !pip install huggingface_hub import os from huggingface_hub import hf_hub_download from mediapipe.tasks.python.genai import converter install_out.clear_output() with install_out: print("Setup done.") ``` -------------------------------- ### Download and Unzip Example Object Detection Dataset Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/object_detector.ipynb Downloads an example dataset for object detection from a Google Cloud Storage bucket and unzips it. It then sets the paths for the training and validation datasets, which are expected to be in COCO format. ```python !wget https://storage.googleapis.com/mediapipe-tasks/object_detector/android_figurine.zip !unzip android_figurine.zip train_dataset_path = "android_figurine/train" validation_dataset_path = "android_figurine/validation" ``` -------------------------------- ### Install MediaPipe and Download Model Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/interactive_segmentation/python/interactive_segmenter.ipynb Installs the necessary MediaPipe library and downloads the required TFLite segmentation model from Google Cloud Storage. ```python !pip install -q mediapipe !wget -O model.tflite -q https://storage.googleapis.com/mediapipe-models/interactive_segmenter/magic_touch/float32/1/magic_touch.tflite ``` -------------------------------- ### Install Dependencies and Download Models (Shell) Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/object_detection/raspberry_pi/README.md This script installs the necessary dependencies and downloads the required TensorFlow Lite models for the object detection example. It assumes you are in the correct directory within the MediaPipe examples repository. ```shell cd mediapipe/examples/object_detection/raspberry_pi sh setup.sh ``` -------------------------------- ### Initialize and Start SFTTrainer Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1B_Fine_Tuning_text_to_sql.ipynb Combines the model, configuration, and dataset into the SFTTrainer to execute the fine-tuning process. ```python from trl import SFTTrainer trainer = SFTTrainer( model=model, args=args, train_dataset=dataset["train"], peft_config=peft_config, processing_class=tokenizer ) trainer.train() ``` -------------------------------- ### Setup Environment and Download Model Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/face_detector/python/face_detector.ipynb Installs the necessary MediaPipe library and downloads a pre-trained BlazeFace TFLite model for face detection. This is the prerequisite step for running the detection pipeline. ```bash !pip install mediapipe !wget -q -O detector.tflite -q https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1B_Fine_Tuning_text_to_sql.ipynb Installs the required Python packages including PyTorch, Transformers, and MediaPipe components. This ensures the environment is prepared for AI model training and deployment tasks. ```bash %pip install --upgrade "torch==2.6.0" \ "transformers>=4.51.3" \ "datasets==3.3.2" \ "accelerate==1.4.0" \ "evaluate==0.4.3" \ "trl==0.15.2" \ "peft==0.14.0" \ "flash-attn==2.7.4.post1" \ ai-edge-torch-nightly==0.6.0.dev20250523 \ ai-edge-litert==1.3.0 \ mediapipe==0.10.21 ``` -------------------------------- ### Install MediaPipe dependencies Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tools/image_generator_converter/README.md Install the required Python packages using pip. ```bash pip install torch typing_extensions numpy Pillow requests pytorch_lightning absl-py ``` -------------------------------- ### Install MediaPipe Dependencies and Models Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/face_detector/raspberry_pi/README.md This script installs the necessary dependencies and downloads the required TFLite models for the face detection example. It assumes you are in the correct directory within the MediaPipe examples repository. ```shell cd mediapipe/examples/face_detection/raspberry_pi sh setup.sh ``` -------------------------------- ### Install MediaPipe Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/hand_landmarker/python/hand_landmarker.ipynb Installs the MediaPipe library, a dependency for hand landmark detection. This command uses pip for installation and the -q flag for quiet output. ```python #@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` ```python !pip install -q mediapipe ``` -------------------------------- ### Install and Configure MediaPipe Environment Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/face_landmarker/python/[MediaPipe_Python_Tasks]_Face_Landmarker.ipynb Commands to install the MediaPipe library and download the necessary pre-trained face landmarker model bundle for inference. ```shell !pip install -q mediapipe !wget -O face_landmarker_v2_with_blendshapes.task -q https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task ``` -------------------------------- ### UI Element Initialization Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/llm_inference/conversion/llm_conversion.ipynb Initializes and displays the user interface elements for model conversion, including a button to start the process, dropdowns for model and backend selection, and an output area for displaying messages and logs. This setup is crucial for user interaction. ```python button = widgets.Button(description="Start Conversion") button.on_click(on_button_clicked) display(button) out = widgets.Output(layout={'border': '1px solid black'}) display(out) print("\nNotice: Converted models are saved under ./converted_models") ``` -------------------------------- ### Download and Preview Images Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/interactive_segmentation/python/interactive_segmenter.ipynb Downloads sample images from a remote URL and provides a utility to resize and display them using OpenCV. ```python import urllib import cv2 from google.colab.patches import cv2_imshow import math IMAGE_FILENAMES = ['cats_and_dogs.jpg'] for name in IMAGE_FILENAMES: url = f'https://storage.googleapis.com/mediapipe-assets/{name}' urllib.request.urlretrieve(url, name) DESIRED_HEIGHT = 480 DESIRED_WIDTH = 480 def resize_and_show(image): h, w = image.shape[:2] if h < w: img = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH)))) else: img = cv2.resize(image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT)) cv2_imshow(img) ``` -------------------------------- ### Run Object Detection Example (Python) Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/object_detection/raspberry_pi/README.md Executes the object detection script using Python 3. This command starts the real-time object detection process, displaying the camera feed with bounding boxes around detected objects. The default model 'efficientdet_lite0.tflite' is used. ```python python3 detect.py \ --model efficientdet_lite0.tflite ``` -------------------------------- ### Install AI Edge and MediaPipe Libraries Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1b_fine_tune.ipynb Installs the AI Edge Torch (LiteRT) and MediaPipe libraries required for converting and deploying models. This step ensures that the necessary tools for on-device inference and model optimization are available. ```python ! pip3 install ai-edge-torch-nightly==0.6.0.dev20250605 ! pip3 install ai-edge-litert==1.3.0 ! pip3 install mediapipe==0.10.21 ``` -------------------------------- ### Install MediaPipe Model Maker and Dependencies Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tutorials/object_detection/Object_Detection_for_3_dogs.ipynb Installs the necessary Python packages including the Model Maker library and Keras. This environment setup is required before importing model training modules. ```bash !pip install --upgrade pip !pip install 'keras<3.0.0' mediapipe-model-maker ``` -------------------------------- ### Visualize Object Detection Dataset with Bounding Boxes Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/object_detector.ipynb Visualizes example images from the object detection dataset along with their corresponding bounding boxes and labels. This function helps in understanding the dataset's content and annotation quality. ```python #@title Visualize the training dataset import matplotlib.pyplot as plt from matplotlib import patches, text, patheffects from collections import defaultdict import math def draw_outline(obj): obj.set_path_effects([patheffects.Stroke(linewidth=4, foreground='black'), patheffects.Normal()]) def draw_box(ax, bb): patch = ax.add_patch(patches.Rectangle((bb[0],bb[1]), bb[2], bb[3], fill=False, edgecolor='red', lw=2)) draw_outline(patch) def draw_text(ax, bb, txt, disp): text = ax.text(bb[0],(bb[1]-disp),txt,verticalalignment='top' ,color='white',fontsize=10,weight='bold') draw_outline(text) def draw_bbox(ax, annotations_list, id_to_label, image_shape): for annotation in annotations_list: cat_id = annotation["category_id"] bbox = annotation["bbox"] draw_box(ax, bbox) draw_text(ax, bbox, id_to_label[cat_id], image_shape[0] * 0.05) def visualize(dataset_folder, max_examples=None): with open(os.path.join(dataset_folder, "labels.json"), "r") as f: labels_json = json.load(f) images = labels_json["images"] cat_id_to_label = {item["id"]:item["name"] for item in labels_json["categories"]} image_annots = defaultdict(list) for annotation_obj in labels_json["annotations"]: image_id = annotation_obj["image_id"] image_annots[image_id].append(annotation_obj) if max_examples is None: max_examples = len(image_annots.items()) n_rows = math.ceil(max_examples / 3) fig, axs = plt.subplots(n_rows, 3, figsize=(24, n_rows*8)) # 3 columns(2nd index), 8x8 for each image for ind, (image_id, annotations_list) in enumerate(list(image_annots.items())[:max_examples]): ax = axs[ind//3, ind%3] img = plt.imread(os.path.join(dataset_folder, "images", images[image_id]["file_name"])) ax.imshow(img) draw_bbox(ax, annotations_list, cat_id_to_label, img.shape) ``` -------------------------------- ### Download and Visualize Training Image Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/face_stylizer.ipynb Downloads a sample style image and uses OpenCV to display it for verification before training. ```python style_image_path = 'color_sketch.jpg' !wget -q -O {style_image_path} https://storage.googleapis.com/mediapipe-assets/face_stylizer_style_color_sketch.jpg import cv2 from google.colab.patches import cv2_imshow style_image_tensor = image_utils.load_image(style_image_path) style_cv_image = cv2.cvtColor(style_image_tensor.numpy(), cv2.COLOR_RGB2BGR) cv2_imshow(style_cv_image) ``` -------------------------------- ### License Information for MediaPipe Examples Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/object_detector.ipynb Standard Apache License 2.0 header used across MediaPipe sample projects to define usage, modification, and distribution rights. ```python # Copyright 2023 The MediaPipe Authors. # Licensed under the Apache License, Version 2.0 (the "License"); # # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` -------------------------------- ### Install MediaPipe Model Maker using pip in Python Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tutorials/gesture_recognizer/WiMLS_2022_MediaPipe_Gesture_Recognizer_Walkthrough.ipynb This command installs the MediaPipe Model Maker library, which is used for training custom recognition models. It uses `pip` for installation and is typically run in an environment like Google Colab. The `-q` flag ensures a quiet installation. ```shell !pip install -q mediapipe-model-maker ``` -------------------------------- ### Setup Live Stream Object Detector in Kotlin Source: https://context7.com/google-ai-edge/mediapipe-samples/llms.txt Initializes the ObjectDetector for live stream mode. Ensure the model asset is available and configure delegate, score threshold, and max results. Set up listeners for results and errors. ```kotlin // Android (Kotlin) - Live Stream Object Detection import com.google.mediapipe.tasks.vision.objectdetector.ObjectDetector import com.google.mediapipe.tasks.vision.core.RunningMode import com.google.mediapipe.tasks.core.BaseOptions import com.google.mediapipe.framework.image.BitmapImageBuilder class ObjectDetectorHelper( private val context: Context, private val listener: DetectorListener ) { private var objectDetector: ObjectDetector? = null fun setupObjectDetector() { val baseOptions = BaseOptions.builder() .setModelAssetPath("efficientdet-lite0.tflite") .setDelegate(Delegate.GPU) // or Delegate.CPU .build() val options = ObjectDetector.ObjectDetectorOptions.builder() .setBaseOptions(baseOptions) .setRunningMode(RunningMode.LIVE_STREAM) .setScoreThreshold(0.5f) .setMaxResults(3) .setResultListener { result, input -> listener.onResults(result, input.width, input.height) } .setErrorListener { error -> listener.onError(error.message ?: "Unknown error") } .build() objectDetector = ObjectDetector.createFromOptions(context, options) } fun detectAsync(bitmap: Bitmap, frameTime: Long) { val mpImage = BitmapImageBuilder(bitmap).build() objectDetector?.detectAsync(mpImage, frameTime) } interface DetectorListener { fun onResults(result: ObjectDetectorResult, width: Int, height: Int) fun onError(error: String) } } ``` -------------------------------- ### Install MediaPipe Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/gesture_recognizer/python/gesture_recognizer.ipynb Installs the MediaPipe library using pip. This is a prerequisite for using MediaPipe functionalities. ```python #@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` -------------------------------- ### Install MediaPipe and Download Model Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/object_detection/python/object_detector.ipynb Installs the required MediaPipe library and downloads a pre-trained EfficientDet TFLite model for object detection. ```python !pip install -q mediapipe !wget -q -O efficientdet.tflite -q https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite0/int8/1/efficientdet_lite0.tflite ``` -------------------------------- ### Download and preview test images Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/image_segmentation/python/image_segmentation.ipynb Downloads test images from a remote URL and provides a utility function to resize and display images using OpenCV. ```python import urllib import cv2 from google.colab.patches import cv2_imshow import math IMAGE_FILENAMES = ['segmentation_input_rotation0.jpg'] for name in IMAGE_FILENAMES: url = f'https://storage.googleapis.com/mediapipe-assets/{name}' urllib.request.urlretrieve(url, name) DESIRED_HEIGHT = 480 DESIRED_WIDTH = 480 def resize_and_show(image): h, w = image.shape[:2] if h < w: img = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH)))) else: img = cv2.resize(image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT)) cv2_imshow(img) ``` -------------------------------- ### Load Gemma 3 Model Pipeline Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/gemma3_1b_tflite.ipynb Initializes the inference pipeline by loading a specific LiteRT task file from a HuggingFace repository. ```python from litert_tools.pipeline import pipeline runner = pipeline.load("Gemma3-1B-IT_seq128_q8_ekv1280.task", repo_id="litert-community/Gemma3-1B-IT") ``` -------------------------------- ### Install Core Libraries for Gemma Fine-tuning Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1b_fine_tune.ipynb Installs essential Python libraries for fine-tuning Gemma models, including bitsandbytes, peft, trl, accelerate, datasets, numpy, and transformers. These libraries are crucial for managing model weights, applying parameter-efficient fine-tuning techniques, and handling training data. ```python !pip3 install --upgrade -q -U bitsandbytes==0.46.0 !pip3 install --upgrade -q -U peft==0.15.2 !pip3 install --upgrade -q -U trl==0.18.1 !pip3 install --upgrade -q -U accelerate==1.7.0 !pip3 install --upgrade -q -U datasets==3.6.0 !pip3 install --upgrade -q -U numpy==2.2.6 !pip3 install --force-reinstall transformers==4.52.3 ``` -------------------------------- ### Install MediaPipe Dependencies in Python Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tutorials/gesture_recognizer/WiMLS_2022_MediaPipe_Gesture_Recognizer_Walkthrough.ipynb Installs the required flatbuffers and MediaPipe packages via pip. This step is necessary to set up the environment for running MediaPipe Tasks. ```python !pip install -q flatbuffers==2.0.0 !pip install -q mediapipe==0.9.0 ``` -------------------------------- ### Run Image Segmentation Inference Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/image_segmentation/python/image_segmentation.ipynb Initializes the MediaPipe ImageSegmenter, processes input images to generate category masks, and visualizes the foreground/background separation. ```python import numpy as np import mediapipe as mp from mediapipe.tasks import python from mediapipe.tasks.python import vision BG_COLOR = (192, 192, 192) MASK_COLOR = (255, 255, 255) base_options = python.BaseOptions(model_asset_path='deeplabv3.tflite') options = vision.ImageSegmenterOptions(base_options=base_options, output_category_mask=True) with vision.ImageSegmenter.create_from_options(options) as segmenter: for image_file_name in IMAGE_FILENAMES: image = mp.Image.create_from_file(image_file_name) segmentation_result = segmenter.segment(image) category_mask = segmentation_result.category_mask image_data = image.numpy_view() fg_image = np.zeros(image_data.shape, dtype=np.uint8) fg_image[:] = MASK_COLOR bg_image = np.zeros(image_data.shape, dtype=np.uint8) bg_image[:] = BG_COLOR condition = category_mask.numpy_view().squeeze(-1) > 0.2 output_image = np.where(condition[...,None], fg_image, bg_image) resize_and_show(output_image) ``` -------------------------------- ### Configure and Train Custom Image Classifier Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/image_classifier.ipynb Demonstrates how to initialize HParams and ImageClassifierOptions to customize training hyperparameters like epochs and dropout rate, then create the classifier model. ```python hparams=image_classifier.HParams(epochs=15, export_dir="exported_model_2") options = image_classifier.ImageClassifierOptions(supported_model=spec, hparams=hparams) options.model_options = image_classifier.ModelOptions(dropout_rate = 0.07) model_2 = image_classifier.ImageClassifier.create( train_data = train_data, validation_data = validation_data, options=options, ) ``` -------------------------------- ### Execute Face Stylizer Model Retraining Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/face_stylizer.ipynb Initializes the retraining process using the create method with the prepared training dataset and previously defined configuration options. ```python face_stylizer_model = face_stylizer.FaceStylizer.create( train_data=data, options=face_stylizer_options ) ``` -------------------------------- ### Initialize and Run LLM Pipeline Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1b_fine_tune.ipynb Demonstrates how to instantiate the LiteRTLlmPipeline and trigger text generation using a prompt. ```python pipeline = LiteRTLlmPipeline(interpreter, tokenizer) prompt = "What is the primary function of mitochondria within a cell" output = pipeline.generate(prompt, max_decode_steps = 100) ``` -------------------------------- ### Install Dependencies and Download Models (Shell) Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/text_classification/raspberry_pi/README.md This script installs the necessary dependencies and downloads the required TFLite models for the text classification example. It assumes you are in the correct directory within the MediaPipe examples repository. ```shell cd mediapipe/examples/text_classification/raspberry_pi sh setup.sh ``` -------------------------------- ### Initialize and Run LLM Pipeline Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1B_Fine_Tuning_text_to_sql.ipynb Demonstrates how to instantiate the LiteRTLlmPipeline and execute a generation task using a prompt. The pipeline processes the input, handles tokenization, and returns the generated text. ```python pipeline = LiteRTLlmPipeline(interpreter, tokenizer) output = pipeline.generate("What is the total number of products made from organic materials?", max_decode_steps = 256) ``` -------------------------------- ### Install MediaPipe Model Maker Libraries (Python) Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/image_classifier.ipynb Installs and upgrades pip, then installs the mediapipe-model-maker package. This is a prerequisite for customizing MediaPipe models using Python. ```python !python --version !pip install --upgrade pip !pip install mediapipe-model-maker ``` -------------------------------- ### Install MediaPipe Model Maker Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/face_stylizer.ipynb Installs the necessary Python package required for customizing on-device machine learning models. ```python !pip install mediapipe-model-maker ``` -------------------------------- ### Perform Initial Inference with Gemma Model Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1b_fine_tune.ipynb Demonstrates how to use the loaded Gemma model and tokenizer to perform a simple text generation task. This example shows the base model's behavior before fine-tuning, highlighting potential repetitions of user prompts. ```python import torch from transformers import pipeline ``` -------------------------------- ### Install MediaPipe Dependency Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/audio_classifier/python/audio_classification.ipynb Installs the MediaPipe library, which is required for using the MediaPipe Tasks API. This is a prerequisite for running the audio classification example. ```python !pip install -q mediapipe ``` -------------------------------- ### Initialize and Run LiteRT Pipeline Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/gemma2_tflite.ipynb Demonstrates the initialization of the LiteRTLlmPipeline and the execution of a text generation request. ```python pipeline = LiteRTLlmPipeline(interpreter, tokenizer) prompt = "what is 8 mod 6" output = pipeline.generate(prompt, max_decode_steps = None) ``` -------------------------------- ### Install MediaPipe Model Maker Libraries Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/object_detector.ipynb Installs the required Python packages for using MediaPipe Model Maker, including upgrading pip and installing the model maker library. This is a prerequisite for customizing and retraining models. ```python !python --version !pip install --upgrade pip !pip install mediapipe-model-maker ``` -------------------------------- ### Install MediaPipe Model Maker Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/text_classifier.ipynb Installs the necessary MediaPipe Model Maker package and upgrades pip. This is a prerequisite for using the MediaPipe Model Maker functionalities. ```python #@title License information # Copyright 2023 The MediaPipe Authors. # Licensed under the Apache License, Version 2.0 (the "License"); # # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` ```python !pip install --upgrade pip !pip install mediapipe-model-maker ``` -------------------------------- ### Import Required Python Classes for MediaPipe Model Maker Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/image_classifier.ipynb Imports necessary libraries and classes from Google Colab, TensorFlow, and MediaPipe Model Maker. This setup is essential for using the image classification customization features. ```python from google.colab import files import os import tensorflow as tf assert tf.__version__.startswith('2') from mediapipe_model_maker import image_classifier import matplotlib.pyplot as plt ``` -------------------------------- ### Configure Environment and Authenticate Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1B_Fine_Tuning_text_to_sql.ipynb Sets up the Hugging Face authentication token and disables Weights & Biases integration. This is required for accessing private models and managing experiment logging. ```python import os from google.colab import userdata from huggingface_hub import login # Login into Hugging Face Hub hf_token = userdata.get('HF_TOKEN') # If you are running inside a Google Colab login(hf_token) # For the purposes of this demonstration we disable W&B integration. # See https://huggingface.co/docs/transformers/main_classes/callback#transformers.integrations.WandbCallback for details. os.environ["WANDB_DISABLED"] = "true" ``` -------------------------------- ### Download and Prepare Dataset Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tutorials/object_detection/Object_Detection_for_3_dogs.ipynb Downloads a sample dog dataset and unzips it into local training and validation directories. This prepares the file structure for the Model Maker dataset loader. ```bash !wget https://storage.googleapis.com/mediapipe-assets/dogs2.zip --no-check-certificate !unzip dogs2.zip train_dataset_path = "dogs/train" validation_dataset_path = "dogs/validate" ``` -------------------------------- ### Initialize and Run LLM Inference Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/llm_inference/js/index.html Uses FilesetResolver to load WASM assets and LlmInference to process user input. Requires a model buffer and handles partial result streaming. ```javascript // For real web apps, the script would be included as a separate file. We inline // it into the HTML here so the code can stay of type "module" while still // allowing the demo to be run trivially by just opening this one file in the // browser. import {FilesetResolver, LlmInference} from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai'; const modelSelector = document.getElementById('model-select'); const cancel = document.getElementById('cancel'); const input = document.getElementById('input'); const output = document.getElementById('output'); const submit = document.getElementById('submit'); /** * Display newly generated partial results to the output text box. */ function displayPartialResults(partialResults, complete) { output.textContent += partialResults; if (complete) { if (!output.textContent) { output.textContent = 'Result is empty'; } submit.disabled = false; cancel.disabled = true; } } /** * Main function to run LLM Inference given a model. */ async function runDemo(modelStream) { const genaiFileset = await FilesetResolver.forGenAiTasks( 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm'); let llmInference; submit.disabled = true; submit.onclick = () => { output.textContent = ''; submit.disabled = true; // Gemma 3 models require a simple template for best results. See // https://ai.google.dev/gemma/docs/core/prompt-structure. // Gemma 4 models use a different structure and have more options: // https://ai.google.dev/gemma/docs/core/prompt-formatting-gemma4. // We use the simple Gemma 3 format here. const query = 'user\n' + input.value + '\nmodel\n'; llmInference.generateResponse(query, displayPartialResults); cancel.disabled = false; }; cancel.onclick = () => { llmInference.cancelProcessing(); }; submit.value = 'Loading the model...' LlmInference .createFromOptions(genaiFileset, { baseOptions: {modelAssetBuffer: modelStream}, // Use modelAssetPath // instead for URLs. // maxTokens: 512, // The maximum number of tokens (input tokens + output // // tokens) the model handles. // randomSeed: 1, // The random seed used during text generation. // topK: 1, // The number of tokens the model considers at each step of // // generation. Limits predictions to the top k most-probable // // tokens. Setting randomSeed is required for this to make // // effects. // temperature: // 1.0, // The amount of randomness introduced during generation. // // Setting randomSeed is required for this to make effects. // For multimodal (Gemma 3n) options and more documentation, see // https://ai.google.dev/edge/mediapipe/solutions/genai/llm_inference/web_js }) .then(llm => { llmInference = llm; submit.disabled = false; submit.value = 'Get Response' }) .catch(() => { alert('Failed to initialize the task.'); }); } // When the user chooses a model from their local hard drive, load it and start // the demo. modelSelector.onchange = async () => { if (modelSelector.files && modelSelector.files.length > 0) { const reader = await modelSelector.files[0].stream().getReader(); runDemo(reader); } }; ``` -------------------------------- ### Prepare Text-to-SQL Dataset with Hugging Face Datasets Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1B_Fine_Tuning_text_to_sql.ipynb Python script using the Hugging Face Datasets library to load, preprocess, and split a Text-to-SQL dataset. It defines system and user prompts, converts data into a conversation format, and splits it into training and testing sets. ```python from datasets import load_dataset # System message for the assistant system_message = """You are a text to SQL query translator. Users will ask you questions in English and you will generate a SQL query based on the provided SCHEMA.""" # User prompt that combines the user query and the schema user_prompt = """Given the and the , generate the corresponding SQL command to retrieve the desired data, considering the query's syntax, semantics, and schema constraints. {context} {question} """ def create_conversation(sample): return { "messages": [ # {"role": "system", "content": system_message}, {"role": "user", "content": user_prompt.format(question=sample["sql_prompt"], context=sample["sql_context"])}, {"role": "assistant", "content": sample["sql"]} ] } # Load dataset from the hub dataset = load_dataset("philschmid/gretel-synthetic-text-to-sql", split="train") dataset = dataset.shuffle().select(range(12500)) # Convert dataset to OAI messages dataset = dataset.map(create_conversation, remove_columns=dataset.features,batched=False) # split dataset into 10,000 training samples and 2,500 test samples dataset = dataset.train_test_split(test_size=2500/12500) # Print formatted user prompt print(dataset["train"][345]["messages"][1]["content"]) ``` -------------------------------- ### Convert model checkpoints Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tools/image_generator_converter/README.md Run the conversion script to process model checkpoints into a bins folder. ```bash python3 convert.py --ckpt_path --output_path ``` -------------------------------- ### Configure Model Selection UI with ipywidgets Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/llm_inference/conversion/llm_conversion.ipynb Sets up dropdown menus and input fields for users to select a machine learning model and backend. It includes event listeners to dynamically update UI elements based on user selection, such as displaying a token input for gated models. ```python model = widgets.Dropdown( options=["Gemma 2B","Gemma 7B", "Falcon 1B", "StableLM 3B", "Phi 2"], value='Gemma 2B', description='model', disabled=False, ) backend = widgets.Dropdown( options=["cpu", "gpu"], value='cpu', description='backend', disabled=False, ) token = widgets.Password( value='', placeholder='huggingface token', description='HF token:', disabled=False ) def on_change_model(change): selected_values = ['Gemma 2B','Gemma 7B'] if change['new'] in selected_values: token.layout.display = 'flex' else: token.layout.display = 'none' model.observe(on_change_model, names='value') display(model) display(backend) display(token) ``` -------------------------------- ### Install ai-edge-litert Package Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/gemma2_tflite.ipynb Installs the necessary `ai-edge-litert` Python package using pip. This package provides the LiteRT interpreter and custom operations for AI model execution. ```python !pip install ai-edge-litert ``` -------------------------------- ### Initialize and Run SFTTrainer Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/codelabs/litert_inference/Gemma3_1b_fine_tune.ipynb This Python code initializes the `SFTTrainer` from the `trl` library for supervised fine-tuning. It configures training arguments such as batch size, gradient accumulation, learning rate, and logging steps. The trainer is then used to fine-tune the model on the prepared SFT dataset using the specified LoRA configuration. ```python import transformers from trl import SFTTrainer trainer = SFTTrainer( model=model, processing_class=tokenizer, train_dataset = ds['train'], args=transformers.TrainingArguments( per_device_train_batch_size=1, gradient_accumulation_steps=4, warmup_steps=2, max_steps=150, #num_train_epochs=1, # Copied from other hugging face tuning blog posts learning_rate=2e-4, #fp16=True, bf16=True, # It makes training faster logging_steps=1, output_dir="outputs", optim="paged_adamw_8bit", report_to = "none", ), peft_config=lora_config, ) trainer.train() ``` -------------------------------- ### Install MediaPipe Model Maker dependencies Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/customization/gesture_recognizer.ipynb Installs the necessary Python packages for model customization, including the MediaPipe Model Maker library and upgrading pip. ```python !pip install --upgrade pip !pip install mediapipe-model-maker ``` -------------------------------- ### Run LLM Inference in Web Browsers Source: https://context7.com/google-ai-edge/mediapipe-samples/llms.txt Initializes the GenAI tasks and generates streaming responses in a browser environment using WebGPU acceleration. ```javascript // Web (JavaScript) - LLM Inference import {FilesetResolver, LlmInference} from '@mediapipe/tasks-genai'; async function runLLM() { // Initialize GenAI tasks const genaiFileset = await FilesetResolver.forGenAiTasks( 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm' ); // Create LLM instance const llmInference = await LlmInference.createFromOptions(genaiFileset, { baseOptions: { modelAssetPath: 'path/to/model.bin' }, maxTokens: 512, temperature: 1.0, topK: 40, randomSeed: 1 }); // Generate response with streaming const query = 'user\nWhat is AI?\nmodel\n'; llmInference.generateResponse(query, (partialResult, done) => { document.getElementById('output').textContent += partialResult; if (done) { console.log('Generation complete'); } }); } ``` -------------------------------- ### Display Images using Matplotlib in Python Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/tutorials/gesture_recognizer/WiMLS_2022_MediaPipe_Gesture_Recognizer_Walkthrough.ipynb This snippet displays a specified number of example images for each label using Matplotlib. It iterates through labels, reads image filenames, and plots them in subplots. Dependencies include `os` and `matplotlib.pyplot`. It takes image paths and a number of examples as input, and outputs displayed figures. ```python import os import matplotlib.pyplot as plt # Assuming IMAGES_PATH and NUM_EXAMPLES are defined elsewhere # IMAGES_PATH = "path/to/your/images" # NUM_EXAMPLES = 5 for label in labels: label_dir = os.path.join(IMAGES_PATH, label) example_filenames = os.listdir(label_dir)[:NUM_EXAMPLES] fig, axs = plt.subplots(1, NUM_EXAMPLES, figsize=(10,2)) for i in range(NUM_EXAMPLES): axs[i].imshow(plt.imread(os.path.join(label_dir, example_filenames[i]))) axs[i].get_xaxis().set_visible(False) axs[i].get_yaxis().set_visible(False) fig.suptitle(f'Showing {NUM_EXAMPLES} examples for {label}') plt.show() ``` -------------------------------- ### Download Sample Images Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/image_embedder/python/image_embedder.ipynb Downloads two sample image files ('burger.jpg' and 'burger_crop.jpg') from a Google Cloud Storage bucket. These images will be used to demonstrate image similarity comparison. ```python import urllib IMAGE_FILENAMES = ['burger.jpg', 'burger_crop.jpg'] for name in IMAGE_FILENAMES: url = f'https://storage.googleapis.com/mediapipe-assets/{name}' urllib.request.urlretrieve(url, name) ``` -------------------------------- ### Initialize Interactive Segmenter Source: https://github.com/google-ai-edge/mediapipe-samples/blob/main/examples/interactive_segmentation/python/interactive_segmenter.ipynb Configures the MediaPipe InteractiveSegmenter options using a TFLite model and prepares the environment for inference. ```python import numpy as np import mediapipe as mp from mediapipe.tasks import python from mediapipe.tasks.python import vision from mediapipe.tasks.python.components import containers base_options = python.BaseOptions(model_asset_path='model.tflite') options = vision.InteractiveSegmenterOptions(base_options=base_options, output_category_mask=True) ```