Build an AI Image Generator app on Vertex AI
Lab
10 годин
universal_currency_alt
No cost
show_chart
Початковий
info
This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.
bb-ide-genai-002

Overview
- Labs are timed and cannot be paused. The timer starts when you click Start Lab.
- The included IDE is preconfigured with the gcloud SDK.
- Use the terminal to execute commands and then click Check my progress to verify your work.
Objective
Generative AI on Vertex AI (also known as genAI or gen AI) gives you access to Google's large generative AI models so you can test, tune, and deploy them for use in your AI-powered applications. In this lab, you will:
-
Connect to Vertex AI (Google Cloud AI platform): Learn how to establish a connection to Google's AI services using the Vertex AI SDK.
-
Use the AI Model: Discover how to use a powerful AI model without building one from scratch.
-
Send text to the AI model: Understand how to provide input for the AI to process.
-
Extract image-based answers from the AI: Learn to handle and interpret the image responses generated by the AI model.
-
Understand the basics of building AI applications: Gain insights into the core concepts of integrating AI into software projects.
Working with Generative AI
After starting the lab, you will get a split pane view consisting of the Code Editor on the left side and the lab instructions on the right side. Follow these steps to interact with the Generative AI APIs using Vertex AI Python SDK.
- Click File > New File to open a new file within the Code Editor.
- Copy and paste the provided code snippet into your file.
from google import genai
from google.genai import types
from PIL import Image
from google.genai.types import HttpOptions, ModelContent, Part, UserContent
from google.cloud import logging as gcp_logging
# ------ Below cloud logging code is for Qwiklab's internal use, do not edit/remove it. --------
# Initialize Google Cloud logging
gcp_logging_client = gcp_logging.Client()
gcp_logging_client.setup_logging()
client = genai.Client(
vertexai=True,
project='{{{ project_0.project_id | "project-id" }}}',
location='{{{ project_0.default_region | "REGION" }}}',
http_options=HttpOptions(api_version="v1")
)
prompt = (
"Create an image of a cricket ground in the heart of Los Angeles",
)
response = client.models.generate_content(
model="{{{project_0.startup_script.gemini_flash_image_model_id | flash-image-model-id}}}",
contents=[prompt],
)
for part in response.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
image = part.as_image()
image.save("image.jpeg")
- Click File > Save, enter
GenerateImage.py for the Name field and click Save.
- Execute the Python file by clicking the triangle icon on the Code Editor or by invoking the below command inside the terminal within the Code Editor pane. This will generate an image file with name image.jpeg.
/usr/bin/python3 /GenerateImage.py
Note: You can ignore any warnings related to Python version dependencies.
- Now to view the generated image, Click EXPLORER > image.jpeg
Code Explanation
- The code snippet is loading an AI model () on Vertex AI.
- The code calls the
generate_content method of the loaded Gemini model.
- The input to the method is a text prompt.
- The code uses Gemini's ability to understand the text prompt and use it to build an AI Image.
Note: By default, a SynthID watermark is added to images, but you can disable it by specifying the optional parameter add_watermark=False. You can't use a seed value and watermark at the same time. Learn more about SynthID watermark
Try it yourself! Experiment with different prompts to explore Gemini's capabilities.
Click Check my progress to verify the objective.
Send a text prompt to Gen AI and receive an image response
Congratulations!
You have completed the lab! Congratulations!!
Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.