File size: 2,704 Bytes
35d483e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Serialization helpers for the stable runtime model contract."""

from __future__ import annotations

from typing import Any

from .features import LogMelConfig


def build_runtime_metadata(
    feature_config: LogMelConfig,
    *,
    max_seconds: float,
    threshold: float,
    model_name: str,
    architecture: str,
    model_version: str = "1",
    development_only: bool = True,
    training_status: str = "development",
    data_scope: str | None = None,
    data_revision: str | None = None,
    parameter_count: int | None = None,
) -> dict[str, Any]:
    """Build JSON accepted by :class:`runtime.predictor.ModelMetadata`.

    The dependency-light runtime currently implements the HTK filterbank.  A
    Slaney-mel Whisper teacher therefore must be distilled before deployment.
    """

    if feature_config.mel_scale != "htk":
        raise ValueError("the runtime frontend currently supports HTK mel filters only")
    if feature_config.normalize:
        raise ValueError("per-utterance standardization is not represented by runtime metadata")
    if feature_config.log_scale not in {"whisper", "standard"}:
        raise ValueError(f"unsupported runtime log scale: {feature_config.log_scale}")
    if not 0.0 <= threshold <= 1.0:
        raise ValueError("threshold must be in [0, 1]")
    return {
        "model_name": model_name,
        "architecture": architecture,
        "frontend": {
            "sample_rate": feature_config.sample_rate,
            "max_seconds": max_seconds,
            "n_fft": feature_config.n_fft,
            "win_length": feature_config.win_length,
            "hop_length": feature_config.hop_length,
            "n_mels": feature_config.n_mels,
            "f_min": feature_config.f_min,
            "f_max": feature_config.f_max,
            "normalization": ("whisper" if feature_config.log_scale == "whisper" else "log10"),
            "pad_side": feature_config.pad_side,
        },
        "threshold": threshold,
        "controller": {
            "endpoint_threshold": threshold,
            "long_pause_threshold": max(0.0, threshold - 0.18),
            "min_silence_ms": 200.0,
            "relax_after_ms": 800.0,
            "max_silence_ms": 1800.0,
            "required_confirmations": 1,
        },
        "input_features_name": "log_mel",
        "frame_mask_name": "frame_mask",
        "endpoint_output_name": "endpoint_probability",
        "output_type": "probability",
        "model_version": model_version,
        "development_only": development_only,
        "training_status": training_status,
        "data_scope": data_scope,
        "data_revision": data_revision,
        "parameter_count": parameter_count,
    }