ステップ 1: プロジェクトとソース リポジトリを設定する

このステップでは、Cloud Shell で Google Cloud プロジェクトと Python 環境を設定し、必要な API を有効にして、チュートリアルの実行に必要な Identity and Access Management(IAM)ロールを割り当てます。また、GoogleCloudPlatform/generative-ai リポジトリをフォークしてクローンを作成し、アプリのソースファイルを含む GitHub リポジトリを設定します。これらの手順を終えたら、Cloud Shell でアプリをローカルで実行してテストを行い、設定を確認します。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI, Compute Engine, Artifact Registry, Identity-Aware Proxy (IAP), Cloud Run Admin, Cloud Build, Identity and Access Management (IAM) API, and Gemini for Google Cloud APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Vertex AI User, Cloud Build Editor, Cloud Run Admin, Artifact Registry Admin, Compute Load Balancer Admin, Service Account User, IAP Policy Admin, OAuth Config Editor, and Service Usage Admin.

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Vertex AI, Compute Engine, Artifact Registry, Identity-Aware Proxy (IAP), Cloud Run Admin, Cloud Build, Identity and Access Management (IAM) API, and Gemini for Google Cloud APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  9. Make sure that you have the following role or roles on the project: Vertex AI User, Cloud Build Editor, Cloud Run Admin, Artifact Registry Admin, Compute Load Balancer Admin, Service Account User, IAP Policy Admin, OAuth Config Editor, and Service Usage Admin.

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  10. ソース リポジトリを設定する

    1. GitHub で、GoogleCloudPlatform/generative-ai リポジトリをフォークします。GitHub でリポジトリをフォークすることに関する詳細

      GitHub でリポジトリをフォークする

    1. In the Google Cloud console, activate Cloud Shell.

      Activate Cloud Shell

      At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    2. Cloud Shell ターミナルで次のコマンドを実行して、フォークされたリポジトリのクローンを作成し、gemini-streamlit-cloudrun ディレクトリをアクティブ ディレクトリとして設定します。
      cd
      git clone https://github.com/GIT_USER_NAME/FORK_NAME/
      cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

      次のように置き換えます。

      • GIT_USER_NAME: GitHub のユーザー名。
      • FORK_NAME: GitHub で作成したフォーク リポジトリの名前。
    3. 環境と依存関係を設定する

      1. Cloud Shell ターミナルで次のコマンドを実行して、仮想環境を設定します。

        python3 -m venv gemini-streamlit
        source gemini-streamlit/bin/activate
        pip install -r requirements.txt
        
      2. 次のコマンドを実行して、Vertex AI の初期化に必要な環境変数を設定します。

        export GCP_PROJECT=$GOOGLE_CLOUD_PROJECT
        export GCP_REGION='us-central1' 
        

      アプリをローカルでテストする

      1. Cloud Shell ターミナルから、次のコマンドを実行してアプリを実行します。

        streamlit run app.py \
          --browser.serverAddress=localhost \
          --server.enableCORS=false \
          --server.enableXsrfProtection=false \
          --server.port 8080
        
      2. アプリをプレビューするには、Cloud Shell のタスクバーで [ウェブでプレビュー] ボタン をクリックし、[ポート 8080 でプレビュー] をクリックします。

        ウェブ プレビュー機能の使用に関する詳細は、ウェブアプリをプレビューするをご覧ください。