Download src/maya_fast/codec.py from mrwine/maya2-native-fast: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/mrwine/maya2-native-fast/resolve/main/src/maya_fast/codec.py
- Command line
-
hf download hf://mrwine/maya2-native-fast/src/maya_fast/codec.py
-
curl -L -o codec.py https://huggingface.co/mrwine/maya2-native-fast/resolve/main/src/maya_fast/codec.py
1.09 kB
| from __future__ import annotations | |
| from pathlib import Path | |
| import numpy as np | |
| import onnxruntime as ort | |
| from .ort_opts import session_options | |
| class MimiDecoder: | |
| def __init__(self, path: str | Path, threads: int | None = None) -> None: | |
| self.path = Path(path) | |
| self.opts = session_options(threads) | |
| self.session = ort.InferenceSession(str(self.path), self.opts, providers=["CPUExecutionProvider"]) | |
| def decode(self, codes: np.ndarray) -> np.ndarray: | |
| """ | |
| Input: | |
| codes: (16, F) int64 | |
| Returns: | |
| wav: (F * 1920,) float32 | |
| """ | |
| codes = np.asarray(codes, dtype=np.int64) | |
| if codes.ndim == 2: | |
| codes_input = codes[None, :, :] # (1, 16, F) | |
| elif codes.ndim == 3: | |
| codes_input = codes | |
| else: | |
| raise ValueError(f"Expected codes to have 2 or 3 dims, got {codes.shape}") | |
| outputs = self.session.run(None, {"codes": codes_input}) | |
| wav = outputs[0] # (1, 1, samples) | |
| wav = np.squeeze(wav) | |
| return wav.astype(np.float32) | |