使用 Cloud Translation 連接器執行批次翻譯作業


本教學課程將說明如何建立工作流程,使用 Cloud Translation API 連接器,以非同步批次模式將檔案翻譯成其他語言。這可在處理輸入內容時提供即時輸出內容。

目標

教學課程內容:

  1. 建立輸入 Cloud Storage 值區。
  2. 請以英文建立兩個檔案,然後上傳至輸入值區。
  3. 建立工作流程,使用 Cloud Translation API 連接器將兩個檔案翻譯成法文和西班牙文,並將結果儲存在輸出值區。
  4. 部署及執行工作流程,以便自動化整個流程。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Storage, Translation, and Workflows APIs:

    gcloud services enable storage.googleapis.com<wbr>&nbsp;translate.googleapis.com<wbr>&nbsp;workflows.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the Cloud Storage, Translation, and Workflows APIs:

    gcloud services enable storage.googleapis.com<wbr>&nbsp;translate.googleapis.com<wbr>&nbsp;workflows.googleapis.com
  14. 更新 gcloud 元件:
    gcloud components update
  15. 使用自己的帳戶登入:
    gcloud auth login
  16. 設定本教學課程中使用的預設位置:
    gcloud config set workflows/location us-central1

    由於本教學課程使用位於 us-central1 的預設 AutoML Translation 模型,因此您必須將位置設為 us-central1

    如果您使用的是預設以外的 AutoML 翻譯模型或詞彙表,請務必確保該模型或詞彙表位於與連接器呼叫相同的位置,否則系統會傳回 INVALID_ARGUMENT (400) 錯誤。詳情請參閱 batchTranslateText 方法

建立輸入 Cloud Storage 值區和檔案

您可以使用 Cloud Storage 儲存物件。物件是不可變更的資料片段,由任何格式的檔案組成,並儲存在稱為值區的容器中。

  1. 建立 Cloud Storage 值區,用於儲存要翻譯的檔案:

    BUCKET_INPUT=${GOOGLE_CLOUD_PROJECT}-input-files
    gcloud storage buckets create gs://${BUCKET_INPUT}
  2. 建立兩個英文檔案,並上傳至輸入值區:

    echo "Hello World!" > file1.txt
    gcloud storage cp file1.txt gs://${BUCKET_INPUT}
    echo "Workflows connectors simplify calling services." > file2.txt
    gcloud storage cp file2.txt gs://${BUCKET_INPUT}

部署及執行工作流程

工作流程由一系列步驟組成,這些步驟可使用 Workflows 語法描述,並以 YAML 或 JSON 格式編寫。這是工作流程的定義。建立工作流程後,您可以部署工作流程,讓系統執行。

  1. 建立檔案名稱為 workflow.yaml 且包含下列內容的文字檔案:

    main:
      steps:
      - init:
          assign:
          - projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
          - location: ${sys.get_env("GOOGLE_CLOUD_LOCATION")}
          - inputBucketName: ${projectId + "-input-files"}
          - outputBucketName: ${projectId + "-output-files-" + string(int(sys.now()))}
      - createOutputBucket:
            call: googleapis.storage.v1.buckets.insert
            args:
              project: ${projectId}
              body:
                name: ${outputBucketName}
      - batchTranslateText:
          call: googleapis.translate.v3beta1.projects.locations.batchTranslateText
          args:
              parent: ${"projects/" + projectId + "/locations/" + location}
              body:
                  inputConfigs:
                    gcsSource:
                      inputUri: ${"gs://" + inputBucketName + "/*"}
                  outputConfig:
                      gcsDestination:
                        outputUriPrefix: ${"gs://" + outputBucketName + "/"}
                  sourceLanguageCode: "en"
                  targetLanguageCodes: ["es", "fr"]
          result: batchTranslateTextResult

    工作流程會指派變數、建立輸出值區,並啟動檔案翻譯作業,將結果儲存至輸出值區。

  2. 建立工作流程後,請部署工作流程:

    gcloud workflows deploy batch-translation --source=workflow.yaml
  3. 執行工作流程:

    gcloud workflows execute batch-translation
  4. 如要查看工作流程狀態,您可以執行傳回的指令。例如:

    gcloud workflows executions describe eb4a6239-cffa-4672-81d8-d4caef7d8424 /
      --workflow batch-translation /
      --location us-central1

    工作流程應為 ACTIVE。幾分鐘後,系統會將翻譯完成的檔案 (法文和西班牙文) 上傳至輸出值區。

列出輸出值區中的物件

您可以列出輸出值區中的物件,確認工作流程是否正常運作。

  1. 擷取輸出值區名稱:

    gcloud storage ls

    輸出結果會與下列內容相似:

    gs://PROJECT_ID-input-files/
    gs://PROJECT_ID-output-files-TIMESTAMP/

  2. 列出輸出值區中的物件:

    gcloud storage ls gs://PROJECT_ID-output-files-TIMESTAMP/** --recursive

    幾分鐘後,系統會列出翻譯完成的檔案,包括法文和西班牙文各兩個。

清除所用資源

如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有的專案,且希望保留該專案而不採用本教學課程中新增的變更,請刪除為教學課程建立的資源

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除教學課程資源

  1. 移除在教學課程設定期間新增的 gcloud 預設設定:

    gcloud config unset workflows/location
  2. 刪除在本教學課程中建立的工作流程:

    gcloud workflows delete WORKFLOW_NAME
  3. 刪除在本教學課程中建立的值區及其物件:

    gcloud storage rm gs://BUCKET_NAME --recursive

    其中 BUCKET_NAME 是要刪除的值區名稱。例如:my-bucket

    回應如下所示:

    Removing gs://my-bucket/...

後續步驟