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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! LID per-domain load entry points, ported from
//! [`mlx_audio.lid.utils`][lid-utils].
//!
//! Faithful 1:1 of mlx-audio's two-tier shape:
//!
//! - [`load_model`] ([lid-utils.py:16-38][lid-utils-loadmodel]) — the
//! inner factory call mlx-audio funnels into
//! `base_load_model(model_path=…, category="lid",
//! model_remapping=MODEL_REMAPPING, …)`.
//! - [`load`] ([lid-utils.py:41-66][lid-utils-load]) — the **main entry
//! point**; thin alias over [`load_model`] (`return load_model(...)`).
//! Carried as a separate function because mlx-audio's public surface
//! carries both (`from mlx_audio.lid import load`).
//!
//! mlx-audio's `MODEL_REMAPPING` table ([lid-utils.py:10-13][lid-utils-remap])
//! routes the two ecapa-tdnn aliases (`ecapa-tdnn` / `ecapa_tdnn`) into
//! `mlx_audio.lid.models.ecapa_tdnn`; per the no-per-model-arch rule
//! mlxrs returns a [`LidModel`] trait object the per-architecture
//! loader's caller constructs, so the table is exposed in
//! [`MODEL_REMAPPING`] for reference but no arch crate is imported
//! here.
//!
//! [lid-utils]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py
//! [lid-utils-loadmodel]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L16-L38
//! [lid-utils-load]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L41-L66
//! [lid-utils-remap]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L10-L13
use crate::;
/// mlx-audio's documented sample rate for every LID architecture —
/// [lid-utils.py:8][lid-utils-sr] (`SAMPLE_RATE = 16000`).
///
/// Exposed here so a caller wiring a per-architecture [`LidModel`] can
/// resample input audio to the model's expected rate without restating
/// the constant.
///
/// [lid-utils-sr]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L8
pub const LID_SAMPLE_RATE: u32 = 16_000;
/// The mlx-audio `MODEL_REMAPPING` table for LID architectures
/// ([lid-utils.py:10-13][lid-utils-remap]) — `(alias,
/// canonical_module_name)` pairs.
///
/// **Reference-only**: per the no per-model arch porting rule,
/// mlxrs does NOT import per-architecture crates from this table — it
/// exists purely so an end-of-pipeline caller can mirror mlx-audio's
/// alias resolution.
///
/// [lid-utils-remap]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L10-L13
pub const MODEL_REMAPPING: & =
&;
/// The trait every per-architecture LID model implements — the analogue
/// of mlx-audio's `Model.predict(audio, top_k=…) -> List[Tuple[str,
/// float]]` ([wav2vec_lid.py:101-148][lid-predict-wav2vec2],
/// [ecapa_tdnn.py:135-163][lid-predict-ecapa]).
///
/// Per the no per-model arch porting rule, mlxrs ships no
/// concrete LID models; this trait is the *shape* per-architecture
/// crates (wav2vec2 / ecapa_tdnn) implement so a caller can hand-off
/// any LID architecture as a `Box<dyn LidModel>` from the [`load`] /
/// [`load_model`] entry points.
///
/// `&self` because weights are immutable after load.
///
/// [lid-predict-wav2vec2]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/models/wav2vec2/wav2vec_lid.py#L101-L148
/// [lid-predict-ecapa]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/models/ecapa_tdnn/ecapa_tdnn.py#L135-L163
/// Helper: build an [`LidOutput`] from the raw `(language_code,
/// probability)` pairs an architecture's `predict` implementation
/// computes. Threading parity with mlx-audio's `return [(label, prob)
/// for …]` list-comprehension output.
///
/// `pairs` is **assumed sorted by probability descending** — the
/// per-architecture code's responsibility, matching mlx-audio's `sorted
/// (…, reverse=True)` precondition.
/// Construct a [`LidModel`] from a local on-disk model directory —
/// faithful 1:1 of `mlx_audio.lid.utils.load_model`
/// ([lid-utils.py:16-38][lid-utils-loadmodel]).
///
/// Routes through the shared
/// [`crate::audio::load::base_load_model`] factory (the analog of
/// mlx-audio's `return base_load_model(model_path=…, category="lid",
/// model_remapping=MODEL_REMAPPING, …)`). The returned bundle is
/// handed to the caller-supplied `constructor` closure (per the
/// no-per-model-arch rule mlxrs does not bundle a built-in registry).
///
/// Failures are typed: missing dir →
/// [`Error::MissingKey`](crate::error::Error::MissingKey), hub path →
/// [`Error::OutOfRange`](crate::error::Error::OutOfRange), malformed JSON →
/// [`Error::Parse`](crate::error::Error::Parse), constructor
/// error → caller-defined.
///
/// [lid-utils-loadmodel]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L16-L38
/// The **main entry point** for loading an LID model — faithful 1:1 of
/// `mlx_audio.lid.utils.load` ([lid-utils.py:41-66][lid-utils-load]).
/// Thin alias over [`load_model`].
///
/// [lid-utils-load]: https://github.com/Blaizzy/mlx-audio/blob/main/mlx_audio/lid/utils.py#L41-L66