50 lines
2.6 KiB
Python
50 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Compare old/new voice embeddings as a heuristic, not a listening test."""
|
|
import json
|
|
from pathlib import Path
|
|
import numpy as np
|
|
import soundfile as sf
|
|
import torch
|
|
from huggingface_hub import snapshot_download
|
|
from qwen_tts import Qwen3TTSModel
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
work = root / 'artifacts/elias-consistent'
|
|
package = root / 'assets/stories/midnight-invitation/narration'
|
|
new = json.loads((package / 'manifest.json').read_text())
|
|
old = json.loads((work / 'original-manifest.json').read_text())
|
|
cache = {}
|
|
for meta in (work / 'takes').glob('*.json'):
|
|
data = json.loads(meta.read_text())
|
|
cache[data['sha256']] = meta.with_suffix('.wav')
|
|
pairs = []
|
|
for page, track in new['tracks'].items():
|
|
for current, previous in zip(track['cues'], old['tracks'][page]['cues'], strict=True):
|
|
if current['speaker'] != 'Elias': continue
|
|
oldpath = root / 'artifacts/midnight-qwen-full/takes' / (previous['requestSha256'] + '.wav')
|
|
newpath = cache[current['takeSha256']]
|
|
if sf.info(oldpath).duration >= 5 and sf.info(newpath).duration >= 5:
|
|
pairs.append((page, oldpath, newpath))
|
|
break
|
|
if len(pairs) == 12: break
|
|
if len(pairs) < 8: raise ValueError('Insufficient matched passages')
|
|
model_path = snapshot_download('Qwen/Qwen3-TTS-12Hz-1.7B-Base', revision='fd4b254389122332181a7c3db7f27e918eec64e3', local_files_only=True)
|
|
model = Qwen3TTSModel.from_pretrained(model_path, device_map='cuda', dtype=torch.float16, attn_implementation='sdpa')
|
|
def embedding(path):
|
|
data, rate = sf.read(path, dtype='float32')
|
|
if rate != 24000 or data.ndim != 1: raise ValueError(path)
|
|
data = data[:rate * 15]
|
|
with torch.inference_mode():
|
|
value = model.model.extract_speaker_embedding(audio=data, sr=rate).float().cpu().numpy()
|
|
return value / np.linalg.norm(value)
|
|
reference = embedding(package / 'references/elias.wav')
|
|
rows = []
|
|
for page, before, after in pairs:
|
|
rows.append({'page':page,'oldCosine':float(reference @ embedding(before)), 'newCosine':float(reference @ embedding(after))})
|
|
print(page, round(rows[-1]['oldCosine'],3), round(rows[-1]['newCosine'],3), flush=True)
|
|
report = {'note':'Same-model speaker embedding similarity is only a heuristic; it cannot establish perceived voice consistency or replace listening.',
|
|
'samples':rows,'oldMean':float(np.mean([r['oldCosine'] for r in rows])),
|
|
'newMean':float(np.mean([r['newCosine'] for r in rows]))}
|
|
(work / 'speaker-consistency.json').write_text(json.dumps(report,indent=2)+'\n')
|
|
print('Means:',round(report['oldMean'],3),round(report['newMean'],3),flush=True)
|