commit f24b50ceaecde7e619d3c0c484e0d0ce13ecc720 Author: Syping Date: Thu Feb 22 02:11:42 2024 +0100 initial commit diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..4a4fbce --- /dev/null +++ b/Containerfile @@ -0,0 +1,53 @@ +FROM almalinux:9 + +ARG DPP_COMMIT=9ccd5db6171862f85c481974a6b3acd6d3ae6741 +ARG DTB_COMMIT=7dcb64f7cf0244dac75b56d8e36dcceb1d683fde + +RUN dnf install -y python3 python3-requests && \ + dnf clean all +RUN dnf install -y python3-pip && \ + dnf clean all && \ + mkdir -p /opt/libretranslate && \ + python3 -m venv /opt/libretranslate && \ + source /opt/libretranslate/bin/activate && \ + pip3 install libretranslate==1.5.5 \ + --extra-index-url https://download.pytorch.org/whl/cpu && \ + dnf remove -y python3-pip +RUN dnf install -y yum-utils && \ + dnf config-manager --set-enabled crb && \ + dnf install -y cmake clang git ninja-build openssl-devel openssl-libs zlib zlib-devel && \ + mkdir -p /root/src && \ + cd /root/src && \ + git clone https://github.com/brainboxdotcc/DPP.git dpp && \ + cd dpp && \ + git reset --hard $DPP_COMMIT && \ + cd /root/src && \ + cmake -B dpp-build dpp \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_SHARED_LINKER_FLAGS=-s \ + -GNinja && \ + cmake --build dpp-build && \ + cmake --install dpp-build && \ + git clone https://github.com/Syping/dtranslatebot.git dtranslatebot && \ + cd dtranslatebot && \ + git reset --hard $DBT_COMMIT && \ + cd /root/src && \ + cmake -B dtranslatebot-build dtranslatebot \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_EXE_LINKER_FLAGS=-s \ + -DCMAKE_INSTALL_RPATH=/usr/local/lib64 \ + -GNinja && \ + cmake --build dtranslatebot-build && \ + cmake --install dtranslatebot-build && \ + cd /root && \ + rm -rf \ + /root/src \ + /usr/local/include \ + /usr/local/lib64/cmake \ + /usr/local/lib64/pkgconfig && \ + dnf remove -y cmake clang git openssl-devel yum-utils zlib-devel && \ + dnf clean all +COPY dtranslatebot-ltd.py /usr/local/bin/dtranslatebot-ltd +ENTRYPOINT ["/usr/local/bin/dtranslatebot-ltd"] diff --git a/dtranslatebot-ltd.py b/dtranslatebot-ltd.py new file mode 100755 index 0000000..74153bb --- /dev/null +++ b/dtranslatebot-ltd.py @@ -0,0 +1,54 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +import json +import requests +import os +import subprocess +import sys +import time + +def main(): + if len(sys.argv) != 2: + print("Usage: dtranslatebot-ltd [token]") + return + + dtb_config = { + 'token': sys.argv[1], + 'translator': { + 'url': 'http://127.0.0.1:5000/' + } + } + dtb_json = json.dumps(dtb_config) + with open("/usr/local/etc/dtranslatebot.json", "w") as dtb_json_file: + dtb_json_file.write(dtb_json) + + os.makedirs("/root/libretranslate", mode=755, exist_ok=True) + lt_env = os.environ.copy() + lt_env["LT_HOST"] = "127.0.0.1" + lt_env["LT_PORT"] = "5000" + lt_env["LT_DISABLE_FILES_TRANSLATION"] = "1" + lt_env["LT_DISABLE_WEB_UI"] = "1" + print("[Service] Launching LibreTranslate...") + lt = subprocess.Popen(["/opt/libretranslate/bin/libretranslate"], cwd="/root/libretranslate", env=lt_env) + + while True: + try: + r = requests.get("http://127.0.0.1:5000/languages", timeout=5) + if r.status_code == 200: + break; + return r.status_code + except requests.exceptions.ConnectionError: + time.sleep(1) + except requests.exceptions.Timeout: + pass + + os.makedirs("/root/dtranslatebot", mode=755, exist_ok=True) + os.makedirs("/root/dtranslatebot/db", mode=755, exist_ok=True) + dtb_env = os.environ.copy() + dtb_env["DTRANSLATEBOT_STORAGE"] = "/root/dtranslatebot/db" + print("[Service] Launching dtranslatebot...") + dtb = subprocess.run(["/usr/local/bin/dtranslatebot", "/usr/local/etc/dtranslatebot.json"], cwd="/root/dtranslatebot", env=dtb_env) + return dtb.returncode + +if __name__ == "__main__": + sys.exit(main())