This quickstart guides the Application Operator (AO) through the process of using the Vertex AI Translation pre-trained API on Google Distributed Cloud (GDC) air-gapped.
Before you begin
Follow these steps before trying Translation:
Set up a project using the GDC console to group the Vertex AI services. For information about creating and using projects, see Create a project.
Ask your Project IAM Admin to grant you the AI Translation Developer (
ai-translation-developer
) role in your project namespace.Download the gdcloud command-line interface (CLI).
Set up your service account
Set up your service account with the name of your service account, project ID,
and service key. Replace the PROJECT_ID
with your project.
${HOME}/gdcloud init # set URI and project
${HOME}/gdcloud auth login
${HOME}/gdcloud iam service-accounts create SERVICE_ACCOUNT --project=PROJECT_ID
${HOME}/gdcloud iam service-accounts keys create "SERVICE_KEY".json --project=PROJECT_ID --iam-account=SERVICE_ACCOUNT
Grant access to project resources
Grant access to the Translation API service account by providing
your project ID, name of your service account, and the role
ai-translation-developer
.
${HOME}/gdcloud iam service-accounts add-iam-policy-binding --project=PROJECT_ID --iam-account=SERVICE_ACCOUNT --role=role/ai-translation-developer
Set your environment variables
Before running the Translation pre-trained service, set your environment variable.
export GOOGLE_APPLICATION_CREDENTIALS="SERVICE_KEY".json
Authenticate the gdcloud CLI
You must get a token to authenticate the gdcloud CLI before sending requests to the Translation pre-trained services. Follow these steps:
Install the
google-auth
client library.pip install google-auth
Save the following code to a Python script, and update the
ENDPOINT
to the Translation endpoint. For more information, see View service statuses and endpoints.import google.auth from google.auth.transport import requests api_endpoint = "https://ENDPOINT" creds, project_id = google.auth.default() creds = creds.with_gdch_audience(api_endpoint) def test_get_token(): req = requests.Request() creds.refresh(req) print(creds.token) if __name__=="__main__": test_get_token()
Run the script to fetch the token.
You must add the fetched token to the header of the
grpcurl
andcurl
requests as in the following example:-H "Authorization: Bearer TOKEN"
Make the
grpcurl
orcurl
request:grpcurl
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).Make the request:
grpcurl -plaintext -d '{"parent": "projects/PROJECT_ID", "source_language_code": "es", "target_language_code": "en", "contents": ["Hola, esto es una prueba"]}' ENDPOINT google.cloud.translation.v3.TranslationService/TranslateText
Replace the following:
PROJECT_ID
: your project ID number.ENDPOINT
: the Translation endpoint that you use for your organization.
curl
curl -X POST -H "Content-Type: application/json" http://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:
PROJECT_ID
: your project ID number.ENDPOINT
: the Translation endpoint that you use for your organization.
Run the Translation pre-trained API sample script
This example shows you how to interact with a Translation pre-trained API.
Check whether there is a client library installed.
pip freeze | grep translation # output example: google-cloud-translation==3.8.0
If the existing version doesn't match the client library in https://CONSOLE_ENDPOINT/.well-known/static/client-libraries, uninstall the client library using the following command.
pip uninstall google-cloud-translation
Specify the console endpoint and the client library for Translation (provided in the example).
wget https://CONSOLE_ENDPOINT/.well-known/static/client-libraries/google-cloud-translation
Extract the
tar
file, and install it usingpip
. If errors are generated because something isn't found, install any missing dependencies.tar -xvzf CLIENT_LIBRARY pip install -r FOLDER/requirements.txt --no-index --find-links FOLDER
Use the Translation client library script to generate the token, and make requests to the Translation service.
Set up your environment variable:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = ""SERVICE_KEY".json"
Translation sample
Replace the ENDPOINT
with the Translation endpoint that you use
for your organization.
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)
For more information on the translate_text
method, see the
Python client library.
What's next
- Learn more about how to Translate a language into English.