Vertex AI Translation 和光學字元辨識 (OCR) 服務結合,提供稱為 Document Translate 的文件處理功能。
Document Translation 可直接翻譯 PDF 等格式化文件。與純文字翻譯相比,這項功能可保留翻譯文件的原始格式和版面配置,協助您保留大部分的原始內容,例如段落換行。
Document Translate 支援內嵌、從 Storage bucket 和批次翻譯文件。
本頁面將引導您使用 Google Distributed Cloud (GDC) 實體隔離方案的文件處理功能,在保留文件格式的情況下翻譯文件。
支援的格式
Document Translate 支援下列輸入檔案類型和相關聯的輸出檔案類型:
| 輸入內容 | 文件 MIME 類型 | 輸出內容 |
|---|---|---|
application/pdf |
PDF、DOCX | |
| DOC | application/msword |
DOC、DOCX |
| DOCX | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
DOCX |
| PPT | application/vnd.ms-powerpoint |
PPT、PPTX |
| PPTX | application/vnd.openxmlformats-officedocument.presentationml.presentation |
PPTX |
| XLS | application/vnd.ms-excel |
XLS、XLSX |
| XLSX | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
XLSX |
翻譯原始和掃描的 PDF 文件
Document Translation 支援原始 PDF 檔案和 PDF 掃描檔,包括翻譯成由右至左書寫的語言,或將由右至左書寫的語言翻譯為一般語言。此外,Document Translate 會保留檔案中的超連結、字型大小和字型顏色。
事前準備
您必須先建立名為 dt-project 的專案,才能開始使用文件處理功能。專案的自訂資源必須如下列範例所示:
apiVersion: resourcemanager.gdc.goog/v1
kind: Project
metadata:
labels:
atat.config.google.com/clin-number: CLIN_NUMBER
atat.config.google.com/task-order-number: TASK_ORDER_NUMBER
name: dt-project
namespace: platform
此外,您必須啟用 Vertex AI Translation 和 OCR 預先訓練 API,並取得適當的憑證。建議安裝 Vertex AI Translation 和 OCR 用戶端程式庫,以便呼叫 API。如要進一步瞭解必要條件,請參閱「設定翻譯專案」。
翻譯儲存空間值區中的文件
如要翻譯儲存在 bucket 中的文件,請使用 Vertex AI Translation API。
本節說明如何翻譯 bucket 中的文件,並將結果儲存至另一個輸出 bucket 路徑。回應也會傳回位元組串流。您可以指定 MIME 類型。如未指定,Document Translation 會根據輸入檔案的副檔名判斷。
Document Translate 支援自動偵測 bucket 中文件的語言。如未指定原文語言代碼,Document Translation 會自動偵測語言,偵測到的語言會納入輸出內容的 detectedLanguageCode 欄位。
請按照下列步驟,翻譯儲存空間 bucket 中的文件:
- 設定 gdcloud CLI 以使用物件儲存庫。
在
dt-project命名空間中建立儲存空間 bucket。使用Standard儲存空間級別。您可以在
dt-project命名空間中部署Bucket資源,建立 Storage bucket:apiVersion: object.gdc.goog/v1 kind: Bucket metadata: name: dt-bucket namespace: dt-project spec: description: bucket for document vision service storageClass: Standard bucketPolicy: lockingPolicy: defaultObjectRetentionDays: 90將 bucket 的
read和write權限授予 Vertex AI Translation 服務使用的服務帳戶 (ai-translation-system-sa和g-vai-translation-sie-sa)。請按照下列步驟,在 Management API 伺服器上使用自訂資源建立角色和角色繫結:
在
dt-project命名空間中部署Role資源,建立角色:apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: dvs-reader-writer namespace: dt-project rules: - apiGroups: - object.gdc.goog resources: - buckets verbs: - read-object - write-object在
dt-project命名空間中部署RoleBinding資源,建立角色繫結:apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: dvs-reader-writer-rolebinding namespace: dt-project roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: dvs-reader-writer subjects: - kind: ServiceAccount name: ai-translation-system-sa namespace: g-vai-translation-sie - kind: ServiceAccount name: g-vai-translation-sie-sa namespace: g-vai-translation-sie
將文件上傳至您建立的 Storage bucket。詳情請參閱「上傳及下載專案中的儲存空間物件」。
向 Vertex AI Translation 預先訓練 API 提出要求:
curl
請按照下列步驟提出
curl要求:儲存下列
request.json檔案:cat <<- EOF > request.json { "parent": "projects/PROJECT_ID/locations/PROJECT_ID", "source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "document_input_config": { "mime_type": "application/pdf", "s3_source": { "input_uri": "s3://INPUT_FILE_PATH" } }, "document_output_config": { "mime_type": "application/pdf" }, "enable_rotation_correction": "true" } EOF更改下列內容:
PROJECT_ID:您的專案 ID。SOURCE_LANGUAGE:文件使用的語言。查看支援的語言清單和對應的語言代碼。TARGET_LANGUAGE:要將文件翻譯成哪種語言。查看支援的語言清單和對應的語言代碼。INPUT_FILE_PATH:文件檔案在儲存空間 bucket 中的路徑。
根據文件修改
mime_type值。提出要求:
curl -vv --data-binary @- -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT:443/v3/projects/PROJECT_ID/locations/PROJECT_ID:translateDocument < request.json更改下列內容:
在文件中翻譯
本節說明如何將文件內嵌在 API 要求中傳送。您必須加入內嵌文件翻譯作業的 MIME 類型。
文件翻譯功能支援自動偵測內嵌文字翻譯的語言。如未指定原文語言代碼,Document Translation 會自動偵測語言,偵測到的語言會納入輸出內容的 detectedLanguageCode 欄位。
向 Vertex AI Translation 預先訓練 API 提出要求:
curl
請按照下列步驟提出 curl 要求:
提出要求:
echo '{"parent": "projects/PROJECT_ID/locations/PROJECT_ID","source_language_code": "SOURCE_LANGUAGE", "target_language_code": "TARGET_LANGUAGE", "document_input_config": { "mime_type": "application/pdf", "content": "'$(base64 -w 0 INPUT_FILE_PATH)'" }, "document_output_config": { "mime_type": "application/pdf" }, "enable_rotation_correction": "true"}' | curl --data-binary @- -H "Content-Type: application/json" -H "Authorization: Bearer TOKEN" https://ENDPOINT/v3/projects/PROJECT_ID/locations/PROJECT_ID:translateDocument
更改下列內容:
批次翻譯文件
批次翻譯可讓您透過單一要求,將多個檔案翻譯成多種語言。每項要求最多可傳送 100 個檔案,內容大小總上限為 1 GB 或 1 億個 Unicode 碼點 (以先達到上限者為準)。您可以為每種語言指定特定翻譯模型。
詳情請參閱「batchTranslateDocument」。
翻譯多份文件
以下範例包含多項輸入設定,每項輸入設定都是指向儲存空間 bucket 中檔案的指標。
向 Vertex AI Translation 預先訓練 API 提出要求:
curl
請按照下列步驟提出 curl 要求:
將下列要求主體儲存至名為
request.json的檔案:{ "source_language_code": "SOURCE_LANGUAGE", "target_language_codes": ["TARGET_LANGUAGE"], "input_configs": [ { "s3_source": { "input_uri": "s3://INPUT_FILE_PATH_1" } }, { "s3_source": { "input_uri": "s3://INPUT_FILE_PATH_2" } } ], "output_config": { "s3_destination": { "output_uri_prefix": "s3://OUTPUT_FILE_PREFIX" } } }更改下列內容:
提出要求:
curl -X POST \ -H "Authorization: Bearer TOKEN" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://ENDPOINT:443/v3/projects/PROJECT_ID/locations/PROJECT_ID:batchTranslateDocument"更改下列內容:
回應會包含長時間執行的作業 ID:
{
"name": "projects/PROJECT_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
"state": "RUNNING"
}
}
翻譯及轉換原始 PDF 檔案
以下範例會翻譯原始 PDF 檔案,並轉換為 DOCX 檔案。輸入內容不一定要是原始 PDF 檔案,您可以指定多個不同檔案類型。不過,如果要求轉換檔案,就無法加入掃描的 PDF 檔案,否則系統會拒絕要求,且不會翻譯內容。系統只會在翻譯原始 PDF 檔案後,將其轉換為 DOCX 檔案。舉例來說,如果加入 PPTX 檔案,系統會翻譯並傳回 PPTX 檔案。
如果您經常翻譯原始 PDF 檔案和 PDF 掃描檔,建議將這兩種檔案分別整理到不同的 bucket。這樣一來,當您要求批次翻譯和轉換時,就能輕鬆排除含有 PDF 掃描檔的 bucket,無須逐一排除檔案。
向 Vertex AI Translation 預先訓練 API 提出要求:
curl
請按照下列步驟提出 curl 要求:
將下列要求主體儲存至名為
request.json的檔案:{ "source_language_code": "SOURCE_LANGUAGE", "target_language_codes": ["TARGET_LANGUAGE"], "input_configs": [ { "s3_source": { "input_uri": "s3://INPUT_FILE_PATH_1" } }, { "s3_source": { "input_uri": "s3://INPUT_FILE_PATH_2" } } ], "output_config": { "s3_destination": { "output_uri_prefix": "s3://OUTPUT_FILE_PREFIX" } }, "format_conversions": { "application/pdf": "application/vnd.openxmlformats-officedocument.wordprocessingml.document" } }更改下列內容:
提出要求:
curl -X POST \ -H "Authorization: Bearer TOKEN" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://ENDPOINT:443/v3/projects/PROJECT_ID/locations/PROJECT_ID:batchTranslateDocument"更改下列內容:
回應會包含長時間執行的作業 ID:
{
"name": "projects/PROJECT_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
"state": "RUNNING"
}
}
使用詞彙表
您可以加入詞彙表,處理特定領域的專有術語。指定詞彙表時,必須指定原文語言。以下範例使用詞彙表。您最多可指定 10 種譯文語言,並為每種語言設定專屬詞彙表。
如果您只為部分譯文語言指定詞彙表,系統不會為未指定的語言設定任何詞彙表。
向 Vertex AI Translation 預先訓練 API 提出要求:
curl
請按照下列步驟提出 curl 要求:
將下列要求主體儲存至名為
request.json的檔案:{ "source_language_code": "SOURCE_LANGUAGE", "target_language_codes": ["TARGET_LANGUAGE"], "input_configs": [ { "s3_source": { "input_uri": "s3://INPUT_FILE_PATH" } } ], "output_config": { "s3_destination": { "output_uri_prefix": "s3://OUTPUT_FILE_PREFIX" } }, "glossaries": { "TARGET_LANGUAGE": { "glossary": "projects/GLOSSARY_PROJECT_ID/glossaries/GLOSSARY_ID" } } }更改下列內容:
提出要求:
curl -X POST \ -H "Authorization: Bearer TOKEN" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://ENDPOINT:443/v3/projects/PROJECT_ID/locations/PROJECT_ID:batchTranslateDocument"更改下列內容:
回應會包含長時間執行的作業 ID:
{
"name": "projects/PROJECT_ID/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.translation.v3.BatchTranslateDocumentMetadata",
"state": "RUNNING"
}
}