@simplexai/localmodels-linux-arm64 0.1.0-dev.101 → 0.1.0-dev.103
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 +56 -37
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +3 -1
- package/modules/localmodels/app.py +35 -7
- package/modules/localmodels/catalog.json +306 -1
- package/modules/localmodels/cli_parser.py +4 -0
- package/modules/localmodels/debug_stub_server.py +280 -7
- package/modules/localmodels/download-mirrors.json +76 -0
- package/modules/localmodels/engine_adapters/suite_plan.py +4 -2
- package/modules/localmodels/engines-speechcpp-v0.11.3.json +84 -0
- package/modules/localmodels/library_serve.py +46 -26
- package/modules/localmodels/remote_ops_common.py +12 -2
- package/modules/localmodels/sidecar_downloads.py +426 -0
- package/modules/localmodels/sidecar_http.py +396 -0
- package/modules/localmodels/sidecar_state.py +466 -0
- package/modules/localmodels/speech_admission.py +13 -0
- package/modules/localmodels/speech_jobs.py +81 -370
- package/modules/localmodels/speech_server.py +17 -8
- package/modules/localmodels/speech_state.py +87 -454
- package/modules/localmodels/speech_workers.py +41 -163
- package/modules/localmodels/stack.py +18 -0
- package/modules/localmodels/stack_capacity.py +37 -1
- package/modules/localmodels/stack_cli.py +1 -0
- package/modules/localmodels/stack_down.py +19 -14
- package/modules/localmodels/stack_hardware.py +18 -16
- package/modules/localmodels/stack_plan.py +21 -0
- package/modules/localmodels/stack_ports.py +1 -0
- package/modules/localmodels/stack_progress.py +15 -0
- package/modules/localmodels/stack_run.py +25 -2
- package/modules/localmodels/stack_suite.py +54 -12
- package/modules/localmodels/stack_up.py +51 -1
- package/modules/localmodels/strata_poly_server.py +140 -3
- package/modules/localmodels/suite.py +10 -1
- package/modules/localmodels/tts-moss.txt +321 -0
- package/modules/localmodels/tts_admission.py +396 -0
- package/modules/localmodels/tts_backend_speechcpp.py +487 -0
- package/modules/localmodels/tts_catalog.py +251 -0
- package/modules/localmodels/tts_cli.py +333 -0
- package/modules/localmodels/tts_http.py +617 -0
- package/modules/localmodels/tts_jobs.py +496 -0
- package/modules/localmodels/tts_model.py +591 -0
- package/modules/localmodels/tts_moss_presets.json +1 -0
- package/modules/localmodels/tts_serve.py +451 -0
- package/modules/localmodels/tts_sidecar.py +750 -0
- package/modules/localmodels/tts_state.py +395 -0
- package/modules/localmodels/tts_worker_kokoro.py +463 -0
- package/modules/localmodels/tts_worker_moss.py +748 -0
- package/modules/localmodels/tts_workers.py +696 -0
- package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/RECORD +53 -34
- package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/licenses/NOTICE +29 -0
- package/package.json +1 -1
- package/tool.json +1 -1
- /package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev101.dist-info → simplex_localmodels-0.1.0.dev103.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -1,32 +1,26 @@
|
|
|
1
1
|
"""Persistent state under ``$LOCALMODELS_STATE/speech/`` and sidecar loopback HTTP client.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
- ``runtime.json`` (0600): live sidecar receipt (base_url, key_file, pid, pool, device telemetry)
|
|
6
|
-
- ``models/<id>.json`` (0600): installed speech model weight/engine record
|
|
7
|
-
- ``jobs/<id>.json``, ``jobs/<id>.pid``, ``jobs/<id>.cancel``: active or failed download jobs
|
|
8
|
-
- ``.lock``: cross-platform exclusive file lock mirroring ``library_state.py``
|
|
3
|
+
Delegates storage, locks, job persistence, and loopback HTTP transport to ``sidecar_state`` and
|
|
4
|
+
``sidecar_http`` while preserving the exact ``localmodels.speech-*`` v1 contract and defaults.
|
|
9
5
|
"""
|
|
10
6
|
|
|
11
7
|
from __future__ import annotations
|
|
12
8
|
|
|
13
|
-
import contextlib
|
|
14
|
-
import json
|
|
15
|
-
import os
|
|
16
|
-
import re
|
|
17
|
-
import secrets
|
|
18
|
-
import time
|
|
19
|
-
import urllib.error
|
|
20
|
-
import urllib.request
|
|
21
|
-
import uuid
|
|
22
9
|
from collections.abc import Iterator, Mapping
|
|
23
10
|
from contextlib import contextmanager
|
|
24
11
|
from pathlib import Path
|
|
25
|
-
from typing import Any, Literal,
|
|
12
|
+
from typing import Any, Literal, cast
|
|
26
13
|
|
|
27
|
-
from localmodels import
|
|
28
|
-
from localmodels.
|
|
29
|
-
|
|
14
|
+
from localmodels import library_state, process_identity, sidecar_http
|
|
15
|
+
from localmodels.sidecar_state import (
|
|
16
|
+
JOB_WRITE_EVERY_S as JOB_WRITE_EVERY_S,
|
|
17
|
+
)
|
|
18
|
+
from localmodels.sidecar_state import (
|
|
19
|
+
SidecarJob,
|
|
20
|
+
SidecarJobHandle,
|
|
21
|
+
SidecarModelRecord,
|
|
22
|
+
SidecarStore,
|
|
23
|
+
)
|
|
30
24
|
from localmodels.speech_catalog import V1_SPEECH_MODEL_IDS
|
|
31
25
|
from localmodels.speech_model import (
|
|
32
26
|
EFFECTIVE_DEVICES,
|
|
@@ -55,120 +49,52 @@ MODEL_RECORD_SCHEMA = "localmodels.speech-model/v1"
|
|
|
55
49
|
JOB_SCHEMA = "localmodels.speech-job/v1"
|
|
56
50
|
CONFIG_FILE_SCHEMA = "localmodels.speech-config-store/v1"
|
|
57
51
|
RUNTIME_FILE_SCHEMA = "localmodels.speech-runtime-record/v1"
|
|
58
|
-
JOB_WRITE_EVERY_S = 1.0
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
class _SpeechModelRecordOptional(TypedDict, total=False):
|
|
62
|
-
engine_dir: str | None
|
|
63
|
-
engine_backend: str | None
|
|
64
|
-
error: SpeechModelError
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class SpeechModelRecord(_SpeechModelRecordOptional):
|
|
68
|
-
schema: str
|
|
69
|
-
id: str
|
|
70
|
-
base_dir: str
|
|
71
|
-
model_dir: str
|
|
72
|
-
files: list[str]
|
|
73
|
-
state: str
|
|
74
|
-
updated_at: str
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
class _SpeechJobOptional(TypedDict, total=False):
|
|
78
|
-
error: SpeechModelError
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
class SpeechJob(_SpeechJobOptional):
|
|
82
|
-
schema: str
|
|
83
|
-
id: str
|
|
84
|
-
kind: str
|
|
85
|
-
model: str
|
|
86
|
-
base_dir: str
|
|
87
|
-
files: list[str]
|
|
88
|
-
step: str
|
|
89
|
-
done_bytes: int
|
|
90
|
-
total_bytes: int
|
|
91
|
-
bytes_per_second: int
|
|
92
|
-
source: str
|
|
93
|
-
connections: int
|
|
94
|
-
started_at: str
|
|
95
|
-
updated_at: str
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def root(state_dir: Path | None = None) -> Path:
|
|
99
|
-
base = hosts.state_dir() if state_dir is None else state_dir
|
|
100
|
-
return base / "speech"
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
def _safe_name(model_id: str) -> str:
|
|
104
|
-
return re.sub(r"[^A-Za-z0-9._-]+", "_", model_id)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def config_path(state_dir: Path | None = None) -> Path:
|
|
108
|
-
return root(state_dir) / "config.json"
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
def runtime_path(state_dir: Path | None = None) -> Path:
|
|
112
|
-
return root(state_dir) / "runtime.json"
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def api_key_path(state_dir: Path | None = None) -> Path:
|
|
116
|
-
return root(state_dir) / "api-key"
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
def run_dir(state_dir: Path | None = None) -> Path:
|
|
120
|
-
return root(state_dir) / "run"
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def server_pid_path(state_dir: Path | None = None) -> Path:
|
|
124
|
-
return root(state_dir) / "server.pid"
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
def server_log_path(state_dir: Path | None = None) -> Path:
|
|
128
|
-
return root(state_dir) / "server.log"
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
def ensure_api_key(state_dir: Path | None = None) -> tuple[Path, str]:
|
|
132
|
-
"""Return ``(key_path, token)``, creating a 0600 bearer key if missing or empty."""
|
|
133
|
-
path = api_key_path(state_dir)
|
|
134
|
-
existing = read_api_key(str(path))
|
|
135
|
-
if existing:
|
|
136
|
-
return path, existing
|
|
137
|
-
token = secrets.token_urlsafe(32)
|
|
138
|
-
http_local.write_private_text(path, token + "\n")
|
|
139
|
-
return path, token
|
|
140
|
-
|
|
141
52
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
for path in (pp, Path(str(pp) + ".identity.json")):
|
|
145
|
-
path.unlink(missing_ok=True)
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
def record_path(model_id: str, state_dir: Path | None = None) -> Path:
|
|
149
|
-
return root(state_dir) / "models" / f"{_safe_name(model_id)}.json"
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
def job_path(model_id: str, state_dir: Path | None = None) -> Path:
|
|
153
|
-
return root(state_dir) / "jobs" / f"{_safe_name(model_id)}.json"
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
def pid_path(model_id: str, state_dir: Path | None = None) -> Path:
|
|
157
|
-
return root(state_dir) / "jobs" / f"{_safe_name(model_id)}.pid"
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
def cancel_path(model_id: str, state_dir: Path | None = None) -> Path:
|
|
161
|
-
return root(state_dir) / "jobs" / f"{_safe_name(model_id)}.cancel"
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
@contextmanager
|
|
165
|
-
def locked(state_dir: Path | None = None) -> Iterator[None]:
|
|
166
|
-
"""Exclusive cross-platform lock serializing speech state transitions."""
|
|
167
|
-
with hosts.file_lock(root(state_dir) / ".lock"):
|
|
168
|
-
yield
|
|
53
|
+
SpeechModelRecord = SidecarModelRecord
|
|
54
|
+
SpeechJob = SidecarJob
|
|
169
55
|
|
|
56
|
+
STORE = SidecarStore(
|
|
57
|
+
namespace="speech",
|
|
58
|
+
model_record_schema=MODEL_RECORD_SCHEMA,
|
|
59
|
+
job_schema=JOB_SCHEMA,
|
|
60
|
+
config_file_schema=CONFIG_FILE_SCHEMA,
|
|
61
|
+
runtime_file_schema=RUNTIME_FILE_SCHEMA,
|
|
62
|
+
shutdown_path="/v1/speech/shutdown",
|
|
63
|
+
)
|
|
170
64
|
|
|
171
|
-
|
|
65
|
+
root = STORE.root
|
|
66
|
+
config_path = STORE.config_path
|
|
67
|
+
runtime_path = STORE.runtime_path
|
|
68
|
+
api_key_path = STORE.api_key_path
|
|
69
|
+
run_dir = STORE.run_dir
|
|
70
|
+
server_pid_path = STORE.server_pid_path
|
|
71
|
+
server_log_path = STORE.server_log_path
|
|
72
|
+
ensure_api_key = STORE.ensure_api_key
|
|
73
|
+
clear_server_pid = STORE.clear_server_pid
|
|
74
|
+
record_path = STORE.record_path
|
|
75
|
+
job_path = STORE.job_path
|
|
76
|
+
pid_path = STORE.pid_path
|
|
77
|
+
cancel_path = STORE.cancel_path
|
|
78
|
+
locked = STORE.locked
|
|
79
|
+
load_runtime_record = STORE.load_runtime_record
|
|
80
|
+
save_runtime_record = STORE.save_runtime_record
|
|
81
|
+
clear_runtime_record = STORE.clear_runtime_record
|
|
82
|
+
load_record = STORE.load_record
|
|
83
|
+
records = STORE.records
|
|
84
|
+
save_record = STORE.save_record
|
|
85
|
+
forget_record = STORE.forget_record
|
|
86
|
+
read_job = STORE.read_job
|
|
87
|
+
job_alive = STORE.job_alive
|
|
88
|
+
jobs = STORE.jobs
|
|
89
|
+
receipt = STORE.receipt
|
|
90
|
+
mark_cancelled = STORE.mark_cancelled
|
|
91
|
+
is_cancelled = STORE.is_cancelled
|
|
92
|
+
clear_job = STORE.clear_job
|
|
93
|
+
current_job = STORE.current_job
|
|
94
|
+
|
|
95
|
+
read_api_key = sidecar_http.read_api_key
|
|
96
|
+
sidecar_request_detail = sidecar_http.sidecar_request_detail
|
|
97
|
+
sidecar_request = sidecar_http.sidecar_request
|
|
172
98
|
|
|
173
99
|
|
|
174
100
|
def validate_config(raw: Mapping[str, object]) -> SpeechConfig:
|
|
@@ -198,128 +124,18 @@ def validate_config(raw: Mapping[str, object]) -> SpeechConfig:
|
|
|
198
124
|
|
|
199
125
|
def load_config(state_dir: Path | None = None) -> SpeechConfig:
|
|
200
126
|
"""Load saved speech configuration, falling back cleanly to defaults."""
|
|
201
|
-
|
|
127
|
+
raw = STORE.load_raw_config(state_dir)
|
|
128
|
+
if raw is None:
|
|
129
|
+
return default_config()
|
|
202
130
|
try:
|
|
203
|
-
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
204
|
-
if not isinstance(raw, dict):
|
|
205
|
-
return default_config()
|
|
206
131
|
return validate_config(raw)
|
|
207
|
-
except
|
|
132
|
+
except SpeechError:
|
|
208
133
|
return default_config()
|
|
209
134
|
|
|
210
135
|
|
|
211
136
|
def save_config(config: SpeechConfig, state_dir: Path | None = None) -> Path:
|
|
212
137
|
"""Validate and atomically save ``config.json`` with 0600 permissions."""
|
|
213
|
-
|
|
214
|
-
payload = {
|
|
215
|
-
"schema": CONFIG_FILE_SCHEMA,
|
|
216
|
-
**validated,
|
|
217
|
-
"updated_at": utc_now(),
|
|
218
|
-
}
|
|
219
|
-
return hosts.save_private(config_path(state_dir), payload)
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
# ---------------------------------------------------------------------------- runtime & loopback HTTP
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
def load_runtime_record(state_dir: Path | None = None) -> dict[str, Any] | None:
|
|
226
|
-
"""Read ``runtime.json`` if present and valid JSON."""
|
|
227
|
-
try:
|
|
228
|
-
raw = json.loads(runtime_path(state_dir).read_text(encoding="utf-8"))
|
|
229
|
-
return raw if isinstance(raw, dict) else None
|
|
230
|
-
except (OSError, ValueError):
|
|
231
|
-
return None
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
def save_runtime_record(record: Mapping[str, Any], state_dir: Path | None = None) -> Path:
|
|
235
|
-
"""Atomically persist ``runtime.json`` with 0600 permissions."""
|
|
236
|
-
payload = {
|
|
237
|
-
"schema": RUNTIME_FILE_SCHEMA,
|
|
238
|
-
**record,
|
|
239
|
-
"updated_at": utc_now(),
|
|
240
|
-
}
|
|
241
|
-
return hosts.save_private(runtime_path(state_dir), payload)
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
def clear_runtime_record(state_dir: Path | None = None) -> None:
|
|
245
|
-
runtime_path(state_dir).unlink(missing_ok=True)
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
def read_api_key(key_file: str | None) -> str | None:
|
|
249
|
-
"""Read the loopback bearer token from ``key_file`` when present."""
|
|
250
|
-
if not key_file:
|
|
251
|
-
return None
|
|
252
|
-
try:
|
|
253
|
-
token = Path(key_file).expanduser().read_text(encoding="utf-8").strip()
|
|
254
|
-
return token or None
|
|
255
|
-
except OSError:
|
|
256
|
-
return None
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
def sidecar_request_detail(
|
|
260
|
-
base_url: str,
|
|
261
|
-
path: str,
|
|
262
|
-
*,
|
|
263
|
-
method: str = "GET",
|
|
264
|
-
key_file: str | None = None,
|
|
265
|
-
payload: Mapping[str, object] | None = None,
|
|
266
|
-
timeout: float = 2.0,
|
|
267
|
-
) -> tuple[int, dict[str, Any] | None]:
|
|
268
|
-
"""Authenticated loopback request returning ``(http_status, parsed_json_or_none)``.
|
|
269
|
-
|
|
270
|
-
Returns ``(0, None)`` when the sidecar is unreachable. Uses
|
|
271
|
-
``no_proxy_opener(allow_redirects=False)`` and ``Connection: close`` to prevent proxy
|
|
272
|
-
hijacking, credential redirects, and Windows loopback keep-alive resets.
|
|
273
|
-
"""
|
|
274
|
-
url = base_url.rstrip("/") + "/" + path.lstrip("/")
|
|
275
|
-
headers: dict[str, str] = {"Connection": "close"}
|
|
276
|
-
token = read_api_key(key_file)
|
|
277
|
-
if token:
|
|
278
|
-
headers["Authorization"] = f"Bearer {token}"
|
|
279
|
-
body: bytes | None = None
|
|
280
|
-
if payload is not None:
|
|
281
|
-
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
282
|
-
headers["Content-Type"] = "application/json"
|
|
283
|
-
req = urllib.request.Request(url, data=body, headers=headers, method=method)
|
|
284
|
-
opener = http_local.no_proxy_opener(allow_redirects=False)
|
|
285
|
-
try:
|
|
286
|
-
with opener.open(req, timeout=timeout) as resp:
|
|
287
|
-
status = int(getattr(resp, "status", 200))
|
|
288
|
-
raw = resp.read().decode("utf-8", "replace").strip()
|
|
289
|
-
if not raw:
|
|
290
|
-
return status, {}
|
|
291
|
-
parsed = json.loads(raw)
|
|
292
|
-
return status, parsed if isinstance(parsed, dict) else None
|
|
293
|
-
except urllib.error.HTTPError as exc:
|
|
294
|
-
try:
|
|
295
|
-
raw = exc.read().decode("utf-8", "replace").strip()
|
|
296
|
-
parsed = json.loads(raw) if raw else None
|
|
297
|
-
return int(exc.code), parsed if isinstance(parsed, dict) else None
|
|
298
|
-
except (OSError, ValueError):
|
|
299
|
-
return int(exc.code), None
|
|
300
|
-
except (urllib.error.URLError, OSError, ValueError):
|
|
301
|
-
return 0, None
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
def sidecar_request(
|
|
305
|
-
base_url: str,
|
|
306
|
-
path: str,
|
|
307
|
-
*,
|
|
308
|
-
method: str = "GET",
|
|
309
|
-
key_file: str | None = None,
|
|
310
|
-
payload: Mapping[str, object] | None = None,
|
|
311
|
-
timeout: float = 2.0,
|
|
312
|
-
) -> dict[str, Any] | None:
|
|
313
|
-
"""Best-effort authenticated loopback request returning parsed JSON on 2xx, else ``None``."""
|
|
314
|
-
status, parsed = sidecar_request_detail(
|
|
315
|
-
base_url,
|
|
316
|
-
path,
|
|
317
|
-
method=method,
|
|
318
|
-
key_file=key_file,
|
|
319
|
-
payload=payload,
|
|
320
|
-
timeout=timeout,
|
|
321
|
-
)
|
|
322
|
-
return parsed if 200 <= status < 300 else None
|
|
138
|
+
return STORE.save_raw_config(validate_config(config), state_dir)
|
|
323
139
|
|
|
324
140
|
|
|
325
141
|
def _opt_str(value: object) -> str | None:
|
|
@@ -430,16 +246,6 @@ def probe_live_runtime(
|
|
|
430
246
|
return stopped_runtime(config)
|
|
431
247
|
|
|
432
248
|
|
|
433
|
-
def _wait_server_pid_exit(pid_file: Path, timeout_s: float) -> bool:
|
|
434
|
-
deadline = time.monotonic() + timeout_s
|
|
435
|
-
while time.monotonic() < deadline:
|
|
436
|
-
state, _pid = process_identity.status(pid_file)
|
|
437
|
-
if state == "inactive":
|
|
438
|
-
return True
|
|
439
|
-
time.sleep(0.05)
|
|
440
|
-
return process_identity.status(pid_file)[0] == "inactive"
|
|
441
|
-
|
|
442
|
-
|
|
443
249
|
def stop_sidecar(
|
|
444
250
|
state_dir: Path | None = None,
|
|
445
251
|
*,
|
|
@@ -449,31 +255,12 @@ def stop_sidecar(
|
|
|
449
255
|
) -> SpeechStopDocument:
|
|
450
256
|
"""Stop the speech sidecar cleanly (HTTP shutdown -> verified ``process_identity.stop``)."""
|
|
451
257
|
cfg = load_config(state_dir)
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
status_code, _parsed = sidecar_request_detail(
|
|
459
|
-
base_url,
|
|
460
|
-
"/v1/speech/shutdown",
|
|
461
|
-
method="POST",
|
|
462
|
-
key_file=key_file if isinstance(key_file, str) else None,
|
|
463
|
-
payload={},
|
|
464
|
-
timeout=http_timeout_s,
|
|
465
|
-
)
|
|
466
|
-
if 200 <= status_code < 300:
|
|
467
|
-
was_running = True
|
|
468
|
-
_wait_server_pid_exit(pid_file, poll_s)
|
|
469
|
-
|
|
470
|
-
if os.path.lexists(pid_file):
|
|
471
|
-
outcome = process_identity.stop(pid_file, grace=grace_s)
|
|
472
|
-
if outcome == "stopped":
|
|
473
|
-
was_running = True
|
|
474
|
-
|
|
475
|
-
clear_runtime_record(state_dir)
|
|
476
|
-
clear_server_pid(state_dir)
|
|
258
|
+
was_running = STORE.stop_sidecar_process(
|
|
259
|
+
state_dir,
|
|
260
|
+
http_timeout_s=http_timeout_s,
|
|
261
|
+
poll_s=poll_s,
|
|
262
|
+
grace_s=grace_s,
|
|
263
|
+
)
|
|
477
264
|
return {
|
|
478
265
|
"schema": STOP_SCHEMA,
|
|
479
266
|
"ok": True,
|
|
@@ -487,129 +274,15 @@ def stop_sidecar_if_recorded(
|
|
|
487
274
|
state_dir: Path | None = None,
|
|
488
275
|
) -> tuple[Literal["none", "stopped", "not-running", "failed"], str]:
|
|
489
276
|
"""Stop any speech sidecar recorded under ``state_dir`` for ``stack down``."""
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
doc = stop_sidecar(state_dir)
|
|
496
|
-
except (RuntimeError, SpeechError, OSError) as exc:
|
|
497
|
-
return "failed", str(exc)[-300:]
|
|
498
|
-
return ("stopped" if doc["stopped"] else "not-running"), ""
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
# ---------------------------------------------------------------------------- model records
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
def load_record(model_id: str, state_dir: Path | None = None) -> SpeechModelRecord | None:
|
|
505
|
-
try:
|
|
506
|
-
raw = json.loads(record_path(model_id, state_dir).read_text(encoding="utf-8"))
|
|
507
|
-
return decode(raw, SpeechModelRecord)
|
|
508
|
-
except (OSError, ValueError):
|
|
509
|
-
return None
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
def records(state_dir: Path | None = None) -> list[SpeechModelRecord]:
|
|
513
|
-
found: list[SpeechModelRecord] = []
|
|
514
|
-
models_dir = root(state_dir) / "models"
|
|
515
|
-
if not models_dir.is_dir():
|
|
516
|
-
return found
|
|
517
|
-
for path in sorted(models_dir.glob("*.json")):
|
|
518
|
-
try:
|
|
519
|
-
found.append(decode(json.loads(path.read_text(encoding="utf-8")), SpeechModelRecord))
|
|
520
|
-
except (OSError, ValueError):
|
|
521
|
-
continue
|
|
522
|
-
return found
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
def save_record(record: SpeechModelRecord, state_dir: Path | None = None) -> Path:
|
|
526
|
-
record["updated_at"] = utc_now()
|
|
527
|
-
return hosts.save_private(record_path(record["id"], state_dir), record)
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
def forget_record(model_id: str, state_dir: Path | None = None) -> None:
|
|
531
|
-
record_path(model_id, state_dir).unlink(missing_ok=True)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
# ---------------------------------------------------------------------------- download jobs
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
def read_job(model_id: str, state_dir: Path | None = None) -> SpeechJob | None:
|
|
538
|
-
try:
|
|
539
|
-
raw = json.loads(job_path(model_id, state_dir).read_text(encoding="utf-8"))
|
|
540
|
-
return decode(raw, SpeechJob)
|
|
541
|
-
except (OSError, ValueError):
|
|
542
|
-
return None
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
def job_alive(model_id: str, state_dir: Path | None = None) -> bool:
|
|
546
|
-
try:
|
|
547
|
-
state, pid = process_identity.status(pid_path(model_id, state_dir))
|
|
548
|
-
except (OSError, RuntimeError, ValueError):
|
|
549
|
-
return False
|
|
550
|
-
return state == "active" and pid is not None and read_job(model_id, state_dir) is not None
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
def jobs(state_dir: Path | None = None) -> dict[str, tuple[SpeechJob, bool]]:
|
|
554
|
-
found: dict[str, tuple[SpeechJob, bool]] = {}
|
|
555
|
-
jobs_dir = root(state_dir) / "jobs"
|
|
556
|
-
if not jobs_dir.is_dir():
|
|
557
|
-
return found
|
|
558
|
-
for path in sorted(jobs_dir.glob("*.json")):
|
|
559
|
-
try:
|
|
560
|
-
job = decode(json.loads(path.read_text(encoding="utf-8")), SpeechJob)
|
|
561
|
-
except (OSError, ValueError):
|
|
562
|
-
continue
|
|
563
|
-
found[job["model"]] = (job, job_alive(job["model"], state_dir))
|
|
564
|
-
return found
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
def receipt(model_id: str, state_dir: Path | None = None) -> process_identity.Identity | None:
|
|
568
|
-
try:
|
|
569
|
-
ident_path = Path(str(pid_path(model_id, state_dir)) + ".identity.json")
|
|
570
|
-
return decode(json.loads(ident_path.read_text(encoding="utf-8")), process_identity.Identity)
|
|
571
|
-
except (OSError, ValueError):
|
|
572
|
-
return None
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
def mark_cancelled(job: SpeechJob, state_dir: Path | None = None) -> None:
|
|
576
|
-
path = cancel_path(job["model"], state_dir)
|
|
577
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
578
|
-
path.write_text(str(job["id"]), encoding="utf-8")
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
def is_cancelled(job: SpeechJob, state_dir: Path | None = None) -> bool:
|
|
582
|
-
try:
|
|
583
|
-
return cancel_path(job["model"], state_dir).read_text(encoding="utf-8") == str(job["id"])
|
|
584
|
-
except OSError:
|
|
585
|
-
return False
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
def clear_job(model_id: str, state_dir: Path | None = None) -> None:
|
|
589
|
-
jp = job_path(model_id, state_dir)
|
|
590
|
-
pp = pid_path(model_id, state_dir)
|
|
591
|
-
for path in (
|
|
592
|
-
jp,
|
|
593
|
-
jp.with_name(jp.name + ".tmp"),
|
|
594
|
-
cancel_path(model_id, state_dir),
|
|
595
|
-
pp,
|
|
596
|
-
Path(str(pp) + ".identity.json"),
|
|
597
|
-
):
|
|
598
|
-
path.unlink(missing_ok=True)
|
|
277
|
+
return STORE.stop_if_recorded(
|
|
278
|
+
state_dir,
|
|
279
|
+
stop_fn=lambda sd: bool(stop_sidecar(sd)["stopped"]),
|
|
280
|
+
error_types=(RuntimeError, OSError, ValueError, SpeechError),
|
|
281
|
+
)
|
|
599
282
|
|
|
600
283
|
|
|
601
284
|
def fail_job(job: SpeechJob, error: SpeechModelError, state_dir: Path | None = None) -> None:
|
|
602
|
-
job
|
|
603
|
-
job["updated_at"] = utc_now()
|
|
604
|
-
hosts.save_private(job_path(job["model"], state_dir), job)
|
|
605
|
-
pp = pid_path(job["model"], state_dir)
|
|
606
|
-
for path in (cancel_path(job["model"], state_dir), pp, Path(str(pp) + ".identity.json")):
|
|
607
|
-
path.unlink(missing_ok=True)
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
def current_job(job: SpeechJob, state_dir: Path | None = None) -> bool:
|
|
611
|
-
found = read_job(job["model"], state_dir)
|
|
612
|
-
return found is not None and found["id"] == job["id"]
|
|
285
|
+
STORE.fail_job(job, {"code": error["code"], "message": error["message"]}, state_dir)
|
|
613
286
|
|
|
614
287
|
|
|
615
288
|
def acquire_job(
|
|
@@ -621,47 +294,20 @@ def acquire_job(
|
|
|
621
294
|
state_dir: Path | None = None,
|
|
622
295
|
) -> SpeechJob:
|
|
623
296
|
"""Under ``locked()``: claim an exclusive download job for ``model_id``."""
|
|
624
|
-
existing = read_job(model_id, state_dir)
|
|
625
|
-
if existing is not None and job_alive(model_id, state_dir):
|
|
626
|
-
raise SpeechError("JOB_RUNNING", f"{model_id} already has a running download")
|
|
627
|
-
clear_job(model_id, state_dir)
|
|
628
|
-
library_state.become_leader()
|
|
629
|
-
now = utc_now()
|
|
630
|
-
job: SpeechJob = {
|
|
631
|
-
"schema": JOB_SCHEMA,
|
|
632
|
-
"id": uuid.uuid4().hex,
|
|
633
|
-
"kind": "download",
|
|
634
|
-
"model": model_id,
|
|
635
|
-
"base_dir": base_dir,
|
|
636
|
-
"files": list(files),
|
|
637
|
-
"step": "download",
|
|
638
|
-
"done_bytes": 0,
|
|
639
|
-
"total_bytes": max(0, int(total_bytes)),
|
|
640
|
-
"bytes_per_second": 0,
|
|
641
|
-
"source": "huggingface",
|
|
642
|
-
"connections": 0,
|
|
643
|
-
"started_at": now,
|
|
644
|
-
"updated_at": now,
|
|
645
|
-
}
|
|
646
|
-
hosts.save_private(job_path(model_id, state_dir), job)
|
|
647
297
|
try:
|
|
648
|
-
|
|
298
|
+
job = STORE.begin_job(model_id, base_dir, files, total_bytes, state_dir=state_dir)
|
|
649
299
|
except (OSError, RuntimeError) as exc:
|
|
650
|
-
clear_job(model_id, state_dir)
|
|
651
300
|
raise SpeechError("INVALID_STATE", f"cannot record speech job process: {exc}") from None
|
|
301
|
+
if job is None:
|
|
302
|
+
raise SpeechError("JOB_RUNNING", f"{model_id} already has a running download")
|
|
652
303
|
return job
|
|
653
304
|
|
|
654
305
|
|
|
655
306
|
@contextmanager
|
|
656
307
|
def held_job(claims: list[SpeechJob], state_dir: Path | None = None) -> Iterator[None]:
|
|
657
308
|
"""Release claimed speech jobs on exit unless already cleared or replaced."""
|
|
658
|
-
|
|
309
|
+
with STORE.held_job(claims, state_dir):
|
|
659
310
|
yield
|
|
660
|
-
finally:
|
|
661
|
-
with locked(state_dir):
|
|
662
|
-
for job in claims:
|
|
663
|
-
if current_job(job, state_dir):
|
|
664
|
-
clear_job(job["model"], state_dir)
|
|
665
311
|
|
|
666
312
|
|
|
667
313
|
def stop_job_process(saved: process_identity.Identity, grace: float = 20.0) -> str:
|
|
@@ -669,17 +315,15 @@ def stop_job_process(saved: process_identity.Identity, grace: float = 20.0) -> s
|
|
|
669
315
|
return library_state.stop_process(saved, grace=grace)
|
|
670
316
|
|
|
671
317
|
|
|
672
|
-
class SpeechJobHandle:
|
|
318
|
+
class SpeechJobHandle(SidecarJobHandle):
|
|
673
319
|
"""Throttled disk progress writer so ``speech status`` observes live downloads."""
|
|
674
320
|
|
|
675
321
|
def __init__(self, job: SpeechJob, state_dir: Path | None = None) -> None:
|
|
676
|
-
|
|
677
|
-
self.state_dir = state_dir
|
|
678
|
-
self._written = 0.0
|
|
322
|
+
super().__init__(STORE, job, state_dir)
|
|
679
323
|
|
|
680
324
|
def update(
|
|
681
325
|
self,
|
|
682
|
-
step: DownloadStep,
|
|
326
|
+
step: DownloadStep | str,
|
|
683
327
|
done_bytes: int,
|
|
684
328
|
total_bytes: int,
|
|
685
329
|
bytes_per_second: int = 0,
|
|
@@ -687,22 +331,11 @@ class SpeechJobHandle:
|
|
|
687
331
|
source: str = "huggingface",
|
|
688
332
|
connections: int = 0,
|
|
689
333
|
) -> None:
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
334
|
+
super().update(
|
|
335
|
+
step,
|
|
336
|
+
done_bytes,
|
|
337
|
+
total_bytes,
|
|
338
|
+
bytes_per_second,
|
|
339
|
+
source=source,
|
|
340
|
+
connections=connections,
|
|
696
341
|
)
|
|
697
|
-
self.job["step"] = step
|
|
698
|
-
self.job["done_bytes"] = done
|
|
699
|
-
self.job["total_bytes"] = total
|
|
700
|
-
self.job["bytes_per_second"] = max(0, int(bytes_per_second))
|
|
701
|
-
self.job["source"] = source or "custom"
|
|
702
|
-
self.job["connections"] = max(0, int(connections))
|
|
703
|
-
now = time.monotonic()
|
|
704
|
-
if changed or now - self._written >= JOB_WRITE_EVERY_S or done >= total:
|
|
705
|
-
self._written = now
|
|
706
|
-
self.job["updated_at"] = utc_now()
|
|
707
|
-
with contextlib.suppress(OSError):
|
|
708
|
-
hosts.save_private(job_path(self.job["model"], self.state_dir), self.job)
|