借助 Vertex AI Translation 服务,您可以将以一种语言编写的文本翻译成任何其他受支持的语言 。
本页介绍了如何在 Google Distributed Cloud (GDC) air-gapped 上使用 Vertex AI Translation API 翻译示例文本。
准备工作
如需开始使用 Vertex AI Translation API,您必须拥有一个启用了 Vertex AI Translation API 的项目,并且必须拥有适当的凭据。您还可以安装客户端库,以便调用 API。如需了解详情,请参阅 设置翻译项目。
翻译文字
translateText 方法会接收特定语言的输入文本,并返回翻译成另一种语言的文本。您可以输入纯文本或 HTML 文本作为输入。
如果您输入 HTML 文本,translateText 方法只会翻译 HTML 标记之间的文本,而不会翻译标记。不过,它会翻译 HTML5 标记中的属性,例如 alt 属性。在语法中,使用 HTML5
标记和属性的示例用于从翻译中排除文本。
输出会保留未翻译的 HTML 标记,并在其中包含翻译后的文本。
向 Vertex AI Translation 预训练 API 发出 curl 请求。否则,请通过 Python 脚本与 Vertex AI Translation 预训练 API 进行交互,以将文本从一种语言翻译成另一种语言。
以下示例展示了如何将输入文本从一种语言翻译成另一种语言:
curl
如需发出 curl 请求,请按以下步骤操作:
发出请求:
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": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "contents": ["INPUT_TEXT"]}'
替换以下内容:
TOKEN:身份验证令牌您获得的。ENDPOINT:您组织使用的 Vertex AI Translation 端点。如需了解详情,请查看服务状态和端点。PROJECT_ID:您的项目 ID。SOURCE_LANGUAGE:输入文本的语言代码。 请参阅支持的语言及其 各自的语言代码列表。TARGET_LANGUAGE:您要将文本翻译成的语言的语言代码。请参阅支持的语言及其各自的语言代码列表。INPUT_TEXT:源语言的输入文本。
使用 mime_type 字段指定文件类型。将 mime_type 字段设置为以下某个值:
text/plain:您的输入是纯文本。text/html:您的输入是 HTML 文本。
如果 mime_type 字段为空,则默认值为 text/html。
以下示例使用了 mime_type 字段:
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d '{"mime_type": "text/html", "parent": "projects/PROJECT_ID", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "contents": ["INPUT_TEXT"]}'
输出会返回翻译后的文本。
Python
如需通过 Python 脚本使用 Vertex AI Translation 服务,请按以下步骤操作:
将以下代码添加到您创建的 Python 脚本中:
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": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "mime_type": "text/plain", "contents": ["INPUT_TEXT"] } resp = tc.translate_text(req) print(resp) if __name__=="__main__": creds = main() translate_func(creds)替换以下内容:
ENDPOINT:您组织使用的 Vertex AI Translation 端点。如需了解详情,请查看服务状态和端点。PROJECT_ID:您的项目 ID。SOURCE_LANGUAGE:输入文本的语言代码。 请参阅支持的语言及其 各自的语言代码列表。TARGET_LANGUAGE:您要将文本翻译成的语言的语言代码。请参阅支持的语言及其各自的语言代码列表。INPUT_TEXT:源语言的输入文本。
使用
mime_type字段指定文件类型。将mime_type字段设置为以下某个值:text/plain:您的输入是纯文本。text/html:您的输入是 HTML 文本。
如果
mime_type字段为空,则默认值为text/html。保存 Python 脚本。
运行 Python 脚本以翻译文本:
python SCRIPT_NAME将
SCRIPT_NAME替换为您为 Python 脚本指定的名称,例如translation.py。
如需详细了解 translateText 方法,请参阅 Python 客户端库。
从翻译中排除文本
在请求的 contents 字段中使用以下某个 HTML 标记,以从翻译中排除部分文本:
<span translate="no">"TEXT"</span><span class="notranslate">"TEXT"</span>
将 TEXT 替换为您要从翻译中排除的文本部分。
例如,如果您有以下西班牙语输入文本:
Hola, esto es una prueba.
预期翻译成英语的文本为:
Hello, this is a test.
假设您只想翻译文本的以下部分,从输入文本中排除 Hola,:
esto es una prueba.
预期翻译成英语的文本为:
this is a test.
使用 HTML 标记从翻译中排除文本。例如,以下 curl 请求使用 <span class="notranslate">"TEXT"</span>
标记在将文本翻译成英语时从之前的西班牙语输入文本中排除 Hola,:
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": ["<span class=\"notranslate\">\"Hola,\"</span>\"esto es una prueba.\""]}'
使用术语表进行翻译
如需使用术语表中的字词翻译文本,您必须创建术语表:
保存以下
request_glossary.json文件:cat <<- EOF > request_glossary.json { "sourceLanguageCode": "SOURCE_LANGUAGE", "targetLanguageCode": "SOURCE_LANGUAGE", "contents": "TEXT", "glossaryConfig": { "glossary": "projects/PROJECT_ID/glossaries/GLOSSARY_ID", "ignoreCase": IGNORE_CASE_BOOLEAN, "contextual_translation_enabled": CONTEXTUAL_BOOLEAN } } EOF替换以下内容:
发出请求:
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:translateText -d @request_glossary.json
示例和预期输出:
请求:
{
"sourceLanguageCode": "es",
"targetLanguageCode": "en",
"contents": "La novela cuenta la historia de una joven que viaja a Madrid",
"glossaryConfig": {
"glossary": "projects/test-project/glossaries/GLOSSARY_ID",
"ignoreCase": true,
"contextual_translation_enabled": true
}
}
回答:
{"translations":[
{
"translatedText":"The novel tells the story of a young woman who travels to Madrid"}],
"glossaryTranslations":[{"translatedText":"The novel account the story of a young woman who travels to Madrid",
"glossaryConfig":
{
"glossary":"projects/test-project/glossaries/GLOSSARY_ID"
}}
]}
translations 字段包含应用术语表之前的常规机器翻译;glossaryTranslations 字段包含应用术语表之后的翻译。当您将 contextual_translation_enabled 字段设置为 true 时,响应将仅包含 glossaryTranslations 字段,而不包含 translations 字段。
检测语言
detectLanguage 方法通过发送 HTTP 请求返回文本字符串的语言。
例如,以下请求会将输入文本 Hello, this is a test 的语言检测为英语:
curl
curl -vv -X POST -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID:detectLanguage -d '{"parent": "projects/PROJECT_ID", "content": "Hello, this is a test"}'
获取操作
getOperation 方法返回
长时间运行的操作的最新状态。使用此方法可检索 Vertex AI Translation API 服务生成的操作结果。如需使用此方法,请指定您的
项目 ID 和 Vertex AI Translation 端点。
例如,以下请求会返回在您的项目中运行的长时间运行的 操作(例如创建术语表) 的状态:
curl
curl -vv -X GET -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/operations/PROJECT_ID
列出操作
listOperations 方法会返回与请求中指定的过滤条件匹配的 长时运行操作
的列表。如需使用此方法,请指定您的
项目 ID 和 Vertex AI Translation 端点。
例如,以下请求会返回在您的项目中运行的操作列表,并将页面大小限制为每页 10 个结果:
curl
curl -vv -X GET -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/operations?page_size=10
获取支持的语言
supportedLanguages 方法会返回 Vertex AI Translation API 支持的语言列表。
例如,以下请求会通过指定 Vertex AI Translation 端点返回支持的语言:
curl
curl -vv -X GET -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/locations/PROJECT_ID/supportedLanguages
如需查看支持的语言的完整列表,请参阅 Vertex AI Translation 支持的语言。