105 lines
7.2 KiB
Python
105 lines
7.2 KiB
Python
import hashlib,json,tempfile,unittest,wave
|
|
from pathlib import Path
|
|
from unittest.mock import Mock,patch
|
|
from articulino_runtime import ArticulinoRuntime,supported,REPO,MODEL_REVISION,MODEL_FILE
|
|
from articulino import Articulino
|
|
from endpoint import APIHandler
|
|
import test_pocket
|
|
|
|
MODEL=dict(kind='audio',repo=REPO,revision=MODEL_REVISION,file=MODEL_FILE)
|
|
|
|
class ArticulinoTests(unittest.TestCase):
|
|
def test_pinned_recipe_library_role_and_execution_routing(self):
|
|
from catalog import file_role
|
|
from execution_setup import assess
|
|
self.assertTrue(supported(MODEL));self.assertFalse(supported(dict(MODEL,revision='0'*40)))
|
|
self.assertFalse(supported(dict(MODEL,file='fp16/dit_step.onnx')))
|
|
self.assertTrue(file_role(MODEL_FILE)['profile_eligible'])
|
|
self.assertFalse(file_role('fp32/voice.onnx')['profile_eligible'])
|
|
result=assess(MODEL,{'articulino':True},{'runnable':True,'blockers':[]})
|
|
self.assertEqual(result['state'],'ready');self.assertEqual(result['candidates'][0]['id'],'articulino')
|
|
def test_offline_registration_is_idempotent_and_never_publishes(self):
|
|
from deploy.articulino.register_profile import register
|
|
with tempfile.TemporaryDirectory() as d,patch('deploy.articulino.register_profile.ArticulinoRuntime') as runtime:
|
|
root=Path(d);ident=hashlib.sha256((REPO+MODEL_REVISION+MODEL_FILE).encode()).hexdigest();model=root/'models'/ident;model.mkdir(parents=True)
|
|
(model/'model.onnx').write_bytes(b'test');(model/'entry.json').write_text(json.dumps(dict(MODEL,size=4,sha256=hashlib.sha256(b'test').hexdigest())))
|
|
ep=root/'endpoint.json';ep.write_text('{"enabled_profiles":["qwen"]}');before=ep.read_bytes()
|
|
runtime.return_value.ensure_library.return_value=ident
|
|
first=register(root);saved=(root/'profiles.json').read_bytes();second=register(root)
|
|
self.assertEqual(first,second);self.assertEqual(first['api_id'],'articulino-anna-cpu')
|
|
self.assertEqual(saved,(root/'profiles.json').read_bytes());self.assertEqual(before,ep.read_bytes())
|
|
def test_private_voice_guard_and_cpu_warm(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root=Path(d);voice=root/'speech-voices/Anna';voice.mkdir(parents=True);(voice/'reference.wav').touch()
|
|
(root/'speech-voices/default.json').write_text('{"voice":"Anna"}')
|
|
worker=Articulino(root/'jobs',Mock(),Mock())
|
|
self.assertEqual(worker.voice_path('default'),(voice/'reference.wav').resolve())
|
|
with self.assertRaises(ValueError):worker.voice_path('../Anna')
|
|
worker.acquire=Mock(side_effect=AssertionError('GPU touched'));worker._profile=Mock(return_value={'id':'a'});worker._ensure_cpu=Mock()
|
|
worker.warm('a');worker.acquire.assert_not_called()
|
|
def test_endpoint_routes_alias_default_and_formats_without_gpu_worker(self):
|
|
for model in ('tts','articulino-anna-cpu'):
|
|
for fmt in ('wav','pcm','mp3'):
|
|
h,ep,pocket,row=test_pocket.SpeechRoutingTests().fixture()
|
|
row.update(model=MODEL,api_id='articulino-anna-cpu');ep.articulino=Mock()
|
|
ep.articulino.start.return_value={'id':'job'}
|
|
ep.articulino.status.return_value=pocket.status.return_value
|
|
ep.articulino.job_status.return_value=pocket.job_status.return_value
|
|
ep.articulino.audio.return_value=b'RIFF-articulino'
|
|
with patch('endpoint.speech_mp3',return_value=b'MP3'):
|
|
APIHandler.speech(h,ep,dict(model=model,input='Ein synthetischer Satz.',response_format=fmt))
|
|
ep.articulino.start.assert_called_once();pocket.start.assert_not_called();ep.tts.start.assert_not_called()
|
|
self.assertEqual(ep.articulino.start.call_args.kwargs['streaming'],fmt=='pcm')
|
|
def test_generic_tts_prefers_audio_publication_over_omnivoice(self):
|
|
h,ep,pocket,row=test_pocket.SpeechRoutingTests().fixture()
|
|
row.update(model=MODEL,api_id='articulino-anna-cpu');ep.articulino=Mock()
|
|
ep.articulino.start.return_value={'id':'job'};ep.articulino.status.return_value=pocket.status.return_value
|
|
ep.articulino.job_status.return_value=pocket.job_status.return_value;ep.articulino.audio.return_value=b'RIFF-articulino'
|
|
omni=dict(id='omni',kind='voice',enabled=True,name='OmniVoice Studio',api_id='omnivoice-studio',model=dict(kind='voice',repo='k2-fsa/OmniVoice',file='model.safetensors'))
|
|
ep.rows=lambda:[row,omni];ep.voxcpm=Mock()
|
|
APIHandler.speech(h,ep,dict(model='tts',input='Synthetischer Text.'))
|
|
ep.articulino.start.assert_called_once();ep.voxcpm.start.assert_not_called();pocket.start.assert_not_called()
|
|
def test_generic_tts_keeps_voice_only_compatibility(self):
|
|
h,ep,pocket,row=test_pocket.SpeechRoutingTests().fixture()
|
|
row.update(kind='voice',name='OmniVoice Studio',api_id='omnivoice-studio',model=dict(kind='voice',repo='k2-fsa/OmniVoice',file='model.safetensors'))
|
|
ep.voxcpm=Mock();ep.voxcpm.start.return_value={'id':'job'};ep.voxcpm.status.return_value=pocket.status.return_value
|
|
ep.voxcpm.job_status.return_value=pocket.job_status.return_value;ep.voxcpm.audio.return_value=b'RIFF-omni';ep.root=Path('/test')
|
|
with patch('endpoint.speech_reference',return_value={}):APIHandler.speech(h,ep,dict(model='tts',input='Synthetischer Text.'))
|
|
ep.voxcpm.start.assert_called_once();pocket.start.assert_not_called()
|
|
def test_worker_pcm_matches_wav_and_cooperative_cancel(self):
|
|
import numpy as np
|
|
from articulino_worker import generate
|
|
for cancelled in (False,True):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root=Path(d);tts=Mock();tts.stream.return_value=iter([np.array([.1,-.2,0.]),np.array([.2,.3])])
|
|
if cancelled:(root/'cancel').touch()
|
|
result=generate(tts,dict(output=d,text='Hallo Welt.',voice='private.wav',streaming=True))
|
|
self.assertTrue(result['ok'])
|
|
if cancelled:
|
|
self.assertTrue(result['cancelled']);self.assertFalse(list(root.glob('chunk-*.pcm')))
|
|
else:
|
|
pcm=b''.join(p.read_bytes() for p in sorted(root.glob('chunk-*.pcm')))
|
|
with wave.open(str(root/'result.wav')) as w:
|
|
self.assertEqual(w.getframerate(),24000);self.assertEqual(w.getnchannels(),1)
|
|
self.assertEqual(w.readframes(w.getnframes()),pcm)
|
|
self.assertEqual(result['duration'],5/24000)
|
|
def test_corrupt_assets_never_get_published(self):
|
|
from articulino_runtime import verify_assets
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root=Path(d);f=root/'assets.json';f.write_text(json.dumps([dict(file='config.json',bytes=2,sha256=hashlib.sha256(b'{}').hexdigest())]))
|
|
(root/'config.json').write_bytes(b'xx')
|
|
with patch('articulino_runtime.ASSETS',f):
|
|
with self.assertRaisesRegex(ValueError,'Prüfsumme'):verify_assets(root)
|
|
def test_endpoint_switch_warm_keeps_qwen_setting_but_selects_articulino(self):
|
|
import test_audio_policy as p
|
|
from audio_policy import AudioPolicy
|
|
with tempfile.TemporaryDirectory() as d,patch('audio_policy.threading.Thread.start'):
|
|
path=Path(d)/'policy.json';path.write_text('{"tts":{"mode":"warm","profile_id":"qwen"}}')
|
|
qwen=p.Worker();art=p.Worker();ep=p.Endpoint();ep.state='running'
|
|
ep.rows=lambda:[dict(id='qwen',kind='audio',enabled=False,runnable=True,model={}),dict(id='art',kind='audio',enabled=True,runnable=True,model=MODEL)]
|
|
manager=AudioPolicy(path,p.Profiles(),p.Worker(),p.Worker(),p.Scheduler(),ep,qwen_native=qwen,articulino=art)
|
|
manager.reconcile();self.assertEqual(art.warmed,['art']);self.assertFalse(qwen.warmed)
|
|
self.assertEqual(manager.settings['tts']['profile_id'],'qwen')
|
|
|
|
if __name__=='__main__':unittest.main()
|