← 研究笔记
无需调用翻译 API:离线运行 Argos Translate

无需调用翻译 API:离线运行 Argos Translate

我使用本地机器翻译模型运行了 Argos Translate,全程不调用云端翻译 API,并禁用了网络访问。一个内部依赖尝试发起网络请求,我追查了原因并修复了问题。

本文涉及的仓库

argosopentech/argos-translate

Open-source offline translation library written in Python

6.3k Python MIT License

许多应用都会调用外部翻译 API。每处理一项内容,就要发起一次网络请求、管理一个 API 密钥,还要把待处理的文本发送到公司外部的服务器。Argos Translate 可以把模型保留在本地机器上,在没有网络访问的情况下完成处理。我安装了这个库,并验证了它在无网络访问时仍能正常翻译。

Argos Translate 是一个可离线运行、基于神经网络的机器翻译库。它以语言对为单位提供翻译模型包。源代码采用 MIT License 发布(*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