LoginSignup
0
1

More than 1 year has passed since last update.

クローズド環境における、お手軽機械翻訳の構築(構築時はネット接続必須)

Last updated at Posted at 2022-12-24

クローズド環境でのお手軽機械翻訳

はじめに(セキュリティ事故もろもろ)

世の中にはGoogle翻訳やDeepLなどウェブ翻訳サービスが色々あります。
社外秘の文書をそれらサービスに突っ込むとセキュリティ事故になりかねません。
(virustotalでも社外秘を突っ込む人が絶えない・・・、アップロードされたものは有料で見えるよ!)(インターネットつながる環境でそんなもの扱うな!)

と、いうわけで、お手軽にクローズドな環境で使える機械翻訳サービスを構築します。

目的

膨大な外国語文書からお目当ての文書を探したい。
(正確な翻訳でなくとも、なんとなく何を言っているかの判別ができればいい)

環境

OS:Linux(Ubuntu22.04)
※ WindowsのWSLでもできたけど、別途環境構築(特にCUDA利用)が必要なので今回はUbuntuで・・・
GPU:Geforce GTX 1050 2GB
※ GPU無しでもできたけど、翻訳速度が桁違い。(メモリ優先、VRAMいっぱい積もう)

ライブラリ

easyNMT」を利用しました。
(とにかくお手軽方針、pretrainedのモデルに全力で乗っかる)

Code(日→英)

sample.py
import easynmt
#引数でモデルを指定。予め用意されたモデルが色々あるので、easynmtのgithub参照
model = easynmt.EasyNMT('opus-mt')
#とりあえず憲法前文
sentence = "日本国民は、正当に選挙された国会における代表者を通じて行動し、われらとわれらの子孫のために、諸国民との協和による成果と、わが国全土にわたつて自由のもたらす恵沢を確保し、政府の行為によつて再び戦争の惨禍が起ることのないやうにすることを決意し、ここに主権が国民に存することを宣言し、この憲法を確定する。"
# 翻訳実行
translated = model.translate(sentence,target_lang="en",max_length=1000)
print(translated)

結果

※ 初回実行時はモデルのダウンロードが始まります。(300MBくらい、キャッシュされます)

'The Japanese people act through representatives of the legitimately elected parliament and, for our children and our children, the results of cooperation with the people, the benefits of freedom, and the benefits of government throughout our nation, are determined to ensure that the scourge of war will not occur again according to the actions of the government, and here they declare that sovereignty will remain with the people and establish this constitution.'

注意事項

GPUがあればGPUを使ってくれますが、その場合、モデルがGPUメモリに乗る必要があります。
メモリが足りないとエラーとなります

補足

サンプルで使った「opus-mt」は、英→日変換に対応していません。(聖書の語句を出してる?)
m2m_100等を試しましょう。

興味があれば、AIコミュニティのHuggingFaceを覗くと、いろいろモデル等が公開されています。

おまけ

EasyNMTは、予め用意されているモデル以外にもHuggingFaceで公開されているモデルに対応しているものがあるようです。
例。英→日Hugging Face

import easynmt
model = easynmt.EasyNMT(translator=easynmt.models.AutoModel('staka/fugumt-en-ja'))
sentence = 'We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defense, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.'
translated = model.translate(sentence,target_lang="ja",max_length=1000)
print(translated)

結果

'私たち米国民は、より完全な連合を形成し、正義を確立し、国内の静けさを保証し、共通の防衛を提供し、一般的な福祉を促進し、自由の恵みを私たち自身と私たちの後身に確保し、命令を行い、米国のためのこの憲法を確立します。'

0
1
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
0
1