initial commit

This commit is contained in:
Syping 2024-02-22 02:11:42 +01:00
commit f24b50ceae
2 changed files with 107 additions and 0 deletions

53
Containerfile Normal file
View file

@ -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"]

54
dtranslatebot-ltd.py Executable file
View file

@ -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())