79 lines
3.5 KiB
Python
79 lines
3.5 KiB
Python
"""Private local held-out voice comparison plus STT of synthetic CPU probes."""
|
|
import argparse
|
|
import csv
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import time
|
|
import wave
|
|
|
|
os.environ.update(CUDA_VISIBLE_DEVICES='',OMP_NUM_THREADS='4',MKL_NUM_THREADS='4')
|
|
import numpy as np
|
|
import onnxruntime as ort
|
|
from piper import PiperVoice
|
|
from piper.config import PiperConfig, SynthesisConfig
|
|
|
|
ROOT=Path('/data/athena-piper-test-20261027')
|
|
CLI=Path('/opt/athena-deck-dev/runtime/state/parakeet-runtime/v0.6.1/runtime/parakeet-v0.6.1-bin-linux-cpu-x64/parakeet-cli')
|
|
STT_MODEL=CLI.parents[2]/'redux-packed.gguf'
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('--model',required=True)
|
|
parser.add_argument('--results',required=True)
|
|
parser.add_argument('--prefix',required=True)
|
|
args=parser.parse_args()
|
|
|
|
def words(text):
|
|
return re.findall(r'[a-zäöüß0-9]+',text.lower())
|
|
|
|
def edit_distance(a,b):
|
|
prev=list(range(len(b)+1))
|
|
for i,x in enumerate(a,1):
|
|
row=[i]
|
|
for j,y in enumerate(b,1):
|
|
row.append(min(row[-1]+1,prev[j]+1,prev[j-1]+(x!=y)))
|
|
prev=row
|
|
return prev[-1]
|
|
|
|
def stt(path,text):
|
|
started=time.monotonic()
|
|
run=subprocess.run([str(CLI),'transcribe','--model',str(STT_MODEL),
|
|
'--input',str(path),'--lang','de','--threads','2','--json'],
|
|
text=True,capture_output=True,check=True,timeout=60)
|
|
response=json.loads(run.stdout)
|
|
a,b=words(text),words(response['text'])
|
|
distance=edit_distance(a,b)
|
|
return dict(file=path.name,expected=text,recognized=response['text'],
|
|
edit_distance=distance,reference_words=len(a),wer=distance/max(1,len(a)),
|
|
stt_seconds=time.monotonic()-started,
|
|
caveat='ASR proxy; lexical tokenization counts merged compounds as errors')
|
|
|
|
results=json.loads(Path(args.results).read_text())
|
|
report=dict(synthetic=[stt(ROOT/'outputs'/row['file'],row['text']) for row in results['runs']],
|
|
voice_identity='Not established by STT; human comparison required')
|
|
with (ROOT/'anna-data/heldout.csv').open() as f:
|
|
clip_id,text=next(csv.reader(f,delimiter='|'))
|
|
source=ROOT/'anna-data/wav'/clip_id
|
|
shutil.copy2(source,ROOT/'outputs/anna-original-heldout.wav')
|
|
opts=ort.SessionOptions();opts.intra_op_num_threads=4;opts.inter_op_num_threads=1
|
|
session=ort.InferenceSession(args.model,sess_options=opts,providers=['CPUExecutionProvider'])
|
|
cfg=PiperConfig.from_dict(json.loads(Path(args.model+'.json').read_text()))
|
|
voice=PiperVoice(session=session,config=cfg,download_dir=ROOT/'cache')
|
|
chunks=list(voice.synthesize(text,syn_config=SynthesisConfig(length_scale=1.,
|
|
noise_scale=.667,noise_w_scale=.8,normalize_audio=True)))
|
|
pcm=b''.join(chunk.audio_int16_bytes for chunk in chunks)
|
|
assert pcm and all(np.isfinite(c.audio_float_array).all() for c in chunks)
|
|
path=ROOT/'outputs'/(args.prefix+'-heldout.wav')
|
|
with wave.open(str(path),'wb') as wav:
|
|
wav.setnchannels(1);wav.setsampwidth(2);wav.setframerate(cfg.sample_rate);wav.writeframes(pcm)
|
|
# Private original transcription remains in the private evaluation file only.
|
|
report['heldout_private']=stt(path,text)
|
|
report['heldout_private']['reference_type']='ASR transcript of untrained source, not manually verified'
|
|
(ROOT/(args.prefix+'-evaluation.private.json')).write_text(json.dumps(report,indent=2,ensure_ascii=False))
|
|
public={k:v for k,v in report.items() if k!='heldout_private'}
|
|
public['heldout_wer_asr_proxy']=report['heldout_private']['wer']
|
|
(ROOT/(args.prefix+'-evaluation.json')).write_text(json.dumps(public,indent=2,ensure_ascii=False))
|
|
print(json.dumps(public,ensure_ascii=False),flush=True)
|