Vertex AI Translation 服務可將文字翻譯成任何其他支援的語言。
本頁面說明如何在 Google Distributed Cloud (GDC) 實體隔離方案,使用 Vertex AI Translation API 翻譯範例文字。
事前準備
如要翻譯文字,請與專案 IAM 管理員聯絡,要求在專案命名空間中取得「AI 翻譯開發人員」(ai-translation-developer) 角色。
如要進一步瞭解這個角色,請參閱「準備 IAM 權限」。
如要開始使用 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 支援的語言」。