Files

45 lines
2.1 KiB
Python

"""Isolated, pinned Piper CPU preparation; no production writes."""
from pathlib import Path
import hashlib
import json
import os
import subprocess
import sys
import urllib.request
ROOT = Path('/data/athena-piper-test-20261027')
ROOT.mkdir(mode=0o700, exist_ok=True)
os.umask(0o077)
available = next(int(s.split()[1]) * 1024 for s in
Path('/proc/meminfo').read_text().splitlines() if s.startswith('MemAvailable:'))
if available < 4 * 1024**3:
raise SystemExit('Insufficient memory reserve for isolated CPU preparation')
manifest = json.loads((ROOT / 'download-manifest.json').read_text())
(ROOT / 'model').mkdir(exist_ok=True)
def download(item, target):
if not target.is_file():
with urllib.request.urlopen(item['url'], timeout=90) as source, target.open('xb') as dest:
while block := source.read(1024 * 1024):
dest.write(block)
digest = hashlib.file_digest(target.open('rb'), 'sha256').hexdigest()
if target.stat().st_size != item['bytes'] or digest != item['sha256']:
raise RuntimeError(f'Public asset checksum mismatch: {target.name}')
for item in manifest['voice_files']:
download(item, ROOT / 'model' / Path(item['path']).name)
wheel = ROOT / manifest['package']['filename']
download(manifest['package'], wheel)
venv = ROOT / 'venv'
if not (venv / 'bin/python').is_file():
subprocess.run([sys.executable, '-m', 'venv', str(venv)], check=True)
subprocess.run([str(venv / 'bin/python'), '-m', 'pip', 'install', '--no-input',
'--no-cache-dir', '--only-binary=:all:', str(wheel),
'onnxruntime==1.23.2', 'numpy==2.2.6', 'pathvalidate==3.3.1'], check=True)
freeze = subprocess.check_output([str(venv / 'bin/python'), '-m', 'pip', 'freeze'], text=True)
(ROOT / 'requirements-resolved.txt').write_text(freeze)
assert not any(s.lower().startswith(('torch', 'nvidia-', 'onnxruntime-gpu')) for s in freeze.splitlines())
print(json.dumps({'prepared': True, 'python': sys.version.split()[0],
'public_model_bytes': sum(i['bytes'] for i in manifest['voice_files']),
'piper_version': manifest['piper_version']}), flush=True)