Pada langkah ini, Anda akan menyiapkan Google Cloud project 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 melakukan fork
dan meng-clone repositori
GoogleCloudPlatform/generative-ai
. Setelah menyelesaikan langkah-langkah ini, Anda akan memverifikasi penyiapan dengan menjalankan dan
menguji aplikasi secara lokal di Cloud Shell.
Sebelum memulai
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
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.
-
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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
Buka IAM - Pilih project.
- Klik Berikan akses.
-
Di kolom New principals, masukkan ID pengguna Anda. Ini biasanya adalah alamat email untuk Akun Google.
- Di daftar Pilih peran, pilih peran.
- Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
- Klik Simpan.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
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.
-
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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
Buka IAM - Pilih project.
- Klik Berikan akses.
-
Di kolom New principals, masukkan ID pengguna Anda. Ini biasanya adalah alamat email untuk Akun Google.
- Di daftar Pilih peran, pilih peran.
- Untuk memberikan peran tambahan, klik Tambahkan peran lain, lalu tambahkan setiap peran tambahan.
- Klik Simpan.
-
Menyiapkan repositori sumber
Di GitHub, buat fork repositori GoogleCloudPlatform/generative-ai. Pelajari lebih lanjut cara membuat fork repositori di GitHub.
-
In the Google Cloud console, 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.
- 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-cloudrunGanti 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
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
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
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
Untuk melihat pratinjau aplikasi, di taskbar Cloud Shell, klik
, lalu klik Preview on port 8080.
Untuk mengetahui informasi selengkapnya tentang penggunaan fitur Pratinjau Web, lihat Mempratinjau aplikasi web.