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:
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.View the service status and endpoint for the Translation pre-trained API.
Install the Translation client library.
Follow these steps to ensure you have the correct version of the client library:
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
If you uninstalled or haven't installed the Translation client library, you must install it by specifying the filename corresponding to your operating system.
Create a notebook to interact with the Translation pre-trained API from a Python script.
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:
Create a Python script on a JupyterLab notebook.
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
.Save the Python script with a name, for example,
translation.py
.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 curl
tool to translate text.
Follow these steps to get an authentication token:
gdcloud CLI
Export the identity token for the specified account to an environment variable:
export TOKEN="$($HOME/gdcloud auth print-identity-token --audiences=https://ENDPOINT)"
Replace ENDPOINT
with the Translation endpoint. For more information, view service statuses and endpoints.
Python
Install the
google-auth
client library:pip install google-auth
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.Save the Python script.
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 curl
requests you make, as in
the following example:
-H "Authorization: Bearer TOKEN"
Translate text
Make a 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
):
curl
Follow these steps to make a curl
request:
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:
TOKEN
: the authentication token you obtained.ENDPOINT
: the Translation endpoint that you use for your organization. For more information, view service status and endpoints.PROJECT_ID
: your project ID.
Python
Follow these steps to try the Translation service from a Python script:
Install the latest version of the Translation client library.
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:
ENDPOINT
: the Translation endpoint that you use for your organization. For more information, view service status and endpoints.PROJECT_ID
: your project ID.
Save the Python script.
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.