Langkah 1: Siapkan project dan repositori sumber

Pada langkah ini, Anda akan menyiapkan project Google Cloud dan lingkungan Python di Cloud Shell, mengaktifkan API yang diperlukan, dan menetapkan peran Identity and Access Management (IAM) yang diperlukan untuk menyelesaikan tutorial. Anda juga menyiapkan repositori GitHub yang berisi file sumber aplikasi dengan membuat fork dan meng-clone repositori GoogleCloudPlatform/generative-ai. Setelah menyelesaikan langkah-langkah ini, Anda memverifikasi penyiapan dengan menjalankan dan menguji aplikasi secara lokal di Cloud Shell.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  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 role (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.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  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 role (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.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

Menyiapkan repositori sumber

  1. Di GitHub, buat fork repositori GoogleCloudPlatform/generative-ai. Pelajari lebih lanjut cara membuat fork repositori di GitHub.

    Buat fork repositori di GitHub

  1. Di konsol Google Cloud , aktifkan Cloud Shell.

    Aktifkan Cloud Shell

    Di bagian bawah konsol Google Cloud , sesi Cloud Shell akan dimulai dan menampilkan perintah command line. Cloud Shell adalah lingkungan shell dengan Google Cloud CLI yang sudah terinstal, dan dengan nilai yang sudah ditetapkan untuk project Anda saat ini. Diperlukan waktu beberapa detik untuk melakukan inisialisasi pada sesi.

  2. Di terminal Cloud Shell, jalankan perintah berikut untuk meng-clone repositori yang di-fork dan menetapkan direktori gemini-streamlit-cloudrun sebagai direktori aktif:
    cd
    git clone https://github.com/GIT_USER_NAME/FORK_NAME/
    cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

    Ganti kode berikut:

    • GIT_USER_NAME: Nama pengguna GitHub Anda.
    • FORK_NAME: Nama repositori fork yang baru saja Anda buat di GitHub.

Menyiapkan lingkungan dan dependensi

  1. Di terminal Cloud Shell, jalankan perintah berikut untuk menyiapkan lingkungan virtual:

    python3 -m venv gemini-streamlit
    source gemini-streamlit/bin/activate
    pip install -r requirements.txt
    
  2. Jalankan perintah berikut untuk menetapkan variabel lingkungan yang diperlukan untuk inisialisasi Vertex AI:

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

Menguji aplikasi secara lokal

  1. Dari terminal Cloud Shell, jalankan aplikasi dengan menjalankan perintah berikut:

    streamlit run app.py \
      --browser.serverAddress=localhost \
      --server.enableCORS=false \
      --server.enableXsrfProtection=false \
      --server.port 8080
    
  2. Untuk melihat pratinjau aplikasi, di panel tugas Cloud Shell, klik Tombol Pratinjau Web, lalu klik Preview on port 8080.

    Untuk mengetahui informasi selengkapnya tentang penggunaan fitur Pratinjau Web, lihat Mempratinjau aplikasi web.