Build a Multi-Modal GenAI Application: Challenge Lab
Challenge scenario
AI 기반 꽃다발 디자인 회사의 개발자로 현실적인 꽃다발 이미지를 생성하고 생성된 꽃다발의 설명 요약을 제공한다.
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.
Code
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."""
vertexai.init(project=project_id, location=location)
model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-002")
images = model.generate_images(
prompt=prompt,
number_of_images=1,
seed=1,
add_watermark=False,
)
images[0].save(location=output_file)
return images
if __name__ == "__main__":
project_id = 'project-id'
location = 'REGION'
prompt = "Create an image containing a bouquet of 2 sunflowers and 3 roses."
output_file = "image.jpg"
generate_bouquet_image(project_id, location, output_file, prompt)
print(f"Image saved to {output_file}")
Result
Create an image containing a bouquet of 2 sunflowers and 3 roses.
여러번 돌려봐도 꽃 개수가 안 맞는다.
Task 2
Develop a second Python function called `analyze_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.
Code
from google import genai
from google.genai.types import HttpOptions, Part
def analyze_bouquet_image(project_id: str, location: str, image_path: str):
client = genai.Client(
vertexai=True,
project=project_id.strip('"'),
location=location.strip('"'),
http_options=HttpOptions(api_version="v1")
)
# 이미지 바이트 읽기
with open(image_path, "rb") as f:
image_bytes = f.read()
prompt = "Generate heartfelt birthday wishes based on this image."
# 스트리밍으로 응답 받아 출력
response_text = ""
for chunk in client.models.generate_content_stream(
model="gemini-2.0-flash-001",
contents=[
prompt,
Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
],
):
print(chunk.text, end="")
response_text += chunk.text
print() # 출력 후 줄바꿈
return response_text
if __name__ == "__main__":
project_id = 'project-id'
location = 'REGION'
image_path = "image.jpg"
analyze_bouquet_image(project_id, location, image_path)
Result
Here are a few heartfelt birthday wishes inspired by the image of sunflowers and roses:
# Option 1 (Focus on brightness and love):
"Happy Birthday! May your day be as bright and cheerful as these sunflowers, and as filled with love and warmth as these beautiful roses. Wishing you a year ahead filled with joy and all the things that make your heart bloom."
# Option 2 (Focus on strength and beauty):
"Happy Birthday! Like the radiant sunflowers that stand tall and the elegant roses that exude grace, you are a strong and beautiful soul. May your birthday be a celebration of all that you are, and may the year ahead bring you even more strength, happiness, and beauty."
# Option 3 (A little more poetic):
"Wishing you a very Happy Birthday! May your life continue to be a bouquet filled with the vibrant joy of sunflowers, the passionate love of red roses, and the tender sweetness of blush pink roses. May each petal of this day unfold with happiness and blessings."
# Option 4 (Simple and sweet):
"Happy Birthday! Sending you sunshine and roses to brighten your special day! May your birthday be as lovely as you are."
I tried to capture the essence of the flowers in each wish! I hope one of these resonates with you. You can always personalize them further by adding a specific memory or quality you admire about the birthday person.