Using Cloud Translation - Advanced API
After having successfully trained your model, you can translate content
using the
Cloud Translation - Advanced API
translateText
method. The Cloud Translation - Advanced API supports using
glossaries
and Batch translation requests.
REST
Make sure you have enabled the Cloud AutoML API for your project. This is necessary when using AutoML models with AutoML API. See this Getting started document to enable the API.Before using any of the request data, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:translateText
Request JSON body:
{ "model": "projects/project-number-or-id/locations/us-central1/models/TRL1395675701985363739", "sourceLanguageCode": "en", "targetLanguageCode": "ru", "contents": ["Dr. Watson, please discard your trash. You've shared unsolicited email with me. Let's talk about spam and importance ranking in a confidential mode."] }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:translateText"
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/v3/projects/project-number-or-id/locations/us-central1:translateText" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "translation": { "translatedText": "Доктор Ватсон, пожалуйста, откажитесь от своего мусора. Вы поделились нежелательной электронной почтой со мной. Давайте поговорим о спаме и важности рейтинга в конфиденциальном режиме.", "model": "projects/project-number/locations/us-central1/models/TRL1395675701985363739" } }
Go
Java
Node.js
Python
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for Ruby.
Using AutoML Translation
You can also use AutoML Translation to translate content by using custom models.
Web UI
Go to the AutoML Translation models page in the Google Cloud console.
If the model that you want to use is located in another project, select the project from the project selector in the title bar.
From the list of models, select the one you will use to translate text.
Click the model's Predict tab.
In the text box, enter the content to translate and then click Translate.
You can compare the results from your custom model to the base model (Google NMT model), which Cloud Translation - Advanced uses by default.
REST
Before using any of the request data, make the following replacements:
- model-name: the full name of your model. The full
name of your model includes your project name and location. A
model name looks similar to the following example:
projects/project-id/locations/us-central1/models/model-id
. - source-language-text: the text that you want to translate from the source language to the target language
- project-id: your Google Cloud Platform project ID
HTTP method and URL:
POST https://automl.googleapis.com/v1/model-name:predict
Request JSON body:
{ "payload" : { "textSnippet": { "content": "source-language-text" } } }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: project-id" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://automl.googleapis.com/v1/model-name:predict"
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "project-id" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://automl.googleapis.com/v1/model-name:predict" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "payload": [ { "translation": { "translatedContent": { "content": "target-language-text" } } } ] }
Go
Java
Node.js
Python
Before you can run this code example, you must install the Python Client Libraries.- The
model_full_id
parameter is the full name of your model. For example:projects/434039606874/locations/us-central1/models/3745331181667467569
.
Additional languages
C#: Please follow the C# setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for .NET.
PHP: Please follow the PHP setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for PHP.
Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the AutoML Translation reference documentation for Ruby.