|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "Tce3stUlHN0L" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "##### Copyright 2023 Google LLC" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": { |
| 16 | + "cellView": "form", |
| 17 | + "id": "tuOe1ymfHZPu" |
| 18 | + }, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 22 | + "# you may not use this file except in compliance with the License.\n", |
| 23 | + "# You may obtain a copy of the License at\n", |
| 24 | + "#\n", |
| 25 | + "# https://www.apache.org/licenses/LICENSE-2.0\n", |
| 26 | + "#\n", |
| 27 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 28 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 29 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 30 | + "# See the License for the specific language governing permissions and\n", |
| 31 | + "# limitations under the License." |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": { |
| 37 | + "id": "FKwyTRdwB8aW" |
| 38 | + }, |
| 39 | + "source": [ |
| 40 | + "## Setup" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "metadata": { |
| 47 | + "id": "RXInneX6xx7c" |
| 48 | + }, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "!pip install -U -q \"google-generativeai>=0.8.2\"" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": { |
| 58 | + "cellView": "form", |
| 59 | + "id": "kWIuwKG2_oWE" |
| 60 | + }, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "# import necessary modules.\n", |
| 64 | + "import base64\n", |
| 65 | + "import copy\n", |
| 66 | + "import json\n", |
| 67 | + "import pathlib\n", |
| 68 | + "import requests\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "import PIL.Image\n", |
| 72 | + "import IPython.display\n", |
| 73 | + "from IPython.display import Markdown\n", |
| 74 | + "\n", |
| 75 | + "try:\n", |
| 76 | + " # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n", |
| 77 | + " # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n", |
| 78 | + " import os\n", |
| 79 | + " from google.colab import userdata\n", |
| 80 | + "\n", |
| 81 | + " os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n", |
| 82 | + "except ImportError:\n", |
| 83 | + " pass\n", |
| 84 | + "\n", |
| 85 | + "import google.generativeai as genai\n", |
| 86 | + "\n", |
| 87 | + "# Parse the arguments\n", |
| 88 | + "\n", |
| 89 | + "model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n", |
| 90 | + "contents_b64 = \"W3sicGFydHMiOiBbeyJ0ZXh0IjogIldoYXQncyBpbiB0aGlzIHBpY3R1cmU/In0sIHsiZmlsZV9kYXRhIjogeyJ1cmwiOiAiaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL2dlbmVyYXRpdmVhaS1kb3dubG9hZHMvaW1hZ2VzL3Njb25lcy5qcGciLCAibWltZV90eXBlIjogImltYWdlL2pwZWcifX1dfV0=\" # @param {isTemplate: true}\n", |
| 91 | + "generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n", |
| 92 | + "safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n", |
| 93 | + "\n", |
| 94 | + "gais_contents = json.loads(base64.b64decode(contents_b64))\n", |
| 95 | + "\n", |
| 96 | + "generation_config = json.loads(base64.b64decode(generation_config_b64))\n", |
| 97 | + "safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n", |
| 98 | + "\n", |
| 99 | + "stream = False\n", |
| 100 | + "\n", |
| 101 | + "# Convert and upload the files\n", |
| 102 | + "\n", |
| 103 | + "tempfiles = pathlib.Path(f\"tempfiles\")\n", |
| 104 | + "tempfiles.mkdir(parents=True, exist_ok=True)\n", |
| 105 | + "\n", |
| 106 | + "\n", |
| 107 | + "drive = None\n", |
| 108 | + "def upload_file_data(file_data, index):\n", |
| 109 | + " \"\"\"Upload files to the Files API.\n", |
| 110 | + "\n", |
| 111 | + " For each file, Google AI Studio either sent:\n", |
| 112 | + " - a Google Drive ID,\n", |
| 113 | + " - a URL,\n", |
| 114 | + " - a file path, or\n", |
| 115 | + " - The raw bytes (`inline_data`).\n", |
| 116 | + "\n", |
| 117 | + " The API only understands `inline_data` or it's Files API.\n", |
| 118 | + " This code, uploads files to the files API where the API can access them.\n", |
| 119 | + " \"\"\"\n", |
| 120 | + "\n", |
| 121 | + " mime_type = file_data[\"mime_type\"]\n", |
| 122 | + " if drive_id := file_data.pop(\"drive_id\", None):\n", |
| 123 | + " if drive is None:\n", |
| 124 | + " from google.colab import drive\n", |
| 125 | + " drive.mount(\"/gdrive\")\n", |
| 126 | + "\n", |
| 127 | + " path = next(\n", |
| 128 | + " pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n", |
| 129 | + " )\n", |
| 130 | + " print(\"Uploading:\", str(path))\n", |
| 131 | + " file_info = genai.upload_file(path=path, mime_type=mime_type)\n", |
| 132 | + " file_data[\"file_uri\"] = file_info.uri\n", |
| 133 | + " return\n", |
| 134 | + "\n", |
| 135 | + " if url := file_data.pop(\"url\", None):\n", |
| 136 | + " response = requests.get(url)\n", |
| 137 | + " data = response.content\n", |
| 138 | + " name = url.split(\"/\")[-1]\n", |
| 139 | + " path = tempfiles / str(index)\n", |
| 140 | + " path.write_bytes(data)\n", |
| 141 | + " print(\"Uploading:\", url)\n", |
| 142 | + " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n", |
| 143 | + " file_data[\"file_uri\"] = file_info.uri\n", |
| 144 | + " return\n", |
| 145 | + "\n", |
| 146 | + " if name := file_data.get(\"filename\", None):\n", |
| 147 | + " if not pathlib.Path(name).exists():\n", |
| 148 | + " raise IOError(\n", |
| 149 | + " f\"local file: `{name}` does not exist. You can upload files \"\n", |
| 150 | + " 'to Colab using the file manager (\"📁 Files\" in the left '\n", |
| 151 | + " \"toolbar)\"\n", |
| 152 | + " )\n", |
| 153 | + " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n", |
| 154 | + " file_data[\"file_uri\"] = file_info.uri\n", |
| 155 | + " return\n", |
| 156 | + "\n", |
| 157 | + " if \"inline_data\" in file_data:\n", |
| 158 | + " return\n", |
| 159 | + "\n", |
| 160 | + " raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n", |
| 161 | + "\n", |
| 162 | + "\n", |
| 163 | + "contents = copy.deepcopy(gais_contents)\n", |
| 164 | + "\n", |
| 165 | + "index = 0\n", |
| 166 | + "for content in contents:\n", |
| 167 | + " for n, part in enumerate(content[\"parts\"]):\n", |
| 168 | + " if file_data := part.get(\"file_data\", None):\n", |
| 169 | + " upload_file_data(file_data, index)\n", |
| 170 | + " index += 1\n", |
| 171 | + "\n", |
| 172 | + "import json\n", |
| 173 | + "print(json.dumps(contents, indent=4))" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "markdown", |
| 178 | + "metadata": { |
| 179 | + "id": "E7zAD69vE92b" |
| 180 | + }, |
| 181 | + "source": [ |
| 182 | + "## Call `generate_content`" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": { |
| 189 | + "id": "LB2LxPmAB95V" |
| 190 | + }, |
| 191 | + "outputs": [], |
| 192 | + "source": [ |
| 193 | + "from IPython.display import display\n", |
| 194 | + "from IPython.display import Markdown\n", |
| 195 | + "\n", |
| 196 | + "# Call the model and print the response.\n", |
| 197 | + "gemini = genai.GenerativeModel(model_name=model)\n", |
| 198 | + "\n", |
| 199 | + "response = gemini.generate_content(\n", |
| 200 | + " contents,\n", |
| 201 | + " generation_config=generation_config,\n", |
| 202 | + " safety_settings=safety_settings,\n", |
| 203 | + " stream=stream,\n", |
| 204 | + ")\n", |
| 205 | + "\n", |
| 206 | + "display(Markdown(response.text))" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "markdown", |
| 211 | + "metadata": { |
| 212 | + "id": "9c9d345e9868" |
| 213 | + }, |
| 214 | + "source": [ |
| 215 | + "<table class=\"tfo-notebook-buttons\" align=\"left\">\n", |
| 216 | + " <td>\n", |
| 217 | + " <a target=\"_blank\" href=\"https://ai.google.dev/gemini-api/docs\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />Docs on ai.google.dev</a>\n", |
| 218 | + " </td>\n", |
| 219 | + " <td>\n", |
| 220 | + " <a target=\"_blank\" href=\"https://github.com/google-gemini/cookbook/blob/main/quickstarts\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />More notebooks in the Cookbook</a>\n", |
| 221 | + " </td>\n", |
| 222 | + "</table>" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": { |
| 228 | + "id": "F91AeeGO1ncU" |
| 229 | + }, |
| 230 | + "source": [ |
| 231 | + "## [optional] Show the conversation\n", |
| 232 | + "\n", |
| 233 | + "This section displays the conversation received from Google AI Studio." |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": null, |
| 239 | + "metadata": { |
| 240 | + "cellView": "form", |
| 241 | + "id": "yoL3p3KPylFW" |
| 242 | + }, |
| 243 | + "outputs": [], |
| 244 | + "source": [ |
| 245 | + "# @title Show the conversation, in colab.\n", |
| 246 | + "import mimetypes\n", |
| 247 | + "\n", |
| 248 | + "def show_file(file_data):\n", |
| 249 | + " mime_type = file_data[\"mime_type\"]\n", |
| 250 | + "\n", |
| 251 | + " if drive_id := file_data.get(\"drive_id\", None):\n", |
| 252 | + " path = next(\n", |
| 253 | + " pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n", |
| 254 | + " )\n", |
| 255 | + " name = path\n", |
| 256 | + " # data = path.read_bytes()\n", |
| 257 | + " kwargs = {\"filename\": path}\n", |
| 258 | + " elif url := file_data.get(\"url\", None):\n", |
| 259 | + " name = url\n", |
| 260 | + " kwargs = {\"url\": url}\n", |
| 261 | + " # response = requests.get(url)\n", |
| 262 | + " # data = response.content\n", |
| 263 | + " elif data := file_data.get(\"inline_data\", None):\n", |
| 264 | + " name = None\n", |
| 265 | + " kwargs = {\"data\": data}\n", |
| 266 | + " elif name := file_data.get(\"filename\", None):\n", |
| 267 | + " if not pathlib.Path(name).exists():\n", |
| 268 | + " raise IOError(\n", |
| 269 | + " f\"local file: `{name}` does not exist. You can upload files to \"\n", |
| 270 | + " 'Colab using the file manager (\"📁 Files\"in the left toolbar)'\n", |
| 271 | + " )\n", |
| 272 | + " else:\n", |
| 273 | + " raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n", |
| 274 | + "\n", |
| 275 | + " print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n", |
| 276 | + " return\n", |
| 277 | + "\n", |
| 278 | + " format = mimetypes.guess_extension(mime_type).strip(\".\")\n", |
| 279 | + " if mime_type.startswith(\"image/\"):\n", |
| 280 | + " image = IPython.display.Image(**kwargs, width=256)\n", |
| 281 | + " IPython.display.display(image)\n", |
| 282 | + " print()\n", |
| 283 | + " return\n", |
| 284 | + "\n", |
| 285 | + " if mime_type.startswith(\"audio/\"):\n", |
| 286 | + " if len(data) < 2**12:\n", |
| 287 | + " audio = IPython.display.Audio(**kwargs)\n", |
| 288 | + " IPython.display.display(audio)\n", |
| 289 | + " print()\n", |
| 290 | + " return\n", |
| 291 | + "\n", |
| 292 | + " if mime_type.startswith(\"video/\"):\n", |
| 293 | + " if len(data) < 2**12:\n", |
| 294 | + " audio = IPython.display.Video(**kwargs, mimetype=mime_type)\n", |
| 295 | + " IPython.display.display(audio)\n", |
| 296 | + " print()\n", |
| 297 | + " return\n", |
| 298 | + "\n", |
| 299 | + " print(f\"File:\\n name: {name}\\n mime_type: {mime_type}\\n\")\n", |
| 300 | + "\n", |
| 301 | + "\n", |
| 302 | + "for content in gais_contents:\n", |
| 303 | + " if role := content.get(\"role\", None):\n", |
| 304 | + " print(\"Role:\", role, \"\\n\")\n", |
| 305 | + "\n", |
| 306 | + " for n, part in enumerate(content[\"parts\"]):\n", |
| 307 | + " if text := part.get(\"text\", None):\n", |
| 308 | + " print(text, \"\\n\")\n", |
| 309 | + "\n", |
| 310 | + " elif file_data := part.get(\"file_data\", None):\n", |
| 311 | + " show_file(file_data)\n", |
| 312 | + "\n", |
| 313 | + " print(\"-\" * 80, \"\\n\")" |
| 314 | + ] |
| 315 | + } |
| 316 | + ], |
| 317 | + "metadata": { |
| 318 | + "colab": { |
| 319 | + "collapsed_sections": [ |
| 320 | + "Tce3stUlHN0L" |
| 321 | + ], |
| 322 | + "name": "aistudio_gemini_prompt_freeform.ipynb", |
| 323 | + "toc_visible": true |
| 324 | + }, |
| 325 | + "kernelspec": { |
| 326 | + "display_name": "Python 3", |
| 327 | + "name": "python3" |
| 328 | + } |
| 329 | + }, |
| 330 | + "nbformat": 4, |
| 331 | + "nbformat_minor": 0 |
| 332 | +} |
0 commit comments