Passaggio 1: configura il progetto e il repository di origine

In questo passaggio, configuri il progetto Google Cloud e l'ambiente Python in Cloud Shell, abiliti le API richieste e assegni i ruoli Identity and Access Management (IAM) necessari per completare il tutorial. Devi anche configurare un repository GitHub contenente i file sorgente dell'app creando un fork e clonando il repository GoogleCloudPlatform/generative-ai. Dopo aver completato questi passaggi, verifica la configurazione eseguendo e testando l'app localmente in Cloud Shell.

Prima di iniziare

  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.

    Go to project selector

  3. Make sure 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.

    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.

      Vai a IAM
    2. Seleziona il progetto.
    3. Fai clic su Concedi accesso.
    4. Nel campo Nuovi principali, inserisci il tuo identificatore utente. In genere si tratta dell'indirizzo email di un Account Google.

    5. Nell'elenco Seleziona un ruolo, seleziona un ruolo.
    6. Per concedere altri ruoli, fai clic su Aggiungi un altro ruolo e aggiungi ogni ruolo aggiuntivo.
    7. Fai clic su Salva.
    8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    9. Make sure that billing is enabled for your Google Cloud project.

    10. 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.

      Enable the APIs

    11. 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.

        Vai a IAM
      2. Seleziona il progetto.
      3. Fai clic su Concedi accesso.
      4. Nel campo Nuovi principali, inserisci il tuo identificatore utente. In genere si tratta dell'indirizzo email di un Account Google.

      5. Nell'elenco Seleziona un ruolo, seleziona un ruolo.
      6. Per concedere altri ruoli, fai clic su Aggiungi un altro ruolo e aggiungi ogni ruolo aggiuntivo.
      7. Fai clic su Salva.

      Configura il repository di origine

      1. In GitHub, esegui il fork del repository GoogleCloudPlatform/generative-ai. Scopri di più sul forking dei repository in GitHub.

        Crea un fork del repository in 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. Nel terminale Cloud Shell, esegui i comandi seguenti per clonare il repository sottoposto a fork e impostare la directory gemini-streamlit-cloudrun come directory attiva:
        cd
        git clone https://github.com/GIT_USER_NAME/FORK_NAME/
        cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

        Sostituisci quanto segue:

        • GIT_USER_NAME: il tuo nome utente GitHub.
        • FORK_NAME: il nome del repository del fork che hai appena creato in GitHub.

      Configura l'ambiente e le dipendenze

      1. Nel terminale Cloud Shell, esegui i seguenti comandi per configurare un ambiente virtuale:

        python3 -m venv gemini-streamlit
        source gemini-streamlit/bin/activate
        pip install -r requirements.txt
        
      2. Esegui i seguenti comandi per impostare le variabili di ambiente necessarie per l'inizializzazione di Vertex AI:

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

      Testa l'app localmente

      1. Dal terminale Cloud Shell, esegui l'app con il seguente comando:

        streamlit run app.py \
          --browser.serverAddress=localhost \
          --server.enableCORS=false \
          --server.enableXsrfProtection=false \
          --server.port 8080
        
      2. Per visualizzare l'anteprima dell'app, fai clic su Pulsante Anteprima web nella barra delle app di Cloud Shell, quindi su Anteprima sulla porta 8080.

        Per ulteriori informazioni sull'utilizzo della funzionalità Anteprima web, consulta Anteprima delle app web.