Created
March 20, 2025 13:56
-
-
Save reddgr/0863ca372bfb99f75d750dfe718760c9 to your computer and use it in GitHub Desktop.
Image generation and multimodal prompts examples with Google AI Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Image generation and multimodal prompt examples for Gemini 2.0 Flash native image generation" | |
], | |
"metadata": { | |
"id": "awTm__zIemBw" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"This notebook contains usage examples of the Gemini API for image generation with Gemini 2.0 Flash shared on Talking to Chatbots: https://talkingtochatbots.com/needs-vs-wants-work-life-thoughts-and-gemini-2-0-flash/\n", | |
"\n", | |
"More info on Google for Developers website: https://developers.googleblog.com/en/experiment-with-gemini-20-flash-native-image-generation/\n", | |
"\n", | |
"Before running this notebook, follow these steps:\n", | |
"\n", | |
"- 1. Get your Gemini API Key on Google AI Studio:\n", | |
"\n", | |
"\n", | |
"- 2. Activate your Gemini API Key on this Colab environment:\n", | |
"\n", | |
"\n" | |
], | |
"metadata": { | |
"id": "kY5DkiQ4fJPm" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## Loading API key and importing libraries" | |
], | |
"metadata": { | |
"id": "l63H7cMTj2il" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "BPY4MZJvedaQ", | |
"outputId": "1a7dfee7-bfc4-4413-e391-039a57013003" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"API key: AIz*********************************mrA\n" | |
] | |
} | |
], | |
"source": [ | |
"# Importing libraries:\n", | |
"from google import genai\n", | |
"from google.genai import types\n", | |
"from PIL import Image\n", | |
"from io import BytesIO\n", | |
"import matplotlib.pyplot as plt\n", | |
"import base64\n", | |
"import urllib.request\n", | |
"from IPython.display import display, HTML, clear_output\n", | |
"\n", | |
"# Setting up and checking the API key:\n", | |
"\n", | |
"COLAB = True # Set this to True for running on Colab\n", | |
"USE_DOTENV = False # Set this to False if you don't have a .env file for storing environment variables\n", | |
"\n", | |
"if COLAB:\n", | |
" USE_DOTENV = False\n", | |
" dotenv_path = None\n", | |
" from google.colab import userdata\n", | |
" gemini_token = userdata.get('GEMINI_TOKEN')\n", | |
"\n", | |
"if not COLAB:\n", | |
" import os\n", | |
" from dotenv import load_dotenv\n", | |
" load_dotenv(\"../../apis/.env\")\n", | |
" gemini_token = os.getenv(\"GEMINI_API_KEY\")\n", | |
"\n", | |
"client = genai.Client(api_key=gemini_token)\n", | |
"def mask_token(token, unmasked_chars=3):\n", | |
" return token[:unmasked_chars] + '*' * (len(token) - unmasked_chars*2) + token[-unmasked_chars:]\n", | |
"print(f\"API key: {mask_token(gemini_token)}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Gy2mqNUJedaR" | |
}, | |
"source": [ | |
"## Zero-shot text-to-image" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "nQduOKEwedaR" | |
}, | |
"source": [ | |
"### Needs vs Wants - no template" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"id": "V54PY_ycedaR" | |
}, | |
"outputs": [], | |
"source": [ | |
"def generate_image(prompt, file_name, save_dir=\".\", temperature=0.5):\n", | |
" response = client.models.generate_content(\n", | |
" model=\"gemini-2.0-flash-exp-image-generation\",\n", | |
" contents=prompt,\n", | |
" config=types.GenerateContentConfig(\n", | |
" response_modalities=['Text', 'Image'],\n", | |
" temperature=temperature,\n", | |
" )\n", | |
" )\n", | |
" for part in response.candidates[0].content.parts:\n", | |
" if part.text is not None:\n", | |
" print(part.text)\n", | |
" elif part.inline_data is not None:\n", | |
" image = Image.open(BytesIO((part.inline_data.data)))\n", | |
" image.save(f'{save_dir}/{file_name}.png')\n", | |
" return response, image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 817 | |
}, | |
"id": "m_x4qIpFedaR", | |
"outputId": "b11678cc-2366-46e3-ee5f-18fbdff157ee" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<PIL.PngImagePlugin.PngImageFile image mode=RGB size=1024x1024>" | |
], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment