Table of contents
- Overview
- Setup and requirements
- Challenge scenario
- Task 1. Use cURL to test a prompt with the API
- Task 2. Write Streamlit framework and prompt Python code to complete chef.py
- Task 3. Test the application
- Task 4. Modify the Dockerfile and push image to the Artifact Registry
- Task 5. Deploy the application to Cloud Run and test
- Solution of Lab
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!
This lab is recommended for students who enrolled in the GenAI application development with Gemini and Streamlit course. Are you ready for the challenge?
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.
Challenge scenario
You onboarded at Cymbal Health just a few months ago. Cymbal Health is an established health network in East Central Minnesota dedicated to reimagining and transforming the way that healthcare can be delivered. Cymbal Health connects care and coverage under one health plan to make it easier for patients to receive high quality care at an affordable cost.
As a value added service, Cymbal Health is interested in improving customer Healthy Living and Wellness, with tips, and advice within apps. One particular area they want to focus on is improving customer nutrition.
By harnessing the power of the Gemini Pro, a multimodal model for generating text, audio, images, and video, Cymbal Health can build applications that generate meal recommendations for its customers.
As an example, your team has been working to create a AI-Based Chef app, that generates recipes based upon customer cuisine preferences, dietary restrictions, food allergies, and what they typically have in their homes or can purchase at a grocery store. Your job is to build, test and deploy a Proof Of Concept (POC) for this Chef app built on the Gemini pro model, Streamlit framework, and Cloud Run. As part of this POC, they have a list of tasks they would like to see you do in an allotted period of time in a sandbox environment.
Your challenge
Your tasks include the following:
Use cURL to test a prompt with the API.
Write Streamlit framework and prompt Python code to complete
chef.py
.Test the application.
Modify the Dockerfile and push the Docker image to the Artifact Registry.
Deploy the application to Cloud Run and test.
Task 1. Use cURL to test a prompt with the API
Before you can begin to create the Chef app in Vertex AI, you should test connectivity with the Gemini API.
Access Vertex AI Workbench User-Manged Notebook provided to you within the Cloud console in the lab environment.
Open a terminal, and download the prompt.ipynb file from Cloud Storage using the command below:
gsutil cp gs://spls/gsp517/prompt.ipynb .
Modify
prompt.ipynb
to include your project_ID and region within cell 3. You can get these in the left panel of the lab instructions.Modify
prompt.ipynb
to use the following prompt with cURL within cell 5, by replacing the existing prompt.I am a Chef. I need to create Japanese recipes for customers who want low sodium meals. However, I do not want to include recipes that use ingredients associated with a peanuts food allergy. I have ahi tuna, fresh ginger, and edamame in my kitchen and other ingredients. The customer wine preference is red. Please provide some for meal recommendations. For each recommendation include preparation instructions, time to prepare and the recipe title at the begining of the response. Then include the wine paring for each recommendation. At the end of the recommendation provide the calories associated with the meal and the nutritional facts.
Run all cells and observe the results.
Save
prompt.ipynb
.
Once you are satisfied with the results, verify the objective.
To verify the objective, click Check my progress.
Use cURL to test a prompt with the API.
Check my progress
Task 2. Write Streamlit framework and prompt Python code to complete chef.py
For this task you will clone a GitHub repo, and download the chef.py
file. Then you will add Streamlit framework code in the chef.py
file for the wine preference, to complete the user interface for the application. You will also include a custom Gemini prompt (similar to the one in task 1), but this one includes variables.
Using Cloud Shell clone the repo below from the default directory.
git clone https://github.com/GoogleCloudPlatform/generative-ai.git
Navigate to the
gemini-streamlit-cloudrun
directory.cd generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun
Important: All work in this challenge lab should be done within this directory. If you do not download the
chef.py
file here and make changes to it here, it will not be able to access the Streamlit framework. You will also not be able to test it in Cloud Shell (Task 3), or build the docker container (Task 4), and deploy then test it Cloud Run (Task 5).Download the
chef.py
file using the following command.gsutil cp gs://spls/gsp517/chef.py .
Open the chef.py file in the Cloud Shell Editor and review the code.
Note: The chef.py file already includes the Streamlit framework user interface code for the cuisine, dietary_preference, allergy, ingredient_1, ingredient_2, and ingredient_3 variables. Review this interface code before completing the next step.
Add Streamlit framework radio button option for the wine variable. Include options for Red, White and None.
Click here for hint!
Save the
chef.py
file.Add the new Gemini prompt below in Python code.
prompt = f"""I am a Chef. I need to create {cuisine} \n recipes for customers who want {dietary_preference} meals. \n However, don't include recipes that use ingredients with the customer's {allergy} allergy. \n I have {ingredient_1}, \n {ingredient_2}, \n and {ingredient_3} \n in my kitchen and other ingredients. \n The customer's wine preference is {wine} \n Please provide some for meal recommendations. For each recommendation include preparation instructions, time to prepare and the recipe title at the begining of the response. Then include the wine paring for each recommendation. At the end of the recommendation provide the calories associated with the meal and the nutritional facts. """
Save the
chef.py
file.
Once you are satisfied with the Gemini prompt code you added in chef.py
, upload the file to qwiklabs-gcp-03-50ffda938926
-generative-ai bucket by running below command in your cloud shell. On cloud console, click Open terminal to open the session in the Cloud Shell.
gcloud storage cp chef.py gs://qwiklabs-gcp-03-50ffda938926-generative-ai/
To verify the objective, click Check my progress.
Write prompt and Streamlit framework Python code to complete chef.py
Check my progress
Note: Make sure to run above command after making any changes in the chef.py
file. So that the updated chef.py
file is present in the bucket.
Task 3. Test the application
For this task you will use the terminal in Cloud Shell to run and test your application.
Make sure your are still in this path, generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun
.
Setup the python virtual environment and intall the dependencies.
Set environment variables for PROJECT (as your Project ID) and REGION (as the region you are using in the lab environment).
Run the
chef.py
application and test it.
Click here for hint!
Once you tested the application in Cloud Shell and confirmed it is performing as designed, without errors, verify the objective.
To verify the objective, click Check my progress.
Test the application.
Check my progress
Task 4. Modify the Dockerfile and push image to the Artifact Registry
In this task you modify the sample Dockerfile
to use your chef.py
file and push the Docker image to the Artifact Registry.
Important: Before completing the steps in this task we recommend you set environment variables for PROJECT (as your Project ID) and REGION (as the region you are using in the lab environment) as you did in a previous task.
Use the Cloud Shell editor to modify the Dockerfile to use
chef.py
, then save the file.In Cloud Shell set the following environment variables.
| Variable | Value | | --- | --- | | AR_REPO | 'chef-repo' | | SERVICE_NAME | 'chef-streamlit-app' |
Note: We recommend you combine this command and the following two commands as a single command, as the process to create the Artificat Registry and submit the build to Cloud Build, takes approximately 8 minutes.
Create the Artifact Registry repository with the
gcloud artifacts repositories create
command and the following parameters.| Parameter | Value | | --- | --- | | repo name | $AR_REPO | | location | $REGION | | repository format | Docker |
Click here for hint!
Submit the build with the
gcloud builds submit
command and the following parameters.| Parameter | Value | | --- | --- | | tag | "$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME" |
Wait for the command to complete.
Once the command is complete, verify the objective.
To verify the objective, click Check my progress.
Modify the Dockerfile and push the Docker image to the Artifact Registry.
Check my progress
Task 5. Deploy the application to Cloud Run and test
In this task you deploy the application (as a Docker Artifact) to Cloud Run and then test the application as running from the Cloud Run service endpoint.
In Cloud Shell deploy the application (as a Docker Artifact), using
glcoud run deploy
command and the following parameter values:| Parameter | Value | | --- | --- | | port | 8080 | | image | "$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME" | | flag | --allow-unauthenticated | | region | REGION | | platform | managed | | project | PROJECT | | set-env-vars | PROJECT=$PROJECT,REGION=$REGION |
Click here for hint!
Note: You may see a prompt asking "Do you want enable these APIs to continue (this will take a few minutes)?" If you do, select Y for yes.
The deployment will take a few minutes to complete and you will you will be provided a URL to the Cloud Run service. You can visit that in the browser to view the Cloud Run application that you just deployed.
Test the application with the link provided.
Once you successfully tested the application running on Cloud Run, verify the objective.
To verify the objective, click Check my progress.
Deploy the application to Cloud Run and test.
Solution of Lab
Task 1: You need to Dowload below file
Step 1: Click here to Download the Jupyter Notebook
Step 2: Open Vertex AI Workbench User-Manged Notebook
export REGION=
gcloud auth list
gcloud services enable run.googleapis.com
git clone https://github.com/GoogleCloudPlatform/generative-ai.git
cd generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun
gsutil cp gs://spls/gsp517/chef.py .
rm -rf Dockerfile chef.py
wget https://raw.githubusercontent.com/quiccklabs/Labs_solutions/master/Develop%20GenAI%20Apps%20with%20Gemini%20and%20Streamlit%20Challenge%20Lab/Dockerfile.txt
wget https://raw.githubusercontent.com/quiccklabs/Labs_solutions/master/Develop%20GenAI%20Apps%20with%20Gemini%20and%20Streamlit%20Challenge%20Lab/chef.py
mv Dockerfile.txt Dockerfile
gcloud storage cp chef.py gs://$DEVSHELL_PROJECT_ID-generative-ai/
export PROJECT="$DEVSHELL_PROJECT_ID"
python3 -m venv gemini-streamlit
source gemini-streamlit/bin/activate
python3 -m pip install -r requirements.txt
streamlit run chef.py \
--browser.serverAddress=localhost \
--server.enableCORS=false \
--server.enableXsrfProtection=false \
--server.port 8080
cd ~/generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun
gcloud storage cp chef.py gs://qwiklabs-gcp-03-50ffda938926-generative-ai/
AR_REPO='chef-repo'
SERVICE_NAME='chef-streamlit-app'
gcloud artifacts repositories create "$AR_REPO" --location="$REGION" --repository-format=Docker
gcloud builds submit --tag "$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME"
gcloud run deploy "$SERVICE_NAME" \
--port=8080 \
--image="$REGION-docker.pkg.dev/$PROJECT/$AR_REPO/$SERVICE_NAME" \
--allow-unauthenticated \
--region=$REGION \
--platform=managed \
--project=$PROJECT \
--set-env-vars=GCP_PROJECT=$PROJECT,GCP_REGION=$REGION