-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_queries.py
More file actions
412 lines (380 loc) · 16 KB
/
Copy pathadmin_queries.py
File metadata and controls
412 lines (380 loc) · 16 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from __future__ import annotations
from importlib import util as importlib_util
from types import MappingProxyType
from typing import Any
from app import schemas
from app.build_info import current_commit
from app.catalog import catalog_source_url, language_names, recommended_ids
from app.context import BOOTSTRAP_TOKEN_ID, TOKEN_FILE_HINT, VERSION, GatewayContext
from app.engine_state import active_model_path, available_engines, engine_id
from app.pairing import default_pairing_url, is_phone_reachable_gateway_url
from app.serializers import metrics_status, model_covers
from app.system import detect_system
PYTHON_PACKAGE_PATH = "Python package"
SIZE_FILTER_CAPS: MappingProxyType[str, int] = MappingProxyType(
{
"100mb": 100_000_000,
"300mb": 300_000_000,
"800mb": 800_000_000,
"1500mb": 1_500_000_000,
}
)
class _SystemDependencyHelper:
def __init__(self, ctx: GatewayContext) -> None:
self.ctx = ctx
self.settings = ctx.settings
self.system = detect_system(
whisper_binary=self.settings.whisper_binary,
whisperkit_binary=self.settings.whisperkit_binary,
handy_binary=self.settings.handy_binary,
vocamac_app=self.settings.vocamac_app,
)
def build_dependencies(self) -> list[schemas.DependencyStatus]:
is_mac = self.system.os_name == "Darwin"
ffmpeg_hint = (
"brew install ffmpeg" if is_mac else "Install FFmpeg with your Linux package manager"
)
whisper_hint = (
"brew install whisper-cpp"
if is_mac
else "Included in Docker or build whisper.cpp from source"
)
deps = [
schemas.DependencyStatus(
name="FFmpeg",
available=self.system.ffmpeg_path is not None,
path=self.system.ffmpeg_path,
install_hint=ffmpeg_hint,
),
schemas.DependencyStatus(
name="whisper.cpp CLI",
available=self.system.whisper_cpp_path is not None,
path=self.system.whisper_cpp_path,
install_hint=whisper_hint,
),
]
deps.extend(self._python_dependencies())
deps.extend(self._app_dependencies(is_mac))
return deps
def build_checklist(
self, dependencies: list[schemas.DependencyStatus], ready: bool
) -> schemas.SetupChecklist:
return schemas.SetupChecklist(
token_configured=True,
ffmpeg_available=self.system.ffmpeg_path is not None,
engine_binary_available=any(dep.available for dep in dependencies[1:]),
model_installed=bool(self.ctx.manager.installed())
or (self.ctx.engine_manager is not None and ready),
engine_ready=ready,
)
def build_commit_status(self) -> schemas.CommitStatus | None:
if self.ctx.settings.debug:
commit = current_commit()
if commit:
return schemas.CommitStatus(
sha=commit.sha,
short_sha=commit.short_sha,
subject=commit.subject,
committed_at=commit.committed_at,
)
return None
def os_summary(self) -> str:
name = self.system.os_name
version = self.system.os_version
return f"{name} {version}"
def _python_dependencies(self) -> list[schemas.DependencyStatus]:
silicon = self.system.is_apple_silicon
mlx_ready = silicon and importlib_util.find_spec("mlx_audio") is not None
mlx_hint = (
"Install vocagateway[apple]" if silicon else "Available only on Apple-silicon Macs"
)
return [
schemas.DependencyStatus(
name="faster-whisper",
available=importlib_util.find_spec("faster_whisper") is not None,
path=PYTHON_PACKAGE_PATH if importlib_util.find_spec("faster_whisper") else None,
install_hint="Install vocagateway[engines] or use the Docker image",
),
schemas.DependencyStatus(
name="Moonshine Voice",
available=importlib_util.find_spec("moonshine_voice") is not None,
path=PYTHON_PACKAGE_PATH if importlib_util.find_spec("moonshine_voice") else None,
install_hint="Install vocagateway[engines] or use the Docker image",
),
schemas.DependencyStatus(
name="sherpa-onnx",
available=importlib_util.find_spec("sherpa_onnx") is not None,
path=PYTHON_PACKAGE_PATH if importlib_util.find_spec("sherpa_onnx") else None,
install_hint="Install vocagateway[engines] or use the Docker image",
),
schemas.DependencyStatus(
name="MLX Audio",
available=mlx_ready,
path=PYTHON_PACKAGE_PATH if mlx_ready else None,
install_hint=mlx_hint,
),
]
def _app_dependencies(self, is_mac: bool) -> list[schemas.DependencyStatus]:
silicon = self.system.is_apple_silicon
wk_hint = "brew install whisperkit-cli" if is_mac else "Available only on Apple platforms"
vocamac_hint = (
"https://github.com/jatinkrmalik/vocamac"
if silicon
else "Available only on Apple silicon Macs"
)
return [
schemas.DependencyStatus(
name="WhisperKit CLI",
available=self.system.whisperkit_cli_path is not None,
path=self.system.whisperkit_cli_path,
install_hint=wk_hint,
),
schemas.DependencyStatus(
name="Handy app",
available=self.system.handy_installed,
path=str(self.settings.handy_binary) if self.system.handy_installed else None,
install_hint="https://handy.computer" if is_mac else "Available only on macOS",
),
schemas.DependencyStatus(
name="VocaMac app",
available=self.system.vocamac_installed,
path=str(self.settings.vocamac_app) if self.system.vocamac_installed else None,
install_hint=vocamac_hint,
),
]
_ModelState = tuple[str, float | None, str | None]
class _ModelEntryHelper:
def __init__(self, ctx: GatewayContext) -> None:
self.ctx = ctx
self.system = detect_system(
whisper_binary=ctx.settings.whisper_binary,
whisperkit_binary=ctx.settings.whisperkit_binary,
handy_binary=ctx.settings.handy_binary,
vocamac_app=ctx.settings.vocamac_app,
)
self.recommended = recommended_ids(self.system)
self.installed = {model.id: model for model in ctx.manager.installed()}
self.active_path = active_model_path(ctx)
def collect_entries(self) -> list[schemas.AdminModelEntry]:
catalog_entries = [
self.build_entry(model) for model in self.ctx.manager.catalog if self.is_visible(model)
]
catalog_ids = {entry.id for entry in catalog_entries}
retired_entries = [
self.build_entry(model)
for installed in self.ctx.manager.installed()
if installed.retired
and installed.id not in catalog_ids
and (model := self.ctx.manager.catalog_model(installed.id)) is not None
]
custom_entries = [
self.build_custom_entry(custom)
for custom in self.ctx.manager.installed()
if custom.id.startswith("custom:")
]
return catalog_entries + retired_entries + custom_entries
def is_visible(self, model: Any) -> bool:
if self.system.is_apple_silicon:
return True
return model.engine != "whisperkit" and not model.apple_silicon_only
def build_entry(self, model: Any) -> schemas.AdminModelEntry:
download = self.ctx.manager.download_state(model.id)
inst = self.installed.get(model.id)
resolution = self._resolve_state(download, inst)
is_active = inst is not None and inst.path == self.active_path
return schemas.AdminModelEntry(
id=model.id,
engine=model.engine,
label=model.label,
size_bytes=inst.size_bytes if inst else model.size_bytes,
languages=model.languages,
quality=model.quality,
family=model.family,
description=model.description,
source=model.source,
source_url=catalog_source_url(model),
supports_streaming=model.supports_streaming,
license_name=model.license_name,
commercial_use=model.commercial_use,
detects_language_automatically=model.detects_language_automatically,
language_names=language_names(model.language_codes),
language_codes=list(model.language_codes),
state=resolution[0],
active=is_active,
recommended=model.id in self.recommended,
progress=resolution[1],
downloaded_bytes=download.downloaded_bytes if download else None,
total_bytes=download.total_bytes if download else None,
error=resolution[2],
retired=model.retired,
replacement_id=model.replacement_id,
retirement_reason=model.retirement_reason,
)
def build_custom_entry(self, custom: Any) -> schemas.AdminModelEntry:
key = custom.key
return schemas.AdminModelEntry(
id=custom.id,
engine=custom.engine,
label=f"Custom: {key}",
size_bytes=custom.size_bytes,
languages="Unknown",
quality="Custom",
family="Custom Whisper",
description="User-provided local model.",
source="Local file",
state="installed",
active=custom.path == self.active_path,
recommended=False,
)
def filter_by_criteria(
self,
entries: list[schemas.AdminModelEntry],
language: Any,
family: Any,
engine: Any,
max_size: Any,
) -> list[schemas.AdminModelEntry]:
matching = entries
if language:
codes = [language] if isinstance(language, str) else list(language)
matching = [
model for model in matching if any(model_covers(model, code) for code in codes)
]
if family:
fam_set = set([family] if isinstance(family, str) else family)
matching = [model for model in matching if model.family in fam_set]
if engine:
eng_set = set([engine] if isinstance(engine, str) else engine)
matching = [model for model in matching if model.engine in eng_set]
cap = SIZE_FILTER_CAPS.get(str(max_size).strip().lower())
if cap is not None:
matching = [model for model in matching if model.size_bytes <= cap]
return matching
def _resolve_state(self, download: Any, inst: Any) -> _ModelState:
if download and download.status == "downloading":
progress = None
if download.total_bytes:
progress = round(download.downloaded_bytes / download.total_bytes, 4)
return "downloading", progress, None
if inst:
return "installed", None, None
if download and download.status == "failed":
return "not_installed", None, download.error
return "not_installed", None, None
async def status_payload(ctx: GatewayContext) -> schemas.AdminStatusResponse:
helper = _SystemDependencyHelper(ctx)
dependencies = helper.build_dependencies()
readiness_details = await ctx.readiness.details()
state = readiness_details.health
metrics = ctx.service.metrics.snapshot(sample=True)
pairing_url = default_pairing_url(
ctx.settings.port, saved_pairing_url=ctx.pairing_config.pairing_url
)
if pairing_url is not None and not is_phone_reachable_gateway_url(pairing_url):
pairing_url = None
return schemas.AdminStatusResponse(
version=VERSION,
commit=helper.build_commit_status(),
engine=schemas.EngineStatus(id=engine_id(ctx), name=state.name, ready=state.ready),
system=schemas.SystemStatus(
os=helper.os_summary(),
arch=helper.system.arch,
chip=helper.system.chip,
ram_gb=helper.system.ram_gb,
is_apple_silicon=helper.system.is_apple_silicon,
logical_cpus=helper.system.logical_cpus,
effective_cpus=helper.system.effective_cpus,
containerized=helper.system.containerized,
accelerators=list(helper.system.accelerators),
cpu_features=list(helper.system.cpu_features),
),
dependencies=dependencies,
paths=schemas.PathStatus(
data_dir=str(ctx.settings.data_dir),
models_dir=str(ctx.manager.models_dir),
config_file=str(ctx.config_path),
token_file=TOKEN_FILE_HINT,
),
bind_host=ctx.settings.bind_host,
port=ctx.settings.port,
setup=helper.build_checklist(dependencies, state.ready),
metrics=metrics_status(metrics),
readiness=schemas.ReadinessStatus(
probe_age_seconds=round(readiness_details.checked_age_seconds, 3),
warmup_state=readiness_details.warmup_state,
warmed_bytes=readiness_details.warmed_bytes,
),
pairable=pairing_url is not None,
pairing_url=pairing_url,
ready_for_dictation=bool(state.ready),
)
def model_entries(ctx: GatewayContext) -> list[schemas.AdminModelEntry]:
return _ModelEntryHelper(ctx).collect_entries()
def filtered_model_entries(
ctx: GatewayContext,
installed_only: bool = False,
language: str | list[str] | None = None,
family: str | list[str] | None = None,
**kwargs: Any,
) -> list[schemas.AdminModelEntry]:
helper = _ModelEntryHelper(ctx)
entries = model_entries(ctx)
if installed_only:
entries = [entry for entry in entries if entry.state == "installed"]
entries = helper.filter_by_criteria(
entries,
language=language,
family=family,
engine=kwargs.get("engine"),
max_size=kwargs.get("max_size", ""),
)
if kwargs.get("recommended_only", False):
entries = [entry for entry in entries if entry.recommended]
return entries
def token_entries(ctx: GatewayContext) -> list[schemas.DeviceTokenEntry]:
entries = [
schemas.DeviceTokenEntry(
id=BOOTSTRAP_TOKEN_ID,
label="Bootstrap token (VOCAGATEWAY_TOKEN / token file)",
created_at=None,
revocable=False,
)
]
entries.extend(
schemas.DeviceTokenEntry(
id=token.id, label=token.label, created_at=token.created_at, revocable=True
)
for token in ctx.token_store.all()
)
return entries
def config_response(ctx: GatewayContext) -> schemas.ConfigResponse:
rc = ctx.engine_manager.runtime_config if ctx.engine_manager else None
if rc:
return schemas.ConfigResponse(
engine=rc.engine,
available_engines=available_engines(ctx),
whisper_model=rc.whisper_model,
whisperkit_model=rc.whisperkit_model,
faster_whisper_model=rc.faster_whisper_model,
moonshine_model=rc.moonshine_model,
moonshine_language=rc.moonshine_language,
sherpa_model=rc.sherpa_model,
mlx_audio_model=rc.mlx_audio_model,
compute_device=rc.compute_device,
compute_type=rc.compute_type,
cpu_threads=rc.cpu_threads,
)
return schemas.ConfigResponse(
engine="custom",
available_engines=available_engines(ctx),
whisper_model=None,
whisperkit_model=None,
faster_whisper_model=None,
moonshine_model="moonshine:en",
moonshine_language="en",
sherpa_model=None,
mlx_audio_model=None,
compute_device="auto",
compute_type="auto",
cpu_threads=0,
)