60 lines
3.0 KiB
Python
60 lines
3.0 KiB
Python
import os, sys, time, json, resource, pathlib, traceback
|
|
ROOT = pathlib.Path(__file__).resolve().parent
|
|
os.environ['HF_HOME'] = str(ROOT / 'hf-cache')
|
|
os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
|
|
os.environ['HF_HUB_DISABLE_IMPLICIT_TOKEN'] = '1'
|
|
os.environ['OMP_NUM_THREADS'] = '4'
|
|
os.environ['MKL_NUM_THREADS'] = '4'
|
|
kind = sys.argv[1]
|
|
prepare = '--prepare' in sys.argv
|
|
text = 'Hallo Michael. Heute testen wir die Sprachausgabe auf deinem Rechner. Dein Zug nach Karlsruhe fährt um fünf Uhr einundzwanzig. Welche Stimme gefällt dir am besten?'
|
|
results = {'model': kind, 'device': 'cpu', 'threads': 4, 'text': text, 'runs': []}
|
|
start = time.perf_counter()
|
|
try:
|
|
import numpy as np
|
|
import torch
|
|
import soundfile as sf
|
|
if kind == 'pocket':
|
|
from pocket_tts import TTSModel
|
|
torch.set_num_threads(4)
|
|
model = TTSModel.load_model(language='german')
|
|
results['load_seconds'] = time.perf_counter() - start
|
|
start = time.perf_counter()
|
|
state = model.get_state_for_audio_prompt('juergen')
|
|
results['voice'] = 'juergen (official German preset; cloning requires gated weights)'
|
|
results['reference_seconds'] = time.perf_counter() - start
|
|
else:
|
|
sys.path.insert(0, str(next(ROOT.glob('MOSS-TTS-Nano-*'))))
|
|
from onnx_tts_runtime import OnnxTtsRuntime
|
|
torch.set_num_threads(4)
|
|
model = OnnxTtsRuntime(thread_count=4, execution_provider='cpu', max_new_frames=750)
|
|
results['load_seconds'] = time.perf_counter() - start
|
|
if not prepare:
|
|
for i in range(2):
|
|
start = time.perf_counter()
|
|
out = ROOT / f'{kind}-{i+1}.wav'
|
|
if kind == 'pocket':
|
|
chunks, first = [], None
|
|
for chunk in model.generate_audio_stream(state, text):
|
|
if first is None: first = time.perf_counter() - start
|
|
chunks.append(chunk)
|
|
audio = torch.cat(chunks, dim=-1).detach().cpu().numpy().squeeze()
|
|
elapsed = time.perf_counter() - start
|
|
sf.write(out, audio, model.sample_rate)
|
|
duration = audio.shape[-1] / model.sample_rate
|
|
else:
|
|
r = model.synthesize(text=text, prompt_audio_path=ROOT/'reference.wav', output_audio_path=out, streaming=True, enable_wetext=False, seed=42, max_new_frames=750)
|
|
elapsed = time.perf_counter() - start
|
|
duration = sf.info(out).duration
|
|
first = None
|
|
results['runs'].append({'seconds': elapsed, 'audio_seconds': duration, 'rtf': elapsed/duration, 'first_chunk_seconds': first, 'file': out.name})
|
|
(ROOT / f'{kind}-results.json').write_text(json.dumps(results, indent=2))
|
|
results['peak_rss_mib'] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
|
|
results['success'] = True
|
|
except Exception as e:
|
|
results['success'] = False
|
|
results['error'] = str(e)
|
|
traceback.print_exc()
|
|
(ROOT / f'{kind}-{"prepare" if prepare else "results"}.json').write_text(json.dumps(results, indent=2))
|
|
print(json.dumps(results, indent=2))
|