Files
Athena-Deck/articulino_runtime.py

78 lines
5.0 KiB
Python

"""Pinned CPU-only Articulino runtime and complete, checksum-verified model bundle."""
import hashlib,json,os,shutil,sys,time
from pathlib import Path
from ltx_original_runtime import LTXOriginalRuntime
VERSION='1.0.0'
REVISION='143d6dd5bbd67a02564be30f15bd8a578dda4987'
REPO='eigenhand/Articulino-DE-EN-Realtime-Stream-TTS-ONNX'
MODEL_REVISION='a760ce8d513534111c7679751869fa4c45ef0e54'
MODEL_FILE='fp32/dit_step.onnx'
ASSETS=Path(__file__).parent/'deploy/articulino/assets.json'
def supported(model):
return bool(model and model.get('kind')=='audio' and model.get('repo')==REPO and model.get('revision')==MODEL_REVISION and model.get('file')==MODEL_FILE)
def verify_assets(directory):
for item in json.loads(ASSETS.read_text()):
path=Path(directory)/item['file']
if path.is_symlink() or not path.is_file() or path.stat().st_size!=item['bytes']:raise ValueError('Articulino-Datei fehlt oder ist unvollständig: '+item['file'])
with path.open('rb') as f:digest=hashlib.file_digest(f,'sha256').hexdigest()
if digest!=item['sha256']:raise ValueError('Articulino-Prüfsumme abweichend: '+item['file'])
class ArticulinoRuntime(LTXOriginalRuntime):
name='Articulino · CPU'
def __init__(self,root):super().__init__(root,None)
def paths(self):
try:
ident=json.loads((self.root/'active.json').read_text())['id']
if len(ident)==32 and all(c in '0123456789abcdef' for c in ident):
base=self.root/ident
if (base/'ready').read_text().strip()==REVISION+':'+MODEL_REVISION:return base/'python/bin/python',base/'model'
except (OSError,ValueError,KeyError,TypeError):pass
return self.root/'missing-python',self.root/'missing-model'
def status(self):
python,model=self.paths()
files=json.loads(ASSETS.read_text())
installed=python.is_file() and all((model/x['file']).is_file() and (model/x['file']).stat().st_size==x['bytes'] for x in files)
return dict(installed=installed,revision=REVISION,model_revision=MODEL_REVISION,version=VERSION,device='cpu',required_disk_gib=2,job=dict(self.job) if self.job else None,speakers=['default','Anna'],languages=['German'],origin='https://github.com/eigenhand/articulino-onnx')
def ensure_library(self):
if not self.status()['installed']:raise ValueError('Articulino zuerst installieren.')
_,model=self.paths();source=model/MODEL_FILE
expected=next(x for x in json.loads(ASSETS.read_text()) if x['file']==MODEL_FILE)
ident=hashlib.sha256((REPO+MODEL_REVISION+MODEL_FILE).encode()).hexdigest()
target=self.root.parent/'models'/ident;target.mkdir(parents=True,exist_ok=True,mode=0o700);dest=target/'model.onnx'
if not dest.exists():
temp=target/'import.tmp'
try:os.link(source,temp)
except OSError:shutil.copyfile(source,temp)
temp.chmod(0o600);temp.replace(dest)
with dest.open('rb') as f:digest=hashlib.file_digest(f,'sha256').hexdigest()
if digest!=expected['sha256']:raise ValueError('Vorhandene Articulino-Bibliotheksdatei weicht ab; nicht überschrieben.')
entry=dict(repo=REPO,revision=MODEL_REVISION,file=MODEL_FILE,size=expected['bytes'],sha256=digest,kind='audio',upstream_hash_verified=True,downloaded_at=time.time(),state='downloaded',runtime_ready=True)
temp=target/'entry.tmp';temp.write_text(json.dumps(entry));temp.chmod(0o600);temp.replace(target/'entry.json');return ident
def _run(self):
base=self.root/self.job['id'];state='failed';phase='Articulino-Einrichtung fehlgeschlagen.'
try:
base.mkdir(mode=0o700)
env={k:v for k,v in os.environ.items() if not k.startswith(('HF_','DECK_','LLAMA_','OPENAI_'))}
env.update(PIP_NO_INPUT='1',PIP_DISABLE_PIP_VERSION_CHECK='1',CUDA_VISIBLE_DEVICES='',HF_HUB_DISABLE_IMPLICIT_TOKEN='1')
self._phase('Eigene Articulino-CPU-Umgebung erstellen');self._command([sys.executable,'-m','venv',base/'python'],env)
python=base/'python/bin/python'
self._phase('Gepinnte ONNX-CPU-Laufzeit installieren · kein PyTorch oder CUDA')
self._command([python,'-m','pip','install','--no-cache-dir','-r',Path(__file__).parent/'deploy/articulino/requirements.lock'],env)
self._command([python,'-m','pip','check'],env)
self._command([python,'-c','import onnxruntime;from articulino_onnx import TTS;assert "CPUExecutionProvider" in onnxruntime.get_available_providers()'],env)
self._phase('Öffentliche FP32-Modelldateien laden und SHA256 prüfen · 537 MB')
self._command([sys.executable,Path(__file__).parent/'deploy/articulino/download.py','--destination',base/'model'],env)
verify_assets(base/'model')
if self.cancel.is_set():raise InterruptedError()
(base/'ready').write_text(REVISION+':'+MODEL_REVISION)
temp=self.root/'active.tmp';temp.write_text(json.dumps({'id':base.name}));temp.replace(self.root/'active.json')
self.ensure_library();state='complete';phase='Articulino bereit · Referenzstimme aus speech-voices, Endpunktfreigabe separat'
except InterruptedError:state='cancelled';phase='Articulino-Einrichtung abgebrochen.'
except Exception as exc:phase=str(exc)
finally:
if state!='complete':shutil.rmtree(base,ignore_errors=True)
with self.lock:self.process=None;self.job.update(state=state,phase=phase,finished_at=time.time());self._save()