Files
javis_bot/scripts/m0/m0lib/tts_melo.py
javis-bot 653d6ecc03 feat(m0): add gaming-PC measurement kit for v2 defaults
One double-click kit (scripts/m0/run_m0.bat) that measures the gaming PC
and packs the results into a single zip:

- PC info (GPU, driver, RAM, disks, monitors)
- Windows checks with the user: bot browser window hidden from Alt+Tab
  but listed in Discord's share picker and still drawing while covered;
  Discord pop-out stream window identification and occlusion; installed
  games, anti-cheat services and the borderless game detector
- MeloTTS, CosyVoice2 and Fun-CosyVoice3 Korean synthesis speed
- faster-whisper large-v3-turbo: final/partial latency, CER, wake-word
  hint, batched decoding for several speakers
- Strata LLM (fast and recommended presets): TTFT by context length,
  decode speed, tool calls, picture reading per quality tier, Korean
  answers, parallel requests, VRAM give-back and restart/switch timings

uv and Python, per-stage venvs, models and Strata all live inside the
work folder. Results are scrubbed of user/computer names and home paths.
Verified on Linux GPU (LLM client against Ollama, STT, MeloTTS,
CosyVoice2/3) and on the Windows 11 CI VM (window checks, game scan,
MeloTTS/CosyVoice2/STT installs, Strata install and plan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:38:41 +09:00

149 lines
6.3 KiB
Python

"""Stage: MeloTTS Korean speed on the GPU (runs in its own Python 3.11 venv)."""
from __future__ import annotations
import os
import sys
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from m0lib.common import StageResult, gpu_memory_mib, say, stage_args, stats # noqa: E402
SENTENCES = {
"short": ["네, 알겠습니다.", "지금 바로 틀어 드릴게요.", "서울은 지금 맑습니다."],
"medium": ["오늘 서울은 맑고 낮 최고 기온은 십삼 도까지 오르겠습니다.",
"내일 아침 일곱 시에 알람을 맞춰 두었습니다. 푹 주무세요.",
"두 번째 모니터에는 메모장과 크롬 창이 함께 떠 있습니다."],
"long": ["회의는 오후 세 시에 시작해서 다섯 시쯤 끝날 예정이고, 끝난 뒤에는 저녁 메뉴를 같이 정하기로 하셨습니다. "
"필요하시면 회의 삼십 분 전에 한 번 더 알려 드릴게요."],
}
def prepare_nltk(folder: Path) -> list[str]:
"""Fetch the English g2p data MeloTTS imports into the work folder (not the user profile)."""
import nltk
folder.mkdir(parents=True, exist_ok=True)
os.environ["NLTK_DATA"] = str(folder)
nltk.data.path.insert(0, str(folder))
got = []
for pkg, path in (("cmudict", "corpora/cmudict"), ("averaged_perceptron_tagger_eng", "taggers/averaged_perceptron_tagger_eng"),
("averaged_perceptron_tagger", "taggers/averaged_perceptron_tagger")):
if not (folder / (path + ".zip")).exists() and not (folder / path).exists():
nltk.download(pkg, download_dir=str(folder), quiet=True)
got.append(pkg)
if os.name == "posix": # newer nltk refuses data folders that others can write to
for root, dirs, files in os.walk(folder):
for n in dirs + files:
p = os.path.join(root, n)
os.chmod(p, os.stat(p).st_mode & ~0o022)
os.chmod(folder, os.stat(folder).st_mode & ~0o022)
return got
def korean_only_patches() -> list[str]:
"""Make MeloTTS Korean work without the Japanese MeCab, on Windows too.
melo imports its Japanese front end (module-level ``MeCab.Tagger()``) even
for Korean, and on Windows g2pkk asks for the ``eunjeon`` package instead of
python-mecab-ko. Both are replaced here; v2 will need the same.
"""
import importlib
import types
done = []
try:
import MeCab # noqa: F401
MeCab.Tagger # noqa: B018
except Exception:
stub = types.ModuleType("MeCab")
class Tagger: # Japanese text is never synthesised in M0
def __init__(self, *a, **k):
pass
def parse(self, text):
raise RuntimeError("Japanese MeCab is not installed")
stub.Tagger = Tagger
sys.modules["MeCab"] = stub
done.append("MeCab stub")
import g2pkk.g2pkk as g2p_mod
if g2p_mod.platform.system() == "Windows":
g2p_mod.G2p.check_mecab = lambda self: None
g2p_mod.G2p.get_mecab = lambda self: importlib.import_module("mecab").MeCab()
done.append("g2pkk uses python-mecab-ko")
return done
def main() -> int:
out, work, opts = stage_args()
r = StageResult("tts_melo", out)
os.environ.setdefault("HF_HOME", str(work / "models" / "hf"))
audio_dir = out.parent / "audio"
audio_dir.mkdir(parents=True, exist_ok=True)
try:
r["nltk"] = prepare_nltk(work / "models" / "nltk_data")
r["patches"] = korean_only_patches()
import numpy as np
import soundfile as sf
import torch
from melo.api import TTS
r["versions"] = {"torch": torch.__version__, "cuda": torch.version.cuda,
"cuda_available": torch.cuda.is_available(),
"gpu": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None}
except Exception as e:
r.error("import", e)
r.save("failed")
return 0
device = "cuda" if torch.cuda.is_available() else "cpu"
try:
before = gpu_memory_mib()
t0 = time.perf_counter()
model = TTS(language="KR", device=device)
spk = model.hps.data.spk2id["KR"]
r["load_s"] = round(time.perf_counter() - t0, 2)
t0 = time.perf_counter()
model.tts_to_file("워밍업입니다.", spk, None, speed=1.0, quiet=True)
r["first_synth_s"] = round(time.perf_counter() - t0, 2)
after = gpu_memory_mib()
if before and after:
r["vram_model_mib"] = after[0] - before[0]
sr = model.hps.data.sampling_rate
say(f" 로딩 {r['load_s']}s, 첫 합성(워밍업) {r['first_synth_s']}s, VRAM 약 {r.get('vram_model_mib')} MiB")
except Exception as e:
r.error("load", e)
r.save("failed")
return 0
for speed in (1.0, 1.5):
rows = []
for kind, texts in SENTENCES.items():
for text in texts:
for i in range(2):
if device == "cuda":
torch.cuda.synchronize()
t0 = time.perf_counter()
wav = model.tts_to_file(text, spk, None, speed=speed, quiet=True)
if device == "cuda":
torch.cuda.synchronize()
sec = time.perf_counter() - t0
dur = len(wav) / sr
rows.append({"kind": kind, "chars": len(text), "seconds": round(sec, 3),
"audio_s": round(dur, 2), "rtf": round(sec / dur, 3), "try": i + 1})
if kind != "short":
name = f"melo_{kind}_{str(speed).replace('.', '_')}.wav"
sf.write(str(audio_dir / name), np.asarray(wav, dtype=np.float32), sr)
r[f"speed_{speed}"] = {"rows": rows, **{k: stats(x["seconds"] for x in rows if x["kind"] == k)
for k in SENTENCES},
"rtf": stats(x["rtf"] for x in rows)}
say(f" 속도 {speed}: 짧은 문장 {r[f'speed_{speed}']['short'].get('median')}s, "
f"보통 문장 {r[f'speed_{speed}']['medium'].get('median')}s")
r["device"] = device
r["audio_files"] = sorted(p.name for p in audio_dir.glob("melo_*.wav"))
r.save("ok" if not r["errors"] else "partial")
return 0
if __name__ == "__main__":
sys.exit(main())