Google Skills Boost
bb-ide-genai-004
GCP Project – qwiklabs-gcp-00-fc150d6744a7
Region – us-central1
- Upgrade Gen AI
-
/usr/bin/pip3 install --upgrade google-genai google-cloud-logging
Task 1: Develop a Python function named generate_bouquet_image(prompt). This function should invoke the imagen-3.0-generate-002 model using the supplied prompt, generate the image, and store it locally. For this challenge, use the prompt: Create an image containing a bouquet of 2 sunflowers and 3 roses.
save as GenerateImage.py and run
import vertexai from vertexai.preview.vision_models import ImageGenerationModel def generate_bouquet_image( project_id: str, location: str, output_file: str, prompt: str ): “””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 # Example usage: generate_bouquet_image( project_id=’qwiklabs-gcp-03-25c367087bd7‘, location=’europe-west4‘, output_file=’image.jpeg’, prompt=’Create an image containing a bouquet of 2 sunflowers and 3 roses’, ) Task 2: Develop a second Python function calledanalyze_bouquet_image(image_path). This function will take the image path as input along with a text prompt to generate birthday wishes based on the image passed and send it to the gemini-2.0-flash-001 model. To ensure responses can be obtained as and when they are generated, enable streaming on the prompt requests.
——-
save as AnalyseImage.py and run
import vertexai
from vertexai.generative_models import GenerativeModel, Part, Image
def analyze_bouquet_image(image_path: str):
vertexai.init(
project=’qwiklabs-gcp-00-fc150d6744a7′, # Your project ID
location=’us-central1′, # Your region
)
multimodal_model = GenerativeModel(“gemini-2.0-flash-001”)
messages = [
“Generate a birthday wish based on the following image”,
Part.from_image(Image.load_from_file(location=image_path))
]
chat = multimodal_model.start_chat()
print(chat.send_message(content=messages, stream=False))
# Analyze the previously saved image
if __name__ == “__main__”:
analyze_bouquet_image(
image_path=’image.jpeg’
)