LoginSignup
3
2

More than 1 year has passed since last update.

GCP Document TranslationでPDFを丸ごと翻訳する

Posted at

英語が苦手で、日頃Google翻訳やDeeplを駆使して論文を読んでいるのですが、
GCPでドキュメントを丸ごと翻訳する機能がプレビュー版で公開されたので試してみました。
Translation API Advanced can translate business documents across 100+ languages

なお、実施手順は公式ページの通りです。
Translate documents

手順

1. サービスアカウントの作成

https://cloud.google.com/docs/authentication/production?hl=JA#manually
もし今のプロジェクトにサービスアカウントが設定されていなければ、上記リンクの手順で作成できます。ロールは編集者でOKです。
2021-05-24-19-23-14.png

キーは忘れずにダウンロードしましょう。

2. Google Cloud SDKのインストール

https://cloud.google.com/sdk/docs/install?hl=JA
コマンドを実行するPCにSDKが導入されていなければ、上記リンクから導入します。
Cloud Shellで実行するのであれば不要です。

3. Cloud Storageの設定

翻訳したいドキュメントと、翻訳後のドキュメントを格納するバケットを作成します。リージョンはどこでもOKです。
2021-05-24-19-27-24.png

今回は最近話題の論文を翻訳します。
2021-05-24-19-27-57.png

gsutil URIは後ほど必要なのでメモっておきます。
2021-05-24-19-28-54.png

4. Cloud Translation APIの有効化

翻訳に必要なCloud Translation APIを有効にします。

翻訳するページ数あたりで料金がかかるため注意。
(1ページ10円は高い…)
image.png

5. 実行

翻訳したい内容に合わせて、request.jsonを作成します。

{
  "source_language_code": "SOURCE_LANGUAGE",
  "target_language_code": "TARGET_LANGUAGE",
  "document_input_config": {
    "gcsSource": {
      "inputUri": "gs://INPUT_FILE_PATH"
    }
  },
  "document_output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://OUTPUT_FILE_PREFIX"
    }
  }
}

具体的には以下の通りです。

{
  "source_language_code": "en",
  "target_language_code": "ja",
  "document_input_config": {
    "gcsSource": {
      "inputUri": "gs://translate-target/2105.08050.pdf"
    }
  },
  "document_output_config": {
    "gcsDestination": {
      "outputUriPrefix": "gs://translate-target/"
    }
  }
}

その後、Terminalから以下のコマンドを実行します。

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/v3beta1/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION:translateDocument

具体例は以下の通りです。

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/v3beta1/projects/trasnlation-documents-test/locations/us-central1:translateDocument

コンソールでだららっと文字が流れたら完了です。
2021-05-24-19-36-23.png

Cloud Storageに翻訳後のファイルが生成されています。
2021-05-24-19-37-45.png

翻訳結果

全般

2021-05-24-19-42-27.png
2021-05-24-19-39-01.png

翻訳したファイルのヘッダーに、Googleで機械翻訳した旨が記載されます。内容は普通のGoogle翻訳でしょうか。

数式

2021-05-24-19-40-22.png
2021-05-24-19-39-58.png

一部フォーマットが崩れる箇所がありました。ここは原文を読むしかないですね。

2021-05-24-19-40-57.png
2021-05-24-19-41-14.png

フォーマット崩れなく翻訳できてます。

グラフ

2021-05-24-19-41-52.png
2021-05-24-19-42-09.png

図の中まで翻訳されています。素晴らしい…

まとめ

今回はプレビュー版のDocument Translation機能を試しましたが、想定より綺麗に翻訳ができて感動しました。ただ、現状だと論文1本あたり100円程度費用がかかるため、破産しないように注意が必要ですね。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2