@simplexai/localmodels-darwin-x64 0.1.0-dev.59 → 0.1.0-dev.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bootstrap.identity +1 -1
- package/bootstrap.sha256 +126 -107
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +107 -44
- package/modules/localmodels/apple_sensors.py +431 -0
- package/modules/localmodels/auth_proxy.py +185 -19
- package/modules/localmodels/bifrost_admin.py +30 -0
- package/modules/localmodels/bifrost_checks.py +2 -2
- package/modules/localmodels/bifrost_config.py +132 -9
- package/modules/localmodels/bifrost_models.py +1 -1
- package/modules/localmodels/catalog.json +509 -54
- package/modules/localmodels/catalog.py +21 -4
- package/modules/localmodels/catalog_add.py +42 -11
- package/modules/localmodels/catalog_license.py +2 -0
- package/modules/localmodels/catalog_recipes.py +2 -0
- package/modules/localmodels/cli_parser.py +6 -2
- package/modules/localmodels/context_policy.py +40 -4
- package/modules/localmodels/custom_hub.py +983 -0
- package/modules/localmodels/custom_hub_http.py +579 -0
- package/modules/localmodels/custom_probe.py +451 -0
- package/modules/localmodels/debug_mode.py +13 -0
- package/modules/localmodels/debug_stub_server.py +156 -1
- package/modules/localmodels/deploy.py +38 -7
- package/modules/localmodels/deploy_steps.py +5 -0
- package/modules/localmodels/download-mirrors.json +20 -0
- package/modules/localmodels/engines-b12757-mix-ef2df21.json +26 -0
- package/modules/localmodels/engines-prism-b12178-03ec600.json +131 -0
- package/modules/localmodels/engines-strata-poly-0.2.20-poly.6.json +94 -0
- package/modules/localmodels/engines-whisper-b5454.json +120 -1
- package/modules/localmodels/engines.py +38 -8
- package/modules/localmodels/estimator.py +60 -7
- package/modules/localmodels/gateway.py +48 -3
- package/modules/localmodels/gateway_app_bifrost.py +18 -18
- package/modules/localmodels/gateway_bifrost.py +102 -16
- package/modules/localmodels/gateway_cli.py +14 -80
- package/modules/localmodels/gateway_server.py +88 -16
- package/modules/localmodels/gguf.py +6 -0
- package/modules/localmodels/gpu_specs.py +35 -1
- package/modules/localmodels/harness.py +1 -2
- package/modules/localmodels/hf_repo.py +12 -4
- package/modules/localmodels/host_sensors.py +704 -0
- package/modules/localmodels/install.py +4 -4
- package/modules/localmodels/library_cli.py +181 -3
- package/modules/localmodels/library_custom.py +831 -0
- package/modules/localmodels/library_elastic.py +35 -7
- package/modules/localmodels/library_gateway.py +94 -32
- package/modules/localmodels/library_jobs.py +76 -11
- package/modules/localmodels/library_kept.py +50 -6
- package/modules/localmodels/library_model.py +254 -2
- package/modules/localmodels/library_pricing.py +83 -4
- package/modules/localmodels/library_serve.py +178 -60
- package/modules/localmodels/library_smoke.py +315 -0
- package/modules/localmodels/library_state.py +77 -1
- package/modules/localmodels/library_status.py +126 -4
- package/modules/localmodels/npm_transport.py +4 -58
- package/modules/localmodels/nvml_ctypes.py +431 -0
- package/modules/localmodels/plan_envelope.py +14 -6
- package/modules/localmodels/recommend_cli.py +2 -1
- package/modules/localmodels/records.py +1 -0
- package/modules/localmodels/remote_ops.py +120 -665
- package/modules/localmodels/remote_ops_common.py +729 -0
- package/modules/localmodels/remote_ops_download.py +396 -241
- package/modules/localmodels/remote_ops_mirrors.py +26 -65
- package/modules/localmodels/remote_ops_prepare.py +43 -35
- package/modules/localmodels/remote_ops_ranges.py +98 -65
- package/modules/localmodels/remote_ops_remove.py +30 -22
- package/modules/localmodels/remote_ops_service.py +103 -95
- package/modules/localmodels/remote_ops_verify.py +113 -65
- package/modules/localmodels/runtime-lock.json +19 -19
- package/modules/localmodels/runtime_cli.py +15 -5
- package/modules/localmodels/runtime_io.py +54 -1
- package/modules/localmodels/service_control.py +32 -1
- package/modules/localmodels/service_wiring.py +2 -3
- package/modules/localmodels/service_worker.py +6 -2
- package/modules/localmodels/services_cli.py +35 -27
- package/modules/localmodels/source_byok.py +3 -5
- package/modules/localmodels/speech-sv.txt +93 -0
- package/modules/localmodels/speech_admission.py +437 -0
- package/modules/localmodels/speech_catalog.py +170 -0
- package/modules/localmodels/speech_cli.py +273 -0
- package/modules/localmodels/speech_jobs.py +772 -0
- package/modules/localmodels/speech_model.py +404 -0
- package/modules/localmodels/speech_serve.py +287 -0
- package/modules/localmodels/speech_server.py +837 -0
- package/modules/localmodels/speech_state.py +697 -0
- package/modules/localmodels/speech_worker_sensevoice.py +255 -0
- package/modules/localmodels/speech_workers.py +783 -0
- package/modules/localmodels/stack.py +0 -33
- package/modules/localmodels/stack_auto.py +5 -3
- package/modules/localmodels/stack_aux_gpu.py +45 -4
- package/modules/localmodels/stack_capacity.py +77 -8
- package/modules/localmodels/stack_cli.py +16 -1
- package/modules/localmodels/stack_compose.py +1 -1
- package/modules/localmodels/stack_down.py +24 -1
- package/modules/localmodels/stack_hardware.py +57 -88
- package/modules/localmodels/stack_hardware_text.py +4 -59
- package/modules/localmodels/stack_lifecycle.py +2 -2
- package/modules/localmodels/stack_memory.py +14 -3
- package/modules/localmodels/stack_suite.py +9 -10
- package/modules/localmodels/stack_summary.py +4 -4
- package/modules/localmodels/stack_view.py +77 -53
- package/modules/localmodels/stack_view_demo.py +36 -5
- package/modules/localmodels/stack_view_events.py +147 -12
- package/modules/localmodels/stack_view_gpu.py +97 -17
- package/modules/localmodels/stack_view_llm.py +16 -1
- package/modules/localmodels/stack_view_measure.py +4 -1
- package/modules/localmodels/stack_view_schema.py +54 -8
- package/modules/localmodels/stack_view_serve.py +40 -3
- package/modules/localmodels/stack_view_slots.py +89 -10
- package/modules/localmodels/stack_view_telemetry.py +163 -0
- package/modules/localmodels/stack_view_text.py +5 -2
- package/modules/localmodels/stack_view_tick.py +658 -0
- package/modules/localmodels/stack_view_words.py +4 -0
- package/modules/localmodels/{strata-poly-serve-0.2.20-poly.5.txt → strata-poly-serve-0.2.20-poly.6.txt} +1 -1
- package/modules/localmodels/suite.py +3 -1
- package/modules/localmodels/telemetry_math.py +538 -0
- package/modules/localmodels/transport.py +3 -2
- package/modules/localmodels/tui_views.py +2 -2
- package/modules/localmodels/web/view/view.js +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/RECORD +123 -104
- package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/licenses/NOTICE +9 -0
- package/package.json +1 -1
- package/tool.json +1 -1
- package/modules/localmodels/bifrost_datasheet.py +0 -53
- package/modules/localmodels/bifrost_provider.py +0 -88
- package/modules/localmodels/bifrost_unit.py +0 -99
- package/modules/localmodels/engines-strata-poly-0.2.20-poly.5.json +0 -94
- package/modules/localmodels/gateway_switch.py +0 -51
- package/modules/localmodels/library_floor.py +0 -78
- package/modules/localmodels/service_stop.py +0 -52
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev61.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
"""Orchestration for read-only ``localmodels library custom search`` and ``custom probe``.
|
|
2
|
+
|
|
3
|
+
Zero state mutation: reads host facts, disk space, and installed library records read-only,
|
|
4
|
+
delegates hub I/O to ``custom_hub``, prices GGUF headers via ``estimator`` when Range reads
|
|
5
|
+
succeed, and falls back to the transparent ``1.2x + 0.8 GB`` estimate when headers are unavailable.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import platform as py_platform
|
|
11
|
+
import sys
|
|
12
|
+
from collections.abc import Mapping, Sequence
|
|
13
|
+
from dataclasses import replace
|
|
14
|
+
from typing import cast
|
|
15
|
+
|
|
16
|
+
from localmodels import (
|
|
17
|
+
budget,
|
|
18
|
+
custom_hub,
|
|
19
|
+
estimator,
|
|
20
|
+
library_elastic,
|
|
21
|
+
library_jobs,
|
|
22
|
+
library_pricing,
|
|
23
|
+
library_state,
|
|
24
|
+
library_status,
|
|
25
|
+
)
|
|
26
|
+
from localmodels.gguf import GGUFHeader
|
|
27
|
+
from localmodels.library_model import (
|
|
28
|
+
CUSTOM_PROBE_SCHEMA,
|
|
29
|
+
CUSTOM_SEARCH_SCHEMA,
|
|
30
|
+
CustomFormatFilter,
|
|
31
|
+
CustomGgufMeta,
|
|
32
|
+
CustomMmprojCandidate,
|
|
33
|
+
CustomProbeDoc,
|
|
34
|
+
CustomProbeHost,
|
|
35
|
+
CustomProbeReadyDoc,
|
|
36
|
+
CustomQuantVariant,
|
|
37
|
+
CustomSearchDoc,
|
|
38
|
+
CustomSearchHub,
|
|
39
|
+
CustomVariantFile,
|
|
40
|
+
LibraryError,
|
|
41
|
+
MemoryEstimateBasis,
|
|
42
|
+
MemoryStatus,
|
|
43
|
+
)
|
|
44
|
+
from localmodels.probe import HostFacts
|
|
45
|
+
|
|
46
|
+
SWEET_SPOT_QUANTS: tuple[str, ...] = (
|
|
47
|
+
"Q4_K_M",
|
|
48
|
+
"UD-Q4_K_XL",
|
|
49
|
+
"UD-Q4_K_M",
|
|
50
|
+
"IQ4_XS",
|
|
51
|
+
"Q4_K_S",
|
|
52
|
+
"Q5_K_M",
|
|
53
|
+
"Q4_0",
|
|
54
|
+
"Q3_K_M",
|
|
55
|
+
"Q6_K",
|
|
56
|
+
"Q8_0",
|
|
57
|
+
)
|
|
58
|
+
ROUGH_OVERHEAD_BYTES = 800_000_000
|
|
59
|
+
ROUGH_WEIGHT_MULTIPLIER = 1.2
|
|
60
|
+
DISK_HEADROOM_MULTIPLIER = 1.1
|
|
61
|
+
PROBE_UBATCH = 512
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _host_platform_arch(facts: HostFacts | None) -> tuple[str, str]:
|
|
65
|
+
if facts is not None:
|
|
66
|
+
plat = library_status.platform_name(facts)
|
|
67
|
+
raw_arch = str(facts.get("arch") or "")
|
|
68
|
+
return plat, library_status.ARCH.get(raw_arch, raw_arch or "x86_64")
|
|
69
|
+
raw_plat = sys.platform
|
|
70
|
+
plat = (
|
|
71
|
+
"linux" if raw_plat.startswith("linux") else "windows" if raw_plat == "win32" else raw_plat
|
|
72
|
+
)
|
|
73
|
+
machine = py_platform.machine().lower()
|
|
74
|
+
return plat, library_status.ARCH.get(machine, machine or "x86_64")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def rough_memory_bytes(weights_bytes: int, mmproj_bytes: int) -> int:
|
|
78
|
+
"""Transparent fallback memory estimate when GGUF header metadata is unavailable."""
|
|
79
|
+
return int((weights_bytes + mmproj_bytes) * ROUGH_WEIGHT_MULTIPLIER) + ROUGH_OVERHEAD_BYTES
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def physics_memory_bytes(header: GGUFHeader, weights_bytes: int, mmproj_bytes: int) -> int:
|
|
83
|
+
"""Physics-priced memory estimate from a GGUF header at the elastic floor posture."""
|
|
84
|
+
profile = estimator.eager(estimator.profile_from_gguf(header))
|
|
85
|
+
variant_profile = replace(profile, weights_bytes=weights_bytes)
|
|
86
|
+
n_ctx = variant_profile.n_ctx_train
|
|
87
|
+
window = (
|
|
88
|
+
min(library_elastic.ELASTIC_FLOOR_WINDOW, n_ctx)
|
|
89
|
+
if n_ctx > 0
|
|
90
|
+
else library_elastic.ELASTIC_FLOOR_WINDOW
|
|
91
|
+
)
|
|
92
|
+
core = estimator.agent_footprint_bytes(
|
|
93
|
+
variant_profile,
|
|
94
|
+
window,
|
|
95
|
+
slots=1,
|
|
96
|
+
kv_types=("q8_0", "q8_0"),
|
|
97
|
+
ubatch=PROBE_UBATCH,
|
|
98
|
+
mtp=False,
|
|
99
|
+
)
|
|
100
|
+
return max(core + mmproj_bytes, weights_bytes + mmproj_bytes)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _memory_status(
|
|
104
|
+
estimated_bytes: int, soft_budget_bytes: int, hard_ceiling_bytes: int
|
|
105
|
+
) -> MemoryStatus:
|
|
106
|
+
if estimated_bytes <= soft_budget_bytes:
|
|
107
|
+
return "comfortable"
|
|
108
|
+
if estimated_bytes <= hard_ceiling_bytes:
|
|
109
|
+
return "tight"
|
|
110
|
+
return "exceeds"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _select_target_pool(facts: HostFacts) -> tuple[str, int, int, int]:
|
|
114
|
+
"""Return ``(pool_id, total_bytes, soft_budget_bytes, hard_ceiling_bytes)``."""
|
|
115
|
+
pool_map = library_pricing.pools(facts)
|
|
116
|
+
preferred = [p for p in pool_map if p != "system"]
|
|
117
|
+
if preferred:
|
|
118
|
+
target = max(
|
|
119
|
+
preferred,
|
|
120
|
+
key=lambda p: (
|
|
121
|
+
library_pricing.pool_free(facts, p),
|
|
122
|
+
library_pricing.pool_total(facts, p),
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
else:
|
|
126
|
+
target = "system"
|
|
127
|
+
total = library_pricing.pool_total(facts, target)
|
|
128
|
+
adm = budget.admission_budget(facts, target)
|
|
129
|
+
if adm is None:
|
|
130
|
+
return target, total, 0, 0
|
|
131
|
+
return target, total, adm.soft_budget_bytes, adm.hard_ceiling_bytes
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _select_sweet_spot(
|
|
135
|
+
variants: Sequence[custom_hub.GgufVariantGroup],
|
|
136
|
+
estimates: Mapping[str, int],
|
|
137
|
+
ceiling_bytes: int,
|
|
138
|
+
) -> str:
|
|
139
|
+
"""Pick the highest-priority quant fitting ``ceiling_bytes``, else largest fitting / smallest."""
|
|
140
|
+
by_quant = {v.quant.upper(): v for v in variants}
|
|
141
|
+
for wanted in SWEET_SPOT_QUANTS:
|
|
142
|
+
cand = by_quant.get(wanted.upper())
|
|
143
|
+
if cand is not None and estimates[cand.variant_id] <= ceiling_bytes:
|
|
144
|
+
return cand.variant_id
|
|
145
|
+
fitting = [v for v in variants if estimates[v.variant_id] <= ceiling_bytes]
|
|
146
|
+
if fitting:
|
|
147
|
+
return max(fitting, key=lambda v: (v.weights_bytes, v.variant_id.lower())).variant_id
|
|
148
|
+
return min(variants, key=lambda v: (v.weights_bytes, v.variant_id.lower())).variant_id
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _match_preselected(
|
|
152
|
+
variants: Sequence[custom_hub.GgufVariantGroup],
|
|
153
|
+
spec: custom_hub.ParsedRepoSpec,
|
|
154
|
+
recommended_id: str,
|
|
155
|
+
) -> str:
|
|
156
|
+
if spec.file_name:
|
|
157
|
+
target = spec.file_name.strip().lstrip("/").lower()
|
|
158
|
+
for v in variants:
|
|
159
|
+
if any(f.path.lower() == target for f in v.files):
|
|
160
|
+
return v.variant_id
|
|
161
|
+
target_base = target.rsplit("/", 1)[-1]
|
|
162
|
+
for v in variants:
|
|
163
|
+
if any(f.path.lower().rsplit("/", 1)[-1] == target_base for f in v.files):
|
|
164
|
+
return v.variant_id
|
|
165
|
+
if spec.quant_hint:
|
|
166
|
+
hint = spec.quant_hint.strip().lower()
|
|
167
|
+
for v in variants:
|
|
168
|
+
if hint in (v.variant_id.lower(), v.quant.lower(), v.stem.lower()):
|
|
169
|
+
return v.variant_id
|
|
170
|
+
return recommended_id
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _gguf_meta_dict(header: GGUFHeader) -> CustomGgufMeta:
|
|
174
|
+
return {
|
|
175
|
+
"architecture": header.architecture or "unknown",
|
|
176
|
+
"n_ctx_train": header.n_ctx_train,
|
|
177
|
+
"n_layer": header.n_layer,
|
|
178
|
+
"moe": header.expert_count > 0,
|
|
179
|
+
"has_chat_template": "tokenizer.chat_template" in header.metadata,
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _optional_str(payload: Mapping[str, object], key: str) -> str | None:
|
|
184
|
+
raw = payload.get(key)
|
|
185
|
+
if raw is None:
|
|
186
|
+
return None
|
|
187
|
+
if not isinstance(raw, str):
|
|
188
|
+
raise LibraryError("INVALID_REPO_SPEC", f"{key} must be a string")
|
|
189
|
+
return raw.strip() or None
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def search(
|
|
193
|
+
payload: Mapping[str, object],
|
|
194
|
+
base_dir: str | None = None,
|
|
195
|
+
*,
|
|
196
|
+
facts: HostFacts | None = None,
|
|
197
|
+
) -> CustomSearchDoc:
|
|
198
|
+
"""Execute read-only ``library custom search`` from a stdin JSON payload."""
|
|
199
|
+
_ = base_dir
|
|
200
|
+
raw_hub = str(payload.get("hub") or "all").strip().lower()
|
|
201
|
+
if raw_hub not in ("all", "huggingface", "modelscope"):
|
|
202
|
+
raise LibraryError("INVALID_REPO_SPEC", f"invalid hub {raw_hub!r}")
|
|
203
|
+
raw_query = payload.get("query", "")
|
|
204
|
+
if not isinstance(raw_query, str):
|
|
205
|
+
raise LibraryError("INVALID_REPO_SPEC", "query must be a string")
|
|
206
|
+
raw_format = str(payload.get("format") or "compatible").strip().lower()
|
|
207
|
+
if raw_format not in ("compatible", "gguf", "mlx", "all"):
|
|
208
|
+
raise LibraryError("INVALID_REPO_SPEC", f"invalid format {raw_format!r}")
|
|
209
|
+
raw_limit = payload.get("limit", 20)
|
|
210
|
+
if isinstance(raw_limit, bool) or not isinstance(raw_limit, int) or raw_limit < 1:
|
|
211
|
+
raise LibraryError("INVALID_REPO_SPEC", "limit must be a positive integer")
|
|
212
|
+
limit = min(raw_limit, 50)
|
|
213
|
+
token = _optional_str(payload, "token")
|
|
214
|
+
|
|
215
|
+
plat, arch = _host_platform_arch(facts)
|
|
216
|
+
supported = list(custom_hub.SUPPORTED_FORMATS)
|
|
217
|
+
hub_val = cast(CustomSearchHub, raw_hub)
|
|
218
|
+
fmt_val = cast(CustomFormatFilter, raw_format)
|
|
219
|
+
results, warnings = custom_hub.search_hubs(
|
|
220
|
+
hub=hub_val,
|
|
221
|
+
query=raw_query,
|
|
222
|
+
format_filter=fmt_val,
|
|
223
|
+
limit=limit,
|
|
224
|
+
token=token,
|
|
225
|
+
supported_formats=supported,
|
|
226
|
+
)
|
|
227
|
+
return {
|
|
228
|
+
"schema": CUSTOM_SEARCH_SCHEMA,
|
|
229
|
+
"ok": True,
|
|
230
|
+
"query": raw_query,
|
|
231
|
+
"hub": hub_val,
|
|
232
|
+
"format": fmt_val,
|
|
233
|
+
"platform": plat,
|
|
234
|
+
"arch": arch,
|
|
235
|
+
"supported_formats": supported,
|
|
236
|
+
"results": results,
|
|
237
|
+
"warnings": warnings,
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _build_variants(
|
|
242
|
+
fetched: custom_hub.FetchedRepo,
|
|
243
|
+
variants: Sequence[custom_hub.GgufVariantGroup],
|
|
244
|
+
*,
|
|
245
|
+
estimates: Mapping[str, int],
|
|
246
|
+
basis: MemoryEstimateBasis,
|
|
247
|
+
disk_free_bytes: int,
|
|
248
|
+
soft_budget_bytes: int,
|
|
249
|
+
hard_ceiling_bytes: int,
|
|
250
|
+
installed_ids: set[str],
|
|
251
|
+
) -> list[CustomQuantVariant]:
|
|
252
|
+
rows: list[CustomQuantVariant] = []
|
|
253
|
+
for v in variants:
|
|
254
|
+
custom_id = custom_hub.make_custom_model_id(fetched.hub, fetched.repo_id, v.variant_id)
|
|
255
|
+
total_dl = v.weights_bytes + v.mmproj_bytes
|
|
256
|
+
disk_req = int(total_dl * DISK_HEADROOM_MULTIPLIER)
|
|
257
|
+
est_mem = estimates[v.variant_id]
|
|
258
|
+
files: list[CustomVariantFile] = [
|
|
259
|
+
{
|
|
260
|
+
"path": f.path,
|
|
261
|
+
"size_bytes": f.size_bytes,
|
|
262
|
+
"sha256": f.sha256,
|
|
263
|
+
"revision": f.revision,
|
|
264
|
+
}
|
|
265
|
+
for f in v.files
|
|
266
|
+
]
|
|
267
|
+
rows.append(
|
|
268
|
+
{
|
|
269
|
+
"id": v.variant_id,
|
|
270
|
+
"custom_model_id": custom_id,
|
|
271
|
+
"label": v.label,
|
|
272
|
+
"quant": v.quant,
|
|
273
|
+
"bits_per_weight": v.bits_per_weight,
|
|
274
|
+
"sharded": len(v.files) > 1,
|
|
275
|
+
"shard_count": len(v.files),
|
|
276
|
+
"files": files,
|
|
277
|
+
"weights_bytes": v.weights_bytes,
|
|
278
|
+
"mmproj_file": v.mmproj_file,
|
|
279
|
+
"mmproj_bytes": v.mmproj_bytes,
|
|
280
|
+
"total_download_bytes": total_dl,
|
|
281
|
+
"disk_required_bytes": disk_req,
|
|
282
|
+
"disk_ok": disk_free_bytes >= disk_req,
|
|
283
|
+
"estimated_memory_bytes": est_mem,
|
|
284
|
+
"memory_estimate_basis": basis,
|
|
285
|
+
"memory_status": _memory_status(est_mem, soft_budget_bytes, hard_ceiling_bytes),
|
|
286
|
+
"already_installed": custom_id in installed_ids,
|
|
287
|
+
}
|
|
288
|
+
)
|
|
289
|
+
return rows
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def probe(
|
|
293
|
+
payload: Mapping[str, object],
|
|
294
|
+
base_dir: str,
|
|
295
|
+
*,
|
|
296
|
+
facts: HostFacts | None = None,
|
|
297
|
+
) -> CustomProbeDoc:
|
|
298
|
+
"""Execute read-only ``library custom probe`` from a stdin JSON payload."""
|
|
299
|
+
target = _optional_str(payload, "target")
|
|
300
|
+
if not target:
|
|
301
|
+
raise LibraryError("INVALID_REPO_SPEC", "target repository is required")
|
|
302
|
+
raw_hub = str(payload.get("hub") or "auto").strip().lower()
|
|
303
|
+
revision = _optional_str(payload, "revision")
|
|
304
|
+
file_name = _optional_str(payload, "file_name")
|
|
305
|
+
token = _optional_str(payload, "token")
|
|
306
|
+
spec = custom_hub.parse_repo_spec(target, hub=raw_hub, revision=revision, file_name=file_name)
|
|
307
|
+
|
|
308
|
+
outcome = custom_hub.fetch_repo(spec, token=token)
|
|
309
|
+
if isinstance(outcome, custom_hub.HubGated):
|
|
310
|
+
return {
|
|
311
|
+
"schema": CUSTOM_PROBE_SCHEMA,
|
|
312
|
+
"ok": True,
|
|
313
|
+
"outcome": "gated",
|
|
314
|
+
"hub_http_status": outcome.http_status,
|
|
315
|
+
"token_provided": outcome.token_provided,
|
|
316
|
+
"repo": {"hub": outcome.hub, "repo_id": outcome.repo_id, "url": outcome.url},
|
|
317
|
+
}
|
|
318
|
+
if isinstance(outcome, custom_hub.HubNotFound):
|
|
319
|
+
return {
|
|
320
|
+
"schema": CUSTOM_PROBE_SCHEMA,
|
|
321
|
+
"ok": True,
|
|
322
|
+
"outcome": "not_found",
|
|
323
|
+
"hub": outcome.hub,
|
|
324
|
+
"repo_id": outcome.repo_id,
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
inspected = custom_hub.inspect_tree(outcome)
|
|
328
|
+
if not inspected.compatible or not inspected.variants:
|
|
329
|
+
return {
|
|
330
|
+
"schema": CUSTOM_PROBE_SCHEMA,
|
|
331
|
+
"ok": True,
|
|
332
|
+
"outcome": "incompatible",
|
|
333
|
+
"reason": inspected.reason or "no_runnable_files",
|
|
334
|
+
"suggested_search_query": inspected.suggested_search_query
|
|
335
|
+
or custom_hub.build_suggested_gguf_query(outcome.repo_id),
|
|
336
|
+
"repo": {
|
|
337
|
+
"hub": outcome.hub,
|
|
338
|
+
"repo_id": outcome.repo_id,
|
|
339
|
+
"url": outcome.url,
|
|
340
|
+
"format": inspected.format,
|
|
341
|
+
},
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
host_facts = facts if facts is not None else library_jobs.probe()
|
|
345
|
+
plat, arch = _host_platform_arch(host_facts)
|
|
346
|
+
disk_free = library_pricing.base_free(base_dir)
|
|
347
|
+
pool_id, pool_total, pool_soft, pool_hard = _select_target_pool(host_facts)
|
|
348
|
+
fit_ceiling = pool_hard if pool_hard > 0 else pool_total
|
|
349
|
+
|
|
350
|
+
rough_map = {
|
|
351
|
+
v.variant_id: rough_memory_bytes(v.weights_bytes, v.mmproj_bytes)
|
|
352
|
+
for v in inspected.variants
|
|
353
|
+
}
|
|
354
|
+
sweet_id = _select_sweet_spot(inspected.variants, rough_map, fit_ceiling)
|
|
355
|
+
sweet_variant = next(v for v in inspected.variants if v.variant_id == sweet_id)
|
|
356
|
+
|
|
357
|
+
resolved_token = custom_hub.resolve_token(outcome.hub, token, hub_explicit=spec.hub is not None)
|
|
358
|
+
range_url = sweet_variant.files[0].download_url
|
|
359
|
+
range_token = custom_hub.token_for_endpoint(outcome.hub, range_url, resolved_token)
|
|
360
|
+
header, note = custom_hub.read_remote_gguf_header(range_url, token=range_token)
|
|
361
|
+
if header is not None:
|
|
362
|
+
try:
|
|
363
|
+
parsed_meta = _gguf_meta_dict(header)
|
|
364
|
+
parsed_estimates = {
|
|
365
|
+
v.variant_id: physics_memory_bytes(header, v.weights_bytes, v.mmproj_bytes)
|
|
366
|
+
for v in inspected.variants
|
|
367
|
+
}
|
|
368
|
+
except (ValueError, KeyError, TypeError, OverflowError) as exc:
|
|
369
|
+
metadata_complete = False
|
|
370
|
+
metadata_note: str | None = custom_hub.redact_secrets(
|
|
371
|
+
f"Malformed GGUF metadata ({type(exc).__name__}); using rough memory estimate",
|
|
372
|
+
extra_token=resolved_token,
|
|
373
|
+
)
|
|
374
|
+
gguf_meta: CustomGgufMeta | None = None
|
|
375
|
+
basis: MemoryEstimateBasis = "rough_1.2x_plus_0.8gb"
|
|
376
|
+
estimates = rough_map
|
|
377
|
+
recommended_id = sweet_id
|
|
378
|
+
else:
|
|
379
|
+
metadata_complete = True
|
|
380
|
+
metadata_note = None
|
|
381
|
+
gguf_meta = parsed_meta
|
|
382
|
+
basis = "gguf_header"
|
|
383
|
+
estimates = parsed_estimates
|
|
384
|
+
recommended_id = _select_sweet_spot(inspected.variants, estimates, fit_ceiling)
|
|
385
|
+
else:
|
|
386
|
+
metadata_complete = False
|
|
387
|
+
metadata_note = note
|
|
388
|
+
gguf_meta = None
|
|
389
|
+
basis = "rough_1.2x_plus_0.8gb"
|
|
390
|
+
estimates = rough_map
|
|
391
|
+
recommended_id = sweet_id
|
|
392
|
+
|
|
393
|
+
selected_id = _match_preselected(inspected.variants, spec, recommended_id)
|
|
394
|
+
installed_ids = {r["id"] for r in library_state.records()}
|
|
395
|
+
mmproj_rows: list[CustomMmprojCandidate] = [
|
|
396
|
+
{
|
|
397
|
+
"path": c.path,
|
|
398
|
+
"size_bytes": c.size_bytes,
|
|
399
|
+
"sha256": c.sha256,
|
|
400
|
+
"recommended": c.recommended,
|
|
401
|
+
}
|
|
402
|
+
for c in inspected.mmproj_candidates
|
|
403
|
+
]
|
|
404
|
+
host_doc: CustomProbeHost = {
|
|
405
|
+
"platform": plat,
|
|
406
|
+
"arch": arch,
|
|
407
|
+
"supported_formats": list(custom_hub.SUPPORTED_FORMATS),
|
|
408
|
+
"disk_free_bytes": disk_free,
|
|
409
|
+
"target_pool": pool_id,
|
|
410
|
+
"pool_total_bytes": pool_total,
|
|
411
|
+
"pool_soft_budget_bytes": pool_soft,
|
|
412
|
+
"pool_hard_ceiling_bytes": pool_hard,
|
|
413
|
+
}
|
|
414
|
+
family = custom_hub.infer_family(
|
|
415
|
+
outcome.repo_id, outcome.tags, gguf_meta["architecture"] if gguf_meta else None
|
|
416
|
+
)
|
|
417
|
+
ready: CustomProbeReadyDoc = {
|
|
418
|
+
"schema": CUSTOM_PROBE_SCHEMA,
|
|
419
|
+
"ok": True,
|
|
420
|
+
"outcome": "ready",
|
|
421
|
+
"repo": {
|
|
422
|
+
"hub": outcome.hub,
|
|
423
|
+
"repo_id": outcome.repo_id,
|
|
424
|
+
"revision": outcome.revision,
|
|
425
|
+
"url": outcome.url,
|
|
426
|
+
"title": outcome.title,
|
|
427
|
+
"owner": outcome.owner,
|
|
428
|
+
"format": "gguf",
|
|
429
|
+
"family": family,
|
|
430
|
+
"license": outcome.license,
|
|
431
|
+
"gated": outcome.gated,
|
|
432
|
+
},
|
|
433
|
+
"metadata_complete": metadata_complete,
|
|
434
|
+
"metadata_note": metadata_note,
|
|
435
|
+
"gguf_meta": gguf_meta,
|
|
436
|
+
"host": host_doc,
|
|
437
|
+
"mmproj_candidates": mmproj_rows,
|
|
438
|
+
"recommended_variant_id": recommended_id,
|
|
439
|
+
"selected_quant_id": selected_id,
|
|
440
|
+
"quant_variants": _build_variants(
|
|
441
|
+
outcome,
|
|
442
|
+
inspected.variants,
|
|
443
|
+
estimates=estimates,
|
|
444
|
+
basis=basis,
|
|
445
|
+
disk_free_bytes=disk_free,
|
|
446
|
+
soft_budget_bytes=pool_soft,
|
|
447
|
+
hard_ceiling_bytes=pool_hard,
|
|
448
|
+
installed_ids=installed_ids,
|
|
449
|
+
),
|
|
450
|
+
}
|
|
451
|
+
return ready
|
|
@@ -325,6 +325,12 @@ def _apply_simulated_occupancy(facts: dict[str, Any]) -> dict[str, Any]:
|
|
|
325
325
|
return adjusted
|
|
326
326
|
|
|
327
327
|
|
|
328
|
+
def simulated_facts(config: DebugConfig | None = None) -> dict[str, Any]:
|
|
329
|
+
"""Synthetic local host facts reflecting simulated VRAM sizing and loaded LLM occupancy."""
|
|
330
|
+
cfg = current() if config is None else config
|
|
331
|
+
return _apply_simulated_occupancy(_synthetic_facts(None, cfg.vram_gb or 24))
|
|
332
|
+
|
|
333
|
+
|
|
328
334
|
def _sim_dir() -> Path:
|
|
329
335
|
path = hosts.state_dir() / "simulate"
|
|
330
336
|
path.mkdir(parents=True, exist_ok=True)
|
|
@@ -373,6 +379,9 @@ class SimulatedExecutor:
|
|
|
373
379
|
binary.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")
|
|
374
380
|
with contextlib.suppress(OSError):
|
|
375
381
|
binary.chmod(0o755)
|
|
382
|
+
complete = directory / ".complete"
|
|
383
|
+
if not complete.exists():
|
|
384
|
+
complete.write_text("simulated\n", encoding="utf-8")
|
|
376
385
|
return {"event": "result", "binary": str(binary), "reused": True}
|
|
377
386
|
|
|
378
387
|
def _download_one(
|
|
@@ -397,6 +406,8 @@ class SimulatedExecutor:
|
|
|
397
406
|
"done": size,
|
|
398
407
|
"total": size,
|
|
399
408
|
"bytes_per_s": max(1, size),
|
|
409
|
+
"source": "cache",
|
|
410
|
+
"connections": 0,
|
|
400
411
|
}
|
|
401
412
|
)
|
|
402
413
|
return
|
|
@@ -416,6 +427,8 @@ class SimulatedExecutor:
|
|
|
416
427
|
"done": done,
|
|
417
428
|
"total": size,
|
|
418
429
|
"bytes_per_s": rate,
|
|
430
|
+
"source": "cache",
|
|
431
|
+
"connections": 0,
|
|
419
432
|
}
|
|
420
433
|
)
|
|
421
434
|
part.unlink(missing_ok=True)
|
|
@@ -4,12 +4,156 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import argparse
|
|
6
6
|
import json
|
|
7
|
+
import math
|
|
8
|
+
import re
|
|
7
9
|
import signal
|
|
8
10
|
import sys
|
|
9
11
|
import time
|
|
12
|
+
import urllib.parse
|
|
13
|
+
from http import HTTPStatus
|
|
10
14
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
11
15
|
from typing import Any
|
|
12
16
|
|
|
17
|
+
from localmodels import speech_worker_sensevoice
|
|
18
|
+
|
|
19
|
+
_SPEECH_SILENCE_RMS = 150.0
|
|
20
|
+
_BOUNDARY_RE = re.compile(r'boundary=(?:"([^"]+)"|([^;\s]+))', re.IGNORECASE)
|
|
21
|
+
_NAME_RE = re.compile(r'name="([^"]+)"')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _speech_wav_metrics(wav_bytes: bytes) -> tuple[float, float]:
|
|
25
|
+
samples, _rate, duration = speech_worker_sensevoice.read_pcm16_wav(wav_bytes)
|
|
26
|
+
if not samples:
|
|
27
|
+
return duration, 0.0
|
|
28
|
+
mean_sq = sum(sample * sample for sample in samples) / len(samples)
|
|
29
|
+
return duration, math.sqrt(mean_sq) * 32768.0
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _extract_multipart_speech(content_type: str, body: bytes) -> tuple[str, bytes]:
|
|
33
|
+
match = _BOUNDARY_RE.search(content_type)
|
|
34
|
+
if match is None:
|
|
35
|
+
raise ValueError("missing multipart boundary")
|
|
36
|
+
boundary = (match.group(1) or match.group(2) or "").encode("latin-1")
|
|
37
|
+
lang = "auto"
|
|
38
|
+
wav_bytes = b""
|
|
39
|
+
for raw_part in body.split(b"--" + boundary)[1:]:
|
|
40
|
+
if raw_part.startswith(b"--"):
|
|
41
|
+
break
|
|
42
|
+
part = raw_part[2:] if raw_part.startswith(b"\r\n") else raw_part.lstrip(b"\n")
|
|
43
|
+
sep = b"\r\n\r\n" if b"\r\n\r\n" in part else b"\n\n"
|
|
44
|
+
if sep not in part:
|
|
45
|
+
continue
|
|
46
|
+
head, content = part.split(sep, 1)
|
|
47
|
+
if content.endswith(b"\r\n"):
|
|
48
|
+
content = content[:-2]
|
|
49
|
+
elif content.endswith(b"\n"):
|
|
50
|
+
content = content[:-1]
|
|
51
|
+
name_match = _NAME_RE.search(head.decode("latin-1", errors="replace"))
|
|
52
|
+
if name_match is None:
|
|
53
|
+
continue
|
|
54
|
+
field_name = name_match.group(1)
|
|
55
|
+
if field_name == "language":
|
|
56
|
+
lang = content.decode("utf-8", errors="replace").strip() or "auto"
|
|
57
|
+
elif field_name == "file":
|
|
58
|
+
wav_bytes = content
|
|
59
|
+
return lang, wav_bytes
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _make_speech_handler(served_model: str, engine_mode: str) -> type[BaseHTTPRequestHandler]:
|
|
63
|
+
engine_name = "whisper.cpp" if engine_mode == "speech-whisper" else "sherpa-onnx"
|
|
64
|
+
|
|
65
|
+
class SpeechHandler(BaseHTTPRequestHandler):
|
|
66
|
+
protocol_version = "HTTP/1.1"
|
|
67
|
+
|
|
68
|
+
def log_message(self, format: str, *args: object) -> None: # noqa: A002
|
|
69
|
+
_ = (format, args)
|
|
70
|
+
|
|
71
|
+
def _send(self, status: HTTPStatus, body: dict[str, Any]) -> None:
|
|
72
|
+
speech_worker_sensevoice.send_json_response(self, status, body)
|
|
73
|
+
|
|
74
|
+
def do_GET(self) -> None: # noqa: N802
|
|
75
|
+
path = urllib.parse.urlsplit(self.path).path
|
|
76
|
+
if path == "/health" or (engine_mode == "speech-whisper" and path == "/"):
|
|
77
|
+
self._send(
|
|
78
|
+
HTTPStatus.OK,
|
|
79
|
+
{"status": "ok", "engine": engine_name, "simulated": True},
|
|
80
|
+
)
|
|
81
|
+
return
|
|
82
|
+
self._send(HTTPStatus.NOT_FOUND, {"error": {"message": "not found"}})
|
|
83
|
+
|
|
84
|
+
def do_POST(self) -> None: # noqa: N802
|
|
85
|
+
parsed = urllib.parse.urlsplit(self.path)
|
|
86
|
+
length = int(self.headers.get("Content-Length") or "0")
|
|
87
|
+
raw = self.rfile.read(length) if length > 0 else b""
|
|
88
|
+
if engine_mode == "speech-whisper" and parsed.path == "/v1/audio/transcriptions":
|
|
89
|
+
try:
|
|
90
|
+
lang, wav_bytes = _extract_multipart_speech(
|
|
91
|
+
self.headers.get("Content-Type") or "", raw
|
|
92
|
+
)
|
|
93
|
+
duration, rms = _speech_wav_metrics(wav_bytes)
|
|
94
|
+
except ValueError as exc:
|
|
95
|
+
self._send(HTTPStatus.BAD_REQUEST, {"error": {"message": str(exc)}})
|
|
96
|
+
return
|
|
97
|
+
if rms < _SPEECH_SILENCE_RMS:
|
|
98
|
+
self._send(
|
|
99
|
+
HTTPStatus.OK,
|
|
100
|
+
{
|
|
101
|
+
"text": "",
|
|
102
|
+
"language": "" if lang == "auto" else lang,
|
|
103
|
+
"duration": round(duration, 3),
|
|
104
|
+
"segments": [],
|
|
105
|
+
},
|
|
106
|
+
)
|
|
107
|
+
return
|
|
108
|
+
resolved_lang = "en" if lang == "auto" else lang
|
|
109
|
+
text = f"Simulated {served_model} transcript ({resolved_lang})."
|
|
110
|
+
dur = round(duration, 3)
|
|
111
|
+
self._send(
|
|
112
|
+
HTTPStatus.OK,
|
|
113
|
+
{
|
|
114
|
+
"text": text,
|
|
115
|
+
"language": resolved_lang,
|
|
116
|
+
"duration": dur,
|
|
117
|
+
"segments": [{"start": 0.0, "end": dur, "text": text}],
|
|
118
|
+
},
|
|
119
|
+
)
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
if engine_mode == "speech-sensevoice" and parsed.path == "/transcribe":
|
|
123
|
+
query = urllib.parse.parse_qs(parsed.query)
|
|
124
|
+
lang = (query.get("language") or ["auto"])[0] or "auto"
|
|
125
|
+
try:
|
|
126
|
+
duration, rms = _speech_wav_metrics(raw)
|
|
127
|
+
except ValueError as exc:
|
|
128
|
+
self._send(HTTPStatus.BAD_REQUEST, {"error": {"message": str(exc)}})
|
|
129
|
+
return
|
|
130
|
+
dur = round(duration, 3)
|
|
131
|
+
if rms < _SPEECH_SILENCE_RMS:
|
|
132
|
+
self._send(
|
|
133
|
+
HTTPStatus.OK,
|
|
134
|
+
{
|
|
135
|
+
"text": "<|zh|><|NEUTRAL|><|nospeech|>",
|
|
136
|
+
"language": "",
|
|
137
|
+
"duration": dur,
|
|
138
|
+
"segments": [],
|
|
139
|
+
},
|
|
140
|
+
)
|
|
141
|
+
return
|
|
142
|
+
resolved_lang = lang if lang in speech_worker_sensevoice.KNOWN_LANGUAGES else "zh"
|
|
143
|
+
text = (
|
|
144
|
+
f"<|{resolved_lang}|><|NEUTRAL|><|Speech|><|withitn|>"
|
|
145
|
+
f"Simulated {served_model} transcript ({resolved_lang})."
|
|
146
|
+
)
|
|
147
|
+
self._send(
|
|
148
|
+
HTTPStatus.OK,
|
|
149
|
+
{"text": text, "language": "", "duration": dur, "segments": []},
|
|
150
|
+
)
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
self._send(HTTPStatus.NOT_FOUND, {"error": {"message": "not found"}})
|
|
154
|
+
|
|
155
|
+
return SpeechHandler
|
|
156
|
+
|
|
13
157
|
|
|
14
158
|
def _last_user_text(messages: Any) -> str:
|
|
15
159
|
if not isinstance(messages, list):
|
|
@@ -176,8 +320,19 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
176
320
|
parser.add_argument("--bind", default="127.0.0.1")
|
|
177
321
|
parser.add_argument("--port", type=int, required=True)
|
|
178
322
|
parser.add_argument("--model", required=True)
|
|
323
|
+
parser.add_argument(
|
|
324
|
+
"--mode",
|
|
325
|
+
choices=("llm", "speech-whisper", "speech-sensevoice"),
|
|
326
|
+
default="llm",
|
|
327
|
+
)
|
|
179
328
|
args = parser.parse_args(argv)
|
|
180
|
-
|
|
329
|
+
handler = (
|
|
330
|
+
_make_handler(args.model)
|
|
331
|
+
if args.mode == "llm"
|
|
332
|
+
else _make_speech_handler(args.model, args.mode)
|
|
333
|
+
)
|
|
334
|
+
server = ThreadingHTTPServer((args.bind, args.port), handler)
|
|
335
|
+
server.daemon_threads = True
|
|
181
336
|
signal.signal(signal.SIGTERM, lambda *_a: sys.exit(0))
|
|
182
337
|
try:
|
|
183
338
|
server.serve_forever(poll_interval=0.2)
|