Build Real World AI Applications with Gemini and Imagen
Solution for Build Real World AI Applications with Gemini and Imagen. 1 lab: GSP. Fast copy-paste commands for Google Cloud.
GSP — Build a Multi-Modal GenAI Application: Challenge Lab
Estimated time: 1 hour
# 🚀 Gemini Enterprise Agent Ready (GEAR) Challenge Lab: Image Generation and Analysis > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide is designed to help learners understand generative AI, Vertex AI, image generation, multimodal analysis, and practical Python workflows using the unified `google-genai` SDK. Always attempt the lab yourself first and follow Google Cl
# ==============================================================================
# ORBIT OF OPS - GEAR CHALLENGE LAB (COMMAND 1 OF 1)
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ __ ___
/ __ \ | | (_) | / _| / _ \
| | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___
| | | | '__| '_ \| | __| / _ \ | _| | | | | '_ \/ __|
| |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GEMINI GEAR CHALLENGE INITIATED <<<${RESET}\n"
# ==============================================================================
# PRE-FLIGHT CHECKS & VARIABLES
# ==============================================================================
echo -e "${BOLD}${YELLOW}[Orbit of Ops] Auto-fetching Project...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [[ -z "$PROJECT_ID" ]]; then export PROJECT_ID=$DEVSHELL_PROJECT_ID; fi
echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}\n"
echo -e "${YELLOW}${BOLD}--- REQUIRED LAB INPUTS ---${RESET}"
read -p "$(echo -e "${CYAN}${BOLD}1. Enter the Lab Region (e.g., us-central1): ${RESET}") " REGION
read -p "$(echo -e "${MAGENTA}${BOLD}2. Enter the flash-image-model-id (e.g., imagen-3.0-generate-001): ${RESET}") " IMAGE_MODEL_ID
read -p "$(echo -e "${BLUE}${BOLD}3. Enter the model-id (e.g., gemini-1.5-flash): ${RESET}") " TEXT_MODEL_ID
export REGION
export IMAGE_MODEL_ID
export TEXT_MODEL_ID
# ==============================================================================
# STEP 1: PROVISION ENVIRONMENT
# ==============================================================================
echo -e "\n${BLUE}${BOLD}[Orbit of Ops] Installing Required Python Packages...${RESET}"
pip install google-genai pillow --quiet --disable-pip-version-check
# ==============================================================================
# STEP 2: WRITE PYTHON SCRIPT
# ==============================================================================
echo -e "${CYAN}${BOLD}[Orbit of Ops] Generating Python Pipeline...${RESET}"
cat << 'EOF' > solution.py
import os
from google import genai
from google.genai import types
from PIL import Image
# Retrieve environment variables
PROJECT_ID = os.environ.get("PROJECT_ID")
REGION = os.environ.get("REGION")
IMAGE_MODEL_ID = os.environ.get("IMAGE_MODEL_ID")
TEXT_MODEL_ID = os.environ.get("TEXT_MODEL_ID")
print(f"\nInitializing Vertex AI client in {REGION} for {PROJECT_ID}...")
client = genai.Client(vertexai=True, project=PROJECT_ID, location=REGION)
# ==========================================
# Task 1: Generate Bouquet Image
# ==========================================
print("\n[Task 1] Generating bouquet image using model:", IMAGE_MODEL_ID)
image_prompt = "Create an image containing a bouquet of 2 sunflowers and 3 roses."
image_path = "bouquet.jpg"
try:
image_result = client.models.generate_images(
model=IMAGE_MODEL_ID,
prompt=image_prompt,
config=types.GenerateImagesConfig(
number_of_images=1,
output_mime_type="image/jpeg"
)
)
for generated_image in image_result.generated_images:
generated_image.image.save(image_path)
print(f"✅ Image successfully generated and saved locally to {image_path}")
except Exception as e:
print(f"❌ Error during image generation: {e}")
exit(1)
# ==========================================
# Task 2: Analyze Bouquet Image (Streamed)
# ==========================================
print(f"\n[Task 2] Analyzing image using model: {TEXT_MODEL_ID}")
def analyze_bouquet_image(image_path):
img = Image.open(image_path)
text_prompt = "Generate birthday wishes inspired by the bouquet image."
print("Initiating streaming request...")
response = client.models.generate_content_stream(
model=TEXT_MODEL_ID,
contents=[img, text_prompt]
)
print("Writing stream to birthday_wishes.txt...\n")
print("-" * 40)
with open("birthday_wishes.txt", "w") as f:
for chunk in response:
f.write(chunk.text)
print(chunk.text, end="", flush=True)
print("\n" + "-" * 40)
print("✅ Successfully streamed and saved to birthday_wishes.txt")
try:
analyze_bouquet_image(image_path)
except Exception as e:
print(f"❌ Error during image analysis: {e}")
exit(1)
EOF
# ==============================================================================
# STEP 3: EXECUTE SCRIPT
# ==============================================================================
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Executing AI Pipeline...${RESET}"
python3 solution.py
echo -e "\n${GREEN}${BOLD}🎉 COMMAND 1 COMPLETE!${RESET}"
echo -e "${CYAN}${BOLD}You can now safely click 'Check my progress' on Task 1 and Task 2 in your lab manual.${RESET}"