Raon-Speech-9B / examples /quickstart.py
changh95's picture
tt-model push raon-speech-9b (container)
4ffdd01 verified
Raw History Blame Contribute Delete
2.97 kB
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Raon-Speech-9B quickstart (the Python API): transcribe the shipped Korean clip, answer a question, speak a sentence
(streamed packet by packet), clone the shipped voice. Non-commercial use only (CC BY-NC 4.0 weights).
python examples/quickstart.py [--fake] [--out quickstart.wav]
Needs ttnn (a tt-metal build with the patches in ../patches/, or the container image) and one p150; ``--fake`` runs
the same host pipeline on the deterministic stand-in for the device (tones instead of speech; no chip, no weights).
The weights are downloaded from KRAFTON's and speechbrain's repositories at the pinned commits.
"""
import argparse
import json
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "code"))
from tt_raon import config as C # noqa: E402
from tt_raon.api import RaonSpeech, open_serving_device # noqa: E402
from tt_raon.frontend.audio import encode_audio # noqa: E402
SAMPLES = ROOT / "code" / "tt_raon" / "samples"
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--fake", action="store_true", help="the deterministic stand-in instead of the p150")
ap.add_argument("--out", default="quickstart.wav")
a = ap.parse_args()
cfg = C.resolve_config({"RAON_BACKEND": "fake"} if a.fake else None)
dev = open_serving_device(cfg) if cfg.backend == "tt" else None
m = None
try:
m = RaonSpeech.from_config(cfg, device=dev)
meta = json.loads((SAMPLES / "stt_ko" / "sample.json").read_text())
r = m.transcribe(str(SAMPLES / "stt_ko" / meta["file"]), seed=1)
print(f"STT: {r['text']!r} (reference {meta['transcript']!r}; first token {r['info'].get('first_token_ms', 0):.0f} ms)")
r = m.textqa("What is the capital city of South Korea?", seed=3)
print(f"TextQA: {r['text']!r}")
t0, first = time.perf_counter(), []
r = m.tts("Hello from a Tenstorrent p150. This voice is generated as you listen.", seed=7,
on_audio=lambda pcm: first.append(time.perf_counter() - t0) if not first else None)
Path(a.out).write_bytes(encode_audio(r["pcm"], "wav")[0])
print(f"TTS: {r['seconds']:.2f} s of audio, first packet after {first[0] * 1e3:.0f} ms, stop {r['stop']} -> {a.out}")
ref = SAMPLES / "voice_ref_en"
m.add_voice("sample", str(ref / json.loads((ref / "sample.json").read_text())["file"]), keep_codes=False)
r = m.tts("This voice was cloned from a short clip.", voice="sample", seed=5)
Path("quickstart_clone.wav").write_bytes(encode_audio(r["pcm"], "wav")[0])
print(f"cloned TTS: {r['seconds']:.2f} s -> quickstart_clone.wav")
finally:
if m is not None:
m.close()
if dev is not None:
from tt_raon.tt.model import close_device
close_device(dev)
if __name__ == "__main__":
main()