Paso 1: Configura tu proyecto y tu repositorio de origen

En este paso, configurarás tu Google Cloud proyecto y el entorno de Python en Cloud Shell, habilitarás las APIs necesarias y asignarás los roles de gestión de identidades y accesos (IAM) que necesitas para completar el tutorial. También debes configurar un repositorio de GitHub que contenga los archivos de origen de la aplicación. Para ello, crea una bifurcación y clona el repositorio GoogleCloudPlatform/generative-ai. Después de completar estos pasos, verifica la configuración ejecutando y probando la aplicación localmente en Cloud Shell.

Antes de empezar

  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.

      Ir a IAM
    2. Selecciona el proyecto.
    3. Haz clic en Conceder acceso.
    4. En el campo Nuevos principales, introduce tu identificador de usuario. Normalmente, se trata de la dirección de correo de una cuenta de Google.

    5. En la lista Selecciona un rol, elige un rol.
    6. Para conceder más roles, haz clic en Añadir otro rol y añade cada rol adicional.
    7. Haz clic en Guardar.
  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.

      Ir a IAM
    2. Selecciona el proyecto.
    3. Haz clic en Conceder acceso.
    4. En el campo Nuevos principales, introduce tu identificador de usuario. Normalmente, se trata de la dirección de correo de una cuenta de Google.

    5. En la lista Selecciona un rol, elige un rol.
    6. Para conceder más roles, haz clic en Añadir otro rol y añade cada rol adicional.
    7. Haz clic en Guardar.
  10. Configurar el repositorio de origen

    1. En GitHub, bifurca el repositorio GoogleCloudPlatform/generative-ai. Consulta más información sobre cómo bifurcar repositorios en GitHub.

      Bifurca el repositorio en 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. En el terminal de Cloud Shell, ejecuta los siguientes comandos para clonar el repositorio bifurcado y definir el directorio gemini-streamlit-cloudrun como directorio activo:
      cd
      git clone https://github.com/GIT_USER_NAME/FORK_NAME/
      cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

      Haz los cambios siguientes:

      • GIT_USER_NAME: tu nombre de usuario de GitHub.
      • FORK_NAME: nombre del repositorio de bifurcación que acabas de crear en GitHub.
    3. Configurar el entorno y las dependencias

      1. En el terminal de Cloud Shell, ejecuta los siguientes comandos para configurar un entorno virtual:

        python3 -m venv gemini-streamlit
        source gemini-streamlit/bin/activate
        pip install -r requirements.txt
        
      2. Ejecuta los siguientes comandos para definir las variables de entorno necesarias para la inicialización de Vertex AI:

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

      Probar la aplicación de forma local

      1. En el terminal de Cloud Shell, ejecuta la aplicación con el siguiente comando:

        streamlit run app.py \
          --browser.serverAddress=localhost \
          --server.enableCORS=false \
          --server.enableXsrfProtection=false \
          --server.port 8080
        
      2. Para obtener una vista previa de la aplicación, en la barra de tareas de Cloud Shell, haz clic en Botón Vista previa web y, a continuación, en Vista previa en el puerto 8080.

        Para obtener más información sobre cómo usar la función Vista previa web, consulta Vista previa de aplicaciones web.