kokoro-kmp-models / scripts /convert_voice.py
Shusek00's picture
Publish complete Kokoro multilingual catalog v2.0.0
0dbae7f verified
Raw
History Blame Contribute Delete
870 Bytes
#!/usr/bin/env python3
"""Convert a Kokoro PyTorch voice tensor to a portable raw float32 matrix."""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
import torch
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("source", type=Path)
parser.add_argument("destination", type=Path)
args = parser.parse_args()
voice = torch.load(args.source, map_location="cpu", weights_only=True)
values = voice.detach().cpu().numpy().astype("<f4", copy=False).reshape(510, 256)
if values.shape != (510, 256) or not np.isfinite(values).all():
raise RuntimeError(f"Invalid Kokoro voice tensor: {args.source}")
args.destination.parent.mkdir(parents=True, exist_ok=True)
values.tofile(args.destination)
print(args.destination)
if __name__ == "__main__":
main()