許多應用程式會呼叫外部翻譯 API。每處理一個項目,都需要發出網路請求、管理 API 金鑰,並將處理的文字傳送至公司外部的伺服器。Argos Translate 可將模型保留在本機,並在沒有網路存取的情況下執行處理。我安裝了這個程式庫,並在無網路存取的情況下驗證翻譯。
Argos Translate 是一個以神經網路為基礎的離線機器翻譯程式庫。它針對每組語言配對,以套件形式提供翻譯模型。原始碼以 MIT 授權條款提供 (*1)。
1. 安裝 Argos Translate 並取得語言模型
我在建置時趁網路可用,下載了英譯日模型套件並將其納入映像檔。
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'argostranslate==1.11.0'
COPY install_model.py /tmp/install_model.py
RUN python /tmp/install_model.py
WORKDIR /work
# install_model.py
import argostranslate.package
import argostranslate.translate
argostranslate.package.update_package_index()
available = argostranslate.package.get_available_packages()
pkg = next(p for p in available if p.from_code == "en" and p.to_code == "ja")
path = pkg.download()
argostranslate.package.install_from_path(path)
print("installed:", pkg)
# also triggers the first download of the sentence-splitting model (more on this below)
warmup = argostranslate.translate.translate("Warm up the pipeline.", "en", "ja")
print("warmup translation:", warmup)
podman build -t argos-translate-sandbox .
2. 遇到的問題:離線執行失敗
我在建置好的映像檔中停用網路存取,然後執行翻譯。
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work argos-translate-sandbox \
python translate_demo.py
-v 選項會讓容器能夠讀寫指定目錄。請在專供實驗使用的空目錄中執行此命令,不要在該目錄中放置私密金鑰或憑證。
命令執行失敗並顯示錯誤。
urllib3.exceptions.NameResolutionError: HTTPSConnection(host='raw.githubusercontent.com', ...):
Failed to resolve 'raw.githubusercontent.com' ...
File ".../argostranslate/sbd.py", line 149, in lazy_pipeline
self.stanza_pipeline = stanza.Pipeline(...)
翻譯模型位於本機。負責句子分段(決定句子切分位置)的內部依賴套件 Stanza 嘗試從 GitHub 取得設定檔,但失敗了。Stanza 的 Pipeline() 使用 DOWNLOAD_RESOURCES 作為 download_method 的預設值。即使模型在本機,Stanza 在使用本機資源之前,仍會透過網路取得最新的設定檔。Argos Translate 包含一個本機設定檔(resources.json),但其檔名與 Stanza 搜尋的檔案不符。Stanza 找不到本機檔案,因此嘗試發出網路請求。
3. 修正方式:重複使用本機資源
Stanza 提供 REUSE_RESOURCES 設定,指示它使用本機資源而不檢查網路。Argos Translate 沒有傳入這個值,因此我在自己的指令碼中包裝 Stanza 的 Pipeline,覆寫預設值。
import functools
import stanza
from stanza.pipeline.core import DownloadMethod
stanza.Pipeline = functools.partial(
stanza.Pipeline, download_method=DownloadMethod.REUSE_RESOURCES
)
import argostranslate.translate
text = "Open source software lets anyone read, modify, and redistribute the code. This freedom is why so many developers trust it for critical infrastructure."
translated = argostranslate.translate.translate(text, "en", "ja")
print("INPUT: ", text)
print("OUTPUT:", translated)
我使用相同的 --network none 設定重新執行命令。
INPUT: Open source software lets anyone read, modify, and redistribute the code. This freedom is why so many developers trust it for critical infrastructure.
OUTPUT: オープンソースソフトウェアは、コードを読み、変更、再配布することができます。 この自由は、多くの開発者が重要なインフラを信頼する理由です。
翻譯完成時沒有任何網路流量。句子的切分自然,意思也保持完整。
本機模型並不能保證離線執行。程式庫可能包含會自行發出網路請求的依賴套件。執行時若發生錯誤,請使用 --network none 並檢查翻譯模型底層的依賴套件。
不使用雲端服務進行翻譯
有些翻譯工作可以不使用外部 API 執行。將模型保留在本機,並驗證是否能離線執行。這樣的設定讓必須留在公司內部的文字也能翻譯,無須放棄翻譯功能。加入一個選項,便能在設計上獲得更大的彈性。
來源
*1 Argos Translate (GitHub), https://github.com/argosopentech/argos-translate, MIT License