{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ur8xi4C7S06n"
      },
      "outputs": [],
      "source": [
        "# Copyright 2024 Google LLC\n",
        "#\n",
        "# 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": "JAPoU8Sm5E6e"
      },
      "source": [
        "# Creative Content Generation with Gemini in Vertex AI and Imagen\n",
        "\n",
        "<table align=\"left\">\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\" alt=\"Google Colaboratory logo\"><br> Run in Colab\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fuse-cases%2Fmarketing%2Fcreative_content_generation.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Run in Colab Enterprise\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://raw.githubusercontent.com/primer/octicons/refs/heads/main/icons/mark-github-24.svg\" alt=\"GitHub logo\"><br> View on GitHub\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/use-cases/marketing/creative_content_generation.ipynb\">\n",
        "      <img src=\"https://www.gstatic.com/images/branding/gcpiconscolors/vertexai/v1/32px.svg\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
        "    </a>\n",
        "  </td>\n",
        "</table>\n",
        "\n",
        "<div style=\"clear: both;\"></div>\n",
        "\n",
        "<b>Share to:</b>\n",
        "\n",
        "<a href=\"https://www.linkedin.com/sharing/share-offsite/?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/8/81/LinkedIn_icon.svg\" alt=\"LinkedIn logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://bsky.app/intent/compose?text=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7a/Bluesky_Logo.svg\" alt=\"Bluesky logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://twitter.com/intent/tweet?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/5a/X_icon_2.svg\" alt=\"X logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://reddit.com/submit?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://redditinc.com/hubfs/Reddit%20Inc/Brand/Reddit_Logo.png\" alt=\"Reddit logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/use-cases/marketing/creative_content_generation.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\" alt=\"Facebook logo\">\n",
        "</a>            "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9P2TcUplDsBV"
      },
      "source": [
        "| | |\n",
        "|-|-|\n",
        "|Author(s) | [Thu Ya Kyaw](https://github.com/iamthuya)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "24743cf4a1e1"
      },
      "source": [
        "**_NOTE_**: This notebook has been tested in the following environment:\n",
        "\n",
        "* python - 3.11\n",
        "* google-cloud-aiplatform - 1.54.0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tvgnzT1CKxrO"
      },
      "source": [
        "## Overview\n",
        "Generative AI is a game-changer for creative professionals in marketing and advertising. It can quickly create eye-catching visuals, write persuasive ad copy, and even come up with fresh ideas for campaigns.\n",
        "\n",
        "Generative AI helps create targeted campaigns with visuals and copy that can adapt to different audiences and platforms. It can also provide personalized customer experiences by analyzing data to tailor ads to specific people, making them more likely to engage and buy. It can also automate tasks like ideation and optimizing content, saving time and effort.\n",
        "\n",
        "In this notebook, you will be exploring how to harness the power of Generative AI do perform aforementioned tasks.\n",
        "\n",
        "### Allowlisting\n",
        "\n",
        "Imagen's features in this sample notebook requires users to be allowlisted. To request access to use this Imagen feature, fill out the [Imagen on Vertex AI access request form](https://docs.google.com/forms/d/1cqt9padvfMgqn23W5FMPTqh7bW1KLkEOsC5G6uC-uuM/viewform)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "d975e698c9a4"
      },
      "source": [
        "### Objective\n",
        "\n",
        "In this tutorial, you learn how to:\n",
        "\n",
        "- Generate creative content from keywords\n",
        "- Personalize generated contents for various audience\n",
        "- Identify the most suitable visual assets\n",
        "- Create visual assets to supercharge the campaign messaging\n",
        "\n",
        "This tutorial uses the following Google Cloud AI services and resources:\n",
        "\n",
        "- Gemini API in Vertex AI\n",
        "- Imagen on Vertex AI\n",
        "\n",
        "The step performed includes:\n",
        "- Generating content using product name, product description, and product image\n",
        "- Product social media post for various platforms such as Facebook, Instagram, LinkedIn, and Twitter\n",
        "- Personalize generated content for various professions, different generations, and different languages\n",
        "- Use outpainting feature from Imagen to generate images of different aspect ratios"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BF1j6f9HApxa"
      },
      "source": [
        "## Before you begin\n",
        "\n",
        "### Set up your Google Cloud project\n",
        "\n",
        "**The following steps are required, regardless of your notebook environment.**\n",
        "\n",
        "1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
        "1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
        "1. Enable the [Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
        "1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i7EUnXsZhAGF"
      },
      "source": [
        "### Installation\n",
        "\n",
        "Install the following packages required to execute this notebook."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2b4ef9b72d43"
      },
      "outputs": [],
      "source": [
        "%pip install --upgrade --user --quiet google-cloud-aiplatform"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sBCra4QMA2wR"
      },
      "source": [
        "### Authenticate your Google Cloud account\n",
        "\n",
        "If you are running this notebook on Google Colab, you will need to authenticate your environment. To do this, run the new cell below. This step is not required if you are using Vertex AI Workbench."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "603adbbf0532"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "\n",
        "if \"google.colab\" in sys.modules:\n",
        "    # Authenticate user to Google Cloud\n",
        "    from google.colab import auth\n",
        "\n",
        "    auth.authenticate_user()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WReHDGG5g0XY"
      },
      "source": [
        "### Set Google Cloud project information and initialize Vertex AI SDK\n",
        "\n",
        "To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
        "\n",
        "Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment).\n",
        "\n",
        "**If you don't know your project ID**, try the following:\n",
        "* Run `gcloud config list`.\n",
        "* Run `gcloud projects list`.\n",
        "* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)\n",
        "\n",
        "You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "oM1iC_MfAts1"
      },
      "outputs": [],
      "source": [
        "# TODO (Developer): Replace with your project ID\n",
        "\n",
        "PROJECT_ID = \"YOUR_PROJECT_ID\"  # @param {type:\"string\"}\n",
        "REGION = \"us-central1\"  # @param {type: \"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uA1PVor3E83R"
      },
      "source": [
        "Initialize the Vertex AI Python SDK for your project:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "init_aip:mbsdk,all"
      },
      "outputs": [],
      "source": [
        "import vertexai\n",
        "\n",
        "vertexai.init(project=PROJECT_ID, location=REGION)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "960505627ddf"
      },
      "source": [
        "### Import libraries"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "PyQmSRbKA8r-"
      },
      "outputs": [],
      "source": [
        "from IPython.display import Markdown, display\n",
        "from vertexai.generative_models import GenerativeModel, Part"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9qI-gI-EDsBY"
      },
      "source": [
        "Initialize the Gemini model from Vertex AI:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "dqdpsB2lDsBY"
      },
      "outputs": [],
      "source": [
        "model = GenerativeModel(\"gemini-2.0-flash\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0O7HQPWXcbzk"
      },
      "source": [
        "## Content generation\n",
        "\n",
        "In this section, you will generate a few social media posts for a new product called **GShoe**."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yF5vDI4egkGt"
      },
      "source": [
        "### Generate with product name\n",
        "\n",
        "Try generating with just a product name."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "rF7wbZwCef2M"
      },
      "outputs": [],
      "source": [
        "product_name = \"GShoe\"\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Generate a few social media posts about a new product, {product_name}\n",
        "\"\"\"\n",
        "\n",
        "print(f\"Prompt: {prompt}\")\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1P0R8bIDGVRR"
      },
      "source": [
        "### Generate with product description\n",
        "\n",
        "Adding a product description can further improve the output quality."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "JvQqIItAfLj3"
      },
      "outputs": [],
      "source": [
        "product_description = \"\"\"\n",
        "  GShoe's design is focused on comfort, durability, and style. It includes the following features:\n",
        "  - Breathable and flexible upper: GShoe's upper is made from breathable and flexible materials to allow for maximum comfort and movement. It's designed to accommodate a range of foot shapes and sizes and provide adequate support and stability.\n",
        "  - Cushioned insole: GShoe's insole is cushioned to provide shock absorption and support. It helps reduce foot fatigue and prevent injuries caused by impact and stress.\n",
        "  - Durable outsole: GShoe's outsole is made from durable rubber to provide traction and protection. It's designed to withstand the wear and tear of daily use and provide grip on a variety of surfaces.\n",
        "  - Stylish design: GShoe's design is sleek and modern, with a range of color and style options to suit any taste. It's designed to look good both on and off the track and complement a variety of workout gear.\n",
        "\"\"\"\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Generate a few social media posts about a new product, {product_name}.\\n\n",
        "  This is the product description: {product_description}\n",
        "\"\"\"\n",
        "\n",
        "print(f\"Prompt: {prompt}\")\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sfMw20zjGBLy"
      },
      "source": [
        "### Generate with product image\n",
        "\n",
        "Since Gemini is [multimodal](https://cloud.google.com/use-cases/multimodal-ai?hl=en), you can also provide a visual asset (e.g. a product image) to generate marketing messages based on that. Here you will generate marketing messages for based on this product image:\n",
        "\n",
        "<a href=\"https://storage.googleapis.com/github-repo/use-cases/marketing/gshoe-images/gshoe-01.jpg\"><img src=\"https://storage.googleapis.com/github-repo/use-cases/marketing/gshoe-images/gshoe-01.jpg\" width=\"200\" height=\"200\" /></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "HyPfbvoGkNUv"
      },
      "outputs": [],
      "source": [
        "# prepare product image\n",
        "\n",
        "product_image_url = \"https://storage.googleapis.com/github-repo/use-cases/marketing/gshoe-images/gshoe-01.jpg\"\n",
        "product_image_gcs_uri = \"gs://github-repo/use-cases/marketing/gshoe-images/gshoe-01.jpg\"\n",
        "product_image_markdown = f\"<img src='{product_image_url}' width='200' height='200' />\"\n",
        "\n",
        "product_image = Part.from_uri(\n",
        "    product_image_gcs_uri,\n",
        "    mime_type=\"image/jpeg\",\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "KRyZNel4N6tA"
      },
      "outputs": [],
      "source": [
        "prompt = f\"\"\"\n",
        "  Generate a few social media posts about a new product, {product_name}.\\n\n",
        "  This is the product description: {product_description}\n",
        "  This is the product image: \"\"\"\n",
        "\n",
        "content = [prompt, product_image]\n",
        "\n",
        "print(f\"Prompt: {prompt}\")\n",
        "display(Markdown(product_image_markdown))\n",
        "\n",
        "display(Markdown(model.generate_content(content).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Cceuou9XGON4"
      },
      "source": [
        "### Generate for various platforms\n",
        "\n",
        "So far, the generated posts are too general. Fortunately, with Gemini, you can target for specific platforms. Here you will generate the messages targeted to various platforms."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "hgcIL6DdfLmR"
      },
      "outputs": [],
      "source": [
        "platforms = \"LinkedIn, Facebook, Twitter, Instagram\"\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Generate a few social media posts about a new product, {product_name}\n",
        "  for platforms: '{platforms}'\\n\n",
        "  This is the product description: {product_description}\n",
        "  This is the product image: \"\"\"\n",
        "\n",
        "content = [prompt, product_image]\n",
        "\n",
        "print(f\"Prompt: {prompt}\")\n",
        "display(Markdown(product_image_markdown))\n",
        "\n",
        "social_media_posts = model.generate_content(content).text\n",
        "display(Markdown(social_media_posts))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jGQoSAw0cbez"
      },
      "source": [
        "## Content personalization\n",
        "\n",
        "In this section, you will use Gemini model to produce the marketing messages for a specific target segment or a customer profile"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bQ3IaMF8JqLI"
      },
      "source": [
        "### Generate for various professions\n",
        "\n",
        "Here you will generate for students and working professionals. Observe the responses to see how the messaging has changed to fit the audience profile."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "oXXCZ9ehd0IJ"
      },
      "outputs": [],
      "source": [
        "# make it suitable for students\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts suitable for students\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "fw9CXRNHgmOI"
      },
      "outputs": [],
      "source": [
        "# make it suitable for working adults\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts suitable for working adults\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "qQsFC_H-l_PT"
      },
      "outputs": [],
      "source": [
        "# make it suitable for retiree\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts suitable for retiree\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ea3jt7e_Jvbx"
      },
      "source": [
        "### Generate for different generations\n",
        "\n",
        "Here you will generate the marketing messaging for millennial and baby boomer generations. Observe how Gemini is able to make the responses more relevant to the target audience."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "e4pvTiE6gl5Z"
      },
      "outputs": [],
      "source": [
        "# make the posts appealing to millennial generation\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts appealing to millennial generation\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "VqCotF0ujblz"
      },
      "outputs": [],
      "source": [
        "# make the posts appealing to baby boomer generation\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts appealing to baby boomer generation\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zVD1XhmZw0Pi"
      },
      "source": [
        "### Generate for different countries and languages"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "Negz0qW0xGI3"
      },
      "outputs": [],
      "source": [
        "# make it suitable for Indonesia\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts suitable for Indonesia\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "jjDxLL1nnP9t"
      },
      "outputs": [],
      "source": [
        "# make it suitable for Japanese-speaking audience\n",
        "\n",
        "prompt = f\"\"\"\n",
        "  Reference to these social media posts: \\n{social_media_posts}\n",
        "  Make the posts suitable for Japanese speaking audience\n",
        "  \"\"\"\n",
        "\n",
        "display(Markdown(model.generate_content(prompt).text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AzOLznmmc5Oa"
      },
      "source": [
        "## Creating visual asset for various platforms\n",
        "\n",
        "You can also create more visual assets for various platform using Imagen. For example, you might want to use 16:9 aspect ratio image instead of 1:1 aspect ratio image that you just used. Here you will use [outpainting feature](https://cloud.google.com/vertex-ai/generative-ai/docs/image/edit-outpainting#generativeaionvertexai_imagen_edit_image_outpainting_mask-python_vertex_ai_sdk) from Imagen to expend the 1:1 aspect ratio to 16:9.\n",
        "\n",
        "\n",
        "Note: To complete the following part, you need to be allowlisted for Imagen's features. To request access to use the Imagen features, fill out the [Imagen on Vertex AI access request form](https://docs.google.com/forms/d/1cqt9padvfMgqn23W5FMPTqh7bW1KLkEOsC5G6uC-uuM/viewform)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "KDhoiWRkFwEm"
      },
      "outputs": [],
      "source": [
        "# initialize and select image generation model\n",
        "\n",
        "from vertexai.preview.vision_models import Image, ImageGenerationModel\n",
        "\n",
        "imagen_model = ImageGenerationModel.from_pretrained(\"imagegeneration@006\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "iO8E3UTlv2vz"
      },
      "outputs": [],
      "source": [
        "import shutil\n",
        "\n",
        "# helper function to download image from a URL\n",
        "import requests\n",
        "\n",
        "def download_image(image_url, save_path):\n",
        "    \"\"\"Downloads an image from a URL and saves it to the specified path.\"\"\"\n",
        "    try:\n",
        "        response = requests.get(\n",
        "            image_url, stream=True\n",
        "        )  # Stream for efficient downloading\n",
        "        response.raise_for_status()  # Check for HTTP errors\n",
        "\n",
        "        with open(save_path, \"wb\") as file:\n",
        "            shutil.copyfileobj(response.raw, file)\n",
        "\n",
        "    except requests.exceptions.RequestException as error:\n",
        "        print(f\"Error downloading image: {error}\")\n",
        "\n",
        "\n",
        "from PIL import Image as PILImage\n",
        "\n",
        "# helper function to expend the image to 16:9 ratio\n",
        "\n",
        "\n",
        "def make_16_9_image_and_mask(image_path):\n",
        "    \"\"\"Expands a 1:1 aspect ratio image into 16:9\"\"\"\n",
        "    with PILImage.open(image_path) as img:\n",
        "        width, height = img.size\n",
        "\n",
        "        # ensure input image is 1:1 aspect ratio\n",
        "        if width != height:\n",
        "            raise ValueError(\"Image is not 1:1 aspect ratio\")\n",
        "\n",
        "        # Calculate the new dimensions\n",
        "        new_width = width * 16 // 9\n",
        "        pad_width = (new_width - width) // 2  # Padding on each side\n",
        "\n",
        "        # Create a new black image with the 16:9 dimensions\n",
        "        new_img = PILImage.new(\"RGB\", (new_width, height), (0, 0, 0))\n",
        "\n",
        "        # Paste the original image in the center\n",
        "        new_img.paste(img, (pad_width, 0))\n",
        "\n",
        "        # Save the expanded image\n",
        "        output_img_path = f\"{image_path.split('.')[0]}_16_9.jpg\"\n",
        "        new_img.save(output_img_path)\n",
        "\n",
        "        # Create a new white mask with the 16:9 dimensions\n",
        "        white_mask = PILImage.new(\"RGB\", (new_width, height), (255, 255, 255))\n",
        "\n",
        "        # Paste the black mask with original image's dimensions in the center\n",
        "        black_mask = PILImage.new(\"RGB\", (width, height), (0, 0, 0))\n",
        "        white_mask.paste(black_mask, (pad_width, 0))\n",
        "\n",
        "        # Save the expanded image\n",
        "        output_mask_path = f\"{image_path.split('.')[0]}_16_9_mask.jpg\"\n",
        "        white_mask.save(output_mask_path)\n",
        "\n",
        "        return output_img_path, output_mask_path"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "id": "wf58dw9Kvx9u"
      },
      "outputs": [],
      "source": [
        "# download and prepare the image and its mask\n",
        "\n",
        "\n",
        "gshoe_image_url = \"https://storage.googleapis.com/github-repo/use-cases/marketing/gshoe-images/gshoe-01.jpg\"\n",
        "gshoe_image_path = \"gshoe-01.jpg\"  # Choose where to save the image\n",
        "download_image(gshoe_image_url, gshoe_image_path)\n",
        "\n",
        "image_path, mask_path = make_16_9_image_and_mask(gshoe_image_path)\n",
        "base_img = Image.load_from_file(location=image_path)\n",
        "mask_img = Image.load_from_file(location=mask_path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "id": "sq4fQXuydwAD"
      },
      "outputs": [],
      "source": [
        "# show base image\n",
        "\n",
        "base_img.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "id": "7AhL-ivvv0ie"
      },
      "outputs": [],
      "source": [
        "# expend to 16:9 using Imagen's outpainting feature\n",
        "\n",
        "images = imagen_model.edit_image(\n",
        "    base_image=base_img,\n",
        "    mask=mask_img,\n",
        "    edit_mode=\"outpainting\",\n",
        "    prompt=\"\",\n",
        ")\n",
        "\n",
        "images[0].show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 58,
      "metadata": {
        "id": "kXQfMOV-3WAw"
      },
      "outputs": [],
      "source": [
        "# expend to 16:9 using Imagen with a modifier prompt\n",
        "\n",
        "images = imagen_model.edit_image(\n",
        "    base_image=base_img,\n",
        "    mask=mask_img,\n",
        "    edit_mode=\"outpainting\",\n",
        "    prompt=\"a shoe surround by flowers\",\n",
        ")\n",
        "\n",
        "images[0].show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TpV-iwP9qw9c"
      },
      "source": [
        "## Cleaning up\n",
        "\n",
        "To avoid incurring charges to your Google Cloud account for the resources used in this notebook, follow these steps:\n",
        "\n",
        "1. To avoid unnecessary Google Cloud charges, use the [Google Cloud console](https://console.cloud.google.com/) to delete your project if you do not need it. Learn more in the Google Cloud documentation for [managing and deleting your project](https://cloud.google.com/resource-manager/docs/creating-managing-projects).\n",
        "1. If you used an existing Google Cloud project, delete the resources you created to avoid incurring charges to your account.\n",
        "1. Disable the [Vertex AI API](https://console.cloud.google.com/apis/api/aiplatform.googleapis.com) in the Google Cloud Console."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "creative_content_generation.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
