Files
Athena-Deck/docs/experiments/mini-tts-cpu-2026-10-26/encode_neutts.py
T

41 lines
1.8 KiB
Python

"""One-time CPU-only Anna preparation, separate from timed TTS and production."""
import os
from pathlib import Path
ROOT = Path('/data/athena-mini-tts-20261026')
os.environ.update(CUDA_VISIBLE_DEVICES='', HF_HUB_OFFLINE='1',
HF_HUB_DISABLE_TELEMETRY='1', HF_HUB_CACHE=str(ROOT/'hf-cache'),
OMP_NUM_THREADS='4', MKL_NUM_THREADS='4', OPENBLAS_NUM_THREADS='1')
import sys
import time
import json
import resource
import numpy as np
import torch
import librosa
import neucodec.model as codec_module
torch.set_num_threads(4)
torch.set_num_interop_threads(1)
assert torch.version.cuda is None
original_download = codec_module.hf_hub_download
def pinned_download(*, repo_id, filename, **kw):
if repo_id == 'neuphonic/neucodec':
path = ROOT/'models/neucodec'/filename
if path.is_file():
return str(path)
return original_download(repo_id=repo_id, filename=filename, **kw)
codec_module.hf_hub_download = pinned_download
started = time.monotonic()
model = codec_module.NeuCodec._from_pretrained(model_id='neuphonic/neucodec', local_files_only=True)
model.eval().to('cpu')
loaded = time.monotonic()
audio, _ = librosa.load('/opt/athena-deck-dev/runtime/state/speech-voices/Anna/reference.wav', sr=16000, mono=True)
with torch.inference_mode():
codes = model.encode_code(torch.from_numpy(audio).float().unsqueeze(0).unsqueeze(0)).squeeze(0).squeeze(0)
np.save(ROOT/'anna-codes.npy', codes.cpu().numpy())
result = dict(seconds=time.monotonic()-started, load_seconds=loaded-started,
encode_seconds=time.monotonic()-loaded, codes=int(codes.numel()),
peak_rss_kib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
torch_cuda=torch.version.cuda)
(ROOT/'reference-encoding.json').write_text(json.dumps(result, indent=2))
print(json.dumps(result), flush=True)