Build an AI Image Generator app using Imagen on Vertex AI - bb-ide-genai-002

A passionate full-stack developer from @ePlus.DEV
Search for a command to run...

A passionate full-stack developer from @ePlus.DEV
No comments yet. Be the first to comment.
Quick and practical tips to help users optimize tasks, improve skills, and solve common problems effectively across various areas like tech, lifestyle, productivity, and more.
Overview This lab demonstrates how Gemini can interpret what is in an image and provide a description quickly. During the exercise, you will provide the following image as input and ask Gemini is: "Wh

Overview In a challenge lab you’re given a scenario and a set of tasks. Instead of following step-by-step instructions, you will use the skills learned from the labs in the course to figure out how to

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary npm bắt đầu quét mã độc ngay tại thời điểm publish và có thể giữ pack

Overview In a challenge lab you’re given a scenario and a set of tasks. Instead of following step-by-step instructions, you will use the skills learned from the labs in the course to figure out how to

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI công bố báo cáo mới về cách AI đang làm thay đổi phạm vi công

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.
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.
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.
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='qwiklabs-gcp-02-54fa7d800cd5',
location='europe-west4',
output_file='image.jpeg',
prompt='Create an image of a cricket ground in the heart of Los Angeles',
)
Click File->Save, choose 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 a image file with name image.jpeg.
/usr/bin/python3 /home/student/GenerateImage.py
Copied!content_copy
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
export REGION=

curl -LO raw.githubusercontent.com/quiccklabs/Labs_solutions/master/Build%20an%20AI%20Image%20Generator%20app%20using%20Imagen%20on%20Vertex%20AI/quicklab.sh
source quicklab.sh