From 0f8d0865336f7d878b2f2ab7fc966d618cb712f8 Mon Sep 17 00:00:00 2001
From: myHerb <137535445+myHerbDev@users.noreply.github.com>
Date: Mon, 27 Jan 2025 08:40:21 +0200
Subject: [PATCH 1/2] Created using Colab
---
aistudio_gemini_prompt_freeform.ipynb | 344 ++++++++++++++++++++++++++
1 file changed, 344 insertions(+)
create mode 100644 aistudio_gemini_prompt_freeform.ipynb
diff --git a/aistudio_gemini_prompt_freeform.ipynb b/aistudio_gemini_prompt_freeform.ipynb
new file mode 100644
index 00000000..fd2998e0
--- /dev/null
+++ b/aistudio_gemini_prompt_freeform.ipynb
@@ -0,0 +1,344 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ ""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "Tce3stUlHN0L"
+ },
+ "source": [
+ "##### Copyright 2023 Google LLC"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "cellView": "form",
+ "id": "tuOe1ymfHZPu"
+ },
+ "outputs": [],
+ "source": [
+ "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
+ "# you may not use this file except in compliance with the License.\n",
+ "# You may obtain a copy of the License at\n",
+ "#\n",
+ "# https://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing, software\n",
+ "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
+ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
+ "# See the License for the specific language governing permissions and\n",
+ "# limitations under the License."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "FKwyTRdwB8aW"
+ },
+ "source": [
+ "## Setup"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "RXInneX6xx7c"
+ },
+ "outputs": [],
+ "source": [
+ "!pip install -U -q \"google-generativeai>=0.8.2\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "cellView": "form",
+ "id": "kWIuwKG2_oWE"
+ },
+ "outputs": [],
+ "source": [
+ "# import necessary modules.\n",
+ "import base64\n",
+ "import copy\n",
+ "import json\n",
+ "import pathlib\n",
+ "import requests\n",
+ "\n",
+ "\n",
+ "import PIL.Image\n",
+ "import IPython.display\n",
+ "from IPython.display import Markdown\n",
+ "\n",
+ "try:\n",
+ " # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n",
+ " # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n",
+ " import os\n",
+ " from google.colab import userdata\n",
+ "\n",
+ " os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n",
+ "except ImportError:\n",
+ " pass\n",
+ "\n",
+ "import google.generativeai as genai\n",
+ "\n",
+ "# Parse the arguments\n",
+ "\n",
+ "model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n",
+ "contents_b64 = \"W3sicGFydHMiOiBbeyJ0ZXh0IjogIldoYXQncyBpbiB0aGlzIHBpY3R1cmU/In0sIHsiZmlsZV9kYXRhIjogeyJ1cmwiOiAiaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL2dlbmVyYXRpdmVhaS1kb3dubG9hZHMvaW1hZ2VzL3Njb25lcy5qcGciLCAibWltZV90eXBlIjogImltYWdlL2pwZWcifX1dfV0=\" # @param {isTemplate: true}\n",
+ "generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n",
+ "safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n",
+ "\n",
+ "gais_contents = json.loads(base64.b64decode(contents_b64))\n",
+ "\n",
+ "generation_config = json.loads(base64.b64decode(generation_config_b64))\n",
+ "safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n",
+ "\n",
+ "stream = False\n",
+ "\n",
+ "# Convert and upload the files\n",
+ "\n",
+ "tempfiles = pathlib.Path(f\"tempfiles\")\n",
+ "tempfiles.mkdir(parents=True, exist_ok=True)\n",
+ "\n",
+ "\n",
+ "drive = None\n",
+ "def upload_file_data(file_data, index):\n",
+ " \"\"\"Upload files to the Files API.\n",
+ "\n",
+ " For each file, Google AI Studio either sent:\n",
+ " - a Google Drive ID,\n",
+ " - a URL,\n",
+ " - a file path, or\n",
+ " - The raw bytes (`inline_data`).\n",
+ "\n",
+ " The API only understands `inline_data` or it's Files API.\n",
+ " This code, uploads files to the files API where the API can access them.\n",
+ " \"\"\"\n",
+ "\n",
+ " mime_type = file_data[\"mime_type\"]\n",
+ " if drive_id := file_data.pop(\"drive_id\", None):\n",
+ " if drive is None:\n",
+ " from google.colab import drive\n",
+ " drive.mount(\"/gdrive\")\n",
+ "\n",
+ " path = next(\n",
+ " pathlib.Path(f\"/gdrive/.shortcut-targets-by-id/{drive_id}\").glob(\"*\")\n",
+ " )\n",
+ " print(\"Uploading:\", str(path))\n",
+ " file_info = genai.upload_file(path=path, mime_type=mime_type)\n",
+ " file_data[\"file_uri\"] = file_info.uri\n",
+ " return\n",
+ "\n",
+ " if url := file_data.pop(\"url\", None):\n",
+ " response = requests.get(url)\n",
+ " data = response.content\n",
+ " name = url.split(\"/\")[-1]\n",
+ " path = tempfiles / str(index)\n",
+ " path.write_bytes(data)\n",
+ " print(\"Uploading:\", url)\n",
+ " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n",
+ " file_data[\"file_uri\"] = file_info.uri\n",
+ " return\n",
+ "\n",
+ " if name := file_data.get(\"filename\", None):\n",
+ " if not pathlib.Path(name).exists():\n",
+ " raise IOError(\n",
+ " f\"local file: `{name}` does not exist. You can upload files \"\n",
+ " 'to Colab using the file manager (\"📁 Files\" in the left '\n",
+ " \"toolbar)\"\n",
+ " )\n",
+ " file_info = genai.upload_file(path, display_name=name, mime_type=mime_type)\n",
+ " file_data[\"file_uri\"] = file_info.uri\n",
+ " return\n",
+ "\n",
+ " if \"inline_data\" in file_data:\n",
+ " return\n",
+ "\n",
+ " raise ValueError(\"Either `drive_id`, `url` or `inline_data` must be provided.\")\n",
+ "\n",
+ "\n",
+ "contents = copy.deepcopy(gais_contents)\n",
+ "\n",
+ "index = 0\n",
+ "for content in contents:\n",
+ " for n, part in enumerate(content[\"parts\"]):\n",
+ " if file_data := part.get(\"file_data\", None):\n",
+ " upload_file_data(file_data, index)\n",
+ " index += 1\n",
+ "\n",
+ "import json\n",
+ "print(json.dumps(contents, indent=4))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "E7zAD69vE92b"
+ },
+ "source": [
+ "## Call `generate_content`"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "LB2LxPmAB95V"
+ },
+ "outputs": [],
+ "source": [
+ "from IPython.display import display\n",
+ "from IPython.display import Markdown\n",
+ "\n",
+ "# Call the model and print the response.\n",
+ "gemini = genai.GenerativeModel(model_name=model)\n",
+ "\n",
+ "response = gemini.generate_content(\n",
+ " contents,\n",
+ " generation_config=generation_config,\n",
+ " safety_settings=safety_settings,\n",
+ " stream=stream,\n",
+ ")\n",
+ "\n",
+ "display(Markdown(response.text))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "9c9d345e9868"
+ },
+ "source": [
+ "
\n",
+ " Docs on ai.google.dev\n",
+ " | \n",
+ " \n",
+ " More notebooks in the Cookbook\n",
+ " | \n",
+ "