Files
Athena-Deck/omnivoice_worker.py
T

25 lines
1.9 KiB
Python

"""Offline OmniVoice worker. Private input/output; no model or credential logging."""
import contextlib,json,sys
from pathlib import Path
def main():
root=Path(sys.argv[1]);request=json.loads(sys.stdin.read());phase='Modell laden'
try:
with open('/dev/null','w') as sink,contextlib.redirect_stdout(sink),contextlib.redirect_stderr(sink):
import torch,soundfile as sf
from omnivoice import OmniVoice,OmniVoiceGenerationConfig
torch.manual_seed(request['seed'])
model=OmniVoice.from_pretrained(str(root/'model'),device_map='cuda:0',dtype=torch.float16,local_files_only=True,load_asr=False)
(root/'phase').write_text('OmniVoice: Stimme wird erzeugt');phase='Stimme erzeugen'
kwargs=dict(text=request['text'],language=request.get('language') or None,instruct=request.get('instruct') or None,speed=request.get('speed',1.0),generation_config=OmniVoiceGenerationConfig(num_step=request['steps'],guidance_scale=request['cfg'],denoise=request.get('denoise',True),preprocess_prompt=request.get('preprocess_prompt',True),postprocess_output=request.get('postprocess_output',True)))
if request.get('duration'):kwargs['duration']=request['duration']
if (root/'reference.wav').exists():kwargs.update(ref_audio=str(root/'reference.wav'),ref_text=request['reference_text'])
wave=model.generate(**kwargs)[0];rate=model.sampling_rate
sf.write(root/'result.wav',wave,rate,subtype='PCM_16')
result=dict(ok=True,sample_rate=rate,duration=len(wave)/rate)
except Exception as exc:
oom='outofmemory' in type(exc).__name__.lower() or 'out of memory' in str(exc).lower()
result=dict(ok=False,error='GPU-Speicher reicht nicht aus. Andere Deck-Modelle entladen oder andere GPU wählen.' if oom else 'OmniVoice: '+phase+' fehlgeschlagen ('+type(exc).__name__+'). Laufzeit und Komponenten prüfen.')
(root/'result.json').write_text(json.dumps(result));return 0 if result['ok'] else 1
if __name__=='__main__':sys.exit(main())