arrow_back

Build an AI Image Generator app using Imagen on Vertex AI

Zyskaj dostęp do ponad 700 modułów i kursów

Build an AI Image Generator app using Imagen on Vertex AI

Moduł 15 godz. universal_currency_alt Bezpłatnie show_chart Wprowadzające
info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
Zyskaj dostęp do ponad 700 modułów i kursów

bb-ide-genai-002

Google Cloud self-paced labs logo

Overview

  • Labs are timed and cannot be paused. The timer starts when you click Start Lab.
  • The included cloud terminal 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.
  • Load a pre-trained Image Generation Model : Discover how to use a powerful, pre-trained 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.

  1. Click File->New File to open a new file within the Code Editor.
  2. Copy and paste the provided code snippet into your file.
import argparse import vertexai from vertexai.preview.vision_models import ImageGenerationModel def generate_image( project_id: str, location: str, output_file: str, prompt: str ) -> vertexai.preview.vision_models.ImageGenerationResponse: """Generate an image using a text prompt. Args: project_id: Google Cloud project ID, used to initialize Vertex AI. location: Google Cloud region, used to initialize Vertex AI. output_file: Local path to the output image file. prompt: The text prompt describing what you want to see.""" vertexai.init(project=project_id, location=location) model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-002") images = model.generate_images( prompt=prompt, # Optional parameters number_of_images=1, seed=1, add_watermark=False, ) images[0].save(location=output_file) return images generate_image( project_id='{{{ project_0.project_id | "project-id" }}}', location='{{{ project_0.default_region | "REGION" }}}', output_file='image.jpeg', prompt='Create an image of a cricket ground in the heart of Los Angeles', )
  1. Click File > Save, enter GenerateImage.py for the Name field and click Save.
  2. 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 a image file with name image.jpeg.
/usr/bin/python3 /GenerateImage.py
  1. Now to view the generated image, Click EXPLORER > image.jpeg

Code Explanation

  • The code snippet is loading a pre-trained AI model called ImageGenerationModel (imagen-3.0-generate-002) on Vertex AI.
  • The code calls the generate_image 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 requests to Gen AI and receive a response

Congratulations!

You have completed the lab! Congratulations!!

Copyright 2025 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.

Zanim zaczniesz

  1. Moduły tworzą projekt Google Cloud i zasoby na określony czas.
  2. Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
  3. Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.

Użyj przeglądania prywatnego

  1. Skopiuj podaną nazwę użytkownika i hasło do modułu.
  2. Kliknij Otwórz konsolę w trybie prywatnym.

Zaloguj się w konsoli

  1. Zaloguj się z użyciem danych logowania do modułu. Użycie innych danych logowania może spowodować błędy lub naliczanie opłat.
  2. Zaakceptuj warunki i pomiń stronę zasobów przywracania.
  3. Nie klikaj Zakończ moduł, chyba że właśnie został przez Ciebie zakończony lub chcesz go uruchomić ponownie, ponieważ spowoduje to usunięcie wyników i projektu.

Ta treść jest obecnie niedostępna

Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

Świetnie

Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

Jeden moduł, a potem drugi

Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten

Aby uruchomić moduł, użyj przeglądania prywatnego

Uruchom ten moduł w oknie incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.