Files
Athena-Deck/tts_test.py
T

178 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Serial own TTS workers with shared model reservation, WAV results and cancellation."""
import json,math,os,signal,subprocess,threading,time,uuid,hashlib,select,logging
from pathlib import Path
from tts_runtime import REPO,REVISION,SPEAKERS,LANGUAGES
from image_test import probe,cgroup_headroom
WORKER=Path(__file__).with_name('tts_worker.py')
class TTSTests:
def __init__(self,root,profiles,runtime):
self.root=Path(root);self.profiles=profiles;self.runtime=runtime;self.lock=threading.RLock();self.job=None;self.process=None;self.gpu_uuid=None;self.profile_id=None;self.cancel=threading.Event();self.acquire=lambda wait=False:lambda:None;self.policy='auto';self.warming=False
def status(self):
with self.lock:return dict(job=dict(self.job) if self.job else None,runtime=self.runtime.status(),loaded=bool(self.process and self.process.poll() is None),gpu_uuid=self.gpu_uuid,profile_id=self.profile_id,warming=self.warming,policy=self.policy)
def job_status(self,ident):
if not isinstance(ident,str) or len(ident)!=32 or any(c not in '0123456789abcdef' for c in ident):raise ValueError('Ungültiger TTS-Auftrag.')
with self.lock:
if self.job and self.job['id']==ident:return dict(self.job)
path=self.root/ident/'job.json'
return json.loads(path.read_text()) if path.is_file() else None
def blockers(self,p):
if p['model']['repo']!=REPO or p['model']['file']!='model.safetensors' or p['model'].get('revision')!=REVISION:return ['Für diese TTS-Variante fehlt die Anbindung in Deck. Unter TTS → Einrichten kannst du ausdrücklich auf die unterstützte Base-Variante mit Preset-Stimmen wechseln. Die ursprüngliche Datei bleibt erhalten.']
return [] if self.runtime.status()['installed'] else ['TTS-Laufzeit unter TTS → Einrichten installieren.']
def assign(self,ident,revision):
if not self.runtime.status()['installed']:raise ValueError('Zuerst TTS einrichten.')
entry_id=self.runtime.ensure_library()
p=next((p for p in self.profiles.status()['profiles'] if p['id']==ident),None)
if not p or p['kind']!='audio':raise ValueError('TTS-Profil nicht gefunden.')
return self.profiles.save(dict(id=ident,revision=revision,name=p['name'],kind='audio',model_id=entry_id,parameters=p['parameters']))
def start(self,profile_id,text,speaker='Ryan',language='German',wait=False,speed=None,streaming=False):
if not isinstance(text,str) or not 1<=len(text.strip())<=1000:raise ValueError('Text mit 1–1000 Zeichen erforderlich.')
if speaker not in SPEAKERS or language not in LANGUAGES:raise ValueError('Ungültige Stimme oder Sprache.')
if speed is not None and (type(speed) not in (int,float) or not .25<=speed<=4):raise ValueError('Geschwindigkeit muss zwischen 0,25 und 4 liegen.')
if type(streaming) is not bool:raise ValueError('Ungültiger Streaming-Modus.')
queued=time.monotonic();release=self.acquire(wait);queue_seconds=time.monotonic()-queued
try:
with self.lock:
if self.warming:raise ValueError('TTS wird gerade vorgeladen. Gleich erneut versuchen.')
if self.job and self.job['state']=='running':raise ValueError('TTS-Auftrag läuft bereits.')
checked=time.monotonic()
p=next((p for p in self.profiles.status()['profiles'] if p['id']==profile_id and p['kind']=='audio'),None)
profile_seconds=time.monotonic()-checked
if not p or not p['runnable']:raise ValueError('TTS-Profil noch nicht eingerichtet.')
selected_speed=speed if speed is not None else p['parameters']['speed']
if streaming and selected_speed!=1:raise ValueError('Streaming benötigt speed=1; Wiedergabegeschwindigkeit im Client einstellen.')
checked=time.monotonic();resident=self.process and self.process.poll() is None
gpu=next((g for g in probe() if g['uuid']==self.gpu_uuid),None) if resident else self._available_gpu()
if not gpu:raise ValueError('Für Qwen-TTS ist nicht genug freier VRAM auf der RTX 3060 verfügbar. Andere Prozesse dürfen die Karte gemeinsam nutzen.')
headroom=cgroup_headroom()
if headroom is not None and headroom<8*1024**3:raise ValueError('Zu wenig freier Deck-RAM für TTS.')
preflight_seconds=time.monotonic()-checked
self.profile_id=profile_id;self.cancel.clear();ident=uuid.uuid4().hex;self.root.mkdir(parents=True,exist_ok=True,mode=0o700)
self.job=dict(id=ident,state='running',phase='Vorhandenes Sprachmodell wird verwendet' if resident else 'Sprachmodell wird geladen',profile_id=profile_id,started_at=time.time())
logging.getLogger('athena.tts').warning('TTS preparation job=%s queue_seconds=%.3f profile_seconds=%.3f preflight_seconds=%.3f resident=%s',ident,queue_seconds,profile_seconds,preflight_seconds,bool(resident))
threading.Thread(target=self._run,args=(ident,text,speaker,language,selected_speed,gpu,release,streaming),daemon=True).start();return dict(self.job)
except Exception:release();raise
def _run(self,ident,text,speaker,language,speed,gpu,release,streaming=False):
directory=self.root/ident;process=None;state='failed';phase='TTS-Ausführung fehlgeschlagen'
try:
directory.mkdir(mode=0o700);process=self._ensure(gpu)
with self.lock:self.job['phase']='Sprache wird auf der RTX 3060 erzeugt'
process.stdin.write(json.dumps(dict(output=str(directory),text=text,speaker=speaker,language=language,speed=speed,streaming=streaming))+'\n');process.stdin.flush()
end=time.monotonic()+600
while not (directory/'result.json').is_file():
if self.cancel.wait(1):raise InterruptedError()
if time.monotonic()>end:raise ValueError('Zeitlimit der Sprachgenerierung erreicht.')
if process.poll() is not None:raise ValueError('TTS-Worker wurde unerwartet beendet.')
current=next((g for g in probe() if g['uuid']==gpu['uuid']),None)
if not current:raise ValueError('Die TTS-GPU ist nicht mehr erreichbar.')
result=json.loads((directory/'result.json').read_text())
if self.cancel.is_set():raise InterruptedError()
if not result['ok']:raise ValueError(result['error'])
wav=directory/'result.wav'
if not wav.exists() or wav.stat().st_size>32*1024**2:raise ValueError('Ungültige Audioausgabe.')
state='complete';phase='Sprache fertig · '+('Modell entladen' if self.policy=='per_request' else 'Modell bleibt geladen')
except InterruptedError:state='cancelled';phase='Sprachgenerierung abgebrochen'
except Exception as exc:phase=str(exc) if isinstance(exc,ValueError) else 'Sprachgenerierung fehlgeschlagen. Laufzeit und Ressourcen prüfen.'
finally:
if state!='complete' or self.policy=='per_request':self.unload_idle()
if state=='complete':(directory/'complete').touch()
if state!='complete':
for chunk in directory.glob('chunk-*.pcm'):chunk.unlink(missing_ok=True)
try:
with self.lock:
self.job.update(state=state,phase=phase,finished_at=time.time())
# A queued request can replace self.job before the previous HTTP client
# polls again. Keep its terminal outcome addressable by its own ID.
if directory.is_dir():(directory/'job.json').write_text(json.dumps(self.job))
finally:release()
def _ensure(self,gpu):
self.runtime.ensure_fast_backend()
python,model=self.runtime.paths()
with self.lock:
if self.cancel.is_set():raise InterruptedError()
process=self.process if self.process and self.process.poll() is None else None
if process:return process
self.root.mkdir(parents=True,exist_ok=True,mode=0o700)
env={k:v for k,v in os.environ.items() if not k.startswith(('HF_','LLAMA_'))};env.update(CUDA_VISIBLE_DEVICES=gpu['uuid'],HF_HUB_OFFLINE='1',TRANSFORMERS_OFFLINE='1',OMP_NUM_THREADS='2',HOME=str(self.root))
process=subprocess.Popen([str(python),str(WORKER),str(model)],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,start_new_session=True,env=env,text=True,bufsize=1);self.process=process;self.gpu_uuid=gpu['uuid']
if not select.select([process.stdout],[],[],180)[0]:raise ValueError('Zeitlimit beim Laden des TTS-Modells.')
if self.cancel.is_set():raise InterruptedError()
ready=json.loads(process.stdout.readline())
if not ready.get('ready'):raise ValueError(ready.get('error','TTS-Modell konnte nicht geladen werden.'))
return process
def _required_vram_mib(self):
# CUDA-graph worker measured at ~4.7 GiB for ~3656 MiB weights.
# Leave 1.5 GiB for bounded sentence workspace and allocation variance.
_,model=self.runtime.paths()
try:return math.ceil((model/'model.safetensors').stat().st_size/1024**2)+1536
except OSError:raise ValueError('Qwen-TTS-Gewichte für die Speicherprüfung fehlen.') from None
def _available_gpu(self):
needed=self._required_vram_mib()
return next((g for g in probe() if 'RTX 3060' in g['name'] and g['free_mib']>=needed),None)
def warm(self,profile_id):
release=self.acquire(True)
try:
with self.lock:
if self.job and self.job.get('state')=='running':return False
if self.process and self.process.poll() is None:return True
if self.warming:return False
p=next((p for p in self.profiles.status()['profiles'] if p['id']==profile_id and p['kind']=='audio' and p['runnable']),None)
if not p:raise ValueError('Das ausgewählte TTS-Profil ist nicht ausführbar.')
self.profile_id=profile_id;self.warming=True;self.cancel.clear()
try:
gpu=self._available_gpu()
if not gpu:raise ValueError(f'Für dauerhaftes TTS sind mindestens {self._required_vram_mib()} MiB freie RTX-3060-VRAM nötig; die Karte darf gemeinsam genutzt werden.')
headroom=cgroup_headroom()
if headroom is not None and headroom<8*1024**3:raise ValueError('Zu wenig freier Deck-RAM für TTS.')
self._ensure(gpu);return True
except Exception:
self.unload_idle();raise
finally:
with self.lock:self.warming=False
finally:release()
def unload_idle(self):
with self.lock:
process=self.process;self.process=None;self.gpu_uuid=None;self.profile_id=None
if process and process.poll() is None:
try:
os.killpg(process.pid,signal.SIGTERM)
try:process.wait(timeout=5)
except subprocess.TimeoutExpired:os.killpg(process.pid,signal.SIGKILL);process.wait()
except ProcessLookupError:pass
if process:
if process.stdin:process.stdin.close()
if process.stdout:process.stdout.close()
return process is not None
def stop(self):
self.cancel.set()
self.unload_idle()
return dict(cancellation_requested=True)
def audio(self,ident):
if not isinstance(ident,str) or len(ident)!=32 or any(c not in '0123456789abcdef' for c in ident) or not (self.root/ident/'complete').exists():raise ValueError('Audio noch nicht verfügbar.')
return (self.root/ident/'result.wav').read_bytes()
def stream_audio(self,ident):
if not isinstance(ident,str) or len(ident)!=32 or any(c not in '0123456789abcdef' for c in ident):raise ValueError('Ungültiger TTS-Auftrag.')
index=0;end=time.monotonic()+620
while time.monotonic()<end:
job=self.job_status(ident)
if not job:raise ValueError('TTS-Auftrag nicht gefunden.')
if job['state'] in ('failed','cancelled'):raise ValueError(job['phase'])
path=self.root/ident/f'chunk-{index:06d}.pcm'
if path.is_file():
chunk=path.read_bytes()
if not chunk or len(chunk)%2 or len(chunk)>32*1024**2:raise ValueError('Ungültige PCM-Ausgabe.')
yield chunk;index+=1;continue
if job['state']=='complete':
# Completion can race with the existence check immediately above.
if path.is_file():continue
if not index:raise ValueError('Keine Audioportion erzeugt.')
return
time.sleep(.02)
raise ValueError('Zeitlimit der Sprachausgabe überschritten.')
def cancel_job(self,ident):
with self.lock:
if self.job and self.job['id']==ident and self.job['state']=='running':self.cancel.set()
def cleanup_stream(self,ident):
job=self.job_status(ident)
if job and job['state']!='running':
for chunk in (self.root/ident).glob('chunk-*.pcm'):chunk.unlink(missing_ok=True)