Download code/tt_raon/cli.py from changh95/Raon-Speech-9B: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/changh95/Raon-Speech-9B/resolve/main/code/tt_raon/cli.py
- Command line
-
hf download hf://changh95/Raon-Speech-9B/code/tt_raon/cli.py
-
curl -L -o cli.py https://huggingface.co/changh95/Raon-Speech-9B/resolve/main/code/tt_raon/cli.py
3.23 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Command line front end of the Python API (one request per run; the chip opened on the main thread). | |
| python -m tt_raon.cli stt clip.wav | |
| python -m tt_raon.cli tts "Hello there." -o out.wav [--speaker ref.wav] | |
| python -m tt_raon.cli continue "And then ..." --ref ref.wav --ref-text "..." -o out.wav | |
| python -m tt_raon.cli chat "What is the capital of Korea?" [--audio q.wav] | |
| python -m tt_raon.cli speech-chat question.wav | |
| Backend / engines / sizes from the ``RAON_*`` environment (config.py); ``--backend cpu`` runs the fp32 host reference. | |
| On the shared workspace run device commands through ``bin/devrun``. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| def main(argv=None) -> int: | |
| ap = argparse.ArgumentParser(prog="tt_raon.cli") | |
| ap.add_argument("task", choices=["stt", "tts", "continue", "chat", "speech-chat", "textqa"]) | |
| ap.add_argument("input", help="audio path (stt, speech-chat) or text") | |
| ap.add_argument("-o", "--out", default="raon_out.wav") | |
| ap.add_argument("--audio") | |
| ap.add_argument("--speaker") | |
| ap.add_argument("--ref") | |
| ap.add_argument("--ref-text") | |
| ap.add_argument("--seed", type=int) | |
| ap.add_argument("--max-new-tokens", type=int) | |
| ap.add_argument("--temperature", type=float) | |
| ap.add_argument("--backend", choices=["tt", "cpu"]) | |
| args = ap.parse_args(argv) | |
| if args.backend: | |
| os.environ["RAON_BACKEND"] = args.backend | |
| from . import config as C | |
| from .api import RaonSpeech, open_serving_device | |
| cfg = C.resolve_config() | |
| dev = open_serving_device(cfg) if cfg.backend == "tt" else None | |
| m = None | |
| try: | |
| m = RaonSpeech.from_config(cfg, device=dev) | |
| p = {k: v for k, v in (("seed", args.seed), ("max_new_tokens", args.max_new_tokens), | |
| ("temperature", args.temperature)) if v is not None} | |
| p["on_text"] = lambda d: (sys.stdout.write(d), sys.stdout.flush()) | |
| if args.task == "stt": | |
| out = m.transcribe(args.input, **p) | |
| elif args.task == "speech-chat": | |
| out = m.speech_chat(args.input, **p) | |
| elif args.task in ("chat", "textqa"): | |
| out = m.textqa(args.input, audio=args.audio, **p) | |
| elif args.task == "tts": | |
| p.pop("on_text") | |
| out = m.tts(args.input, speaker_audio=args.speaker, **p) | |
| else: | |
| p.pop("on_text") | |
| out = m.tts_continuation(args.input, ref_audio=args.ref, ref_text=args.ref_text, | |
| speaker_audio=args.speaker, **p) | |
| print() | |
| if "pcm" in out: | |
| from .frontend.audio import encode_audio | |
| with open(args.out, "wb") as f: | |
| f.write(encode_audio(out["pcm"], "wav")[0]) | |
| print(f"wrote {args.out} ({out['seconds']:.2f} s)") | |
| print(json.dumps({k: v for k, v in out.items() if k not in ("pcm",)}, default=str, ensure_ascii=False)) | |
| finally: | |
| if m is not None: | |
| m.close() | |
| if dev is not None: | |
| from .tt.model import close_device | |
| close_device(dev) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |