arrow_back

Build an AI Image Generator app using Imagen on Vertex AI

Acceder Unirse
Pon a prueba tus conocimientos y compártelos con nuestra comunidad
done
Obtén acceso a más de 700 labs prácticos, insignias de habilidad y cursos

Build an AI Image Generator app using Imagen on Vertex AI

Lab 15 minutos universal_currency_alt Sin costo show_chart Introductorio
info Es posible que este lab incorpore herramientas de IA para facilitar tu aprendizaje.
Pon a prueba tus conocimientos y compártelos con nuestra comunidad
done
Obtén acceso a más de 700 labs prácticos, insignias de habilidad y cursos

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("imagegeneration@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, choose 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 /home/student/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 (imagegeneration@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 2024 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.

Este contenido no está disponible en este momento

Te enviaremos una notificación por correo electrónico cuando esté disponible

¡Genial!

Nos comunicaremos contigo por correo electrónico si está disponible