Text-to-Image Generation with Gemini on Vertex AI (Solution)

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 complete the tasks on your own! An automated scoring system (shown on this page) will provide feedback on whether you have completed your tasks correctly.

When you take a challenge lab, you will not be taught new Google Cloud concepts. You are expected to extend your learned skills, like changing default values and reading and researching error messages to fix your own mistakes.

To score 100% you must successfully complete all tasks within the time period! Are you ready for the challenge?

Challenge scenario

Scenario: You're a developer at Cymbal Solutions which is an educational technology company that provides online tutoring and educational resources. They want to create an interactive science tutoring assistant to help students with questions related to astronomy and other scientific topics. They decide to use Google Cloud’s Vertex AI SDK to build a chat-based solution that can provide informative answers. you need to finish the below tasks:

Task: Develop a Python function named get_chat_response(prompt). This function should invoke the gemini-2.5-flash model using the supplied prompt, which will uses Gemini's ability to understand the text prompt and use it to build an AI Image.

For this challenge, use these questions in the prompt: "Hello! What are all the colors in a rainbow?" and "What is Prism?".

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. Write the Python code to use Google's Vertex AI SDK to interact with the pre-trained Text Generation AI model.

  3. Create and save the python file.

  4. Execute the Python file by invoking the below command by replacing the FILE_NAME inside the terminal within the Code Editor pane to view the output.

/usr/bin/python3 /FILE_NAME.py

Copied!

Note: You can ignore any warnings related to Python version dependencies.

Click Check my progress to verify the objective.

Send the text prompt requests to GenAI and receive the chat responses


Solution of Lab

curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/text-to-image-generation-with-gemini-on-vertex-ai-solution/lab.sh
source lab.sh