← 연구 노트
번역 API를 호출하지 않고 번역하기: Argos Translate 오프라인 실행

번역 API를 호출하지 않고 번역하기: Argos Translate 오프라인 실행

클라우드 번역 API를 사용하지 않고 네트워크 접근을 차단한 상태에서 로컬 기계 번역 모델로 Argos Translate를 실행했습니다. 내부 의존성 하나가 네트워크 요청을 시도했기에 원인을 추적해 수정했습니다.

이 글에서 다루는 리포지터리

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