beleata74's picture
Release BgTTS 1.0.0 for Home Assistant and Raspberry Pi 5
d83d78a verified
Raw History Blame Contribute Delete
2.63 kB
"""Install only this service; does not change audio, Wi-Fi, or other services."""
import argparse
import os
from pathlib import Path
import pwd
import re
import subprocess
import sys
p = argparse.ArgumentParser()
p.add_argument('--python', required=True, type=Path, help='Python with requirements installed')
p.add_argument('--user', required=True)
p.add_argument('--model-dir', type=Path)
p.add_argument('--service-name', default='bgtts-wyoming')
p.add_argument('--port', type=int, default=10251)
p.add_argument('--voice', choices=('F1','F5','F7','M1','Voice4'), default='F5')
p.add_argument('--threads', type=int, choices=range(1,33), default=4)
args = p.parse_args()
if os.geteuid() != 0:
p.error('Run through sudo')
account = pwd.getpwnam(args.user)
if account.pw_uid == 0:
p.error('Choose a non-root service user')
if not re.fullmatch(r'bgtts[-a-z0-9]*', args.service_name) or not 1 <= args.port <= 65535:
p.error('Invalid service name or TCP port')
root = Path(__file__).resolve().parents[1]
app = root / 'bgtts'
models = (args.model_dir or app/'models').absolute()
python = args.python.absolute()
if not python.is_file() or not (models/'encoder.int8u8.onnx').is_file():
p.error('Python or model files are missing')
for path in (root, python, models):
if any(c in str(path) for c in '\n\r"%\\'):
p.error('Unsupported character in installation path')
unit_name = args.service_name+'.service'
unit_path = Path('/etc/systemd/system')/unit_name
marker = '# Managed by wyoming-bgtts install-service.py'
if unit_path.exists() and marker not in unit_path.read_text():
p.error('Refusing to replace an unrelated unit with the same name')
unit = f'''{marker}
[Unit]
Description=BgTTS Bulgarian Wyoming service
After=network.target
[Service]
Type=simple
User={account.pw_name}
Group={account.pw_gid}
WorkingDirectory={app}
Environment="PYTHONPATH={app / 'src'}"
Environment=PYTHONUNBUFFERED=1
Environment=OPENBLAS_NUM_THREADS=1
Environment=PYTHONDONTWRITEBYTECODE=1
ExecStart="{python}" -m wyoming_bgtts --model-dir "{models}" --manifest "{app / 'model-manifest.json'}" --uri tcp://0.0.0.0:{args.port} --voice {args.voice} --threads {args.threads}
Restart=on-failure
RestartSec=5
TimeoutStopSec=30
MemoryMax=2G
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=read-only
[Install]
WantedBy=multi-user.target
'''
unit_path.write_text(unit)
subprocess.run(['systemctl', 'daemon-reload'], check=True)
subprocess.run(['systemctl', 'enable', unit_name], check=True)
subprocess.run(['systemctl', 'restart', unit_name], check=True)
print(f'Installed {unit_name} on TCP port {args.port}')