Skip to content

Commit 72365eb

Browse files
authored
feat: Nano Banana feature updates (#2381)
1 parent acf950e commit 72365eb

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

gemini/getting-started/intro_gemini_2_5_image_gen.ipynb

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"id": "ZPC2X_a9ErW7"
3030
},
3131
"source": [
32-
"# Gemini 2.5 Flash Image (Nano Banana 🍌) Generation in Vertex AI\n",
32+
"# Gemini 2.5 Flash Image (Nano Banana 🍌) Generation on Vertex AI\n",
3333
"\n",
3434
"<table align=\"left\">\n",
3535
" <td style=\"text-align: center\">\n",
@@ -152,7 +152,7 @@
152152
},
153153
{
154154
"cell_type": "code",
155-
"execution_count": 2,
155+
"execution_count": null,
156156
"metadata": {
157157
"id": "NyKGtVQjgx13"
158158
},
@@ -177,7 +177,7 @@
177177
},
178178
{
179179
"cell_type": "code",
180-
"execution_count": 3,
180+
"execution_count": null,
181181
"metadata": {
182182
"id": "bPd_HnO0AQmL"
183183
},
@@ -188,7 +188,7 @@
188188
"from IPython.display import Image, Markdown, display\n",
189189
"from PIL import Image as PIL_Image\n",
190190
"from google import genai\n",
191-
"from google.genai.types import GenerateContentConfig, Part\n",
191+
"from google.genai.types import FinishReason, GenerateContentConfig, ImageConfig, Part\n",
192192
"import matplotlib.image as img\n",
193193
"import matplotlib.pyplot as plt\n",
194194
"import requests"
@@ -209,7 +209,7 @@
209209
},
210210
{
211211
"cell_type": "code",
212-
"execution_count": 4,
212+
"execution_count": null,
213213
"metadata": {
214214
"id": "u8IivOG5SqY6"
215215
},
@@ -237,13 +237,13 @@
237237
},
238238
{
239239
"cell_type": "code",
240-
"execution_count": 5,
240+
"execution_count": null,
241241
"metadata": {
242242
"id": "7eeb063ac6d4"
243243
},
244244
"outputs": [],
245245
"source": [
246-
"MODEL_ID = \"gemini-2.5-flash-image-preview\""
246+
"MODEL_ID = \"gemini-2.5-flash-image\""
247247
]
248248
},
249249
{
@@ -270,12 +270,10 @@
270270
" - `model`: The ID of the model you want to use.\n",
271271
" - `contents`: This is your prompt, in this case, a text only user message describing the image to be generated.\n",
272272
" - `config`: A config for specifying content settings.\n",
273-
" - `response_modalities`: To generate an image, you must include `IMAGE` in the `response_modalities` list. Note that `IMAGE` cannot be the only value specified; it must be accompanied by `TEXT`. If you omit `IMAGE` from the list, you will only receive a text response.\n",
273+
" - `response_modalities`: To generate an image, you must include `IMAGE` in the `response_modalities` list.\n",
274+
" - `ImageConfig`: Set the `aspect_ratio`. Valid ratios are: \"1:1\", \"3:2\", \"2:3\", \"3:4\", \"4:3\", \"4:5\", \"5:4\", \"9:16\", \"16:9\", \"21:9\"\n",
274275
" - `candidate_count`: The number of candidates to generate.\n",
275-
" - `safety_settings`:\n",
276-
" - `method`: HARM_BLOCK_METHOD_UNSPECIFIED, SEVERITY, PROBABILITY\n",
277-
" - `category`: HARM_CATEGORY_UNSPECIFIED, HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_CIVIC_INTEGRITY\n",
278-
" - `threshold`: HARM_BLOCK_THRESHOLD_UNSPECIFIED, BLOCK_LOW_AND_ABOVE, BLOCK_MEDIUM_AND_ABOVE, BLOCK_ONLY_HIGH, BLOCK_NONE, OFF\n",
276+
"\n",
279277
"\n",
280278
"All generated images include a [SynthID watermark](https://deepmind.google/technologies/synthid/), which can be verified via the Media Studio in [Vertex AI Studio](https://cloud.google.com/generative-ai-studio?hl=en)."
281279
]
@@ -292,21 +290,25 @@
292290
" model=MODEL_ID,\n",
293291
" contents=\"a cartoon infographic on flying sneakers\",\n",
294292
" config=GenerateContentConfig(\n",
295-
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
293+
" response_modalities=[\"IMAGE\"],\n",
294+
" image_config=ImageConfig(\n",
295+
" aspect_ratio=\"9:16\",\n",
296+
" ),\n",
296297
" candidate_count=1,\n",
297-
" safety_settings=[\n",
298-
" {\"method\": \"PROBABILITY\"},\n",
299-
" {\"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\"},\n",
300-
" {\"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"},\n",
301-
" ],\n",
302298
" ),\n",
303299
")\n",
304300
"\n",
301+
"# Check for errors if an image is not generated\n",
302+
"if response.candidates[0].finish_reason != FinishReason.STOP:\n",
303+
" reason = response.candidates[0].finish_reason\n",
304+
" raise ValueError(f\"Prompt Content Error: {reason}\")\n",
305+
"\n",
306+
"\n",
305307
"for part in response.candidates[0].content.parts:\n",
306308
" if part.text:\n",
307309
" display(Markdown(part.text))\n",
308310
" if part.inline_data:\n",
309-
" display(Image(data=part.inline_data.data, width=400))"
311+
" display(Image(data=part.inline_data.data, width=500))"
310312
]
311313
},
312314
{
@@ -323,7 +325,7 @@
323325
"\n",
324326
"Let's try out the interleaved text and image functionality by prompting Gemini 2.5 Flash Image to create a tutorial for assembling a peanut butter and jelly sandwich.\n",
325327
"\n",
326-
"You'll notice that in the prompt we ask the model to generate both text and images for each step. This encourages the model to create interleaved text and images."
328+
"You'll notice that in the prompt we ask the model to generate both text and images for each step. This encourages the model to create interleaved text and images. Also, make sure to set `response_modalities=[\"TEXT\", \"IMAGE\"]` so you'll get both modalities in the response."
327329
]
328330
},
329331
{
@@ -339,19 +341,17 @@
339341
" contents=\"Create a tutorial explaining how to make a peanut butter and jelly sandwich in three easy steps. For each step, provide a title with the number of the step, an explanation, and also generate an image to illustrate the content. Label each image with the step number but no other words.\",\n",
340342
" config=GenerateContentConfig(\n",
341343
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
342-
" safety_settings=[\n",
343-
" {\"method\": \"PROBABILITY\"},\n",
344-
" {\"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\"},\n",
345-
" {\"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"},\n",
346-
" ],\n",
344+
" image_config=ImageConfig(\n",
345+
" aspect_ratio=\"4:3\",\n",
346+
" ),\n",
347347
" ),\n",
348348
")\n",
349349
"\n",
350350
"for part in response.candidates[0].content.parts:\n",
351351
" if part.text:\n",
352352
" display(Markdown(part.text))\n",
353353
" if part.inline_data:\n",
354-
" display(Image(data=part.inline_data.data, width=400))"
354+
" display(Image(data=part.inline_data.data, width=500))"
355355
]
356356
},
357357
{
@@ -366,7 +366,7 @@
366366
"\n",
367367
"### Subject customization\n",
368368
"\n",
369-
"Let's try out a subject customization example by asking Gemini 2.5 Flash Image to create an image of this dog in both a pencil sketch and watercolor style.\n"
369+
"Let's try out a subject customization example by asking Gemini 2.5 Flash Image to create an image of this dog in a pencil sketch style.\n"
370370
]
371371
},
372372
{
@@ -437,24 +437,22 @@
437437
" data=image,\n",
438438
" mime_type=\"image/jpeg\",\n",
439439
" ),\n",
440-
" \"Create a pencil sketch image of this dog wearing a cowboy hat in a western-themed setting. Generate another image of this dog in a watercolor style floating down a river on a paddle board.\",\n",
440+
" \"Create a pencil sketch image of this dog wearing a cowboy hat in a western-themed setting.\",\n",
441441
" ],\n",
442442
" config=GenerateContentConfig(\n",
443-
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
443+
" response_modalities=[\"IMAGE\"],\n",
444+
" image_config=ImageConfig(\n",
445+
" aspect_ratio=\"1:1\",\n",
446+
" ),\n",
444447
" candidate_count=1,\n",
445-
" safety_settings=[\n",
446-
" {\"method\": \"PROBABILITY\"},\n",
447-
" {\"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\"},\n",
448-
" {\"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"},\n",
449-
" ],\n",
450448
" ),\n",
451449
")\n",
452450
"\n",
453451
"for part in response.candidates[0].content.parts:\n",
454452
" if part.text:\n",
455453
" display(Markdown(part.text))\n",
456454
" if part.inline_data:\n",
457-
" display(Image(data=part.inline_data.data, width=400))"
455+
" display(Image(data=part.inline_data.data, width=500))"
458456
]
459457
},
460458
{
@@ -537,21 +535,19 @@
537535
" \"Using the concepts, colors, and themes from this living room generate a kitchen and dining room with the same aesthetic.\",\n",
538536
" ],\n",
539537
" config=GenerateContentConfig(\n",
540-
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
538+
" response_modalities=[\"IMAGE\"],\n",
539+
" image_config=ImageConfig(\n",
540+
" aspect_ratio=\"21:9\",\n",
541+
" ),\n",
541542
" candidate_count=1,\n",
542-
" safety_settings=[\n",
543-
" {\"method\": \"PROBABILITY\"},\n",
544-
" {\"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\"},\n",
545-
" {\"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"},\n",
546-
" ],\n",
547543
" ),\n",
548544
")\n",
549545
"\n",
550546
"for part in response.candidates[0].content.parts:\n",
551547
" if part.text:\n",
552548
" display(Markdown(part.text))\n",
553549
" if part.inline_data:\n",
554-
" display(Image(data=part.inline_data.data, width=400))"
550+
" display(Image(data=part.inline_data.data, width=500))"
555551
]
556552
},
557553
{
@@ -621,7 +617,10 @@
621617
" \"change the perfume color to a light purple\",\n",
622618
" ],\n",
623619
" config=GenerateContentConfig(\n",
624-
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
620+
" response_modalities=[\"IMAGE\"],\n",
621+
" image_config=ImageConfig(\n",
622+
" aspect_ratio=\"3:2\",\n",
623+
" ),\n",
625624
" ),\n",
626625
")\n",
627626
"\n",
@@ -630,7 +629,7 @@
630629
" if part.text:\n",
631630
" display(Markdown(part.text))\n",
632631
" if part.inline_data:\n",
633-
" display(Image(data=part.inline_data.data, width=400))\n",
632+
" display(Image(data=part.inline_data.data, width=500))\n",
634633
" data = part.inline_data.data"
635634
]
636635
},
@@ -660,15 +659,18 @@
660659
" \"inscribe the word flowers in French on the perfume bottle in a delicate white cursive font\",\n",
661660
" ],\n",
662661
" config=GenerateContentConfig(\n",
663-
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
662+
" response_modalities=[\"IMAGE\"],\n",
663+
" image_config=ImageConfig(\n",
664+
" aspect_ratio=\"3:2\",\n",
665+
" ),\n",
664666
" ),\n",
665667
")\n",
666668
"\n",
667669
"for part in response.candidates[0].content.parts:\n",
668670
" if part.text:\n",
669671
" display(Markdown(part.text))\n",
670672
" if part.inline_data:\n",
671-
" display(Image(data=part.inline_data.data, width=400))"
673+
" display(Image(data=part.inline_data.data, width=500))"
672674
]
673675
},
674676
{
@@ -742,12 +744,10 @@
742744
" ],\n",
743745
" config=GenerateContentConfig(\n",
744746
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
747+
" image_config=ImageConfig(\n",
748+
" aspect_ratio=\"9:16\",\n",
749+
" ),\n",
745750
" candidate_count=1,\n",
746-
" safety_settings=[\n",
747-
" {\"method\": \"PROBABILITY\"},\n",
748-
" {\"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\"},\n",
749-
" {\"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"},\n",
750-
" ],\n",
751751
" ),\n",
752752
")\n",
753753
"\n",
@@ -756,7 +756,7 @@
756756
" if part.text:\n",
757757
" display(Markdown(part.text))\n",
758758
" if part.inline_data:\n",
759-
" display(Image(data=part.inline_data.data, width=400))"
759+
" display(Image(data=part.inline_data.data, width=500))"
760760
]
761761
}
762762
],

0 commit comments

Comments
 (0)