forked from TheDeathDragon/LiveTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_manager.py
More file actions
686 lines (583 loc) · 23.4 KB
/
Copy pathmodel_manager.py
File metadata and controls
686 lines (583 loc) · 23.4 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import os
import contextlib
import logging
from pathlib import Path
log = logging.getLogger("LiveTranslate.ModelManager")
_PROXY_ENV_KEYS = (
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
"http_proxy",
"https_proxy",
"all_proxy",
)
@contextlib.contextmanager
def _proxy_env(proxy: str):
"""Temporarily route all download backends through a proxy.
proxy:
"system" / "" / None -> leave ambient env & OS proxy untouched
"none" -> force-disable any proxy for this download
a URL -> send urllib/requests/httpx traffic through it
Covers torch.hub (urllib), huggingface_hub and modelscope (requests),
which all honor the *_PROXY env vars; urllib additionally gets an explicit
opener so a previously cached default opener cannot bypass the setting.
"""
import urllib.request
if proxy in ("system", "", None):
yield
return
saved_env: dict = {key: os.environ.get(key) for key in _PROXY_ENV_KEYS}
saved_no_proxy = os.environ.get("NO_PROXY")
saved_opener = getattr(urllib.request, "_opener", None)
try:
if proxy == "none":
for key in _PROXY_ENV_KEYS:
os.environ.pop(key, None)
os.environ["NO_PROXY"] = "*"
handler = urllib.request.ProxyHandler({})
else:
for key in _PROXY_ENV_KEYS:
os.environ[key] = proxy
os.environ.pop("NO_PROXY", None)
handler = urllib.request.ProxyHandler({"http": proxy, "https": proxy})
urllib.request.install_opener(urllib.request.build_opener(handler))
log.info(f"Download proxy active: {proxy}")
yield
finally:
for key, value in saved_env.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
if saved_no_proxy is None:
os.environ.pop("NO_PROXY", None)
else:
os.environ["NO_PROXY"] = saved_no_proxy
urllib.request.install_opener(saved_opener)
APP_DIR = Path(__file__).parent
MODELS_DIR = APP_DIR / "models"
ASR_MODEL_IDS = {
"sensevoice": "iic/SenseVoiceSmall",
"funasr-nano": "FunAudioLLM/Fun-ASR-Nano-2512",
"funasr-mlt-nano": "FunAudioLLM/Fun-ASR-MLT-Nano-2512",
"anime-whisper": "litagin/anime-whisper",
}
FUNASR_MODEL_PROFILES = {
"sensevoice-small": {
"display_name": "SenseVoice Small",
"family": "sensevoice",
"legacy_engine": "sensevoice",
"modelscope_id": "iic/SenseVoiceSmall",
"huggingface_id": "FunAudioLLM/SenseVoiceSmall",
"estimated_bytes": 940_000_000,
"supports_padding": True,
"supports_language": True,
},
"funasr-nano-2512": {
"display_name": "Fun-ASR-Nano",
"family": "funasr-nano",
"legacy_engine": "funasr-nano",
"modelscope_id": "FunAudioLLM/Fun-ASR-Nano-2512",
"huggingface_id": "FunAudioLLM/Fun-ASR-Nano-2512",
"estimated_bytes": 1_050_000_000,
"supports_padding": False,
"supports_language": True,
},
"funasr-mlt-nano-2512": {
"display_name": "Fun-ASR-MLT-Nano",
"family": "funasr-nano",
"legacy_engine": "funasr-mlt-nano",
"modelscope_id": "FunAudioLLM/Fun-ASR-MLT-Nano-2512",
"huggingface_id": "FunAudioLLM/Fun-ASR-MLT-Nano-2512",
"estimated_bytes": 1_050_000_000,
"supports_padding": False,
"supports_language": True,
},
}
DEFAULT_FUNASR_MODEL = "sensevoice-small"
FUNASR_LEGACY_ENGINE_ALIASES = {
"sensevoice": "sensevoice-small",
"funasr-nano": "funasr-nano-2512",
"funasr-mlt-nano": "funasr-mlt-nano-2512",
}
# HuggingFace repo ids for engines whose namespace differs from ModelScope.
# SenseVoice lives under `iic/` on ModelScope but `FunAudioLLM/` on HuggingFace.
ASR_MODEL_IDS_HF = {
"sensevoice": "FunAudioLLM/SenseVoiceSmall",
}
def asr_model_id(
engine_type: str, hub: str = "ms", funasr_model: str | None = None
) -> str:
"""Return the repo id for an engine on the given hub ('ms' or 'hf')."""
if engine_type == "funasr":
return funasr_model_id(funasr_model, hub)
if engine_type in FUNASR_LEGACY_ENGINE_ALIASES:
return funasr_model_id(FUNASR_LEGACY_ENGINE_ALIASES[engine_type], hub)
if hub == "hf" and engine_type in ASR_MODEL_IDS_HF:
return ASR_MODEL_IDS_HF[engine_type]
return ASR_MODEL_IDS[engine_type]
ASR_DISPLAY_NAMES = {
"funasr": "FunASR",
"sensevoice": "SenseVoice Small",
"funasr-nano": "Fun-ASR-Nano",
"funasr-mlt-nano": "Fun-ASR-MLT-Nano",
"whisper": "Whisper",
"anime-whisper": "Anime-Whisper",
"remote-whisper": "Remote-Whisper",
}
_MODEL_SIZE_BYTES = {
"silero-vad": 2_000_000,
"sensevoice": 940_000_000,
"funasr-nano": 1_050_000_000,
"funasr-mlt-nano": 1_050_000_000,
"whisper-tiny": 78_000_000,
"whisper-base": 148_000_000,
"whisper-small": 488_000_000,
"whisper-medium": 1_530_000_000,
"whisper-large-v3": 3_100_000_000,
"anime-whisper": 3_100_000_000,
}
_WHISPER_SIZES = ["tiny", "base", "small", "medium", "large-v3"]
_CACHE_MODELS = [
("SenseVoice Small", "funasr", "sensevoice-small"),
("Fun-ASR-Nano", "funasr", "funasr-nano-2512"),
("Fun-ASR-MLT-Nano", "funasr", "funasr-mlt-nano-2512"),
("Anime-Whisper", "anime-whisper"),
]
def normalize_funasr_model_key(model_key: str | None) -> str:
if model_key in FUNASR_MODEL_PROFILES:
return model_key
if model_key in FUNASR_LEGACY_ENGINE_ALIASES:
return FUNASR_LEGACY_ENGINE_ALIASES[model_key]
return DEFAULT_FUNASR_MODEL
def normalize_asr_engine_selection(
engine_type: str | None, funasr_model: str | None = None
) -> tuple[str, str]:
if engine_type in FUNASR_LEGACY_ENGINE_ALIASES:
return "funasr", FUNASR_LEGACY_ENGINE_ALIASES[engine_type]
if engine_type == "funasr":
return "funasr", normalize_funasr_model_key(funasr_model)
return engine_type or "funasr", normalize_funasr_model_key(funasr_model)
def migrate_funasr_settings(settings: dict | None) -> dict | None:
if not settings:
return settings
engine, model_key = normalize_asr_engine_selection(
settings.get("asr_engine"), settings.get("funasr_model")
)
settings["asr_engine"] = engine
if engine == "funasr":
settings["funasr_model"] = model_key
else:
settings.setdefault("funasr_model", DEFAULT_FUNASR_MODEL)
return settings
def funasr_profile(model_key: str | None) -> dict:
return FUNASR_MODEL_PROFILES[normalize_funasr_model_key(model_key)]
def funasr_model_options() -> list[tuple[str, str]]:
return [
(key, profile["display_name"])
for key, profile in FUNASR_MODEL_PROFILES.items()
]
def funasr_display_name(model_key: str | None) -> str:
return funasr_profile(model_key)["display_name"]
def funasr_supports_padding(model_key: str | None) -> bool:
return bool(funasr_profile(model_key).get("supports_padding"))
def funasr_model_id(model_key: str | None, hub: str = "ms") -> str:
profile = funasr_profile(model_key)
return profile["huggingface_id"] if hub == "hf" else profile["modelscope_id"]
def _custom_whisper_path(value) -> Path | None:
if not value or value in _WHISPER_SIZES:
return None
path = Path(str(value)).expanduser()
if not path.is_absolute():
path = APP_DIR / path
return path
def is_faster_whisper_model_dir(path) -> bool:
"""True when path looks like a CTranslate2 faster-whisper model directory."""
if not path:
return False
path = Path(path)
return (
path.is_dir()
and (path / "model.bin").is_file()
and (path / "config.json").is_file()
)
def resolve_custom_whisper_model(value) -> str | None:
path = _custom_whisper_path(value)
if path and is_faster_whisper_model_dir(path):
return str(path.resolve())
return None
def _is_builtin_whisper_cache(path: Path) -> bool:
parts = set(path.parts)
return any(f"models--Systran--faster-whisper-{s}" in parts for s in _WHISPER_SIZES)
def _hf_snapshot_name(path: Path) -> str | None:
"""Return 'org/repo' for .../models--org--repo/snapshots/<hash>."""
if path.parent.name != "snapshots":
return None
repo_dir = path.parent.parent
if not repo_dir.name.startswith("models--"):
return None
encoded = repo_dir.name.removeprefix("models--")
parts = encoded.split("--", 1)
if len(parts) != 2 or not all(parts):
return None
return f"{parts[0]}/{parts[1]}"
def list_local_faster_whisper_models() -> list[dict]:
"""Scan ./models for user-provided faster-whisper model directories."""
if not MODELS_DIR.exists():
return []
entries = []
name_counts = {}
seen = set()
try:
model_bins = list(MODELS_DIR.rglob("model.bin"))
except (OSError, PermissionError):
return []
for model_bin in model_bins:
model_dir = model_bin.parent
if _is_builtin_whisper_cache(model_dir):
continue
if not is_faster_whisper_model_dir(model_dir):
continue
try:
resolved = str(model_dir.resolve())
except OSError:
continue
if resolved in seen:
continue
seen.add(resolved)
name = _hf_snapshot_name(model_dir) or model_dir.name
name_counts[name] = name_counts.get(name, 0) + 1
if name_counts[name] > 1:
name = f"{name} ({model_dir.name[:8]})"
entries.append({"name": name, "path": resolved})
entries.sort(key=lambda item: item["name"].lower())
return entries
def local_faster_whisper_display_name(path) -> str | None:
"""Return the same display name used by the local Whisper model selector."""
resolved = resolve_custom_whisper_model(path)
if not resolved:
return None
for item in list_local_faster_whisper_models():
if item["path"] == resolved:
return item["name"]
return _hf_snapshot_name(Path(resolved)) or Path(resolved).name
def apply_cache_env():
"""Point all model caches to ./models/."""
resolved = str(MODELS_DIR.resolve())
os.environ["MODELSCOPE_CACHE"] = os.path.join(resolved, "modelscope")
os.environ["HF_HOME"] = os.path.join(resolved, "huggingface")
os.environ["TORCH_HOME"] = os.path.join(resolved, "torch")
log.info(f"Cache env set: {resolved}")
def _has_silero_pkg() -> bool:
"""True when the silero-vad PyPI package (model bundled in wheel) is installed."""
import importlib.util
return importlib.util.find_spec("silero_vad") is not None
def is_silero_cached() -> bool:
if _has_silero_pkg():
return True
torch_hub = MODELS_DIR / "torch" / "hub"
return any(torch_hub.glob("snakers4_silero-vad*")) if torch_hub.exists() else False
def _ms_model_path(org, name):
"""Return the first existing ModelScope cache path, or the default."""
for sub in (
MODELS_DIR / "modelscope" / org / name,
MODELS_DIR / "modelscope" / "hub" / "models" / org / name,
):
if sub.exists():
return sub
return MODELS_DIR / "modelscope" / org / name
def _hf_repo_complete(org: str, name: str, min_bytes: int = 50_000_000) -> bool:
"""True if a HuggingFace repo cache exists AND finished downloading.
A killed/aborted download leaves snapshot entries pointing at missing blobs
(broken symlinks) or '.incomplete' blobs; treating that as cached makes the
model load hang. Validate a snapshot where every file resolves (stat follows
symlinks; a broken link raises) and the resolved bytes are substantial. This
ignores orphan '.incomplete' blobs left behind by an earlier interrupted run.
"""
snap_root = MODELS_DIR / "huggingface" / "hub" / f"models--{org}--{name}" / "snapshots"
if not snap_root.exists():
return False
for snap in snap_root.iterdir():
if not snap.is_dir():
continue
total = 0
broken = False
for f in snap.rglob("*"):
if f.is_dir():
continue
try:
total += f.stat().st_size
except OSError:
broken = True
break
if not broken and total >= min_bytes:
return True
return False
def is_asr_cached(engine_type, model_size="medium", hub="ms") -> bool:
if engine_type == "funasr" or engine_type in FUNASR_LEGACY_ENGINE_ALIASES:
model_key = (
FUNASR_LEGACY_ENGINE_ALIASES[engine_type]
if engine_type in FUNASR_LEGACY_ENGINE_ALIASES
else normalize_funasr_model_key(model_size)
)
# Accept cache from either hub to avoid redundant downloads; the repo
# namespace can differ between ModelScope and HuggingFace (SenseVoice).
ms_org, ms_name = funasr_model_id(model_key, "ms").split("/")
if _ms_model_path(ms_org, ms_name).exists():
return True
hf_org, hf_name = funasr_model_id(model_key, "hf").split("/")
if _hf_repo_complete(hf_org, hf_name):
return True
return False
if engine_type == "anime-whisper":
# HF-only (not published to ModelScope). Check that snapshots dir actually
# contains weight files; an .incomplete blob means a prior run aborted mid-download.
model_id = ASR_MODEL_IDS[engine_type]
org, name = model_id.split("/")
snap_root = (
MODELS_DIR / "huggingface" / "hub" / f"models--{org}--{name}" / "snapshots"
)
if not snap_root.exists():
return False
for snap in snap_root.iterdir():
if not snap.is_dir():
continue
has_weights = any(
(snap / fn).exists()
for fn in ("model.safetensors", "pytorch_model.bin")
)
has_config = (snap / "config.json").exists()
if has_weights and has_config:
return True
return False
elif engine_type == "whisper":
if model_size not in _WHISPER_SIZES:
return resolve_custom_whisper_model(model_size) is not None
min_bytes = int(
_MODEL_SIZE_BYTES.get(f"whisper-{model_size}", 50_000_000) * 0.5
)
return _hf_repo_complete(
"Systran", f"faster-whisper-{model_size}", min_bytes=min_bytes
)
return True
def get_missing_models(engine, model_size, hub) -> list:
missing = []
if not is_silero_cached():
missing.append(
{
"name": "Silero VAD",
"type": "silero-vad",
"estimated_bytes": _MODEL_SIZE_BYTES["silero-vad"],
}
)
if not is_asr_cached(engine, model_size, hub):
if engine == "whisper" and model_size not in _WHISPER_SIZES:
return missing
if engine == "funasr" or engine in FUNASR_LEGACY_ENGINE_ALIASES:
model_key = (
FUNASR_LEGACY_ENGINE_ALIASES[engine]
if engine in FUNASR_LEGACY_ENGINE_ALIASES
else normalize_funasr_model_key(model_size)
)
profile = funasr_profile(model_key)
key = f"funasr:{model_key}"
display = profile["display_name"]
estimated_bytes = profile["estimated_bytes"]
elif engine == "whisper":
key = engine if engine != "whisper" else f"whisper-{model_size}"
display = f"Whisper {model_size}"
estimated_bytes = _MODEL_SIZE_BYTES.get(key, 0)
else:
key = engine
display = ASR_DISPLAY_NAMES.get(engine, engine)
estimated_bytes = _MODEL_SIZE_BYTES.get(key, 0)
missing.append(
{
"name": display,
"type": key,
"estimated_bytes": estimated_bytes,
}
)
return missing
def get_local_model_path(engine_type, hub="ms", funasr_model: str | None = None):
"""Return local snapshot path if model is cached, else None.
Checks the preferred hub first, then falls back to the other hub.
"""
if engine_type == "funasr" or engine_type in FUNASR_LEGACY_ENGINE_ALIASES:
model_key = (
FUNASR_LEGACY_ENGINE_ALIASES[engine_type]
if engine_type in FUNASR_LEGACY_ENGINE_ALIASES
else normalize_funasr_model_key(funasr_model)
)
ms_org, ms_name = funasr_model_id(model_key, "ms").split("/")
hf_org, hf_name = funasr_model_id(model_key, "hf").split("/")
elif engine_type in ASR_MODEL_IDS:
ms_org, ms_name = asr_model_id(engine_type, "ms").split("/")
hf_org, hf_name = asr_model_id(engine_type, "hf").split("/")
else:
return None
def _try_ms():
local = _ms_model_path(ms_org, ms_name)
return str(local) if local.exists() else None
def _try_hf():
snap_dir = (
MODELS_DIR
/ "huggingface"
/ "hub"
/ f"models--{hf_org}--{hf_name}"
/ "snapshots"
)
if snap_dir.exists():
snaps = sorted(snap_dir.iterdir())
if snaps:
return str(snaps[-1])
return None
if hub == "ms":
return _try_ms() or _try_hf()
else:
return _try_hf() or _try_ms()
def download_silero(proxy: str = "system"):
if _has_silero_pkg():
log.info("Silero VAD bundled by silero-vad package, no download needed")
return
import torch
log.info("Downloading Silero VAD...")
with _proxy_env(proxy):
try:
model, _ = torch.hub.load(
repo_or_dir="snakers4/silero-vad:master",
model="silero_vad",
trust_repo=True,
)
except Exception as exc:
if "CERTIFICATE_VERIFY" not in str(exc):
raise
log.warning("SSL strict verification failed, retrying with relaxed flags")
model, _ = _load_silero_relaxed_ssl()
del model
log.info("Silero VAD downloaded")
def _load_silero_relaxed_ssl():
# Python 3.13 enables VERIFY_X509_STRICT by default, rejecting certificates
# without an Authority Key Identifier (common behind SSL-inspecting proxies).
import ssl
import torch
strict = getattr(ssl, "VERIFY_X509_STRICT", 0)
original = ssl._create_default_https_context
def relaxed_context(*args, **kwargs):
ctx = ssl.create_default_context(*args, **kwargs)
ctx.verify_flags &= ~strict
return ctx
ssl._create_default_https_context = relaxed_context
try:
return torch.hub.load(
repo_or_dir="snakers4/silero-vad:master",
model="silero_vad",
trust_repo=True,
force_reload=True,
)
finally:
ssl._create_default_https_context = original
def download_asr(engine, model_size="medium", hub="ms", proxy="system"):
resolved = str(MODELS_DIR.resolve())
ms_cache = os.path.join(resolved, "modelscope")
hf_cache = os.path.join(resolved, "huggingface", "hub")
with _proxy_env(proxy):
if engine == "funasr" or engine in FUNASR_LEGACY_ENGINE_ALIASES:
model_key = (
FUNASR_LEGACY_ENGINE_ALIASES[engine]
if engine in FUNASR_LEGACY_ENGINE_ALIASES
else normalize_funasr_model_key(model_size)
)
if hub == "ms":
from modelscope import snapshot_download
model_id = funasr_model_id(model_key, "ms")
log.info(f"Downloading {model_id} from ModelScope...")
snapshot_download(model_id=model_id, cache_dir=ms_cache)
else:
from huggingface_hub import snapshot_download
model_id = funasr_model_id(model_key, "hf")
log.info(f"Downloading {model_id} from HuggingFace...")
snapshot_download(repo_id=model_id, cache_dir=hf_cache)
neutralize_funasr_requirements(
get_local_model_path("funasr", hub=hub, funasr_model=model_key)
)
elif engine == "anime-whisper":
# HF-only, ignore hub setting
from huggingface_hub import snapshot_download
model_id = ASR_MODEL_IDS[engine]
log.info(f"Downloading {model_id} from HuggingFace...")
snapshot_download(repo_id=model_id, cache_dir=hf_cache)
elif engine == "whisper":
if model_size not in _WHISPER_SIZES:
raise ValueError(f"Invalid local faster-whisper model: {model_size}")
from huggingface_hub import snapshot_download
model_id = f"Systran/faster-whisper-{model_size}"
log.info(f"Downloading {model_id} from HuggingFace...")
snapshot_download(repo_id=model_id, cache_dir=hf_cache)
log.info(f"ASR model downloaded: {engine}")
def neutralize_funasr_requirements(model_dir) -> None:
"""Skip FunASR's load-time `pip install -r requirements.txt`.
With trust_remote_code=True, FunASR detects requirements.txt in the model
dir and runs pip in a subprocess whose output is swallowed (PIPE). On a slow
or proxy-blocked PyPI this hangs indefinitely with no log output, and it can
pull heavy unused deps (e.g. gradio). All real deps already live in the venv,
so rename the file out of the way to make the check miss.
"""
if not model_dir:
return
req = Path(model_dir) / "requirements.txt"
if req.exists():
try:
req.replace(req.with_name("requirements.txt.bundled"))
log.info(f"Skipped FunASR requirements install: {req}")
except OSError as exc:
log.warning(f"Failed to neutralize {req}: {exc}")
def dir_size(path) -> int:
total = 0
try:
for f in Path(path).rglob("*"):
if f.is_file():
total += f.stat().st_size
except (OSError, PermissionError):
pass
return total
def format_size(size_bytes: int) -> str:
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024**2:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024**3:
return f"{size_bytes / (1024**2):.1f} MB"
else:
return f"{size_bytes / (1024**3):.2f} GB"
def get_cache_entries():
"""Scan ./models/ for cached models."""
entries = []
hf_base = MODELS_DIR / "huggingface" / "hub"
torch_base = MODELS_DIR / "torch" / "hub"
for entry in _CACHE_MODELS:
if len(entry) == 3:
name, engine, model_key = entry
else:
name, engine = entry
model_key = None
ms_org, ms_model = asr_model_id(engine, "ms", model_key).split("/")
hf_org, hf_model = asr_model_id(engine, "hf", model_key).split("/")
ms_path = _ms_model_path(ms_org, ms_model)
hf_path = hf_base / f"models--{hf_org}--{hf_model}"
if ms_path.exists():
entries.append((f"{name} (ModelScope)", ms_path))
if hf_path.exists():
entries.append((f"{name} (HuggingFace)", hf_path))
for size in _WHISPER_SIZES:
hf_path = hf_base / f"models--Systran--faster-whisper-{size}"
if hf_path.exists() and is_asr_cached("whisper", size, "hf"):
entries.append((f"Whisper {size}", hf_path))
for item in list_local_faster_whisper_models():
entries.append((f"Whisper Local: {item['name']}", Path(item["path"])))
if torch_base.exists():
for d in sorted(torch_base.glob("snakers4_silero-vad*")):
if d.is_dir():
entries.append(("Silero VAD", d))
break
return entries