Deploy a Streamlit App Integrated with Gemini Pro on Cloud Run - GSP1229

Deploy a Streamlit App Integrated with Gemini Pro on Cloud Run - GSP1229

Overview

Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases. The Gemini API gives you access to the Gemini Pro Vision and Gemini Pro models. In this lab you will understand how to integrate Vertex AI Gemini API with applications and build and deploy the developed sample application on Google Cloud Run.

Vertex AI Gemini API

The Vertex AI Gemini API provides a unified interface for interacting with Gemini models. There are currently two models available in the Gemini API:

  1. Gemini Pro model (gemini-pro): Designed to handle natural language tasks, multiturn text and code chat, and code generation.

  2. Gemini Pro Vision model (gemini-pro-vision): Supports multimodal prompts. You can include text, images, and video in your prompt requests and get text or code responses.

You can interact with the Gemini API using the following methods:

  • Use the Vertex AI Studio for quick testing and command generation

  • Use cURL commands

  • Use the Vertex AI SDK

This lab focuses on integrating the Gemini Pro model with a Streamlit application and deploying the application on Google Cloud Run.

For more information, see the Generative AI on Vertex AI documentation.

Objectives

In this lab, you will learn how to:

  • Integrate Vertex AI Gemini API with applications

  • Build and deploy the developed sample application on Google Cloud Run

  • Use the Streamlit framework to build a Cloud Run application

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.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button

    • Time remaining

    • The temporary credentials that you must use for this lab

    • Other information, if needed, to step through this lab

  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.

  3. If necessary, copy the Username below and paste it into the Sign in dialog.

     student-02-2d57356c8920@qwiklabs.net
    

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

     1qOgFL9Bwgjj
    

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.

    Note: Using your own Google Cloud account for this lab may incur extra charges.

  7. Click through the subsequent pages:

    • Accept the terms and conditions.

    • Do not add recovery options or two-factor authentication (because this is a temporary account).

    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left.

Navigation menu icon

Task 1. Run the Application Locally

In this section, you will run the Streamlit application locally in Cloud Shell.

Clone the Repository

  1. Open a new Cloud Shell terminal by clicking on the Cloud Shell icon in the top right corner of the Cloud console.

  2. Run the following commands to clone the repo and navigate to gemini-streamlit-cloudrun directory in Cloud Shell using the following commands.

git clone https://github.com/GoogleCloudPlatform/generative-ai.git
cd generative-ai/gemini/sample-apps/gemini-streamlit-cloudrun

To run the Streamlit Application, you will need to perform some additional steps.

Run the application

  1. Setup the Python virtual environment and install the dependencies:
python3 -m venv gemini-streamlit
source gemini-streamlit/bin/activate
pip install -r requirements.txt
  1. Your application requires access to two environment variables:
  • GCP_PROJECT : This the Google Cloud project ID.

  • GCP_REGION : This is the region in which you are deploying your Cloud Run app. For e.g. us-central1.

These variables are needed since the Vertex AI initialization needs the Google Cloud project ID and the region. The specific code line from the app.py function is shown here: vertexai.init(project=PROJECT_ID, location=LOCATION)

In Cloud Shell, execute the following commands:

GCP_PROJECT='qwiklabs-gcp-02-ebf297ab9b6f'
GCP_REGION='us-east4'
  1. To run the application locally, execute the following command.
streamlit run app.py \
  --browser.serverAddress=localhost \
  --server.enableCORS=false \
  --server.enableXsrfProtection=false \
  --server.port 8080

Output:

Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.

You can now view your Streamlit app in your browser.

URL: http://localhost:8080
  1. The application will startup and you will be provided a URL to the application. Click the link to view the application in the browser or use Cloud Shell's web preview function to launch the preview page.

  2. Adjust the parameters for the story generation and click Generate my story.

  3. Navigate back to Cloud Shell and authorize the application to access the Gemini API. Once you have authorized the application, you can navigate back to the application to see the response.

Note: The application will prompt the Vertex AI Gemini API and display the responses. In order for this to work, after selecting Generate my story you will need to navigate back to Cloud Shell to authorize the application to access the Gemini API.

Streamlit Application Page

  1. After you have finished testing the application, you can stop the application by entering Ctrl + C in Cloud Shell.

Task 2. Build and Deploy the Application to Cloud Run

In this section, you will deploy the Streamlit Application in Cloud Run.

You will now build the Docker image for the application and push it to Artifact Registry. To do this, you will need one environment variable set that will point to the Artifact Registry name. The commands below will create this Artifact Registry repository for you.

  1. In Cloud Shell, execute the following command:
AR_REPO='gemini-repo'
SERVICE_NAME='gemini-streamlit-app' 
gcloud artifacts repositories create "$AR_REPO" --location="$GCP_REGION" --repository-format=Docker
gcloud builds submit --tag "$GCP_REGION-docker.pkg.dev/$GCP_PROJECT/$AR_REPO/$SERVICE_NAME"

Output:

DONE
--------------------------------------------------------------------------------
ID                                    CREATE_TIME                DURATION  SOURCE                                                                                                     IMAGES                                                                                              STATUS
a601ffd1-c282-43d2-942c-53cc13f43bf2  2023-12-18T11:37:30+00:00  2M29S     gs://qwiklabs-gcp-00-eb090e9513e8_cloudbuild/source/1702899440.87287-549e1a0cc5644b3c9535ff57f4a63d02.tgz  us-central1-docker.pkg.dev/qwiklabs-gcp-00-eb090e9513e8/gemini-repo/gemini-streamlit-app (+1 more)  SUCCESS
  1. The final step is to deploy the service in Cloud Run with the image that we had built and had pushed to the Artifact Registry in the previous step.
gcloud run deploy "$SERVICE_NAME" \
  --port=8080 \
  --image="$GCP_REGION-docker.pkg.dev/$GCP_PROJECT/$AR_REPO/$SERVICE_NAME" \
  --allow-unauthenticated \
  --region=$GCP_REGION \
  --platform=managed  \
  --project=$GCP_PROJECT \
  --set-env-vars=GCP_PROJECT=$GCP_PROJECT,GCP_REGION=$GCP_REGION

On successful deployment, 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.

Output:

Deploying new service... Done.

✓ Deploying new service... Done.

Done.  
Service [gemini-streamlit-app] revision [gemini-streamlit-app-00001-srg] has been deployed and is serving 100 percent of traffic. Service URL: https://gemini-streamlit-app-hc2gb6hsia-uc.a.run.app

Choose the functionality that you would like to check out and the application will prompt the Vertex AI Gemini API and display the responses.

Vertex Gemini

Click Check my progress to verify the objective.

Build and Deploy the Application to Cloud Run


Solution of Lab

export REGION=

curl -LO raw.githubusercontent.com/quiccklabs/Labs_solutions/master/Deploy%20a%20Streamlit%20App%20Integrated%20with%20Gemini%20Pro%20on%20Cloud%20Run/quicklabgsp1229.sh
sudo chmod +x quicklabgsp1229.sh
./quicklabgsp1229.sh