├── data └── animals.png ├── README.md ├── LICENSE └── notebooks └── gemma-3-1b-workout-summaries.ipynb /data/animals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickloeber/genai-tutorials/HEAD/data/animals.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # genai-tutorials 2 | Code examples showing how to use Gemini, Gemma, Imagen, and more. 3 | 4 | | Notebook | Description | 5 | |:-------------------------------------------------------------------------|:---------------------------------------------------------------------------------| 6 | | [gemini-multimodal.ipynb](notebooks/gemini-multimodal.ipynb) | Multimodality with Gemini 2.0: Process text, audio, images, videos, and PDFs | 7 | | [gemma-3-1b-workout-summaries.ipynb](notebooks/gemma-3-1b-workout-summaries.ipynb) | Create workout summaries with Gemma 3 1B | 8 | | [gemini-image-editing.ipynb](notebooks/gemini-image-editing.ipynb) | Image creation and editing with Gemini 2.0 | 9 | | [gemini-object-detection.ipynb](notebooks/gemini-object-detection.ipynb) | Bounding box and object detection with Gemini | 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Patrick Loeber 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /notebooks/gemma-3-1b-workout-summaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "gpuType": "T4" 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | }, 16 | "accelerator": "GPU" 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "# Create workout summaries with Gemma 3 1B\n", 23 | "\n", 24 | "\"Open\n", 25 | "\n", 26 | "Gemma 3 1B can be used for data captioning and summary creation tasks.\n", 27 | "\n", 28 | "For example, personal push notifications in (mobile) apps are a good use case for this.\n", 29 | "\n", 30 | "This notebook shows how to do it. We'll use an example workout data in json format and turn it into a fun workout summary.\n", 31 | "\n", 32 | "Install the release for Gemma 3 and login to access the model." 33 | ], 34 | "metadata": { 35 | "id": "umZ3bEQhAiFD" 36 | } 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "id": "yMyqiuS0mcEf" 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "!pip install git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "source": [ 52 | "!huggingface-cli login" 53 | ], 54 | "metadata": { 55 | "id": "aRDYIIVanObJ" 56 | }, 57 | "execution_count": null, 58 | "outputs": [] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "source": [ 63 | "Let's load the model and pipeline:" 64 | ], 65 | "metadata": { 66 | "id": "xZuEc2YOBbpy" 67 | } 68 | }, 69 | { 70 | "cell_type": "code", 71 | "source": [ 72 | "import torch\n", 73 | "from transformers import AutoTokenizer, pipeline\n", 74 | "\n", 75 | "model_id=\"google/gemma-3-1b-it\"\n", 76 | "\n", 77 | "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", 78 | "\n", 79 | "pipe = pipeline(\"text-generation\", model=\"google/gemma-3-1b-it\",\n", 80 | " device=\"cuda\", torch_dtype=torch.bfloat16, tokenizer=tokenizer)" 81 | ], 82 | "metadata": { 83 | "colab": { 84 | "base_uri": "https://localhost:8080/" 85 | }, 86 | "id": "Nd1_vl4kmuLa", 87 | "outputId": "3d94e4bf-e8c1-406d-bd9f-858531259be4" 88 | }, 89 | "execution_count": 116, 90 | "outputs": [ 91 | { 92 | "output_type": "stream", 93 | "name": "stderr", 94 | "text": [ 95 | "Device set to use cuda\n" 96 | ] 97 | } 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "source": [ 103 | "Create the summary prompt and function to create the summary:" 104 | ], 105 | "metadata": { 106 | "id": "o22Lq8iLBkDW" 107 | } 108 | }, 109 | { 110 | "cell_type": "code", 111 | "source": [ 112 | "summary_prompt = \"\"\"You are a motivating fitness coach. Generate a short,\n", 113 | "personal, and fun workout summary for the user from this data:\n", 114 | "\n", 115 | "{data}\n", 116 | "\n", 117 | "Keep it under 20 words and use emojies. Only return the summary.\n", 118 | "\"\"\"\n", 119 | "\n", 120 | "def generate_workout_summary(workout_data):\n", 121 | " message = [\n", 122 | " {\n", 123 | " \"role\": \"user\",\n", 124 | " \"content\": [{\"type\": \"text\", \"text\": summary_prompt.format(data=workout_data)}]\n", 125 | " }\n", 126 | " ]\n", 127 | " output = pipe(message, max_new_tokens=50, do_sample=True)\n", 128 | " return output[0]['generated_text'][-1][\"content\"]" 129 | ], 130 | "metadata": { 131 | "id": "qc_VTmwr0zpz" 132 | }, 133 | "execution_count": 155, 134 | "outputs": [] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "source": [ 139 | "Let's feed in the workout data and try it:" 140 | ], 141 | "metadata": { 142 | "id": "A6ntQey7BsK5" 143 | } 144 | }, 145 | { 146 | "cell_type": "code", 147 | "source": [ 148 | "workout_data = {\n", 149 | " \"type\": \"run\", \"duration_minutes\": 48, \"avg_heart_rate_bpm\": 150,\n", 150 | " \"details\": {\"elevation_gain_m\": 320, \"pace_kmh\": 10.2,\n", 151 | " \"distance_km\": 8.1, \"personal_record\": True}\n", 152 | "}\n", 153 | "\n", 154 | "summary = generate_workout_summary(workout_data)\n", 155 | "print(summary)" 156 | ], 157 | "metadata": { 158 | "colab": { 159 | "base_uri": "https://localhost:8080/" 160 | }, 161 | "id": "llk7Ih9uztma", 162 | "outputId": "b1371ac7-cfe2-4ad6-ac9e-2277d968510d" 163 | }, 164 | "execution_count": 165, 165 | "outputs": [ 166 | { 167 | "output_type": "stream", 168 | "name": "stdout", 169 | "text": [ 170 | "🎉 Great run! 48 mins, 150bpm, 8.1km! 🚀 Personal record achieved! 💪\n" 171 | ] 172 | } 173 | ] 174 | } 175 | ] 176 | } --------------------------------------------------------------------------------