Instructions to use semfence/pivo-ptbr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use semfence/pivo-ptbr with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download semfence/pivo-ptbr --local-dir pivo-ptbr
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
File size: 2,620 Bytes
ee383fc aca3639 ee383fc aca3639 ee383fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | """Converte `best.pt` (PyTorch) e os ONNX do vocoder em pesos MLX (safetensors).
Uso: python code/convert.py pivo-ptbr.pt onnx mlx onnx/metadata.json
"""
import json, sys
from pathlib import Path
import numpy as np
import onnx
import torch
from onnx import numpy_helper as nh
from safetensors.numpy import save_file
def acoustic(ckpt):
sd = {k: v.numpy().astype(np.float32) for k, v in ckpt["model"].items()}
out = {}
for k, v in sd.items():
if v.ndim == 3: # Conv1d (out,in/groups,k) -> MLX (out,k,in/groups)
v = v.transpose(0, 2, 1)
if ".dw." in k or ".pw." in k or ".norms." in k:
pass
out[k] = np.ascontiguousarray(v)
return out
def vocoder(directory):
d = Path(directory)
c = {i.name: nh.to_array(i) for i in onnx.load(d / "conditioning.onnx").graph.initializer}
s = {i.name: nh.to_array(i) for i in onnx.load(d / "vocoder_step.onnx").graph.initializer}
m = lambda n: s[f"onnx::MatMul_{n}"]
w = {"cond.pitch_embedding": c["pitch_embedding.weight"], "cond.dense1": c["onnx::MatMul_20"],
"cond.conv": c["conv.weight"].transpose(0, 2, 1), "cond.dense2": c["onnx::MatMul_21"],
"cond_gain.w": s["cond_gain.weight"], "cond_gain.b": s["cond_gain.bias"],
"pitch_gain.w": s["pitch_gain.weight"], "pitch_gain.b": s["pitch_gain.bias"],
"frame_conv": m(274), "frame_glu": m(275),
"gru1.input": m(282), "gru1.hidden": m(283), "glu1": m(286),
"gru2.input": m(293), "gru2.hidden": m(294), "glu2": m(297),
"gru3.input": m(304), "gru3.hidden": m(305), "glu3": m(308),
"skip_dense": m(315), "skip_glu": m(318), "signal_out": m(319)}
return {k: np.ascontiguousarray(v.astype(np.float32)) for k, v in w.items()}
if __name__ == "__main__":
ckpt_path, vdir, out = sys.argv[1:4]
out = Path(out); out.mkdir(parents=True, exist_ok=True)
ck = torch.load(ckpt_path, map_location="cpu", weights_only=True)
save_file(acoustic(ck), str(out / "acoustic.safetensors"))
save_file(vocoder(vdir), str(out / "vocoder.safetensors"))
meta = json.loads(Path(sys.argv[4] if len(sys.argv) > 4 else Path(vdir) / "metadata.json").read_text())
meta["format"] = "pivo-ptbr-mlx-v1"; meta["config"] = {**ck["config"]}
(out / "config.json").write_text(json.dumps({"config": ck["config"], "sample_rate": 16000, "step": ck["step"],
"voices": meta["voices"], "symbols": meta["symbols"], "preemphasis": 0.85, "max_utterance_frames": 3000,
"format": "pivo-ptbr-mlx-v1"}, ensure_ascii=False, indent=1))
print("ok", out)
|