Try Translation

This page guides you through an interactive experience using the Vertex AI Translation pre-trained API on Google Distributed Cloud (GDC) air-gapped.

Before you begin

Follow these steps before trying Translation:

  1. Set up a project for Vertex AI.

  2. Assign the AI Translation Developer (ai-translation-developer) role to a service account. You require this role to generate a token for request authentication and authorization.

  3. Enable the Translation pre-trained API.

  4. View the service status and endpoint for the Translation pre-trained API.

  5. Install the Translation client library.

    Follow these steps to ensure you have the correct version of the client library:

    1. Check if the Translation client library is installed and obtain the version number:

      pip freeze | grep translation
      

      If the client library is already installed, you obtain an output similar to the following example:

      google-cloud-translation==3.8.0
      

      The version number you obtain must match the client library at the following endpoint:

      https://GDC_URL/.well-known/static/client-libraries
      

      Replace GDC_URL with the URL of your organization in GDC.

      If the version numbers don't match, uninstall the client library:

      pip uninstall google-cloud-translation
      
    2. If you uninstalled or haven't installed the Translation client library, you must install it by specifying the filename corresponding to your operating system.

  6. Create a notebook to interact with the Translation pre-trained API from a Python script.

  7. Ask your Project IAM Admin to grant you the AI Translation Developer (ai-translation-developer) role in your project namespace.

Set your environment variables

Follow these steps to set the required environment variables on a Python script:

  1. Create a Python script on a JupyterLab notebook.

  2. Add the following code to the Python script:

    import os
    
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "APPLICATION_DEFAULT_CREDENTIALS_FILENAME"
    

    Replace APPLICATION_DEFAULT_CREDENTIALS_FILENAME with the name of the JSON file that contains the service account keys you created in the project, for example, my-service-key.json.

  3. Save the Python script with a name, for example, translation.py.

  4. Run the Python script to set the environment variables:

    python SCRIPT_NAME
    

    Replace SCRIPT_NAME with the name you gave to your Python script, for example, translation.py.

Get an authentication token

Obtain a token to authenticate the request before making a Translation API call from the CLI. This step is necessary if you use the grpcurl or curl tools to translate text.

Follow these steps to get an authentication token:

  1. Install the google-auth client library:

    pip install google-auth
    
  2. Add the following code to the Python script you created when setting up the environment variables:

    import google.auth
    from google.auth.transport import requests
    import requests as reqs
    
    audience = "https://ENDPOINT"
    
    creds, project_id = google.auth.default()
    print(project_id)
    creds = creds.with_gdch_audience(audience)
    
    def test_get_token():
      sesh = reqs.Session()
      req = requests.Request(session=sesh)
      creds.refresh(req)
      print(creds.token)
    
    if __name__=="__main__":
      test_get_token()
    

    Replace ENDPOINT with the Translation endpoint that you use for your organization. For more information, view service status and endpoints.

  3. Save the Python script.

  4. Run the Python script to fetch the token:

    python SCRIPT_NAME
    

    Replace SCRIPT_NAME with the name you gave to your Python script, for example, translation.py.

    The output shows the authentication token.

Add the token to the header of the grpcurl and curl requests you make, as in the following example:

-H "Authorization: Bearer TOKEN"

Translate text

Make a grpcurl or curl request to the Translation pre-trained API to use the service from the CLI. Otherwise, interact with the Translation pre-trained API from a Python script to translate text from one language to another.

The following examples show how to translate an input text from Spanish (Hola, esto es una prueba) to English (Hello, this is a test):

grpcurl

Follow these steps to make a grpcurl request:

  1. If you don't have grpcurl installed, download and install it from a resource outside of Distributed Cloud (https://github.com/fullstorydev/grpcurl#from-source).

  2. Get an authentication token.

  3. Make the request:

    grpcurl -vv -H "Authorization: Bearer TOKEN" -authority ENDPOINT -d '{"parent": "projects/PROJECT_ID", "source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba"]}' ENDPOINT:443 google.cloud.translation.v3.TranslationService/TranslateText
    

    Replace the following:

curl

Follow these steps to make a curl request:

  1. Get an authentication token.

  2. Make the request:

    curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"parent": "projects/PROJECT_ID", "source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba"]}'
    

    Replace the following:

Python

Follow these steps to try the Translation service from a Python script:

  1. Install the latest version of the Translation client library.

  2. Add the following code to the Python script you created when setting up the environment variables:

    from google.cloud import translate
    import google.auth
    from google.auth.transport import requests
    from google.api_core.client_options import ClientOptions
    
    audience = "https://ENDPOINT:443"
    api_endpoint="ENDPOINT:443"
    
    def translate_client(creds):
      opts = ClientOptions(api_endpoint=api_endpoint)
      return translate.TranslationServiceClient(credentials=creds, client_options=opts)
    
    def main():
      creds = None
      try:
        creds, project_id = google.auth.default()
        creds = creds.with_gdch_audience(audience)
        req = requests.Request()
        creds.refresh(req)
        print("Got token: ")
        print(creds.token)
      except Exception as e:
        print("Caught exception" + str(e))
        raise e
      return creds
    
    def translate_func(creds):
      tc = translate_client(creds)
      req = {
        "parent": "projects/PROJECT_ID",
        "source_language_code": "es",
        "target_language_code": "en",
        "contents": ["Hola, esto es una prueba"]
      }
    
      resp = tc.translate_text(req)
      print(resp)
    
    if __name__=="__main__":
      creds = main()
      translate_func(creds)
    

    Replace the following:

  3. Save the Python script.

  4. Run the Python script to translate the text:

    python SCRIPT_NAME
    

    Replace SCRIPT_NAME with the name you gave to your Python script, for example, translation.py.

For more information on the translate_text method, see the Python client library.

What's next