aboutsummaryrefslogtreecommitdiff
path: root/app/backends/audiocpp
diff options
context:
space:
mode:
Diffstat (limited to 'app/backends/audiocpp')
-rw-r--r--app/backends/audiocpp/__init__.py4
-rw-r--r--app/backends/audiocpp/build.py24
-rw-r--r--app/backends/audiocpp/prebuilt.py167
-rw-r--r--app/backends/audiocpp/wizard.py96
4 files changed, 212 insertions, 79 deletions
diff --git a/app/backends/audiocpp/__init__.py b/app/backends/audiocpp/__init__.py
index 2bb9cc7..d65c36b 100644
--- a/app/backends/audiocpp/__init__.py
+++ b/app/backends/audiocpp/__init__.py
@@ -72,7 +72,9 @@ from .prebuilt import (
fetch_latest_release,
install_prebuilt,
installed_release,
+ resolve_latest_tag,
select_assets,
+ synthesize_assets,
)
from .remote import fetch_server_models, fetch_server_voices
from .wizard import (
@@ -108,7 +110,7 @@ __all__ = [
"apply_ggml_patches", "build_audiocpp", "uninstall", "update",
# prebuilt
"fetch_latest_release", "install_prebuilt", "installed_release",
- "select_assets",
+ "select_assets", "resolve_latest_tag", "synthesize_assets",
# remote
"fetch_server_models", "fetch_server_voices",
# wizard / status
diff --git a/app/backends/audiocpp/build.py b/app/backends/audiocpp/build.py
index f840193..e3536cf 100644
--- a/app/backends/audiocpp/build.py
+++ b/app/backends/audiocpp/build.py
@@ -138,22 +138,26 @@ def _update_prebuilt(checkout: Path, backend: Optional[str], *,
"""The update route for a prebuilt (release-downloaded) install.
Source builds update by rebuilding after a git pull; a prebuilt
- install instead re-downloads when upstream published a newer release
- (the marker's tag is compared against the latest release tag). The
- recorded/detected backend selects the asset, mirroring what was
- originally installed. A GitHub outage is not fatal — the installed
- binary keeps working. Returns the install exit code (0 when already
- current or when the check could not run).
+ install instead re-downloads when upstream published a newer release.
+ The current-version check resolves the newest tag from the release
+ page's redirect (no API quota); the API is only consulted by the
+ re-download itself, for the checksum digests. The recorded/detected
+ backend selects the asset, mirroring what was originally installed.
+ A GitHub outage is not fatal — the installed binary keeps working.
+ Returns the install exit code (0 when already current or when the
+ check could not run).
"""
marker = _prebuilt.installed_release(checkout, backend)
- release = _prebuilt.fetch_latest_release()
- if release is None:
+ tag = _prebuilt.resolve_latest_tag()
+ if tag is None:
+ release = _prebuilt.fetch_latest_release()
+ tag = str(release.get("tag_name") or "") if release else ""
+ if not tag:
print("[WARNING] Could not check GitHub for a newer prebuilt "
"audiocpp_server; keeping the installed one ("
f"{marker.get('tag') if marker else 'unknown'}).")
return 0
- tag = str(release.get("tag_name") or "")
- if marker and tag and tag == marker.get("tag"):
+ if marker and tag == marker.get("tag"):
print(f"[OK] Prebuilt audiocpp_server is current ({tag}).")
return 0
print(f"[INFO] audio.cpp {tag} is available; downloading the prebuilt "
diff --git a/app/backends/audiocpp/prebuilt.py b/app/backends/audiocpp/prebuilt.py
index 28d60b5..13edbc0 100644
--- a/app/backends/audiocpp/prebuilt.py
+++ b/app/backends/audiocpp/prebuilt.py
@@ -24,8 +24,11 @@ a newer release instead of rebuilding from source.
Everything downloads through urllib (no new dependencies); the GitHub API
publishes a ``sha256:<hex>`` digest per asset, which is verified before
-extraction. Files saved by urllib carry no macOS quarantine attribute, so
-the ad-hoc-signed binaries run without Gatekeeper prompts.
+extraction. When the API is rate-limited, the release tag is resolved from
+the release page's redirect instead (no API quota) and the download
+proceeds without verification (with a warning). Files saved by urllib
+carry no macOS quarantine attribute, so the ad-hoc-signed binaries run
+without Gatekeeper prompts.
"""
import hashlib
@@ -49,6 +52,13 @@ from backends import common
RELEASES_API_URL = \
"https://api.github.com/repos/0xShug0/audio.cpp/releases/latest"
+# The release pages (NOT the API): following /releases/latest's redirect
+# reveals the newest tag without spending any of the API's rate limit
+# (60 requests/hour unauthenticated, shared per IP), and the asset
+# download URLs are deterministic from the tag.
+RELEASES_LATEST_URL = "https://github.com/0xShug0/audio.cpp/releases/latest"
+RELEASES_DOWNLOAD_URL = "https://github.com/0xShug0/audio.cpp/releases/download"
+
# GitHub rejects API/HTTP requests without a User-Agent header.
USER_AGENT = "tts-audiobook-generator (+https://github.com/0xShug0/audio.cpp)"
@@ -68,6 +78,8 @@ _ASSET_RE = re.compile(
_CUDART_RE = re.compile(
r"^audio-v[^/]+-cudart-windows-x64-cuda(?P<variant>[a-z0-9.]+)\.zip$")
+_TAG_RE = re.compile(r"/releases/tag/(v[^/?#]+)")
+
# Download progress reporting: emit a line at most every 4 MiB.
_REPORT_STEP = 4 * 1024 * 1024
@@ -206,6 +218,52 @@ def fetch_latest_release(*, timeout: int = API_TIMEOUT) -> Optional[dict]:
return None
+def resolve_latest_tag(*, timeout: int = API_TIMEOUT) -> Optional[str]:
+ """The latest release tag, resolved WITHOUT the GitHub API.
+
+ ``/releases/latest`` redirects to ``/releases/tag/vX.Y.Z``; reading
+ the final URL is an ordinary page hit that costs none of the API's
+ rate limit, so "already current" checks and rate-limited installs
+ never depend on the API. Returns None when the redirect cannot be
+ followed (offline, unexpected page shape).
+ """
+ request = urllib.request.Request(RELEASES_LATEST_URL,
+ headers={"User-Agent": USER_AGENT})
+ try:
+ with urllib.request.urlopen(request, timeout=timeout) as response:
+ url = response.geturl() or ""
+ except OSError:
+ return None
+ match = _TAG_RE.search(url)
+ return match.group(1) if match else None
+
+
+def _variant_tokens(backend: Optional[str], *,
+ platform: Optional[str] = None,
+ machine: Optional[str] = None,
+ cuda_variant: Optional[str] = None
+ ) -> Optional[Tuple[str, str, str, Optional[str]]]:
+ """The (platform, arch, variant, cudart) tokens for this machine.
+
+ Returns None when this platform/machine/backend combination has no
+ release asset naming (see ``prebuilt_supported``). The binary asset's
+ variant token carries a "cuda" prefix for CUDA builds
+ (``...-bin-windows-x64-cuda12.4.zip``) while the cudart archive uses
+ the bare version (``...-cudart-...-cuda12.4.zip``).
+ """
+ token = _platform_token(platform)
+ arch = _arch_token(machine)
+ if token is None or arch is None or not prebuilt_supported(
+ backend, platform=platform):
+ return None
+ if token == "macos":
+ return token, arch, "metal", None
+ if backend == "cuda":
+ cuda = cuda_variant or _default_cuda_variant()
+ return token, arch, "cuda" + cuda, cuda
+ return token, arch, backend, None
+
+
def select_assets(assets: List[dict], backend: Optional[str], *,
platform: Optional[str] = None,
machine: Optional[str] = None,
@@ -218,23 +276,11 @@ def select_assets(assets: List[dict], backend: Optional[str], *,
matches (unsupported platform, HIP, an unexpected arch). The CUDA
variant defaults to the driver-appropriate one (``_default_cuda_variant``).
"""
- token = _platform_token(platform)
- arch = _arch_token(machine)
- if token is None or arch is None or not prebuilt_supported(
- backend, platform=platform):
+ tokens = _variant_tokens(backend, platform=platform, machine=machine,
+ cuda_variant=cuda_variant)
+ if tokens is None:
return None
- variant: Optional[str]
- cuda: Optional[str] = None
- if token == "macos":
- variant = "metal"
- elif backend == "cuda":
- # The binary asset names carry a "cuda" prefix on the variant
- # token (``...-bin-windows-x64-cuda12.4.zip``); the cudart asset
- # names use the bare version (``...-cudart-...-cuda12.4.zip``).
- cuda = cuda_variant or _default_cuda_variant()
- variant = "cuda" + cuda
- else:
- variant = backend
+ token, arch, variant, cuda = tokens
main = _find_asset(assets, token, arch, variant)
if main is None:
return None
@@ -246,6 +292,38 @@ def select_assets(assets: List[dict], backend: Optional[str], *,
return main, extra
+def synthesize_assets(backend: Optional[str], tag: str, *,
+ platform: Optional[str] = None,
+ machine: Optional[str] = None,
+ cuda_variant: Optional[str] = None
+ ) -> Optional[Tuple[dict, Optional[dict]]]:
+ """Asset entries built from the tag alone, without the GitHub API.
+
+ The rate-limited fallback of ``install_prebuilt``: asset names and
+ ``releases/download/<tag>/`` URLs are deterministic, but the sha256
+ digests are not — callers download these WITHOUT checksum
+ verification and must say so. Same (main, extra) contract as
+ ``select_assets``.
+ """
+ tokens = _variant_tokens(backend, platform=platform, machine=machine,
+ cuda_variant=cuda_variant)
+ if tokens is None:
+ return None
+ token, arch, variant, cuda = tokens
+
+ def asset(name: str) -> dict:
+ return {"name": name, "size": None, "digest": None,
+ "browser_download_url":
+ f"{RELEASES_DOWNLOAD_URL}/{tag}/{name}"}
+
+ ext = ".tar.gz" if token == "macos" else ".zip"
+ main = asset(f"audio-{tag}-bin-{token}-{arch}-{variant}{ext}")
+ extra = None
+ if token == "windows" and backend == "cuda":
+ extra = asset(f"audio-{tag}-cudart-windows-{arch}-cuda{cuda}.zip")
+ return main, extra
+
+
def _find_asset(assets: List[dict], platform_token: str, arch: str,
variant: str) -> Optional[dict]:
for asset in assets:
@@ -426,13 +504,19 @@ def install_prebuilt(audiocpp_dir: Path, backend: Optional[str], *,
cuda_variant: Optional[str] = None) -> int:
"""Download and install the latest prebuilt audiocpp_server for BACKEND.
- Fetches the latest release, picks this machine's asset(s), downloads
- them to a temp directory, verifies the published sha256 digests, then
- extracts into ``build/<dir>/bin/`` (replacing any previous content —
- including a source build there), syncs the checkout to the release
- tag, and writes the ``prebuilt.json`` marker. Streaming output and
- cancellation behave like the other install steps. Returns 0 on
- success, 130 when cancelled, 1 on any failure.
+ Resolution degrades gracefully when GitHub's API is unavailable (its
+ unauthenticated limit is 60 requests/hour per IP): normally the API
+ supplies the release assets and their sha256 digests; when it is
+ rate-limited the newest tag is resolved from the release page's
+ redirect (no API quota), the asset names are synthesized, and the
+ download proceeds WITHOUT checksum verification (with a loud
+ warning). Only when neither path can resolve the release does the
+ install fail. The archive(s) are downloaded to a temp directory,
+ verified, then extracted into ``build/<dir>/bin/`` (replacing any
+ previous content — including a source build there), the checkout is
+ synced to the release tag, and the ``prebuilt.json`` marker written.
+ Streaming output and cancellation behave like the other install
+ steps. Returns 0 on success, 130 when cancelled, 1 on any failure.
"""
say = emit if emit is not None else print
bin_dir = install_dir(audiocpp_dir, backend)
@@ -442,16 +526,35 @@ def install_prebuilt(audiocpp_dir: Path, backend: Optional[str], *,
return 1
say("[INFO] Checking the latest audio.cpp release...")
release = fetch_latest_release()
- if release is None:
- return 1
- tag = str(release.get("tag_name") or "?")
pair = select_assets(release.get("assets") or [], backend,
- cuda_variant=cuda_variant)
- if pair is None:
+ cuda_variant=cuda_variant) if release else None
+ if release is not None and pair is None:
+ # API reachable but nothing matches: a hard mismatch (unsupported
+ # backend, renamed assets) — do not paper over it with a
+ # synthesized name that would just 404.
print(f"[ERROR] No prebuilt audiocpp_server asset for "
- f"{backend!r} in release {tag}; build from source instead.")
+ f"{backend!r} in release {release.get('tag_name')}; build "
+ "from source instead.")
return 1
- main, extra = pair
+ if pair is not None:
+ tag = str(release.get("tag_name") or "?")
+ main, extra = pair
+ else:
+ tag = resolve_latest_tag()
+ if tag is None:
+ print("[ERROR] Could not reach GitHub to resolve the latest "
+ "audio.cpp release (rate limit or offline); try again "
+ "later or build from source.")
+ return 1
+ pair = synthesize_assets(backend, tag, cuda_variant=cuda_variant)
+ if pair is None:
+ print(f"[ERROR] No prebuilt audiocpp_server asset exists for "
+ f"{backend!r} in release {tag}; build from source "
+ "instead.")
+ return 1
+ main, extra = pair
+ say("[WARNING] GitHub's release API is unreachable (rate limit?) "
+ f"— installing {tag} WITHOUT checksum verification.")
existing = installed_release(audiocpp_dir, backend)
if existing and existing.get("tag") == tag \
and existing.get("asset") == main.get("name"):
diff --git a/app/backends/audiocpp/wizard.py b/app/backends/audiocpp/wizard.py
index 5b237c4..f35aac1 100644
--- a/app/backends/audiocpp/wizard.py
+++ b/app/backends/audiocpp/wizard.py
@@ -303,6 +303,7 @@ def _wizard(stdscr, args: argparse.Namespace, parser: argparse.ArgumentParser
"backend": backend,
"build": build,
"build_mode": s.get("build_mode"),
+ "prebuilt_forced": False,
"lazy_load": True,
"sync_port": None,
"wav_dir": s["wav_dir"],
@@ -621,39 +622,44 @@ def _execute_lanes(settings: dict,
if build:
mode = settings.get("build_mode") or "source"
+ forced = bool(settings.get("prebuilt_forced"))
+
+ def source_build_step(emit, cancel) -> int:
+ """Build from source, warning-and-continue like the setup."""
+ rc = _build.build_audiocpp(audiocpp_dir, settings["backend"],
+ emit=emit, cancel=cancel)
+ if rc == 124:
+ print("[WARNING] build went silent and was stopped; the "
+ "server.json was still written — build "
+ "audiocpp_server manually before starting it")
+ elif rc != 0:
+ print(f"[WARNING] build exited with code {rc}; the "
+ "server.json was still written — build "
+ "audiocpp_server manually before starting it")
+ else:
+ print("[OK] build complete")
+ return rc
+
if mode == "prebuilt":
def build_step(emit, cancel):
rc = _prebuilt.install_prebuilt(
audiocpp_dir, settings["backend"],
emit=emit, cancel=cancel)
- if rc == 130:
- return rc
- if rc != 0:
- print(f"[WARNING] prebuilt download failed (exit {rc}); "
- "the server.json was still written — build "
- "audiocpp_server from source ('Build audio.cpp "
- "Server', or re-run with --prebuilt no) before "
- "starting it")
- else:
+ if rc == 0:
print("[OK] prebuilt audiocpp_server installed")
- return rc
- build_title = (f"Download prebuilt audiocpp_server "
- f"({settings['backend']})")
+ return 0
+ if rc == 130 or forced:
+ # A cancelled download, or one the user explicitly
+ # forced with --prebuilt yes: fail fast.
+ return rc
+ # The usual failure is GitHub's API rate limit — recover
+ # in place instead of leaving the setup half-installed.
+ print(f"[WARNING] prebuilt download failed (exit {rc}); "
+ "falling back to a source build...")
+ return source_build_step(emit, cancel)
+ build_title = f"Install audiocpp_server ({settings['backend']})"
else:
- def build_step(emit, cancel):
- rc = _build.build_audiocpp(audiocpp_dir, settings["backend"],
- emit=emit, cancel=cancel)
- if rc == 124:
- print("[WARNING] build went silent and was stopped; the "
- "server.json was still written — build "
- "audiocpp_server manually before starting it")
- elif rc != 0:
- print(f"[WARNING] build exited with code {rc}; the "
- "server.json was still written — build "
- "audiocpp_server manually before starting it")
- else:
- print("[OK] build complete")
- return rc
+ build_step = source_build_step
build_title = f"Build audiocpp_server ({settings['backend']})"
lanes.append(taskview.TaskLane(
"Build",
@@ -831,14 +837,25 @@ def build_screen(stdscr) -> int:
if mode is _GO_BACK:
return 1
+ outcome: dict = {"prebuilt": False}
+
def build_step(emit, cancel):
if mode == "prebuilt":
- return _prebuilt.install_prebuilt(checkout, backend,
- emit=emit, cancel=cancel)
+ rc = _prebuilt.install_prebuilt(checkout, backend,
+ emit=emit, cancel=cancel)
+ if rc == 0:
+ outcome["prebuilt"] = True
+ return 0
+ if rc == 130:
+ return rc
+ # The usual failure is GitHub's API rate limit — recover in
+ # place instead of sending the user back to the menus.
+ print(f"[WARNING] prebuilt download failed (exit {rc}); "
+ "falling back to a source build...")
return _build.build_audiocpp(checkout, backend, emit=emit,
cancel=cancel)
- action = "Download prebuilt audiocpp_server" if mode == "prebuilt" \
+ action = f"Install audiocpp_server ({backend})" if mode == "prebuilt" \
else f"Build audiocpp_server ({backend})"
lanes = [taskview.TaskLane("Build", [taskview.TaskStep(action,
build_step)])]
@@ -858,15 +875,15 @@ def build_screen(stdscr) -> int:
"Download models",
[taskview.TaskStep("Download missing models", download_step)]))
- install_title = ("Download prebuilt audiocpp_server"
- if mode == "prebuilt" else "Build audiocpp_server")
+ install_title = ("Install audiocpp_server" if mode == "prebuilt"
+ else "Build audiocpp_server")
title = f"{install_title} & download models" if len(lanes) == 2 \
else install_title
rc = taskview.run_lanes(stdscr, title, lanes)
if rc != 0:
return rc
if not _configsync.update_server_backend(backend):
- verb = "installed" if mode == "prebuilt" else "built"
+ verb = "installed" if outcome["prebuilt"] else "built"
tui.flash(stdscr, f"audiocpp_server {verb} for {backend}. (Could not "
"update server.json's backend field — reconfigure audio.cpp "
"if it was already configured.)", "warn")
@@ -995,8 +1012,12 @@ def _collect_from_flags(args: argparse.Namespace,
build = False
# How a pending install happens: the prebuilt release by default on
# macOS/Windows (``--prebuilt no`` forces the source build), always
- # the source build elsewhere.
+ # the source build elsewhere. A ``--prebuilt yes`` download that
+ # fails stays a failure (no source-build fallback) — the flag's
+ # whole point is "do not build".
build_mode = _flag_build_mode(args, backend) if build else None
+ prebuilt_forced = bool(build) and getattr(args, "prebuilt", "auto") \
+ == "yes"
port = _configsync.config_port()
lazy_load = True
@@ -1039,6 +1060,7 @@ def _collect_from_flags(args: argparse.Namespace,
"backend": backend,
"build": build,
"build_mode": build_mode,
+ "prebuilt_forced": prebuilt_forced,
"lazy_load": lazy_load,
"sync_port": None,
"wav_dir": wav_dir,
@@ -1088,9 +1110,11 @@ def build_parser() -> argparse.ArgumentParser:
default="auto",
help="How to install audiocpp_server when it is "
"missing: auto downloads the prebuilt release "
- "on macOS/Windows and builds from source "
- "elsewhere; yes forces the prebuilt download; "
- "no forces a source build")
+ "on macOS/Windows (falling back to a source "
+ "build if the download fails) and builds from "
+ "source elsewhere; yes forces the prebuilt "
+ "download and fails if it cannot; no forces a "
+ "source build")
parser.add_argument("--whisper-model", type=str, default="base",
help="Whisper model size for transcription "
"(default: base)")