Files
Athena-Deck/kikiri.py
T

50 lines
3.1 KiB
Python

"""Private trained Anna on CPU, independent of the GPU/LLM scheduler."""
import json,os,select,subprocess
from pathlib import Path
from pocket import Pocket
from kikiri_runtime import supported
import cpu_partition
class Kikiri(Pocket):
name='Kikiri Anna'
cooperative_cancel=True
def voice_path(self,name):
if name not in ('default','Anna','anna'):raise ValueError('Dieses Kikiri-Profil verwendet voice=default oder Anna.')
path=self.runtime.paths()[1]/'voice.pt'
if path.is_symlink() or not path.is_file():raise ValueError('Die trainierte Anna-Stimme fehlt.')
return path.resolve()
def cancel_job(self,ident):
with self.lock:
if self.job and self.job['id']==ident and self.job.get('state')=='running':
self.cancel.set();(self.root/ident/'cancel').touch()
def blockers(self,p):
if not supported(p.get('model')):return ['Nur der geprüfte Kikiri-Anna-plus20-Stand ist qualifiziert.']
errors=[]
if not self.runtime.status()['installed']:errors.append('Kikiri-CPU-Laufzeit und privaten Anna-Stand unter TTS → Einrichten bereitstellen.')
return errors
def _profile(self,ident):
p=next((p for p in self.profiles.status()['profiles'] if p['id']==ident and supported(p.get('model'))),None)
if not p or not p['runnable']:raise ValueError('Kikiri-Anna-Profil ist nicht ausführbar.')
if p['parameters'].get('speed',1)!=1:raise ValueError('Kikiri benötigt hier speed=1.')
return p
def _ensure_cpu(self,p):
python,bundle=self.runtime.paths();layout=cpu_partition.load(self.root.parent);cpus=layout['qwen_tts'] if layout else []
key=(p['id'],p['revision'],str(python),tuple(cpus))
if self.process and self.process.poll() is None and self.session_key==key:return self.process
from inference import cgroup_headroom
available=cgroup_headroom()
if available is not None and available<1536*1024**2:raise ValueError('Kikiri benötigt 1,5 GiB freien Deck-RAM; laufende Modelle bleiben erhalten.')
self.unload_idle();self.root.mkdir(parents=True,exist_ok=True,mode=0o700)
(self.root/'tmp').mkdir(exist_ok=True,mode=0o700)
env={k:v for k,v in os.environ.items() if not k.startswith(('HF_','DECK_','LLAMA_','OPENAI_'))}
env.update(CUDA_VISIBLE_DEVICES='',HF_HUB_OFFLINE='1',HF_HUB_DISABLE_TELEMETRY='1',HF_HUB_DISABLE_IMPLICIT_TOKEN='1',
OMP_NUM_THREADS='4',MKL_NUM_THREADS='4',OPENBLAS_NUM_THREADS='1',HOME=str(self.root),TMPDIR=str(self.root/'tmp'),XDG_CACHE_HOME=str(self.root/'cache'))
argv=[str(python),str(Path(__file__).with_name('kikiri_worker.py')),str(bundle)]
process=subprocess.Popen(cpu_partition.command(argv,cpus),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,env=env,start_new_session=True,text=True,bufsize=1)
with self.lock:self.process=process;self.profile_id=p['id']
if not select.select([process.stdout],[],[],90)[0]:self.unload_idle();raise ValueError('Zeitlimit beim Laden von Kikiri Anna.')
try:ready=json.loads(process.stdout.readline())
except ValueError:ready={}
if not ready.get('ready'):self.unload_idle();raise ValueError('Kikiri-CPU-Worker konnte nicht starten. Privaten Anna-Stand und Laufzeit prüfen.')
self.session_key=key;return process