Files
Athena-Deck/docs/experiments/articulino-reference-comparison-2026-10-27/run_references.py
T

109 lines
4.8 KiB
Python

"""Three voice references, fixed synthetic text; CPU only, no production writes."""
import os
os.environ.update(CUDA_VISIBLE_DEVICES='', HF_HUB_OFFLINE='1',
HF_HUB_DISABLE_TELEMETRY='1', OMP_NUM_THREADS='4',
OPENBLAS_NUM_THREADS='1', MKL_NUM_THREADS='4')
import hashlib
import json
from pathlib import Path
import resource
import subprocess
import time
import numpy as np
import soundfile as sf
from articulino_onnx import TTS, dsp
ROOT = Path('/data/athena-articulino-references-20261027')
BASE = Path('/data/athena-articulino-test-20261026')
OUT = ROOT/'outputs'
OUT.mkdir(exist_ok=True, mode=0o700)
TEXT = ('Wenn du magst, erzähle ich dir eine kurze Geschichte. '
'Anna öffnete das Fenster und hörte drei Vögel singen. '
'Auf dem Tisch lagen sieben Bücher und eine rote Tasse. '
'Zum Schluss sagte sie: Das hat wunderbar funktioniert.')
(ROOT/'test-text.txt').write_text(TEXT+'\n')
STT = Path('/opt/athena-deck-dev/runtime/state/parakeet-runtime/v0.6.1')
CLI = STT/'runtime/parakeet-v0.6.1-bin-linux-cpu-x64/parakeet-cli'
MODEL = STT/'redux-packed.gguf'
def transcribe(path):
start = time.monotonic()
process = subprocess.run([str(CLI), 'transcribe', '--model', str(MODEL),
'--input', str(path), '--lang', 'de',
'--threads', '2', '--json'],
capture_output=True, text=True, timeout=90)
row = dict(file=path.name, exit_code=process.returncode,
seconds=time.monotonic()-start)
if process.returncode == 0:
try:
row['result'] = json.loads(process.stdout)
except ValueError:
row['stdout'] = process.stdout
else:
row['stderr'] = process.stderr[-2000:]
return row
references = sorted((ROOT/'references').glob('anna-reference-*.wav'))
assert len(references) == 3
reference_stt = [transcribe(p) for p in references]
(ROOT/'reference-transcriptions.json').write_text(json.dumps(reference_stt, ensure_ascii=False, indent=2))
print(json.dumps(dict(reference_stt=reference_stt), ensure_ascii=False), flush=True)
checks = []
original_check = dsp.broken
def observed_check(*args, **kwargs):
reason = original_check(*args, **kwargs)
checks.append(reason)
return reason
dsp.broken = observed_check
start = time.monotonic()
tts = TTS.from_pretrained(BASE/'model', precision='fp32',
providers=['CPUExecutionProvider'], threads=4)
load_seconds = time.monotonic()-start
providers = {name: session.get_providers() for name, session in tts.engine.sessions.items()}
assert all(v == ['CPUExecutionProvider'] for v in providers.values())
setup = dict(precision='fp32', model_load_seconds=load_seconds, providers=providers,
threads=4, affinity=sorted(os.sched_getaffinity(0)),
uniform_rate=tts.uniform_rate, retry_broken=tts.retry_broken,
sentence_pause_seconds=tts.pause, language='de', seed=42,
normalize=True, text=TEXT)
(ROOT/'setup.json').write_text(json.dumps(setup, ensure_ascii=False, indent=2))
results = []
for reference in references:
label = reference.stem[-1]
row = dict(label=label, reference=reference.name, seed=42)
start = time.monotonic()
tts.set_voice(str(reference))
row['voice_preparation_seconds'] = time.monotonic()-start
checks.clear()
chunks = []
first = None
start = time.monotonic()
for pcm in tts.stream(TEXT, language='de', seed=42, normalize=True):
if first is None:
first = time.monotonic()-start
assert len(pcm) and np.isfinite(pcm).all()
chunks.append(pcm)
assert chunks
pcm = np.concatenate(chunks)
target = OUT/f'articulino-anna-{label}.wav'
sf.write(target, pcm, 24000, subtype='PCM_16')
elapsed = time.monotonic()-start
row.update(file=target.name, generation_seconds=elapsed,
duration_seconds=len(pcm)/24000, rtf=elapsed/(len(pcm)/24000),
first_sentence_seconds=first, chunks=len(chunks),
peak=float(np.max(np.abs(pcm))),
clipped_fraction=float(np.mean(np.abs(pcm) >= 1)),
retry_trigger_count=sum(bool(x) for x in checks),
retry_reasons=[x for x in checks if x],
peak_rss_kib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
sha256=hashlib.sha256(target.read_bytes()).hexdigest())
results.append(row)
(ROOT/'results.json').write_text(json.dumps(results, ensure_ascii=False, indent=2))
print(json.dumps(row), flush=True)
# Release the candidate before loading the existing STT weights again.
del tts
output_stt = [transcribe(p) for p in sorted(OUT.glob('*.wav'))]
(ROOT/'output-transcriptions.json').write_text(json.dumps(output_stt, ensure_ascii=False, indent=2))
print(json.dumps(dict(output_stt=output_stt), ensure_ascii=False), flush=True)