61 lines
4.4 KiB
Python
61 lines
4.4 KiB
Python
"""Synthetic test of the real Deck endpoint classes; production settings untouched."""
|
|
import hashlib,io,json,os,pathlib,secrets,socket,sys,tempfile,time,urllib.request,wave
|
|
from types import SimpleNamespace
|
|
sys.path.insert(0,'/app')
|
|
from endpoint import Endpoint
|
|
from stt import STT,normalize_transcription_audio
|
|
from parakeet_runtime import supported as redux_supported
|
|
root=pathlib.Path('/var/lib/deck');output=pathlib.Path(os.environ.get('REDUX_TEST_OUTPUT','/var/lib/deck/parakeet-comparison-prepared'))/'integration-results.json'
|
|
model_rows=[]
|
|
for path in (root/'models').glob('*/entry.json'):
|
|
d=json.loads(path.read_text());d['id']=path.parent.name
|
|
if d.get('repo') in ('ggml-org/Qwen3-ASR-0.6B-GGUF','mudler/parakeet-cpp-gguf'):model_rows.append(d)
|
|
entries={d['id']:d for d in model_rows}
|
|
selected=[]
|
|
for row in json.loads((root/'profiles.json').read_text()):
|
|
if row['kind']=='stt' and row['model_id'] in entries and entries[row['model_id']]['file'] in ('Qwen3-ASR-0.6B-Q8_0.gguf','redux-packed.gguf'):
|
|
selected.append(dict(row,model=entries[row['model_id']],runnable=True,blockers=[]))
|
|
qwen=next(p for p in selected if not redux_supported(p['model']));redux=next(p for p in selected if redux_supported(p['model']))
|
|
catalog=SimpleNamespace(root=root/'models',entry=lambda ident:entries[ident],status=lambda:{'entries':model_rows})
|
|
profiles=SimpleNamespace(status=lambda:{'profiles':selected})
|
|
rt=json.loads((root/'runtime/state.json').read_text());runtime=SimpleNamespace(root=root/'runtime',status=lambda:{'active':rt['active']})
|
|
stt=STT(profiles,catalog,runtime)
|
|
for p in selected:
|
|
blockers=stt.blockers(p)
|
|
if blockers:raise RuntimeError('STT preflight failed: '+' '.join(blockers))
|
|
key=secrets.token_hex(32);credentials=SimpleNamespace(read=lambda:{'api_token_hash':hashlib.sha256(key.encode()).hexdigest()})
|
|
worker=SimpleNamespace(status=lambda:{'state':'stopped'})
|
|
scheduler=SimpleNamespace(status=lambda:{'active_requests':0},unload_idle=lambda:True,gpu_mode='llm')
|
|
images=SimpleNamespace(status=lambda:{'job':None})
|
|
os.environ['DECK_API_PORTS']='';os.environ['DECK_API_BIND']='127.0.0.1'
|
|
results=[]
|
|
with tempfile.TemporaryDirectory(prefix='redux-api-check-') as directory:
|
|
ep=Endpoint(directory,profiles,worker,scheduler,images,credentials,0);ep.stt=stt
|
|
with socket.socket() as s:s.bind(('127.0.0.1',0));port=s.getsockname()[1]
|
|
ep.config['port']=port
|
|
cases=json.loads((root/'parakeet-comparison-prepared/cases/cases.json').read_text())
|
|
try:
|
|
ep.enable({'id':qwen['id'],'enabled':True});ep.start()
|
|
for profile in (qwen,redux):
|
|
ep.enable({'id':profile['id'],'enabled':True})
|
|
for case in cases:
|
|
audio=normalize_transcription_audio((root/'parakeet-comparison-prepared/cases'/case['file']).read_bytes())
|
|
with wave.open(io.BytesIO(audio)) as w:duration=w.getnframes()/w.getframerate()
|
|
for repetition in (1,2):
|
|
boundary='synthetic-redux-integration'
|
|
# Deliberately keep the OLD Qwen name when switching to Redux.
|
|
body=(f'--{boundary}\r\nContent-Disposition: form-data; name="model"\r\n\r\n{qwen["name"]}\r\n--{boundary}\r\nContent-Disposition: form-data; name="language"\r\n\r\nde\r\n--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="synthetic.wav"\r\nContent-Type: audio/wav\r\n\r\n').encode()+audio+f'\r\n--{boundary}--\r\n'.encode()
|
|
start=time.monotonic()
|
|
request=urllib.request.Request(f'http://127.0.0.1:{port}/v1/audio/transcriptions',data=body,headers={'Authorization':'Bearer '+key,'Content-Type':'multipart/form-data; boundary='+boundary})
|
|
try:
|
|
with urllib.request.urlopen(request,timeout=300) as response:payload=json.load(response);status=response.status
|
|
except urllib.error.HTTPError as exc:
|
|
print(json.dumps({'synthetic_test_error':json.loads(exc.read()),'status':exc.code}),flush=True);raise
|
|
elapsed=time.monotonic()-start
|
|
row=dict(backend='redux' if redux_supported(profile['model']) else 'qwen',case=case['name'],repetition=repetition,status=status,seconds=round(elapsed,3),audio_seconds=round(duration,3),rtf=round(elapsed/duration,4),text=payload['text'],expected=case['expected'],actual_profile=stt.profile_id,legacy_name_preserved=True)
|
|
results.append(row);output.write_text(json.dumps(results,indent=2,ensure_ascii=False));print(json.dumps({k:v for k,v in row.items() if k not in ('text','expected')}),flush=True)
|
|
finally:
|
|
stt.stop()
|
|
if ep.http:ep.http.shutdown();ep.http.server_close()
|
|
(output.parent/'integration-complete').write_text('Synthetic endpoint switching verified\n')
|