Smart Turn v3.2 fine-tuned for phone lines: ONNX and PyTorch weights, with tests and scripts
6daf493 Download scripts/test_smartturn.py from matthiastl/smart-turn-telephony: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/matthiastl/smart-turn-telephony/resolve/main/scripts/test_smartturn.py
- Command line
-
hf download hf://matthiastl/smart-turn-telephony/scripts/test_smartturn.py
-
curl -L -o test_smartturn.py https://huggingface.co/matthiastl/smart-turn-telephony/resolve/main/scripts/test_smartturn.py
1.68 kB
| """The PyTorch Smart Turn must give the same answers as pipecat's ONNX file, and survive export. | |
| python -I -m pytest test_smartturn.py | |
| """ | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pytest | |
| import torch | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| import smartturn # noqa: E402 | |
| ORIGINAL = Path(__file__).parent / "data" / "smartturn" / "smart-turn-v3.2-gpu.onnx" | |
| pytestmark = pytest.mark.skipif(not ORIGINAL.exists(), reason="pipecat's smart-turn-v3.2-gpu.onnx is not downloaded") | |
| def onnx_probability(path: Path, features: np.ndarray) -> np.ndarray: | |
| import onnxruntime as ort | |
| session = ort.InferenceSession(str(path), providers=["CPUExecutionProvider"]) | |
| return session.run(None, {"input_features": features})[0][:, 0] | |
| def test_rebuild_matches_onnx(): | |
| features = np.random.default_rng(0).standard_normal((3, 80, 800)).astype(np.float32) | |
| with torch.no_grad(): | |
| ours = torch.sigmoid(smartturn.from_onnx(ORIGINAL)(torch.from_numpy(features))).numpy() | |
| assert np.abs(ours - onnx_probability(ORIGINAL, features)).max() < 1e-5 | |
| def test_export_round_trip(tmp_path): | |
| model = smartturn.from_onnx(ORIGINAL) | |
| path = tmp_path / "exported.onnx" | |
| smartturn.export(model, path) | |
| features = np.random.default_rng(1).standard_normal((2, 80, 800)).astype(np.float32) | |
| assert np.abs(onnx_probability(path, features) - onnx_probability(ORIGINAL, features)).max() < 1e-5 | |
| def test_windows_pad_at_the_front(): | |
| window = smartturn.windows([np.ones(16000, dtype=np.float32)])[0] | |
| assert window.shape == (smartturn.LENGTH,) | |
| assert window[: smartturn.LENGTH - 16000].abs().max() == 0 and window[-16000:].min() == 1 | |