Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: MCP audio reports what it applied, explains audio errors, checks takes and cancels by id (#14080)
Etherl82523339bdb5
diff --git a/studio/MCP.md b/studio/MCP.mdindex 27aa34442d2..5dab0708b61 100644--- a/studio/MCP.md+++ b/studio/MCP.md@@ -275,7 +275,8 @@ api_key_format = "Bearer {token}"| `system_one` | Ask the Decision API typed questions. The Decision API must be on. || `generate_image` | Text to image, image to image, inpaint, edit, reference, outpaint and upscale, one image or a batch of seeds or prompts. Left out, steps and guidance follow the loaded model. || `generate_audio` | Clone, speak, edit, convert, music and separate, on the loaded model or a named `model`. |-| `transcribe` | Speech to text, or translation to English. |+| `transcribe` | Speech to text, or translation to English; `speakers` labels who speaks, and `expected_text` adds a match score. |+| `analyze_audio` | A clip's duration, loudness, true peak, clipping and speech span, plus a match score against `expected_text`. || `generate_video` | Start a video, with a seed, frame count, last frame and image or audio references. One renders at a time. Poll it with `get_job`. || `get_job` | The state of a video, recipe or export job. || `run_recipe` | Validate, preview or run a Data Recipe. |diff --git a/studio/backend/core/inference/audio_cpp_backend.py b/studio/backend/core/inference/audio_cpp_backend.pyindex 9858a9229a4..c542bfc0127 100644--- a/studio/backend/core/inference/audio_cpp_backend.py+++ b/studio/backend/core/inference/audio_cpp_backend.py@@ -72,6 +72,24 @@ def _raise_if_cancelled(cancel_event) -> None:raise AudioGenerationCancelledError("Audio generation cancelled")+def _runtime_stopped(exc: BaseException) -> BaseException:+ """A runtime that died or went silent mid-request, as a 500 with code runtime_stopped."""+ from core.inference.audio_cpp_failures import AudioCppFailure+ from core.inference.audio_errors import AudioRuntimeError, sanitize_runtime_detail++ reason = sanitize_runtime_detail(str(exc)).removeprefix("The audio runtime ")+ # "stopped while serving the request. Last output: ..." -> "it quit while serving ..."+ if reason.startswith("stopped "):+ reason = "it quit " + reason.removeprefix("stopped ")+ reason = reason.rstrip(".").strip() or "no reason given"+ message = (+ f"The audio runtime stopped: {reason}. Try again; if it fails again, load the model again."+ )+ return attach_failure(+ AudioRuntimeError(message, status = 500), AudioCppFailure("runtime_stopped", message, 500)+ )++_inflight_lock = threading.Lock()_inflight: dict[tuple[str, str, str], tuple[threading.Event, list]] = {}@@ -813,6 +831,10 @@ class AudioCppBackend:raiseexcept AudioCppRequestError as exc:raise self._runtime_error("generate audio", exc) from exc+ except AudioCppUnavailableError as exc:+ if failure_of(exc) is not None: # already named (out of memory, ...)+ raise+ raise _runtime_stopped(exc) from exc_raise_if_cancelled(cancel_event)return wav, _wav_sample_rate(wav)@@ -889,6 +911,10 @@ class AudioCppBackend:raiseexcept AudioCppRequestError as exc:raise self._runtime_error("separate the track", exc) from exc+ except AudioCppUnavailableError as exc:+ if failure_of(exc) is not None: # already named (out of memory, ...)+ raise+ raise _runtime_stopped(exc) from excexcept SeparationOutputError as exc:logger.warning("audio.cpp: %s answered without decodable stems: %s", model.id, exc)return []@@ -1254,6 +1280,10 @@ class AudioCppBackend:raiseexcept AudioCppRequestError as exc:raise self._runtime_error("generate audio", exc) from exc+ except AudioCppUnavailableError as exc:+ if failure_of(exc) is not None: # already named (out of memory, ...)+ raise+ raise _runtime_stopped(exc) from exc_raise_if_cancelled(cancel_event)if output_dir:_write_outputs(output_dir, outputs)diff --git a/studio/backend/core/inference/audio_cpp_failures.py b/studio/backend/core/inference/audio_cpp_failures.pyindex 89e7531672b..39c93a92ed9 100644--- a/studio/backend/core/inference/audio_cpp_failures.py+++ b/studio/backend/core/inference/audio_cpp_failures.py@@ -48,7 +48,7 @@ _PATTERNS = tuple(),))-_CODES = frozenset(code for code, _ in _PATTERNS)+_CODES = frozenset(code for code, _ in _PATTERNS) | {"runtime_stopped"}_STATUS = {"file_incomplete": 422} # else 503# Messages that already come from Unsloth Studio, so no runtime line is appended.diff --git a/studio/backend/core/inference/audio_edit.py b/studio/backend/core/inference/audio_edit.pyindex f7220cbe4e1..f2a565a8ea1 100644--- a/studio/backend/core/inference/audio_edit.py+++ b/studio/backend/core/inference/audio_edit.py@@ -207,10 +207,14 @@ def wav_seconds(path: Path) -> Optional[float]:def save_source_clip(- source, prepared_path: Path, reference_text: Optional[str]+ source,+ prepared_path: Path,+ reference_text: Optional[str],+ created: Optional[list[str]] = None,) -> Optional[dict[str, Any]]:"""Keep an uploaded recording in history beside its edit, so A/B outlives the upload. Reuses a- source clip of the same audio; None for a history clip or an upload with no record."""+ source clip of the same audio; None for a history clip or an upload with no record. The id of+ a clip it had to save is appended to ``created``."""import wavefrom core.inference import audio_gallery, audio_inputs@@ -250,4 +254,7 @@ def save_source_clip("duration_s": duration_s,"created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),}- return audio_gallery.save(Path(prepared_path).read_bytes(), meta)+ record = audio_gallery.save(Path(prepared_path).read_bytes(), meta)+ if record is not None and created is not None:+ created.append(record["id"])+ return recorddiff --git a/studio/backend/core/inference/audio_gallery.py b/studio/backend/core/inference/audio_gallery.pyindex 8c58ad1384b..0dfee14a67e 100644--- a/studio/backend/core/inference/audio_gallery.py+++ b/studio/backend/core/inference/audio_gallery.py@@ -508,8 +508,9 @@ def move(audio_id: str, after_id: Optional[str]) -> Optional[dict[str, Any]]:return _record(audio_id, meta)-def delete(audio_id: str) -> bool:- """Remove both files of an owned pair; True if the WAV existed and was ours.+def delete(audio_id: str, *, cascade: bool = True) -> bool:+ """Remove both files of an owned pair; True if the WAV existed and was ours. ``cascade=False``+ keeps the hidden Original even when no other clip plays it.WAV first: sidecar-first then failing would lose a clip with no retry, while thisleaves at worst an orphan sidecar list_audio ignores."""@@ -534,7 +535,8 @@ def delete(audio_id: str) -> bool:# An Edit's hidden Original goes with the last clip that plays it.original = str(meta.get("source_clip_id") or "")if (- _ID_RE.match(original)+ cascade+ and _ID_RE.match(original)and (_read_meta(_sidecar_path(original)) or {}).get("role") == "source"and original not in _sources_in_use(gallery_dir(), set())):diff --git a/studio/backend/core/inference/audio_metrics.py b/studio/backend/core/inference/audio_metrics.pynew file mode 100644index 00000000000..145b5246df1--- /dev/null+++ b/studio/backend/core/inference/audio_metrics.py@@ -0,0 +1,428 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Measure a take: duration, integrated loudness (ITU-R BS.1770-4), true peak, clipping and the span that holds sound, plus a word match against the text it should say. numpy only, so no new dependency."""++from __future__ import annotations++import functools+import math+import re+import struct+import unicodedata+import wave+from pathlib import Path+from typing import Any, Optional++import numpy as np++# BS.1770 gating: 400 ms blocks with 75% overlap, an absolute gate at -70 LUFS and a relative gate 10 LU below the ungated mean.+_BLOCK_S = 0.4+_HOP_S = 0.1+_ABSOLUTE_GATE = -70.0+_RELATIVE_GATE = 10.0+# The K-weighting high-pass rings for well under a second; past this many seconds its impulse response is below float precision.+_IR_SECONDS = 1.0+# Every measure walks the audio this many frames at a time, so a 10-minute take never needs+# full-length float64 copies.+_CHUNK = 1 << 18+# Clipping: at least 3 identical samples in a row at full scale, reached or left by a jump a smooth+# peak could not make. PCM rounds a clean peak, even a low tone's, to a flat top of L samples, and the+# curve drops off it by at most about ((L + 1) / (L - 1))**2 quantization steps (3 steps at L = 3,+# 1 from L = 5 in a sweep of clean tones); a clipped waveform hits the rail at speed. The step used is+# at most _CLIP_STEP_MAX, so coarse 8-bit steps (1/128) cannot hide an overdriven tone; 8-bit is too+# coarse to tell a clean low tone's flat top from clipping. 0.99 lets the 8-bit positive rail+# (127/128) count.+_CLIP_LEVEL = 0.99+_CLIP_RUN = 3+_CLIP_EDGE = 2+_CLIP_STEP_MAX = 2.0**-13+# Sound span: 20 ms frames louder than 40 dB under the loudest frame, never below -60 dBFS.+_FRAME_S = 0.02+_SPAN_RANGE_DB = 40.0+_SPAN_FLOOR_DB = -60.0+++def read_wav(path: Path) -> tuple[np.ndarray, int]:+ """(frames x channels float32, sample rate) for an 8/16/24/32-bit PCM or 32/64-bit float WAV."""+ data, rate, _step = _read_pcm(path)+ return data, rate+++def _float_layout(path: Path) -> Optional[tuple[int, int, str, int, int]]:+ """(rate, channels, dtype, data offset, frames) for an IEEE float WAV, else None.++ ``wave`` reads PCM only; decoding a float WAV to 16-bit PCM clamps every sample past full+ scale, which would invent clipping and hide the true peak."""+ size = path.stat().st_size+ with open(path, "rb") as f:+ head = f.read(12)+ if len(head) < 12 or head[:4] != b"RIFF" or head[8:12] != b"WAVE":+ return None+ fmt = None+ while True:+ chunk = f.read(8)+ if len(chunk) < 8:+ return None+ name, length = chunk[:4], struct.unpack("<I", chunk[4:])[0]+ if name == b"fmt ":+ body = f.read(length)+ if len(body) < 16:+ return None+ tag, channels, rate, _byte_rate, _align, bits = struct.unpack("<HHIIHH", body[:16])+ if tag == 0xFFFE and len(body) >= 26:+ tag = struct.unpack("<H", body[24:26])[0]+ if tag != 3 or bits not in (32, 64) or not channels or not rate:+ return None+ fmt = (rate, channels, "<f4" if bits == 32 else "<f8")+ f.seek(length % 2, 1)+ elif name == b"data":+ if fmt is None:+ return None+ offset = f.tell()+ # A streamed writer leaves the size at 0 or 0xFFFFFFFF; the file length is the truth.+ if length == 0 or length > size - offset:+ length = size - offset+ width = np.dtype(fmt[2]).itemsize * fmt[1]+ return fmt[0], fmt[1], fmt[2], offset, length // width+ else:+ f.seek(length + (length % 2), 1)+++def wav_seconds(path: Path) -> float:+ """Duration of a PCM or IEEE float WAV; raises wave.Error, EOFError or ValueError otherwise."""+ layout = _float_layout(path)+ if layout is not None:+ return layout[4] / layout[0]+ with wave.open(str(path)) as w:+ rate = w.getframerate()+ return w.getnframes() / rate if rate else 0.0+++def _read_pcm(path: Path) -> tuple[np.ndarray, int, float]:+ """``read_wav`` plus the size of one quantization step (0 for float samples)."""+ layout = _float_layout(path)+ if layout is not None:+ rate, channels, dtype, offset, frames = layout+ data = np.fromfile(path, dtype = dtype, count = frames * channels, offset = offset)+ return data.astype(np.float32).reshape(-1, channels), rate, 0.0+ with wave.open(str(path)) as w:+ rate = w.getframerate()+ channels = w.getnchannels()+ width = w.getsampwidth()+ raw = w.readframes(w.getnframes())+ if width == 1:+ data = (np.frombuffer(raw, dtype = np.uint8).astype(np.float32) - 128.0) / 128.0+ elif width == 2:+ data = np.frombuffer(raw, dtype = "<i2").astype(np.float32) / 32768.0+ elif width == 3:+ bytes3 = np.frombuffer(raw, dtype = np.uint8).reshape(-1, 3)+ ints = (+ bytes3[:, 0].astype(np.int32)+ | (bytes3[:, 1].astype(np.int32) << 8)+ | (bytes3[:, 2].astype(np.int32) << 16)+ )+ ints = np.where(ints >= 1 << 23, ints - (1 << 24), ints)+ data = ints.astype(np.float32) / float(1 << 23)+ elif width == 4:+ data = np.frombuffer(raw, dtype = "<i4").astype(np.float32) / float(1 << 31)+ else:+ raise ValueError(f"Unsupported WAV sample width: {width * 8} bits")+ # float32 holds samples near full scale 2**-24 apart, coarser than 32-bit PCM's own step.+ return data.reshape(-1, max(channels, 1)), rate, max(2.0 ** (1 - 8 * width), 2.0**-24)+++def _biquad_coefficients(rate: int) -> list[tuple[np.ndarray, np.ndarray]]:+ """The two K-weighting stages (high shelf, then RLB high-pass) at any rate, from the BS.1770 analog prototypes."""+ stages = []+ # Stage 1: high shelf, +4 dB above about 1.7 kHz.+ f0, gain_db, q = 1681.974450955533, 3.999843853973347, 0.7071752369554196+ k = math.tan(math.pi * f0 / rate)+ vh = 10.0 ** (gain_db / 20.0)+ vb = vh**0.4996667741545416+ a0 = 1.0 + k / q + k * k+ b = np.array(+ [(vh + vb * k / q + k * k) / a0, 2.0 * (k * k - vh) / a0, (vh - vb * k / q + k * k) / a0]+ )+ a = np.array([1.0, 2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0])+ stages.append((b, a))+ # Stage 2: second-order high-pass at about 38 Hz.+ f0, q = 38.13547087602444, 0.5003270373238773+ k = math.tan(math.pi * f0 / rate)+ a0 = 1.0 + k / q + k * k+ b = np.array([1.0, -2.0, 1.0])+ a = np.array([1.0, 2.0 * (k * k - 1.0) / a0, (1.0 - k / q + k * k) / a0])+ stages.append((b, a))+ return stages++[email protected]_cache(maxsize = 8)+def _impulse_response(rate: int) -> np.ndarray:+ """The K-weighting filter's impulse response, truncated where it has died out. Cached per rate:+ the filter runs sample by sample in Python. Callers must not modify it."""+ n = max(int(rate * _IR_SECONDS), 64)+ x = np.zeros(n)+ x[0] = 1.0+ for b, a in _biquad_coefficients(rate):+ y = np.zeros(n)+ x1 = x2 = y1 = y2 = 0.0+ b0, b1, b2 = b+ a1, a2 = a[1], a[2]+ for i in range(n):+ xi = x[i]+ yi = b0 * xi + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2+ x2, x1 = x1, xi+ y2, y1 = y1, yi+ y[i] = yi+ x = y+ return x+++def _fft_convolve(signal: np.ndarray, kernel: np.ndarray):+ """``signal`` (1-D) convolved with ``kernel`` by overlap-add, cut to the signal's length and+ yielded in order, one chunk at a time."""+ m = len(kernel)+ size = 1 << int(math.ceil(math.log2(_CHUNK + m - 1)))+ kernel_f = np.fft.rfft(kernel, size)+ tail = np.zeros(m - 1)+ for start in range(0, len(signal), _CHUNK):+ block = signal[start : start + _CHUNK].astype(np.float64)+ piece = np.fft.irfft(np.fft.rfft(block, size) * kernel_f, size)[: len(block) + m - 1]+ piece[: m - 1] += tail+ yield piece[: len(block)]+ tail = piece[len(block) :]+++def _padded(x: np.ndarray, start: int, stop: int) -> np.ndarray:+ """``x[start:stop]`` as float64, zero where the range runs past either end."""+ out = np.zeros(stop - start)+ lo, hi = max(start, 0), min(stop, len(x))+ if hi > lo:+ out[lo - start : hi - start] = x[lo:hi]+ return out+++def integrated_loudness(data: np.ndarray, rate: int) -> Optional[float]:+ """Gated integrated loudness in LUFS, or None when every block sits under the absolute gate (silence, or shorter than one block)."""+ block = int(round(_BLOCK_S * rate))+ hop = max(int(round(_HOP_S * rate)), 1)+ if data.shape[0] < block or block <= 0:+ return None+ kernel = _impulse_response(rate)+ count = 1 + (data.shape[0] - block) // hop+ starts = np.arange(count) * hop+ # The running sum of squares is only read at block edges, so only those points are kept.+ edges = np.unique(np.concatenate((starts, starts + block)))+ first, last = np.searchsorted(edges, starts), np.searchsorted(edges, starts + block)+ powers = np.zeros(count)+ for channel in range(data.shape[1]):+ sums = np.zeros(edges.size) # sums[i]: energy of the first edges[i] filtered samples+ total, done = 0.0, 0+ for piece in _fft_convolve(data[:, channel], kernel):+ running = np.cumsum(piece * piece) + total+ lo = np.searchsorted(edges, done, side = "right")+ hi = np.searchsorted(edges, done + piece.size, side = "right")+ sums[lo:hi] = running[edges[lo:hi] - done - 1]+ total, done = float(running[-1]), done + piece.size+ # Channel weights are 1.0 for mono, L, R and C; surround layouts are not something audio runs produce.+ powers += (sums[last] - sums[first]) / block+ with np.errstate(divide = "ignore"):+ loudness = -0.691 + 10.0 * np.log10(powers)+ kept = powers[loudness > _ABSOLUTE_GATE]+ if kept.size == 0:+ return None+ relative = -0.691 + 10.0 * math.log10(kept.mean()) - _RELATIVE_GATE+ with np.errstate(divide = "ignore"):+ kept = kept[-0.691 + 10.0 * np.log10(kept) > relative]+ if kept.size == 0:+ return None+ return -0.691 + 10.0 * math.log10(kept.mean())+++def _oversampler(factor: int = 4, taps_per_phase: int = 12) -> np.ndarray:+ """A windowed-sinc interpolation filter, shaped (factor, taps_per_phase) by phase."""+ length = factor * taps_per_phase+ n = np.arange(length) - (length - 1) / 2.0+ taps = np.sinc(n / factor) * np.kaiser(length, 8.0)+ taps *= factor / taps.sum()+ return taps.reshape(taps_per_phase, factor).T+++def true_peak(data: np.ndarray) -> float:+ """The largest absolute sample after 4x oversampling (BS.1770 Annex 2), linear."""+ phases = _oversampler()+ length = phases.shape[1]+ # np.convolve's "same" output i reads input i - before .. i + after.+ after = (length - 1) // 2+ before = length - 1 - after+ peak = 0.0+ for channel in range(data.shape[1]):+ x = data[:, channel]+ for start in range(0, len(x), _CHUNK):+ stop = min(start + _CHUNK, len(x))+ peak = max(peak, float(np.max(np.abs(x[start:stop]))))+ window = _padded(x, start - before, stop + after)+ for taps in phases:+ peak = max(peak, float(np.max(np.abs(np.convolve(window, taps, mode = "valid")))))+ return peak+++def _steep_samples(x: np.ndarray, first: np.ndarray, length: np.ndarray, step: float) -> int:+ """Samples in the runs (start ``first``, ``length`` samples) of ``x`` that are long enough and+ that the waveform reaches or leaves more steeply than a smooth peak could."""+ keep = length >= _CLIP_RUN+ first, length = first[keep], length[keep]+ if not first.size:+ return 0+ last = first + length - 1+ top = x[first].astype(np.float64)+ # The larger jump onto or off the run; -1 at an end of the audio, where there is none, and+ # a run that fills the whole channel counts.+ into = np.where(first > 0, np.abs(top - x[np.maximum(first - 1, 0)]), -1.0)+ out = np.where(last < len(x) - 1, np.abs(top - x[np.minimum(last + 1, len(x) - 1)]), -1.0)+ edge = np.maximum(into, out)+ limit = _CLIP_EDGE * min(step, _CLIP_STEP_MAX) * ((length + 1) / (length - 1)) ** 2+ return int(length[(edge > limit) | (edge < 0)].sum())+++def clipped_samples(data: np.ndarray, step: float = 0.0) -> int:+ """Samples in runs of at least ``_CLIP_RUN`` identical full-scale samples, per channel, that the+ waveform reaches or leaves more steeply than a smooth peak could; ``step`` is the quantization+ step (0 for float audio). Runs are judged a chunk at a time, so memory stays bounded."""+ total = 0+ for channel in range(data.shape[1]):+ x = data[:, channel]+ open_start, open_length = 0, 0 # the run still open at the end of the last chunk+ for start in range(0, len(x), _CHUNK):+ # One sample of overlap, so a run that crosses chunks stays one run.+ lead = 1 if start else 0+ seg = x[start - lead : start + _CHUNK]+ rail = np.abs(seg) >= _CLIP_LEVEL+ same = np.zeros(len(seg), dtype = bool)+ same[1:] = rail[1:] & rail[:-1] & (seg[1:] == seg[:-1])+ rail, same = rail[lead:], same[lead:]+ # Each rail sample that does not continue the previous one opens a run; id 0 is the+ # run carried in from the last chunk.+ opens = rail & ~same+ ids = np.cumsum(opens)+ counts = np.bincount(ids[rail], minlength = int(ids[-1]) + 1)+ counts[0] += open_length+ firsts = np.concatenate(([open_start], np.flatnonzero(opens) + start))+ if rail[-1]:+ open_start, open_length = int(firsts[-1]), int(counts[-1])+ firsts, counts = firsts[:-1], counts[:-1]+ else:+ open_start, open_length = 0, 0+ total += _steep_samples(x, firsts, counts, step)+ total += _steep_samples(x, np.array([open_start]), np.array([open_length]), step)+ return total+++def _db(value: float) -> Optional[float]:+ return round(20.0 * math.log10(value), 2) if value > 0 else None+++def sound_span(data: np.ndarray, rate: int) -> Optional[tuple[float, float]]:+ """(start_s, end_s) of the part that holds sound, or None for silence."""+ if rate <= 0:+ return None+ frame = max(int(rate * _FRAME_S), 1)+ total = data.shape[0]+ count = -(-total // frame) # the last, shorter frame counts too+ if count == 0:+ return None+ # Energy over every channel, so channels in opposite phase do not cancel out.+ rms = np.empty(count)+ full = total // frame+ step = max(_CHUNK // frame, 1)+ for lo in range(0, full, step):+ hi = min(lo + step, full)+ frames = data[lo * frame : hi * frame].astype(np.float64)+ frames = frames.reshape(hi - lo, frame, data.shape[1])+ rms[lo:hi] = np.sqrt(np.mean(frames * frames, axis = (1, 2)))+ if full < count:+ tail = data[full * frame :].astype(np.float64)+ rms[full] = np.sqrt(np.mean(tail * tail))+ loudest = float(rms.max())+ if loudest <= 0:+ return None+ floor = max(20.0 * math.log10(loudest) - _SPAN_RANGE_DB, _SPAN_FLOOR_DB)+ with np.errstate(divide = "ignore"):+ loud = np.nonzero(20.0 * np.log10(rms) > floor)[0]+ if loud.size == 0:+ return None+ end = min(int(loud[-1] + 1) * frame, total)+ return round(float(loud[0]) * frame / rate, 3), round(end / rate, 3)+++def measure(+ data: np.ndarray,+ rate: int,+ step: float = 0.0,+) -> dict[str, Any]:+ """The metrics for ``data`` (frames x channels in [-1, 1]) at ``rate``; ``step`` is the PCM+ quantization step the samples were decoded from (0 for float audio)."""+ duration = data.shape[0] / rate if rate else 0.0+ lufs = integrated_loudness(data, rate)+ clipped = clipped_samples(data, step)+ span = sound_span(data, rate)+ return {+ "duration_s": round(duration, 3),+ "sample_rate": rate,+ "channels": int(data.shape[1]),+ "integrated_lufs": round(lufs, 2) if lufs is not None else None,+ "true_peak_dbtp": _db(true_peak(data)),+ "clipped_samples": clipped,+ "clipping_ratio": round(clipped / data.size, 6) if data.size else 0.0,+ "speech_start_s": span[0] if span else None,+ "speech_end_s": span[1] if span else None,+ }+++def measure_file(path: Path) -> dict[str, Any]:+ """``measure`` for a PCM or IEEE float WAV; raises wave.Error, EOFError or ValueError for anything else."""+ data, rate, step = _read_pcm(path)+ return measure(data, rate, step)+++# Scripts written without spaces between words: each character counts as a word.+_UNSPACED = re.compile(r"[\u0e00-\u0e7f\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]")+++def _words(text: str) -> list[str]:+ folded = unicodedata.normalize("NFKC", text or "").casefold()+ # "don't" and "dont" are one word; other punctuation splits words.+ folded = re.sub(r"(?<=\w)['\u2019](?=\w)", "", folded)+ folded = "".join(" " if unicodedata.category(ch)[0] in "PS" else ch for ch in folded)+ return _UNSPACED.sub(lambda m: f" {m.group(0)} ", folded).split()+++# The word alignment compares every expected word with every heard one; past this many pairs+# (about a second of work) there is no score.+_MATCH_MAX_PAIRS = 50_000_000+++def match_score(expected: str, heard: str) -> Optional[float]:+ """1 - word error rate after case, punctuation and spacing are normalised, floored at 0.+ None when the two texts are too long to align."""+ ref = _words(expected)+ hyp = _words(heard)+ if not ref:+ return 1.0 if not hyp else 0.0+ if not hyp:+ return 0.0+ if len(ref) * len(hyp) > _MATCH_MAX_PAIRS:+ return None+ ids: dict[str, int] = {}+ ref_ids = [ids.setdefault(word, len(ids)) for word in ref]+ hyp_ids = np.array([ids.setdefault(word, len(ids)) for word in hyp])+ steps = np.arange(len(hyp) + 1)+ previous = steps.copy()+ # One row of the edit-distance table per expected word, a whole row at a time.+ for i, word in enumerate(ref_ids, 1):+ current = np.empty_like(previous)+ current[0] = i+ current[1:] = np.minimum(previous[1:] + 1, previous[:-1] + (hyp_ids != word))+ # An insertion carries a cell to the right: current[j] = min over k <= j of current[k] + j - k.+ previous = np.minimum.accumulate(current - steps) + steps+ return round(max(0.0, 1.0 - int(previous[-1]) / len(ref)), 4)diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex ef9763e71b0..72e8d4ec0cc 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -5944,6 +5944,13 @@ class AudioRunRequest(BaseModel):# One take of a music run the page sends take by take; its clips share group_id.take: Optional[int] = Field(None, ge = 1, le = 4)group_id: Optional[str] = Field(None, pattern = r"^[0-9a-f]{32}$")+ cancel_id: Optional[str] = Field(+ None,+ min_length = 8,+ max_length = 64,+ pattern = r"^[A-Za-z0-9_-]+$",+ description = "Your id for this run; POST it to /audio/cancel to stop the run",+ )@model_validator(mode = "after")def _take_fields(self):@@ -6017,6 +6024,8 @@ class AudioRunClip(BaseModel):duration_s: floatworkflow: strseed: Optional[int] = None+ voice_id: Optional[str] = None+ source_clip_id: Optional[str] = Noneclass AudioRunAudio(BaseModel):@@ -6037,6 +6046,20 @@ class AudioRunResponse(BaseModel):ignored_options: List[str] = Field(default_factory = list, description = "Sent options the model does not take; not sent")+ applied: Dict[str, Any] = Field(+ default_factory = dict,+ description = "Values Unsloth Studio used for settings it can change, e.g. duration_s",+ )+ warnings: List[str] = Field(default_factory = list)++ @model_validator(mode = "after")+ def _ignored_option_warnings(self):+ # FastAPI validates the response model again, so add each line once.+ for name in self.ignored_options:+ line = f"Ignored option {name}: the model does not take it."+ if line not in self.warnings:+ self.warnings.append(line)+ return selfclass AudioInputRecord(BaseModel):@@ -6097,6 +6120,39 @@ class TranscribeSourceRequest(BaseModel):title: Optional[str] = Field(None, max_length = 255, description = "Defaults to the source's name")+class AudioAnalyzeRequest(TranscribeSourceRef):+ """``POST /audio/analyze``: measure one clip, upload or voice; nothing is saved."""++ expected_text: Optional[str] = Field(+ None, max_length = 20000, description = "Text the audio should say; adds match_score"+ )+++class AudioAnalyzeResponse(BaseModel):+ duration_s: float+ sample_rate: int+ channels: int+ integrated_lufs: Optional[float] = Field(None, description = "BS.1770 loudness; None for silence")+ true_peak_dbtp: Optional[float] = None+ clipped_samples: int+ clipping_ratio: float+ speech_start_s: Optional[float] = None+ speech_end_s: Optional[float] = None+ transcript: Optional[str] = None+ match_score: Optional[float] = Field(+ None,+ description = "1 - word error rate against expected_text; null when the texts are too "+ "long to align",+ )+ stt_model: Optional[str] = None+++class AudioCancelRequest(BaseModel):+ model_config = ConfigDict(extra = "forbid")++ cancel_id: str = Field(..., min_length = 8, max_length = 64, pattern = r"^[A-Za-z0-9_-]+$")++class TranscriptPatch(BaseModel):"""``PATCH /audio/transcripts/{id}``: archive or restore, and name speakers (None clears)."""diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 4d467b7f6fb..1f603bcb89a 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -3805,6 +3805,9 @@ from models.inference import (AudioInputTranscribeRequest,AudioInputTranscript,AudioRunInputs,+ AudioAnalyzeRequest,+ AudioAnalyzeResponse,+ AudioCancelRequest,AudioRunRequest,AudioRunResponse,AudioSourceRef,@@ -23417,7 +23420,7 @@ async def _generate_tts_wav(# Created before the backend pick so the GGUF lambda can close over it; the registration# that arms it is below, once the model name is known.- _audio_cancel = threading.Event()+ _audio_cancel = _audio_run_event()prompt_for_budget = text# Pick backend - both return (wav_bytes, sample_rate)@@ -23554,14 +23557,17 @@ async def _generate_tts_wav(# TTS holds the model for the whole request, so unregistered a non-forced swap counted zero# generations and tore the model down mid-generation. Both GGUF and subprocess paths observe- # the request event. No cancel keys: /cancel addresses streams, and this route has none; the- # disconnect watcher below is the cancellation source.+ # the request event. An /audio/run cancel_id (POST /audio/cancel) is registered for the whole+ # run by _dispatch_audio_run; otherwise the disconnect watcher below is the cancellation source.with _TrackedCancel(_audio_cancel,thread_id = getattr(payload, "thread_id", None),model = model_name,kind = "audio",):+ # Cancelled while the model was loading: nothing has been generated yet.+ if _audio_cancel.is_set():+ raise HTTPException(status_code = 499, detail = "Audio generation cancelled")# Stop in the UI aborts the fetch and nothing more, and this route has no cancel id to# address, so without watching the disconnect llama-server kept generating for the rest# of the request timeout after the chat had already reported it stopped.@@ -23875,6 +23881,8 @@ def _audio_run_clip(record: dict[str, Any], role: str = "output") -> dict[str, A"role": role,**{k: record[k] for k in ("id", "url", "sample_rate", "duration_s", "workflow")},"seed": (record.get("settings") or {}).get("seed"),+ "voice_id": record.get("voice_id"),+ "source_clip_id": record.get("source_clip_id"),}@@ -23964,33 +23972,33 @@ async def _run_audio_edit(requested_model = body.model or _RELOAD_ONLY_MODEL,stats_holder = stats,)- source_record = None- try:- source_record = await asyncio.to_thread(- audio_edit.save_source_clip, source, source_path, reference_text- )- except Exception as exc: # noqa: BLE001 - the edit is served even when its source is not kept- logger.warning("audio_edit.source_save_failed: %s", exc)- if source_record is not None:- source_clip_id = source_record["id"]- else:- source_clip_id = source.id if source.kind == "clip" else None- extra_meta: dict[str, Any] = {- "role": "output",- "source_clip_id": source_clip_id,- "reference_name": source.name,- "settings": _audio_edit_settings(body),- }- record = await asyncio.to_thread(- _persist_tts_clip,- wav_bytes,- sample_rate,- body.text,- model_name,- audio_type,- extra_meta,- "edit",- )++ def keep() -> tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]:+ # Under the cancel cleanup's lock, so a cancelled Edit of the same recording cannot remove+ # the source between this run picking it and saving the output that plays it.+ with _EDIT_SOURCE_LOCK:+ kept = None+ try:+ created = _AUDIO_RUN_CREATED.get()+ kept = audio_edit.save_source_clip(source, source_path, reference_text, created)+ except Exception as exc: # noqa: BLE001 - the edit is served even when its source is not kept+ logger.warning("audio_edit.source_save_failed: %s", exc)+ if kept is not None:+ source_clip_id = kept["id"]+ else:+ source_clip_id = source.id if source.kind == "clip" else None+ extra_meta: dict[str, Any] = {+ "role": "output",+ "source_clip_id": source_clip_id,+ "reference_name": source.name,+ "settings": _audio_edit_settings(body),+ }+ output = _persist_tts_clip(+ wav_bytes, sample_rate, body.text, model_name, audio_type, extra_meta, "edit"+ )+ return kept, output++ source_record, record = await asyncio.to_thread(keep)sources = [_audio_run_clip(source_record, "source")] if source_record is not None else []return _audio_run_response(record, wav_bytes, sample_rate, model_name, sources, _audio_stats(stats)@@ -24161,6 +24169,15 @@ def _read_music_outputs(run_dir: Path) -> list[tuple[bytes, int, Optional[int]]]return outputs+def _clamp_note(+ applied: dict[str, Any], warnings: list[str], asked: Optional[float], used: float+) -> None:+ """Report the music length used, with a warning when it differs from the one asked for."""+ applied["duration_s"] = used+ if asked is not None and used != asked:+ warnings.append(f"duration_s {asked:g} clamped to {used:g}")++async def _run_music_workflow(body: AudioRunRequest, request: Request, current_subject: str) -> AudioRunResponse:@@ -24197,6 +24214,8 @@ async def _run_music_workflow(source_path = Nonesource = Noneseconds: Optional[float] = None+ applied: dict[str, Any] = {}+ warnings: list[str] = []if body.mode == "edit":spec = MUSIC_SPECS.get(str(family or ""))rate = spec.edit_rate if spec is not None else None@@ -24222,6 +24241,7 @@ async def _run_music_workflow(work_s = max([work_s, *(r.end_s for r in body.edit.ranges)])if body.edit.action == "continue" and body.duration_s:seconds = min(song_max, float(body.duration_s))+ _clamp_note(applied, warnings, body.duration_s, seconds)work_s = max(work_s, seconds)else:mode = _music_mode_rules(rules, body.mode) or {}@@ -24230,6 +24250,13 @@ async def _run_music_workflow(high = float(bounds.get("max") or 240.0)wanted = body.duration_s if body.duration_s is not None else bounds.get("default")seconds = max(low, min(high, float(wanted if wanted is not None else low)))+ spec = MUSIC_SPECS.get(str(family or ""))+ if spec is not None and spec.self_ending_s:+ # The model ends the song itself and is sent no length.+ if body.duration_s is not None:+ warnings.append(f"duration_s ignored: {label} sets its own length")+ else:+ _clamp_note(applied, warnings, body.duration_s, seconds)work_s = secondsvariations = body.variations if body.mode != "edit" else 1music = {@@ -24306,6 +24333,8 @@ async def _run_music_workflow("seed": seed,},ignored_options = ignored,+ applied = applied,+ warnings = warnings,)continueclips.append(@@ -24317,10 +24346,16 @@ async def _run_music_workflow("duration_s": record["duration_s"],"workflow": record["workflow"],"seed": seed,+ "source_clip_id": record.get("source_clip_id"),})return AudioRunResponse(- clips = clips, group_id = group_id, model = model_name, ignored_options = ignored+ clips = clips,+ group_id = group_id,+ model = model_name,+ ignored_options = ignored,+ applied = applied,+ warnings = warnings,)@@ -24468,8 +24503,100 @@ async def run_audio_workflow(return response+# The /audio/run cancel_id, read where the run registers its cancel event.+_AUDIO_RUN_CANCEL_ID: "contextvars.ContextVar[Optional[str]]" = contextvars.ContextVar(+ "audio_run_cancel_id", default = None+)+++# The event that run's cancel_id sets. It is registered before any model switch, so a cancel sent+# while a cold model loads (often longer than the 30 s a stashed cancel is kept) still stops it.+_AUDIO_RUN_CANCEL: "contextvars.ContextVar[Optional[threading.Event]]" = contextvars.ContextVar(+ "audio_run_cancel", default = None+)+++# Clips a run saved that its response lists as reused sources, so a cancel can remove them too.+_AUDIO_RUN_CREATED: "contextvars.ContextVar[Optional[list[str]]]" = contextvars.ContextVar(+ "audio_run_created", default = None+)+++def _audio_run_cancel_key() -> Optional[tuple[str, str]]:+ # Namespaced, so a chat cancel_id can never stop an audio run.+ cancel_id = _AUDIO_RUN_CANCEL_ID.get()+ return ("audio", cancel_id) if cancel_id else None+++def _audio_run_event() -> threading.Event:+ """The current /audio/run's cancel event, or a fresh one outside a run."""+ return _AUDIO_RUN_CANCEL.get() or threading.Event()++async def _dispatch_audio_run(body: AudioRunRequest, request: Request, current_subject: str+) -> AudioRunResponse:+ if body.cancel_id:+ # One cancel_id names one run: a second run under a live id would be stopped by the same+ # cancel. Nothing awaits between this check and the registration below.+ with _CANCEL_LOCK:+ busy = bool(_CANCEL_REGISTRY.get(_account_cancel_key(("audio", body.cancel_id))))+ if busy:+ raise HTTPException(+ status_code = 409, detail = "Another audio run is using this cancel_id."+ )+ token = _AUDIO_RUN_CANCEL_ID.set(body.cancel_id)+ cancel = threading.Event()+ event_token = _AUDIO_RUN_CANCEL.set(cancel)+ created: list[str] = []+ created_token = _AUDIO_RUN_CREATED.set(created)+ try:+ # Registry only: each stage records itself as a generation once a model is loaded.+ with _TrackedCancel(cancel, _audio_run_cancel_key(), track_run = False):+ response = await _dispatch_audio_workflow(body, request, current_subject)+ # Unregistered now, so a later cancel finds nothing to stop. One that arrived while the+ # clips were being saved still wins: the clips this run made are removed (a source clip+ # it reused stays) and the run answers 499, as a cancel during generation would.+ if cancel.is_set():+ made = [clip.id for clip in response.clips if clip.role != "source"]+ await asyncio.to_thread(_discard_audio_clips, made, created)+ raise HTTPException(status_code = 499, detail = "Audio generation cancelled")+ return response+ finally:+ _AUDIO_RUN_CREATED.reset(created_token)+ _AUDIO_RUN_CANCEL.reset(event_token)+ _AUDIO_RUN_CANCEL_ID.reset(token)+++# Held while an Edit picks or saves its source and saves its output, and while a cancel removes clips.+_EDIT_SOURCE_LOCK = threading.Lock()+++def _discard_audio_clips(outputs: list[str], sources: list[str]) -> None:+ """Remove a cancelled run's outputs, then the sources it saved unless another clip, such as a+ concurrent Edit of the same recording, now plays them."""+ from core.inference import audio_gallery++ def remove(clip_id: str, cascade: bool = True) -> None:+ try:+ audio_gallery.delete(clip_id, cascade = cascade)+ except Exception as exc: # noqa: BLE001 - remove the rest even if one delete fails+ logger.warning("audio_run.cancel_cleanup_failed: %s", exc)++ with _EDIT_SOURCE_LOCK:+ # No cascade: a reused source an earlier run kept is not this run's to remove.+ for clip_id in outputs:+ remove(clip_id, cascade = False)+ in_use = (+ audio_gallery._sources_in_use(audio_gallery.gallery_dir(), set()) if sources else ()+ )+ for clip_id in sources:+ if clip_id not in in_use:+ remove(clip_id)+++async def _dispatch_audio_workflow(+ body: AudioRunRequest, request: Request, current_subject: str) -> AudioRunResponse:if body.workflow == "separate":return await _run_separation(body, request, current_subject)@@ -24776,8 +24903,10 @@ async def _run_separation()try:await asyncio.to_thread(staging.mkdir, parents = True)- cancel = threading.Event()+ cancel = _audio_run_event()with _TrackedCancel(cancel, model = model_name, kind = "audio"):+ if cancel.is_set():+ raise HTTPException(status_code = 499, detail = "Audio generation cancelled")watcher = asyncio.create_task(_await_disconnect_then_cancel(request, cancel))try:outputs = await asyncio.to_thread(@@ -24836,6 +24965,7 @@ async def _run_separation("sample_rate": record["sample_rate"],"duration_s": record["duration_s"],"workflow": record["workflow"],+ "source_clip_id": record.get("source_clip_id"),}for record in records],@@ -26577,6 +26707,100 @@ async def stt_model_capabilities(return await asyncio.to_thread(stt_capabilities.capabilities_for, model, engine)[email protected]("/audio/cancel")+async def cancel_audio_run(+ body: AudioCancelRequest, current_subject: str = Depends(get_current_subject)+):+ """Stop the /audio/run sent with this cancel_id, as a disconnect would: it answers 499 and saves+ nothing. A cancel that arrives first is kept for 30 s. Returns {"cancelled": N}."""+ return {"cancelled": _cancel_by_cancel_id_or_stash(("audio", body.cancel_id))}+++_ANALYZE_MAX_SECONDS = 600.0+++def _analyze_metrics(path: Path) -> dict[str, Any]:+ """Metrics for a PCM or float WAV as is; anything else is decoded to a temporary 16-bit WAV first."""+ import wave++ from core.inference import audio_inputs, audio_metrics++ try:+ if audio_metrics.wav_seconds(path) > _ANALYZE_MAX_SECONDS:+ raise audio_inputs.AudioInputError(413, "Audio is longer than 10 minutes.")+ return audio_metrics.measure_file(path)+ except audio_inputs.AudioInputError:+ raise+ except (wave.Error, EOFError, ValueError):+ pass+ decoded = audio_inputs.inputs_dir() / f".analyze-{uuid.uuid4().hex}.wav"+ try:+ audio_inputs.transcode(path, decoded, max_seconds = _ANALYZE_MAX_SECONDS)+ return audio_metrics.measure_file(decoded)+ finally:+ decoded.unlink(missing_ok = True)+++async def _analyze_transcript(source, expected_text: str, request: Request) -> dict[str, Any]:+ """Transcribe with the resident speech-to-text model, else the default, and score the match."""+ from core.inference import audio_inputs, stt_capabilities, stt_details, stt_registry+ from core.inference.audio_metrics import match_score+ from core.inference.stt_sidecar import DEFAULT_STT_MODEL++ def usable(resident: Optional[str], name: str) -> bool:+ # A managed caller cannot use a resident model another account loaded outside its access.+ if not resident or not account_access.managed_account():+ return bool(resident)+ try:+ account_access.require_model_access(_stt_repo_reference(resident, name))+ except HTTPException:+ return False+ return True++ model, engine = DEFAULT_STT_MODEL, None+ for name in stt_registry.STT_ENGINES:+ resident = await asyncio.to_thread(_stt_resident_model, name)+ if await asyncio.to_thread(usable, resident, name):+ model, engine = resident, name+ break+ serving_engine = _resolve_serving_stt_engine(engine)+ caps = await asyncio.to_thread(stt_capabilities.capabilities_for, model, serving_engine)+ rate = stt_details.FIXED_SPAN_RATES.get(caps["family"], 16000)+ try:+ path = await asyncio.to_thread(audio_inputs.prepared_path, source, rate)+ except audio_inputs.AudioInputError as exc:+ raise _audio_source_error(exc) from None+ result = await _transcribe_audio_result(+ b"", model, None, False, engine, request, None, source_path = path+ )+ transcript = str(result.get("text") or "").strip()+ # Word edit distance grows with both lengths, so it stays off the event loop.+ score = await asyncio.to_thread(match_score, expected_text, transcript)+ return {"transcript": transcript, "stt_model": model, "match_score": score}++[email protected]("/audio/analyze", response_model = AudioAnalyzeResponse)+async def analyze_audio(+ body: AudioAnalyzeRequest,+ request: Request,+ current_subject: str = Depends(get_current_subject),+):+ """Measure an upload, history clip or saved voice: duration, loudness (LUFS), true peak,+ clipping and the span that holds sound. With expected_text, also the transcript and how well+ it matches. Nothing is saved."""+ from core.inference import audio_inputs++ ref = body.model_dump(include = {"input_id", "clip_id", "voice_id"}, exclude_none = True)+ try:+ source = await asyncio.to_thread(audio_inputs.resolve_source, ref)+ metrics = await asyncio.to_thread(_analyze_metrics, source.path)+ except audio_inputs.AudioInputError as exc:+ raise _audio_source_error(exc) from None+ if body.expected_text is not None:+ metrics.update(await _analyze_transcript(source, body.expected_text, request))+ return AudioAnalyzeResponse(**metrics)++# =====================================================================# OpenAI-Compatible Chat Completions (/chat/completions)diff --git a/studio/backend/studio_mcp/errors.py b/studio/backend/studio_mcp/errors.pyindex 541ca8700d7..b52d248ec55 100644--- a/studio/backend/studio_mcp/errors.py+++ b/studio/backend/studio_mcp/errors.py@@ -169,3 +169,63 @@ def raise_for_route(except (ValueError, UnicodeDecodeError):return Nonereturn raise_for_payload(payload)+++# Audio failures carry a code (detail.code on /api, error.code on /v1); each gets what to do next.+_UPDATE_AUDIO_RUNTIME = (+ "Update the audio runtime (`unsloth studio update`), then load the model again."+)+AUDIO_HINTS = {+ "out_of_memory": "Load the audio model again with device='cpu'{variants}.",+ "gpu_unavailable": "Load the audio model again with device='cpu'.",+ "runtime_missing": _UPDATE_AUDIO_RUNTIME,+ "family_unknown_to_runtime": _UPDATE_AUDIO_RUNTIME,+ "espeak_missing": _UPDATE_AUDIO_RUNTIME,+ "runtime_stopped": "Retry once; if it fails again, run studio_status and load_model the audio "+ "model again.",+ "file_incomplete": "Download the model again, or load another variant.",+}+++def _audio_failure(body: Any) -> Optional[dict]:+ if not isinstance(body, dict):+ return None+ for holder in (body.get("detail"), body.get("error")):+ if isinstance(holder, dict) and isinstance(holder.get("code"), str):+ return holder if holder["code"] in AUDIO_HINTS else None+ return None+++def audio_code(body: Any) -> Optional[str]:+ """The audio failure code a route's error body names, if it is one MCP has a hint for."""+ failure = _audio_failure(body)+ return failure["code"] if failure is not None else None+++def audio_hint(body: Any) -> Optional[str]:+ failure = _audio_failure(body)+ if failure is None:+ return None+ names = []+ for row in failure.get("smaller_variants") or []:+ name = row.get("variant") if isinstance(row, dict) else row+ if isinstance(name, str) and name:+ names.append(name)+ variants = (+ f", or a smaller variant with load_model(variant=...): {', '.join(names)}" if names else ""+ )+ return AUDIO_HINTS[failure["code"]].format(variants = variants)+++def raise_for_audio(resp: httpx.Response, *, hints: Optional[Mapping[int, str]] = None) -> Any:+ """``raise_for_route`` for an audio route: a coded failure starts with its ``[code]`` tag and ends with what to do about it."""+ body = _body(resp) if resp.status_code >= 400 else None+ code = audio_code(body)+ if code is None or _gpu_busy(body) is not None:+ return raise_for_route(resp, hints = hints)+ try:+ return raise_for_route(resp, hints = {**(hints or {}), resp.status_code: audio_hint(body)})+ except ToolError as exc:+ # The /v1 envelope repeats the code after the message; the tag already names it.+ text = str(exc).replace(f" ({code})", "", 1)+ raise ToolError(text if text.startswith("[") else f"[{code}] {text}") from Nonediff --git a/studio/backend/studio_mcp/outputs.py b/studio/backend/studio_mcp/outputs.pyindex eb1eec71f0d..f05cc214ba1 100644--- a/studio/backend/studio_mcp/outputs.py+++ b/studio/backend/studio_mcp/outputs.py@@ -79,12 +79,43 @@ def audio_options(rows: Any) -> Optional[list[AudioOption]]:return options or None+class MusicModeBounds(ToolOutput):+ # A music mode's duration_s range and default, in seconds.+ id: RouteText+ min_s: Optional[float] = None+ max_s: Optional[float] = None+ default_s: Optional[float] = None++class ChatModel(ToolOutput):id: RouteTextdisplay_name: Optional[RouteText] = Noneis_gguf: Optional[bool] = None# The active audio model's options for generate_audio, built-in voices included.audio_options: Optional[list[AudioOption]] = None+ # The active music model's duration_s bounds per mode; a longer duration is clamped.+ audio_music: Optional[list[MusicModeBounds]] = None++ @staticmethod+ def music_modes(raw: Any) -> Optional[list[MusicModeBounds]]:+ """The bounds of each mode in a status ``audio_music``, or None when it lists none."""+ from studio_mcp.tools import number++ modes = raw.get("modes") if isinstance(raw, dict) else None+ bounds = []+ for mode in modes if isinstance(modes, list) else []:+ duration = mode.get("duration") if isinstance(mode, dict) else None+ if not isinstance(duration, dict) or not isinstance(mode.get("id"), str):+ continue+ bounds.append(+ MusicModeBounds(+ id = mode["id"],+ min_s = number(duration.get("min")),+ max_s = number(duration.get("max")),+ default_s = number(duration.get("default")),+ )+ )+ return bounds or Noneclass ChatSlot(ToolOutput):@@ -297,14 +328,36 @@ class AudioResult(ToolOutput):model: Optional[RouteText] = Nonegroup_id: Optional[RouteText] = Noneclips: list[AudioClip] = []+ # Options sent that the model does not take; they were left out.+ ignored_options: list[RouteText] = []+ # Values Unsloth Studio used instead of the ones asked for, such as a clamped duration_s.+ applied: dict[str, float] = {}+ warnings: list[RouteText] = []+ # Pass to cancel(kind="audio", id=...) from another call to stop the run.+ cancel_id: Optional[str] = None# False when Studio could not save the clip to Audio history; the audio then comes back inline only.saved: bool = True+ @classmethod+ def from_route(cls, payload: Any, **overrides: Any) -> "AudioResult":+ applied = payload.get("applied") if isinstance(payload, dict) else None+ overrides.setdefault(+ "applied",+ {+ name: float(value)+ for name, value in (applied.items() if isinstance(applied, dict) else ())+ if isinstance(value, (int, float)) and not isinstance(value, bool)+ },+ )+ return super().from_route(payload, **overrides)+class TranscriptSegment(ToolOutput):start: Optional[float] = Noneend: Optional[float] = Nonetext: str = ""+ # The speaker's label, such as "Speaker 1", when transcribe was asked for speakers.+ speaker: Optional[RouteText] = Noneclass TranscriptResult(ToolOutput):@@ -312,9 +365,31 @@ class TranscriptResult(ToolOutput):language: Optional[RouteText] = Nonemodel: Optional[RouteText] = Nonesegments: Optional[list[TranscriptSegment]] = None+ # 1 - word error rate against expected_text, from 0 to 1; None without expected_text or when+ # the texts are too long to align.+ match_score: Optional[float] = Nonesaved_to_history: bool = False+class AnalysisResult(ToolOutput):+ duration_s: Optional[float] = None+ sample_rate: Optional[int] = None+ channels: Optional[int] = None+ # BS.1770 loudness; None for silence.+ integrated_lufs: Optional[float] = None+ true_peak_dbtp: Optional[float] = None+ clipped_samples: Optional[int] = None+ clipping_ratio: Optional[float] = None+ # Where the sound starts and ends; None for silence.+ speech_start_s: Optional[float] = None+ speech_end_s: Optional[float] = None+ # Model-written, so never rewritten; only with expected_text.+ transcript: Optional[str] = None+ # None when the texts are too long to align.+ match_score: Optional[float] = None+ stt_model: Optional[RouteText] = None++class VideoJobRef(ToolOutput):id: RouteTextstatus: RouteTextdiff --git a/studio/backend/studio_mcp/tools/audio.py b/studio/backend/studio_mcp/tools/audio.pyindex d01e23ac2fc..6c09851497e 100644--- a/studio/backend/studio_mcp/tools/audio.py+++ b/studio/backend/studio_mcp/tools/audio.py@@ -1,21 +1,26 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""Audio tools: ``generate_audio`` runs any Audio page workflow and ``transcribe`` turns speech into text."""+"""Audio tools: ``generate_audio`` runs any Audio page workflow, ``transcribe`` turns speech into text and ``analyze_audio`` measures a clip."""from __future__ import annotations+import asyncioimport base64import binascii-from typing import Any, Literal, Optional+import time+import uuid+from dataclasses import dataclass+from typing import Annotated, Any, Literal, Optionalfrom fastmcp import Contextfrom fastmcp.exceptions import ToolErrorfrom fastmcp.tools import ToolResult+from pydantic import Fieldfrom studio_mcp import loadingfrom studio_mcp.caller import Caller, current_caller-from studio_mcp.errors import raise_for_route+from studio_mcp.errors import raise_for_audio, raise_for_routefrom studio_mcp.forward import forward, ndjson_lastfrom studio_mcp.inputs import AudioInput, audio_bytes, audio_ref, upload_audiofrom studio_mcp.media import (@@ -26,7 +31,13 @@ from studio_mcp.media import (public_url,resource_link,)-from studio_mcp.outputs import AudioClip, AudioResult, TranscriptResult, TranscriptSegment+from studio_mcp.outputs import (+ AnalysisResult,+ AudioClip,+ AudioResult,+ TranscriptResult,+ TranscriptSegment,+)from studio_mcp.tools import WRITES, opt_text, present, route_json, strings, try_jsonLOAD_AUDIO_HINT = "Load a text-to-speech or music model with load_model(kind='tts') first."@@ -84,6 +95,145 @@ async def _checked_options(return checked+@dataclass+class _Run:+ cancelled: bool = False+ in_flight: bool = False+++# generate_audio runs by (account, cancel_id), and ids cancelled lately; the route keeps an early+# cancel as long, so a reused id would be stopped on arrival.+_RUNS: dict[tuple[str, str], _Run] = {}+_CANCELLED: dict[tuple[str, str], float] = {}+CANCEL_MEMORY_S = 30.0+++def _forget_old_cancels() -> None:+ cutoff = time.monotonic() - CANCEL_MEMORY_S+ for key in [key for key, at in _CANCELLED.items() if at < cutoff]:+ del _CANCELLED[key]+++def stop_run(account: str, cancel_id: str) -> Optional[bool]:+ """Mark run ``cancel_id`` cancelled: None when no generate_audio run here has that id, True when it stops between takes, False when a take is running and the route must stop it."""+ _forget_old_cancels()+ _CANCELLED[(account, cancel_id)] = time.monotonic()+ run = _RUNS.get((account, cancel_id))+ if run is None:+ return None+ run.cancelled = True+ return not run.in_flight+++async def _post_run(caller: Caller, body: dict[str, Any]) -> Any:+ response = await forward(caller, "POST", "/v1/audio/run", json_body = body)+ named = response.status_code == 400 and NOT_LOADED in response.text+ return raise_for_audio(response, hints = {400: LOAD_AUDIO_HINT} if named else None)+++async def _takes_how(caller: Caller, body: dict[str, Any]) -> Optional[str]:+ """How the loaded music model makes variations of this mode: "batch", "sequential" or None."""+ status = await try_json(caller, "/api/inference/status")+ if not isinstance(status, dict):+ return None+ if body.get("model") and body["model"] != status.get("model_identifier"):+ return None+ music = status.get("audio_music")+ modes = music.get("modes") if isinstance(music, dict) else None+ for mode in modes if isinstance(modes, list) else []:+ if isinstance(mode, dict) and mode.get("id") == (body.get("mode") or "song"):+ how = mode.get("variations")+ return opt_text(how.get("how")) if isinstance(how, dict) else None+ return None+++def _merge_take(merged: dict[str, Any], payload: dict[str, Any]) -> None:+ merged["clips"].extend(row for row in payload.get("clips") or [] if isinstance(row, dict))+ merged["model"] = payload.get("model") or merged.get("model")+ for key in ("ignored_options", "warnings"):+ for item in payload.get(key) or []:+ if item not in merged[key]:+ merged[key].append(item)+ if isinstance(payload.get("applied"), dict):+ merged["applied"].update(payload["applied"])+++async def _run_takes(+ caller: Caller, body: dict[str, Any], *, variations: int, cancel_id: str+) -> Any:+ """Run ``body`` under ``cancel_id``. Music whose model makes variations one at a time goes take by take under one group_id, as the Audio page sends it, so a cancel keeps the finished takes."""+ key = (caller.account_id, cancel_id)+ _forget_old_cancels()+ if key in _RUNS or key in _CANCELLED:+ raise ToolError(+ f"cancel_id {cancel_id!r} is running or was just cancelled; pass a new cancel_id."+ )+ if not 1 <= variations <= 4:+ # The route refuses more, but take by take only after the first four ran.+ raise ToolError("variations must be 1 to 4.")+ run = _RUNS[key] = _Run()+ try:+ takes = variations if body["workflow"] == "music" else 1+ sequential = takes > 1 and await _takes_how(caller, body) == "sequential"+ if run.cancelled:+ raise ToolError("The audio run was cancelled before it started.")+ if not sequential:+ run.in_flight = True+ payload = await _post_run(caller, {**body, "cancel_id": cancel_id})+ if isinstance(payload, dict):+ payload["cancel_id"] = cancel_id+ return payload+ group_id = uuid.uuid4().hex+ seed = body.get("seed")+ single = {name: value for name, value in body.items() if name != "variations"}+ merged: dict[str, Any] = {+ "clips": [],+ "group_id": group_id,+ "ignored_options": [],+ "warnings": [],+ "applied": {},+ "cancel_id": cancel_id,+ }+ finished = 0+ for take in range(1, takes + 1):+ if run.cancelled:+ break+ sent = {**single, "take": take, "group_id": group_id, "cancel_id": cancel_id}+ if seed is not None:+ sent["seed"] = seed+ run.in_flight = True+ try:+ payload = await _post_run(caller, sent)+ except ToolError as exc:+ if not merged["clips"]:+ raise+ if not run.cancelled:+ merged["warnings"].append(f"Take {take} of {takes} failed: {exc}")+ break+ finally:+ run.in_flight = False+ if not isinstance(payload, dict):+ break+ if take == 1:+ # Later takes reuse take 1's seed, as the Audio page does.+ first = next(iter(payload.get("clips") or []), None)+ drawn = first.get("seed") if isinstance(first, dict) else None+ seed = drawn if isinstance(drawn, int) and not isinstance(drawn, bool) else seed+ _merge_take(merged, payload)+ finished = take+ if isinstance(payload.get("audio"), dict):+ # History could not save this take, so it came back inline; stop here.+ merged["audio"] = payload["audio"]+ break+ if run.cancelled and finished < takes:+ merged["warnings"].append(f"Stopped after take {finished} of {takes}: cancelled.")+ return merged+ finally:+ _RUNS.pop(key, None)+ if run.cancelled:+ _CANCELLED[key] = time.monotonic()++async def generate_audio(workflow: Literal["clone", "speak", "edit", "convert", "music", "separate"],model: Optional[str] = None,@@ -101,6 +251,16 @@ async def generate_audio(instrumental: bool = False,duration_s: Optional[float] = None,variations: int = 1,+ cancel_id: Annotated[+ Optional[str],+ Field(+ min_length = 8,+ max_length = 64,+ pattern = r"^[A-Za-z0-9_-]+$",+ description = "Your id for this run, so cancel(kind='audio', id=...) can stop it from "+ "another call; made when omitted and returned as cancel_id.",+ ),+ ] = None,edit: Optional[dict[str, Any]] = None,convert: Optional[dict[str, Any]] = None,speed: Optional[float] = None,@@ -108,7 +268,7 @@ async def generate_audio(max_tokens: Optional[int] = None,options: Optional[dict[str, Any]] = None,) -> ToolResult:- """Run an Audio page workflow with the audio model loaded in Unsloth Studio, or with ``model`` (an id from list_models(workflow=...)), which Unsloth Studio loads first when "Switch model by request" is on in Settings > API; a model that cannot run the workflow is refused before anything is unloaded, and the refusal names the pages it runs on. Clips are saved to Audio history and returned inline when small, else as links. clone: speak ``text`` in the voice of ``reference`` (a clip, or a saved voice as voice_id; ``reference_text`` is its transcript). speak: ``text`` in the loaded model's default voice, or a saved voice via ``reference``. edit: change the words or delivery of ``source`` per ``edit``. convert: make ``source`` sound like ``target``. music: ``mode`` song or sfx from ``text`` (the style) and ``lyrics``; ``variations`` up to 4 share a group_id. separate: split ``source`` into stems. ``options`` are the loaded model's ``audio_options`` that studio_status lists, such as a built-in ``voice``. Audio inputs are an Unsloth Studio id, inline base64 with a filename, or a path on the Unsloth Studio computer."""+ """Run an Audio page workflow with the audio model loaded in Unsloth Studio, or with ``model`` (an id from list_models(workflow=...)), which Unsloth Studio loads first when "Switch model by request" is on in Settings > API; a model that cannot run the workflow is refused before anything is unloaded, and the refusal names the pages it runs on. Clips are saved to Audio history and returned inline when small, else as links. clone: speak ``text`` in the voice of ``reference`` (a clip, or a saved voice as voice_id; ``reference_text`` is its transcript). speak: ``text`` in the loaded model's default voice, or a saved voice via ``reference``. edit: change the words or delivery of ``source`` per ``edit``. convert: make ``source`` sound like ``target``. music: ``mode`` song or sfx from ``text`` (the style) and ``lyrics``; ``variations`` up to 4 share a group_id. separate: split ``source`` into stems. ``options`` are the loaded model's ``audio_options`` that studio_status lists, such as a built-in ``voice``. Audio inputs are an Unsloth Studio id, inline base64 with a filename, or a path on the Unsloth Studio computer. Failures start with a [code] tag, such as [runtime_stopped] (retry once, then reload) or [out_of_memory] (CPU or a smaller variant)."""caller = current_caller()inputs: dict[str, Any] = {}for key, audio in (@@ -144,12 +304,8 @@ async def generate_audio(body["instrumental"] = Trueif variations != 1:body["variations"] = variations- payload = await route_json(- "POST",- "/v1/audio/run",- caller = caller,- json_body = body,- hint_if = (NOT_LOADED, {400: LOAD_AUDIO_HINT}),+ payload = await _run_takes(+ caller, body, variations = variations, cancel_id = cancel_id or f"mcp-{uuid.uuid4().hex}")if not isinstance(payload, dict):raise ToolError("Unsloth Studio returned no audio")@@ -169,12 +325,19 @@ async def generate_audio(data = base64.b64decode(fallback["data"], validate = True)except (binascii.Error, ValueError):raise ToolError("Unsloth Studio returned unreadable audio") from None- if len(data) > INLINE_CAP:- raise ToolError(- f"Unsloth Studio could not save the clip to Audio history, and at "- f"{len(data) / (1024 * 1024):.1f} MiB it is too large to return inline."+ too_large = (+ f"Unsloth Studio could not save the clip to Audio history, and at "+ f"{len(data) / (1024 * 1024):.1f} MiB it is too large to return inline."+ )+ if len(data) <= INLINE_CAP:+ contents.append(+ audio_content(data, f"audio/{opt_text(fallback.get('format')) or 'wav'}"))- contents.append(audio_content(data, f"audio/{opt_text(fallback.get('format')) or 'wav'}"))+ elif clips:+ # Earlier takes were saved: report them, with the lost take as a warning.+ payload["warnings"] = [*(payload.get("warnings") or []), too_large]+ else:+ raise ToolError(too_large)if not clips and saved:raise ToolError("Unsloth Studio returned no audio")return media_result(contents, AudioResult.from_route(payload, clips = clips, saved = saved))@@ -194,7 +357,30 @@ def _segments(payload: dict) -> list[TranscriptSegment]:rows = payload.get("segments")if not isinstance(rows, list):return []- return [TranscriptSegment.from_route(row) for row in rows if isinstance(row, dict)]+ speakers = payload.get("speakers") if isinstance(payload.get("speakers"), list) else []+ labels = {str(s.get("id")): s.get("label") for s in speakers if isinstance(s, dict)}+ return [+ TranscriptSegment.from_route(row, speaker = _speaker(row.get("speaker"), labels))+ for row in rows+ if isinstance(row, dict)+ ]+++def _speaker(raw: Any, labels: dict[str, Any]) -> Optional[str]:+ if raw is None or isinstance(raw, bool):+ return None+ return opt_text(labels.get(str(raw))) or opt_text(str(raw))+++async def _scored(result: TranscriptResult, expected: Optional[str]) -> TranscriptResult:+ """``result`` with match_score against ``expected``, when given; scored off the event loop,+ which this server shares with every other request."""+ if expected is None:+ return result+ from core.inference.audio_metrics import match_score++ score = await asyncio.to_thread(match_score, expected, result.text)+ return result.model_copy(update = {"match_score": score})def _default_stt(status: dict) -> Optional[str]:@@ -260,6 +446,7 @@ async def _transcribe_source(*,language: Optional[str],timestamps: bool,+ speakers: bool = False,model: Optional[str],) -> TranscriptResult:"""Audio Unsloth Studio already holds, transcribed by id; this route saves the transcript to history and streams NDJSON."""@@ -272,6 +459,10 @@ async def _transcribe_source(}if language:body["language"] = language+ if speakers:+ body["speakers"] = True+ # A diarized transcript comes back with its segments.+ timestamps = timestamps or speakersasync def send():response = await forward(@@ -317,17 +508,40 @@ async def transcribe(language: Optional[str] = None,translate: bool = False,timestamps: bool = False,+ speakers: Annotated[+ bool,+ Field(+ description = "Label who speaks each segment (segments[].speaker); needs a model that "+ "tells speakers apart, such as MOSS-Transcribe-Diarize or VibeVoice-ASR."+ ),+ ] = False,+ expected_text: Annotated[+ Optional[str],+ Field(+ max_length = 20000,+ description = "The words the audio should say; adds match_score, 1.0 when every "+ "word matches and null when the texts are too long to align.",+ ),+ ] = None,model: Optional[str] = None,ctx: Optional[Context] = None,) -> TranscriptResult:- """Transcribe speech with Unsloth Studio's speech-to-text, or translate it to English with ``translate``. ``audio`` is inline base64 with a filename, a path on the Unsloth Studio computer, or an Unsloth Studio id. Audio over 25 MB or given by id is stored with Unsloth Studio first and its transcript is saved to Audio history (``saved_to_history``); translation needs a smaller file. ``timestamps`` adds segments and needs a model that times them, such as Qwen3-ASR (audio.cpp) or Parakeet-TDT; others refuse it, and translation has none. A missing model is downloaded first. Without ``model`` the loaded speech-to-text model or Unsloth Studio's default is used."""- if translate and timestamps:+ """Transcribe speech with Unsloth Studio's speech-to-text, or translate it to English with ``translate``.+ ``audio`` is inline base64 with a filename, a path on the Unsloth Studio computer, or an Unsloth Studio id.+ Audio over 25 MB, given by id, or sent with ``speakers`` is stored with Unsloth Studio first and its transcript is saved to Audio history (``saved_to_history``); translation needs a smaller file.+ ``timestamps`` adds segments and needs a model that times them, such as Qwen3-ASR (audio.cpp) or Parakeet-TDT; others refuse it, and translation has none.+ ``speakers`` labels who speaks each segment and needs a model that tells speakers apart.+ ``expected_text`` adds match_score: 1 minus the word error rate against it, so 1.0 when every word matches; it is null when the texts are too long to align (over 50 million word pairs; in Chinese, Japanese and Thai each character counts as a word).+ A missing model is downloaded first.+ Without ``model`` the loaded speech-to-text model or Unsloth Studio's default is used."""+ if translate and (timestamps or speakers):raise ToolError(- "Translation returns text only; leave out timestamps, or transcribe without translate."+ "Translation returns text only; leave out timestamps and speakers, or transcribe "+ "without translate.")caller = current_caller()data, name = audio_bytes(caller, audio) if audio.is_upload else (None, None)- if data is None or len(data) > MULTIPART_LIMIT:+ if speakers or data is None or len(data) > MULTIPART_LIMIT:if translate:raise ToolError("Translation needs a file under 25 MB sent as data or a path.")source = (@@ -335,16 +549,28 @@ async def transcribe(if data is not Noneelse await audio_ref(caller, audio))- return await _transcribe_source(- caller, ctx, source, language = language, timestamps = timestamps, model = model+ return await _scored(+ await _transcribe_source(+ caller,+ ctx,+ source,+ language = language,+ timestamps = timestamps,+ speakers = speakers,+ model = model,+ ),+ expected_text,)# Without a model the multipart route runs its Transformers default, not the loaded one.model, engine = await _resident_stt(caller, model)# A whisper.cpp id is shared with Transformers, so only the stored-audio route keeps that engine.if engine == "gguf" and not translate:source = {"input_id": await upload_audio(caller, data, name)}- return await _transcribe_source(- caller, ctx, source, language = language, timestamps = timestamps, model = model+ return await _scored(+ await _transcribe_source(+ caller, ctx, source, language = language, timestamps = timestamps, model = model+ ),+ expected_text,)async def send():@@ -368,7 +594,7 @@ async def transcribe(payload = raise_for_route(response, hints = hints)if not isinstance(payload, dict):raise ToolError("Unsloth Studio returned no transcript")- return TranscriptResult(+ result = TranscriptResult(text = payload.get("text") if isinstance(payload.get("text"), str) else "",# This route answers with the text alone, so report what was asked for and what ran.# A translation is always English; its verbose form spells it "english".@@ -377,6 +603,7 @@ async def transcribe(segments = _segments(payload) if timestamps else None,saved_to_history = False,)+ return await _scored(result, expected_text)async def _resident_stt(@@ -394,4 +621,42 @@ async def _resident_stt(return None, None-TOOLS = ((generate_audio, WRITES, AudioResult.model_json_schema()), (transcribe, WRITES))+ANALYZE_STT_HINT = (+ "Run transcribe once to download Unsloth Studio's speech-to-text model, or leave out "+ "expected_text."+)+++async def analyze_audio(+ audio: AudioInput,+ expected_text: Annotated[+ Optional[str],+ Field(+ max_length = 20000,+ description = "The words the audio should say; adds the transcript and match_score, "+ "1.0 when every word matches and null when the texts are too long to align.",+ ),+ ] = None,+) -> AnalysisResult:+ """Measure a clip: its duration, integrated loudness (LUFS), true peak (dBTP), clipped samples and where the sound starts and ends.+ ``audio`` is an Unsloth Studio id (such as a clip_id from generate_audio), inline base64 with a filename, or a path on the Unsloth Studio computer.+ With ``expected_text`` the clip is also transcribed by the loaded or default speech-to-text model, and match_score is 1 minus the word error rate, or null when the texts are too long to align.+ Nothing is added to Audio history."""+ caller = current_caller()+ body = {**await audio_ref(caller, audio), **present(expected_text = expected_text)}+ payload = await route_json(+ "POST",+ "/api/inference/audio/analyze",+ caller = caller,+ json_body = body,+ hint_if = (NOT_DOWNLOADED, {409: ANALYZE_STT_HINT}),+ )+ return AnalysisResult.from_route(payload)+++TOOLS = (+ (generate_audio, WRITES, AudioResult.model_json_schema()),+ (transcribe, WRITES),+ # Not read-only: inline audio or a path is uploaded to Unsloth Studio first.+ (analyze_audio, WRITES),+)diff --git a/studio/backend/studio_mcp/tools/cancel.py b/studio/backend/studio_mcp/tools/cancel.pyindex a5ce332cae9..cb9d3c8635e 100644--- a/studio/backend/studio_mcp/tools/cancel.py+++ b/studio/backend/studio_mcp/tools/cancel.py@@ -14,6 +14,7 @@ from studio_mcp import export_jobsfrom studio_mcp.caller import current_callerfrom studio_mcp.outputs import CancelResultfrom studio_mcp.tools import DESTRUCTIVE, EXPORT_STATUS, as_dict, opt_text, route_json+from studio_mcp.tools.audio import stop_runCancelKind = Literal["training",@@ -23,6 +24,7 @@ CancelKind = Literal["recipe","image","video",+ "audio","chat","dataset_download",]@@ -31,6 +33,7 @@ NEEDS_ID = {"training_start": "the start request id","recipe": "the recipe job id","chat": "the chat's cancel_id",+ "audio": "the run's cancel_id (generate_audio returns it)","dataset_download": "the dataset's repo_id",}# The route each other kind posts to, and the body field that carries its id (or ``save``), if any.@@ -41,6 +44,7 @@ ROUTES = {"image": ("/api/inference/images/generate/cancel", None),"video": ("/api/inference/video/generate/cancel", None),"chat": ("/api/inference/cancel", "cancel_id"),+ "audio": ("/api/inference/audio/cancel", "cancel_id"),"dataset_download": ("/api/hub/datasets/download/cancel", "repo_id"),}@@ -55,7 +59,7 @@ def _stopped(kind: str, answer: Any) -> bool:"""Whether the route says it stopped something. Each route answers an idle cancel normally, so success alone means nothing."""if not isinstance(answer, dict):return False- if kind in ("image", "video", "chat"):+ if kind in ("image", "video", "chat", "audio"):return bool(answer.get("cancelled"))if kind == "training":return answer.get("status") == "stopped"@@ -72,6 +76,7 @@ def _stopped(kind: str, answer: Any) -> bool:return answer.get("state") == "cancelling"+KEPT_TAKES = "finished takes stay in Audio history."# How long a cancel waits for a job whose export just finished to record its own result.SETTLE_S = 2.0@@ -103,7 +108,7 @@ async def cancel(id: Optional[str] = None,save: bool = True,) -> CancelResult:- """Stop Unsloth Studio work. "training" stops the LLM training run ``id`` (or the current one) at its next safe point, saving a checkpoint unless ``save`` is false. "training_start" withdraws a start request that has not begun. "diffusion_training" stops image LoRA training, saving the partial adapter unless ``save`` is false. "export" stops the running export and unloads its checkpoint; with ``id`` (an export_model job) only that job's own step, never someone else's export. "recipe" stops recipe job ``id``. "image" and "video" stop the generation in progress. "chat" stops the reply with cancel_id ``id``. "dataset_download" stops downloading the dataset ``id`` (its repo id). Audio runs cannot be cancelled. ``cancelled`` says whether this call stopped something."""+ """Stop Unsloth Studio work. "training" stops the LLM training run ``id`` (or the current one) at its next safe point, saving a checkpoint unless ``save`` is false. "training_start" withdraws a start request that has not begun. "diffusion_training" stops image LoRA training, saving the partial adapter unless ``save`` is false. "export" stops the running export and unloads its checkpoint; with ``id`` (an export_model job) only that job's own step, never someone else's export. "recipe" stops recipe job ``id``. "image" and "video" stop the generation in progress. "chat" stops the reply with cancel_id ``id``. "dataset_download" stops downloading the dataset ``id`` (its repo id). "audio" stops the generate_audio run whose cancel_id is ``id``; takes it already finished stay in Audio history. ``cancelled`` says whether this call stopped something."""caller = current_caller()def done(cancelled: bool, message: Optional[str]) -> CancelResult:@@ -125,6 +130,15 @@ async def cancel(caller = caller,json_body = {"save": save, "expected_job_id": id},)+ elif kind == "audio":+ between = stop_run(caller.account_id, id)+ if between:+ return done(True, f"The run stops before its next take; {KEPT_TAKES}")+ path, id_field = ROUTES[kind]+ answer = await route_json("POST", path, caller = caller, json_body = {id_field: id})+ if between is False:+ # The route stopped the take in flight, or it just ended; either way no take follows.+ return done(True, f"The run was stopped; {KEPT_TAKES}")elif kind == "export":if id:job = export_jobs.lookup(caller.account_id, id)diff --git a/studio/backend/studio_mcp/tools/status.py b/studio/backend/studio_mcp/tools/status.pyindex 5edcc027ad6..595237a28d7 100644--- a/studio/backend/studio_mcp/tools/status.py+++ b/studio/backend/studio_mcp/tools/status.py@@ -49,12 +49,14 @@ STT_ENGINES = ("transformers", "mtmd", "gguf", "audiocpp")def _chat(payload: dict) -> ChatSlot:active = payload.get("model_identifier")options = audio_options(payload.get("audio_options"))+ music = ChatModel.music_modes(payload.get("audio_music"))loaded = [ChatModel(id = model_id,display_name = opt_text(payload.get("active_model")) if model_id == active else None,is_gguf = bool(payload.get("is_gguf")) if model_id == active else None,audio_options = options if model_id == active else None,+ audio_music = music if model_id == active else None,)for model_id in payload.get("loaded") or []if isinstance(model_id, str)diff --git a/studio/backend/tests/test_audio_analyze_route.py b/studio/backend/tests/test_audio_analyze_route.pynew file mode 100644index 00000000000..c7a83e90bee--- /dev/null+++ b/studio/backend/tests/test_audio_analyze_route.py@@ -0,0 +1,220 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""POST /audio/analyze: metrics for audio named by id, and a transcript match when asked."""++from __future__ import annotations++import sys+import wave+from pathlib import Path++import numpy as np+import pytest+from fastapi import HTTPException+from fastapi.testclient import TestClient++from core.inference import audio_inputs, stt_capabilities+from routes import inference++_TESTS_DIR = str(Path(__file__).resolve().parent)+if _TESTS_DIR not in sys.path:+ sys.path.insert(0, _TESTS_DIR)++import test_account_media_isolation as _media # noqa: E402+from test_account_media_isolation import ALICE # noqa: E402+from test_audio_run_extras import app_for # noqa: E402++# Shared fixtures, bound by assignment so pytest finds them in this module.+isolated = _media.isolated++CLIP = "c" * 32+FIELDS = {+ "duration_s", "sample_rate", "channels", "integrated_lufs", "true_peak_dbtp",+ "clipped_samples", "clipping_ratio", "speech_start_s", "speech_end_s", "transcript",+ "match_score", "stt_model",+} # fmt: skip+++def _wav(+ path,+ seconds = 1.0,+ rate = 16000,+ amplitude = 0.1,+):+ t = np.arange(int(seconds * rate)) / rate+ pcm = np.round(amplitude * np.sin(2 * np.pi * 1000 * t) * 32767).astype("<i2")+ with wave.open(str(path), "wb") as w:+ w.setnchannels(1)+ w.setsampwidth(2)+ w.setframerate(rate)+ w.writeframes(pcm.tobytes())+ return path+++def source(monkeypatch, tmp_path):+ """The file the request's id resolves to; ``heard`` is what speech-to-text returns."""+ state = {"path": _wav(tmp_path / "clip.wav"), "heard": "hello world", "stt": []}+ monkeypatch.setattr(+ audio_inputs,+ "resolve_source",+ lambda ref: audio_inputs.Source("clip", ref["clip_id"], state["path"], "Clip"),+ )+ monkeypatch.setattr(audio_inputs, "prepared_path", lambda src, rate: src.path)+ monkeypatch.setattr(stt_capabilities, "capabilities_for", lambda *_a: {"family": "whisper"})+ monkeypatch.setattr(inference, "_stt_resident_model", lambda engine: None)++ async def transcribe(+ raw,+ model,+ *args,+ source_path = None,+ **_kwargs,+ ):+ state["stt"].append((model, source_path))+ if isinstance(state["heard"], Exception):+ raise state["heard"]+ return {"text": state["heard"]}++ monkeypatch.setattr(inference, "_transcribe_audio_result", transcribe)+ return state+++def _post(body, path = "/api/inference/audio/analyze"):+ with TestClient(app_for(ALICE)) as client:+ return client.post(path, json = body)++[email protected]("path", ["/api/inference/audio/analyze", "/v1/audio/analyze"])+def test_metrics_come_back_on_both_mounts_without_stt(source, path):+ response = _post({"clip_id": CLIP}, path)+ assert response.status_code == 200, response.text+ body = response.json()+ assert set(body) == FIELDS+ assert (body["duration_s"], body["sample_rate"], body["channels"]) == (1.0, 16000, 1)+ assert body["integrated_lufs"] == pytest.approx(-23.0, abs = 0.1)+ assert (body["transcript"], body["match_score"], body["stt_model"]) == (None, None, None)+ assert source["stt"] == []+++def test_expected_text_scores_the_transcript(source):+ from core.inference.stt_sidecar import DEFAULT_STT_MODEL++ exact = _post({"clip_id": CLIP, "expected_text": "Hello, world."}).json()+ assert (exact["transcript"], exact["match_score"]) == ("hello world", 1.0)+ assert exact["stt_model"] == DEFAULT_STT_MODEL+ assert source["stt"][0] == (DEFAULT_STT_MODEL, source["path"])+ wrong = _post({"clip_id": CLIP, "expected_text": "goodbye cruel world"}).json()+ assert wrong["match_score"] < 1.0+++def test_a_resident_stt_model_is_used_first(source, monkeypatch):+ from hub.services.models import account_access++ monkeypatch.setattr(account_access, "require_model_access", lambda *_a, **_k: None)+ monkeypatch.setattr(+ inference, "_stt_resident_model", lambda engine: "m/asr" if engine == "mtmd" else None+ )+ body = _post({"clip_id": CLIP, "expected_text": "hello world"}).json()+ assert body["stt_model"] == "m/asr" and source["stt"][0][0] == "m/asr"+++def test_a_resident_model_outside_the_callers_access_is_skipped(source, monkeypatch):+ from core.inference.stt_sidecar import DEFAULT_STT_MODEL+ from hub.services.models import account_access++ monkeypatch.setattr(+ inference, "_stt_resident_model", lambda engine: "o/large" if engine == "mtmd" else None+ )+ monkeypatch.setattr(inference, "_stt_repo_reference", lambda model, engine: model)+ monkeypatch.setattr(account_access, "managed_account", lambda: True)++ def require(reference, *_a, **_k):+ if reference == "o/large":+ raise HTTPException(status_code = 403, detail = "Model not allowed")++ monkeypatch.setattr(account_access, "require_model_access", require)+ body = _post({"clip_id": CLIP, "expected_text": "hello world"}).json()+ assert body["stt_model"] == DEFAULT_STT_MODEL and source["stt"][0][0] == DEFAULT_STT_MODEL+++def test_an_stt_refusal_passes_through(source):+ source["heard"] = HTTPException(status_code = 409, detail = "Model is not downloaded.")+ response = _post({"clip_id": CLIP, "expected_text": "hello"})+ assert (response.status_code, response.json()["detail"]) == (409, "Model is not downloaded.")+++def test_non_wav_audio_is_decoded_to_a_temporary_wav(source, monkeypatch, tmp_path):+ source["path"] = tmp_path / "clip.webm"+ source["path"].write_bytes(b"\x1aE\xdf\xa3 not a wav")+ made = []++ def transcode(+ src,+ dst,+ *,+ max_seconds = None,+ **_kwargs,+ ):+ made.append((src, dst, max_seconds))+ _wav(dst, seconds = 2.0)++ monkeypatch.setattr(audio_inputs, "transcode", transcode)+ response = _post({"clip_id": CLIP})+ assert response.status_code == 200 and response.json()["duration_s"] == 2.0+ ((src, dst, cap),) = made+ assert (src, cap) == (source["path"], 600.0) and dst.name.startswith(".analyze-")+ assert not dst.exists()+++def _float_wav(+ path,+ samples,+ rate = 48000,+ extensible = False,+):+ import struct++ data = np.asarray(samples, dtype = "<f4").tobytes()+ if extensible:+ guid = struct.pack("<H", 3) + b"\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71"+ fmt = struct.pack("<HHIIHHHHI", 0xFFFE, 1, rate, rate * 4, 4, 32, 22, 32, 4) + guid+ else:+ fmt = struct.pack("<HHIIHHH", 3, 1, rate, rate * 4, 4, 32, 0)+ body = b"WAVE" + b"fmt " + struct.pack("<I", len(fmt)) + fmt+ body += b"data" + struct.pack("<I", len(data)) + data+ path.write_bytes(b"RIFF" + struct.pack("<I", len(body)) + body)+ return path++[email protected]("extensible", [False, True])+def test_float_wav_is_measured_without_a_lossy_decode(source, monkeypatch, tmp_path, extensible):+ # A float stem can peak past full scale; a 16-bit decode would clamp it into fake clipping.+ def transcode(*_a, **_k):+ raise AssertionError("a float WAV must not be decoded to 16-bit PCM")++ monkeypatch.setattr(audio_inputs, "transcode", transcode)+ t = np.arange(48000) / 48000+ source["path"] = _float_wav(+ tmp_path / "stem.wav", 1.2 * np.sin(2 * np.pi * 997 * t), extensible = extensible+ )+ response = _post({"clip_id": CLIP})+ assert response.status_code == 200, response.text+ body = response.json()+ assert (body["duration_s"], body["sample_rate"], body["channels"]) == (1.0, 48000, 1)+ assert body["true_peak_dbtp"] == pytest.approx(20 * np.log10(1.2), abs = 0.05)+ assert body["clipped_samples"] == 0+++def test_long_audio_and_bad_requests_are_refused(source, tmp_path):+ source["path"] = _wav(tmp_path / "long.wav", seconds = 601, rate = 100)+ response = _post({"clip_id": CLIP})+ assert (response.status_code, response.json()["detail"]) == (+ 413,+ "Audio is longer than 10 minutes.",+ )+ source["path"] = _float_wav(tmp_path / "long_float.wav", np.zeros(60100), rate = 100)+ assert _post({"clip_id": CLIP}).status_code == 413+ assert _post({"clip_id": CLIP, "input_id": CLIP}).status_code == 422+ assert _post({"clip_id": CLIP, "expected_text": "x" * 20001}).status_code == 422diff --git a/studio/backend/tests/test_audio_edit_route.py b/studio/backend/tests/test_audio_edit_route.pyindex cd35d4836d1..219dad0efc9 100644--- a/studio/backend/tests/test_audio_edit_route.py+++ b/studio/backend/tests/test_audio_edit_route.py@@ -259,3 +259,72 @@ def test_the_worker_carries_the_edit():)assert seen[0]["edit"] == edit and seen[0]["audio_inputs"] == sourceassert "edit" not in seen[1]+++def _cancel_during_save(monkeypatch, keep_output = True):+ """The output save sets the run's cancel event, as a POST /audio/cancel landing then would."""+ persist = inference._persist_tts_clip++ def save_then_cancel(*args, **kwargs):+ record = persist(*args, **kwargs) if keep_output else None+ inference._AUDIO_RUN_CANCEL.get().set()+ return record++ monkeypatch.setattr(inference, "_persist_tts_clip", save_then_cancel)++[email protected]("keep_output", [True, False])+def test_a_cancel_during_the_save_removes_the_source_the_run_kept(stub, monkeypatch, keep_output):+ # Without a kept output nothing else would take the source with it.+ _load(stub)+ input_id = _input(ALICE)+ _cancel_during_save(monkeypatch, keep_output)+ with _client(ALICE) as client:+ assert _edit(client, input_id).status_code == 499+ assert run_as(ALICE, audio_gallery.list_audio) == []+++def test_a_cancel_keeps_a_source_another_run_now_plays(stub, monkeypatch):+ _load(stub)+ input_id = _input(ALICE)+ persist = inference._persist_tts_clip+ later = []++ def save_then_cancel(*args):+ record = persist(*args)+ # Another Edit of the same recording saves its output, playing this run's new source.+ later.append((persist(*args)["id"], args[5]["source_clip_id"]))+ inference._AUDIO_RUN_CANCEL.get().set()+ return record++ monkeypatch.setattr(inference, "_persist_tts_clip", save_then_cancel)+ with _client(ALICE) as client:+ assert _edit(client, input_id).status_code == 499+ (other_output, source), = later+ assert {clip["id"] for clip in run_as(ALICE, audio_gallery.list_audio)} == {other_output, source}+++def test_a_cancel_during_the_save_keeps_a_source_an_earlier_run_kept(stub, monkeypatch):+ _load(stub)+ input_id = _input(ALICE)+ with _client(ALICE) as client:+ first = _edit(client, input_id).json()["clips"]+ _cancel_during_save(monkeypatch)+ assert _edit(client, input_id).status_code == 499+ kept = {clip["id"] for clip in run_as(ALICE, audio_gallery.list_audio)}+ assert kept == {clip["id"] for clip in first}+++def test_a_cancel_keeps_a_source_an_earlier_inline_edit_kept(stub, monkeypatch):+ # The first Edit kept its source but served its output inline, so no saved clip plays it.+ _load(stub)+ input_id = _input(ALICE)+ with _client(ALICE) as client:+ with monkeypatch.context() as patch:+ patch.setattr(inference, "_persist_tts_clip", lambda *args, **kwargs: None)+ first = _edit(client, input_id)+ assert first.status_code == 200, first.text+ (source,) = [clip for clip in first.json()["clips"] if clip["role"] == "source"]+ _cancel_during_save(monkeypatch)+ assert _edit(client, input_id).status_code == 499+ assert [clip["id"] for clip in run_as(ALICE, audio_gallery.list_audio)] == [source["id"]]diff --git a/studio/backend/tests/test_audio_metrics.py b/studio/backend/tests/test_audio_metrics.pynew file mode 100644index 00000000000..0877b8cb221--- /dev/null+++ b/studio/backend/tests/test_audio_metrics.py@@ -0,0 +1,173 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Take metrics against signals with known answers."""++from __future__ import annotations++import wave++import numpy as np+import pytest++from core.inference import audio_metrics as am++RATE = 48000+++def _sine(+ seconds = 3.0,+ amplitude = 0.1,+ freq = 1000.0,+ rate = RATE,+) -> np.ndarray:+ t = np.arange(int(seconds * rate)) / rate+ return (amplitude * np.sin(2 * np.pi * freq * t))[:, None]+++def _write(+ path,+ data,+ rate = RATE,+):+ pcm = np.clip(np.round(data * 32767), -32768, 32767).astype("<i2")+ with wave.open(str(path), "wb") as w:+ w.setnchannels(data.shape[1])+ w.setsampwidth(2)+ w.setframerate(rate)+ w.writeframes(pcm.tobytes())+ return path+++def test_a_minus_20_dbfs_sine_reads_its_known_loudness_and_peak(tmp_path):+ report = am.measure_file(_write(tmp_path / "s.wav", _sine()))+ assert report["integrated_lufs"] == pytest.approx(-23.0, abs = 0.05)+ assert report["true_peak_dbtp"] == pytest.approx(-20.0, abs = 0.05)+ assert (report["duration_s"], report["sample_rate"], report["channels"]) == (3.0, RATE, 1)+ assert report["clipped_samples"] == 0+++def test_loudness_agrees_with_pyloudnorm():+ pyln = pytest.importorskip("pyloudnorm")+ rng = np.random.default_rng(0)+ data = np.concatenate([_sine(2.0, 0.3, 440.0), _sine(2.0, 0.05, 3000.0)])+ data = data + 0.01 * rng.standard_normal(data.shape)+ expected = pyln.Meter(RATE).integrated_loudness(data[:, 0])+ assert am.integrated_loudness(data, RATE) == pytest.approx(expected, abs = 0.1)+ stereo = np.hstack([data, 0.5 * data])+ expected = pyln.Meter(RATE).integrated_loudness(stereo)+ assert am.integrated_loudness(stereo, RATE) == pytest.approx(expected, abs = 0.1)+++def test_hard_clipping_counts_and_a_clean_full_scale_sine_does_not(tmp_path):+ clipped = am.measure(np.clip(3.0 * _sine(1.0, 1.0, 100.0), -1.0, 1.0), RATE)+ assert clipped["clipping_ratio"] > 0.3+ assert am.measure(_sine(1.0, 1.0), RATE)["clipped_samples"] == 0+ # A low full-scale sine lingers near the rail for many samples but never holds one value.+ for freq in (100.0, 10.0):+ assert am.measure(_sine(1.0, 1.0, freq), RATE)["clipped_samples"] == 0+ # 16-bit PCM rounds a low full-scale sine to a flat top, which it eases onto one step at a+ # time; an overdriven one lands on the rail with a jump.+ for freq, amplitude in ((10.0, 1.0), (40.0, 1.0), (50.0, 0.999)):+ clean = _write(tmp_path / f"clean{freq}.wav", _sine(1.0, amplitude, freq))+ assert am.measure_file(clean)["clipped_samples"] == 0+ # float32 decoding spaces 32-bit samples near full scale 2**-24 apart, so that is the step.+ pcm = np.round(_sine(2.0, 1.0, 1.0)[:, 0] * (2**31 - 1)).astype("<i4")+ with wave.open(str(tmp_path / "s32.wav"), "wb") as w:+ w.setnchannels(1)+ w.setsampwidth(4)+ w.setframerate(RATE)+ w.writeframes(pcm.tobytes())+ assert am.measure_file(tmp_path / "s32.wav")["clipped_samples"] == 0+ loud = np.clip(1.1 * _sine(1.0, 1.0, 10.0), -1.0, 1.0)+ assert am.measure_file(_write(tmp_path / "loud.wav", loud))["clipping_ratio"] > 0.2+ # Two samples at the rail are a peak, three in a row are clipping.+ burst = np.zeros((100, 1))+ burst[10:12] = 1.0+ assert am.clipped_samples(burst) == 0+ burst[50:53] = -1.0+ assert am.clipped_samples(burst) == 3+++def test_silence_padding_gives_the_speech_span_and_silence_gives_none():+ pad = np.zeros((RATE // 2, 1))+ report = am.measure(np.concatenate([pad, _sine(1.0, 0.3), pad]), RATE)+ assert report["speech_start_s"] == pytest.approx(0.5, abs = 0.02)+ assert report["speech_end_s"] == pytest.approx(1.5, abs = 0.02)+ # Channels in opposite phase still hold sound.+ tone = np.concatenate([pad, _sine(1.0, 0.3), pad])+ report = am.measure(np.hstack([tone, -tone]), RATE)+ assert report["speech_start_s"] == pytest.approx(0.5, abs = 0.02)+ # A beep shorter than one 20 ms frame, and sound only in the last partial frame, still count.+ assert am.sound_span(np.full((300, 1), 0.5), RATE) == (0.0, 0.006)+ tail = np.concatenate([np.zeros((960 * 3, 1)), np.full((100, 1), 0.5)])+ assert am.sound_span(tail, RATE) == (0.06, 0.062)+ quiet = am.measure(np.zeros((RATE, 2)), RATE)+ assert (quiet["integrated_lufs"], quiet["true_peak_dbtp"], quiet["speech_start_s"]) == (+ None,+ None,+ None,+ )+ assert quiet["clipping_ratio"] == 0.0+++def test_a_few_hertz_sample_rate_still_measures():+ report = am.measure(np.full((40, 1), 0.5), 4)+ assert report["duration_s"] == 10.0 and report["integrated_lufs"] is not None+++def test_24_bit_wav_reads_like_16_bit(tmp_path):+ data = _sine(0.5, 0.25)+ pcm = np.round(data[:, 0] * (1 << 23)).astype("<i4")+ raw = b"".join(int(v).to_bytes(3, "little", signed = True) for v in pcm)+ with wave.open(str(tmp_path / "s24.wav"), "wb") as w:+ w.setnchannels(1)+ w.setsampwidth(3)+ w.setframerate(RATE)+ w.writeframes(raw)+ read, rate = am.read_wav(tmp_path / "s24.wav")+ assert rate == RATE and np.allclose(read, data, atol = 1e-6)+++def test_match_score_is_one_minus_wer_after_normalising():+ assert am.match_score("Hello, World!", "hello world") == 1.0+ assert am.match_score("Don’t stop", "dont stop") == 1.0+ assert am.match_score("the quick brown fox", "the quick red fox") == 0.75+ assert am.match_score("one two", "three four five six") == 0.0+ assert am.match_score("", "") == 1.0 and am.match_score("", "words") == 0.0+ # Scripts without spaces count per character.+ assert am.match_score("你好世界", "你好世") == 0.75+ # Too long to align: no score rather than seconds of work.+ assert am.match_score("字" * 20000, "字" * 20000) is None+ jiwer = pytest.importorskip("jiwer")+ ref, hyp = "please call stella ask her", "please call stela ask her to"+ assert am.match_score(ref, hyp) == pytest.approx(1 - jiwer.wer(ref, hyp), abs = 1e-4)+++def _write_8_bit(path, pcm):+ with wave.open(str(path), "wb") as w:+ w.setnchannels(1)+ w.setsampwidth(1)+ w.setframerate(RATE)+ w.writeframes(pcm.astype(np.uint8).tobytes())+ return path+++def test_8_bit_clipping_counts_on_both_rails(tmp_path):+ pcm = np.full(100, 128)+ pcm[10:15], pcm[50:55] = 255, 0+ assert am.measure_file(_write_8_bit(tmp_path / "s8.wav", pcm))["clipped_samples"] == 10+ # 8-bit steps are coarse, but an overdriven low tone still lands on the rail with a jump.+ loud = np.clip(np.round(np.clip(1.1 * _sine(1.0, 1.0, 100.0)[:, 0], -1, 1) * 127) + 128, 0, 255)+ assert am.measure_file(_write_8_bit(tmp_path / "loud8.wav", loud))["clipping_ratio"] > 0.2+++def test_chunked_measures_match_one_pass(monkeypatch):+ rng = np.random.default_rng(1)+ noise = 0.3 * rng.standard_normal((RATE * 2 + 17, 2))+ clipped = np.clip(3.0 * _sine(2.0, 1.0, 100.0), -1.0, 1.0)+ whole = am.measure(noise, RATE), am.measure(clipped, RATE)+ # Chunks far smaller than a block, a frame or a clipped run must not change any number.+ for chunk in (4096, 1000, 7):+ monkeypatch.setattr(am, "_CHUNK", chunk)+ assert (am.measure(noise, RATE), am.measure(clipped, RATE)) == wholediff --git a/studio/backend/tests/test_audio_run_cancel.py b/studio/backend/tests/test_audio_run_cancel.pynew file mode 100644index 00000000000..d39a1b2b965--- /dev/null+++ b/studio/backend/tests/test_audio_run_cancel.py@@ -0,0 +1,208 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""POST /audio/cancel stops the one /audio/run sent with that cancel_id, and nothing else."""++from __future__ import annotations++import asyncio+import sys+import threading+from pathlib import Path++import httpx++from core.inference.audio_errors import AudioGenerationCancelledError++_TESTS_DIR = str(Path(__file__).resolve().parent)+if _TESTS_DIR not in sys.path:+ sys.path.insert(0, _TESTS_DIR)++import test_account_media_isolation as _media # noqa: E402+from test_account_media_isolation import ALICE # noqa: E402+import test_audio_run_extras as _extras # noqa: E402+from test_audio_run_extras import Backend, app_for # noqa: E402++KOKORO = "audio-cpp/audio.cpp-gguf/Kokoro-82M-GGUF"+SPEAK_INFO = {+ "is_audio": True,+ "audio_type": "audiocpp_tts",+ "audio_workflows": ["speak"],+ "audio_reference_text": None,+ "audio_required_inputs": [],+}+RUN_A, RUN_B, EARLY = "run-aaaaaaaa", "run-bbbbbbbb", "run-cccccccc"++# Shared fixtures, bound by assignment so pytest finds them in this module.+isolated = _media.isolated+use_backend = _extras.use_backend+++def _blocking_backend():+ """Each run waits for its cancel event or ``release``; ``started`` names the runs under way."""+ started = {RUN_A: threading.Event(), RUN_B: threading.Event(), EARLY: threading.Event()}+ release = threading.Event()++ def run(kwargs):+ started[kwargs["text"]].set()+ cancel = kwargs["cancel_event"]+ while not release.is_set():+ if cancel.wait(0.02):+ raise AudioGenerationCancelledError("Audio generation cancelled")++ return Backend(KOKORO, SPEAK_INFO, run = run), started, release+++def test_cancel_by_id_stops_only_that_run(use_backend):+ backend, started, release = _blocking_backend()+ use_backend(backend)++ async def scenario():+ transport = httpx.ASGITransport(app = app_for(ALICE))+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:++ def run(cancel_id):+ body = {"workflow": "speak", "text": cancel_id, "cancel_id": cancel_id}+ return asyncio.create_task(client.post("/api/inference/audio/run", json = body))++ a, b = run(RUN_A), run(RUN_B)+ for name in (RUN_A, RUN_B):+ assert await asyncio.to_thread(started[name].wait, 10)+ # A chat cancel_id lives in another namespace.+ chat = await client.post("/api/inference/cancel", json = {"cancel_id": RUN_A})+ stopped = await client.post("/v1/audio/cancel", json = {"cancel_id": RUN_A})+ try:+ done_a = await asyncio.wait_for(asyncio.shield(a), 10)+ finally:+ release.set()+ return chat.json(), stopped.json(), done_a, await b++ chat, stopped, done_a, done_b = asyncio.run(scenario())+ assert chat == {"cancelled": 0}+ assert stopped == {"cancelled": 1}+ assert done_a.status_code == 499+ assert done_b.status_code == 200 and len(done_b.json()["clips"]) == 1+++def test_an_early_cancel_is_kept_for_the_run_that_follows(use_backend):+ backend, started, _release = _blocking_backend()+ use_backend(backend)++ async def scenario():+ transport = httpx.ASGITransport(app = app_for(ALICE))+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:+ early = await client.post("/api/inference/audio/cancel", json = {"cancel_id": EARLY})+ body = {"workflow": "speak", "text": EARLY, "cancel_id": EARLY}+ return early.json(), await client.post("/api/inference/audio/run", json = body)++ early, response = asyncio.run(scenario())+ assert early == {"cancelled": 0}+ assert response.status_code == 499+++def test_a_cancel_during_a_slow_model_load_stops_the_run(use_backend, monkeypatch):+ from routes import inference++ backend, started, release = _blocking_backend()+ use_backend(backend)+ loading, loaded = threading.Event(), threading.Event()++ async def slow_switch(*args, **kwargs):+ loading.set()+ # Stands in for a cold load that outlasts the 30 s a stashed cancel is kept.+ await asyncio.to_thread(loaded.wait, 10)+ inference._PENDING_CANCELS.clear()++ monkeypatch.setattr(inference, "_maybe_auto_switch_model", slow_switch)++ async def scenario():+ transport = httpx.ASGITransport(app = app_for(ALICE))+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:+ body = {"workflow": "speak", "text": RUN_A, "cancel_id": RUN_A}+ run = asyncio.create_task(client.post("/api/inference/audio/run", json = body))+ assert await asyncio.to_thread(loading.wait, 10)+ stopped = await client.post("/api/inference/audio/cancel", json = {"cancel_id": RUN_A})+ loaded.set()+ try:+ return stopped.json(), await asyncio.wait_for(asyncio.shield(run), 10)+ finally:+ release.set()++ stopped, response = asyncio.run(scenario())+ assert stopped == {"cancelled": 1}+ assert response.status_code == 499+ assert not started[RUN_A].is_set()+++def test_a_cancel_while_the_clip_saves_removes_it(use_backend, monkeypatch):+ from routes import inference++ backend, _started, release = _blocking_backend()+ release.set()+ use_backend(backend)+ saved_paths, saving, go_on = [], threading.Event(), threading.Event()+ persist = inference._persist_tts_clip++ def slow_persist(*args, **kwargs):+ record = persist(*args, **kwargs)+ from core.inference import audio_gallery++ saved_paths.append(audio_gallery.audio_path(record["id"]))+ saving.set()+ go_on.wait(10)+ return record++ monkeypatch.setattr(inference, "_persist_tts_clip", slow_persist)++ async def scenario():+ transport = httpx.ASGITransport(app = app_for(ALICE))+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:+ body = {"workflow": "speak", "text": RUN_A, "cancel_id": RUN_A}+ run = asyncio.create_task(client.post("/api/inference/audio/run", json = body))+ try:+ assert await asyncio.to_thread(saving.wait, 10)+ stopped = await client.post(+ "/api/inference/audio/cancel", json = {"cancel_id": RUN_A}+ )+ finally:+ go_on.set()+ return stopped.json(), await asyncio.wait_for(run, 10)++ stopped, response = asyncio.run(scenario())+ assert stopped == {"cancelled": 1}+ assert response.status_code == 499+ assert saved_paths and saved_paths[0] is not None and not saved_paths[0].exists()+++def test_a_cancel_id_already_running_is_refused(use_backend):+ backend, started, release = _blocking_backend()+ use_backend(backend)++ async def scenario():+ transport = httpx.ASGITransport(app = app_for(ALICE))+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:+ body = {"workflow": "speak", "text": RUN_A, "cancel_id": RUN_A}+ first = asyncio.create_task(client.post("/api/inference/audio/run", json = body))+ try:+ assert await asyncio.to_thread(started[RUN_A].wait, 10)+ second = await client.post("/api/inference/audio/run", json = body)+ finally:+ release.set()+ done = await asyncio.wait_for(first, 10)+ # The id is free again once its run ends.+ again = await client.post("/api/inference/audio/run", json = body)+ return second, done, again++ second, done, again = asyncio.run(scenario())+ assert second.status_code == 409+ assert done.status_code == 200 and again.status_code == 200+++def test_cancel_ids_are_checked():+ from fastapi.testclient import TestClient+ with TestClient(app_for(ALICE)) as client:+ for bad in ("short", "has space!", "x" * 65):+ response = client.post("/api/inference/audio/cancel", json = {"cancel_id": bad})+ assert response.status_code == 422+ run = {"workflow": "speak", "text": "hi", "cancel_id": "no"}+ assert client.post("/api/inference/audio/run", json = run).status_code == 422diff --git a/studio/backend/tests/test_audio_run_extras.py b/studio/backend/tests/test_audio_run_extras.pynew file mode 100644index 00000000000..eeefcba97cf--- /dev/null+++ b/studio/backend/tests/test_audio_run_extras.py@@ -0,0 +1,195 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""POST /audio/run says what it changed (applied, warnings) and where each clip came from."""++from __future__ import annotations++import io+import sys+import wave+from pathlib import Path++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from auth.authentication import authenticated_via_api_key, get_current_subject+from core.inference import audio_inputs+from models.inference import AudioRunResponse+from routes import inference+from utils.account_context import bind_account, reset_account++_TESTS_DIR = str(Path(__file__).resolve().parent)+if _TESTS_DIR not in sys.path:+ sys.path.insert(0, _TESTS_DIR)++import test_account_media_isolation as _media # noqa: E402+from test_account_media_isolation import ALICE # noqa: E402++MUSIC = "audio-cpp/audio.cpp-gguf/Stable-Audio-3-Small-Music-GGUF"+TTS = "audio-cpp/audio.cpp-gguf/Qwen3-TTS-12Hz-0.6B-Base-GGUF"+MUSIC_INFO = {+ "is_audio": True,+ "audio_type": "audiocpp_music",+ "audio_family": "stable_audio",+ "audio_workflows": ["music"],+ "audio_music": {"modes": [{"id": "song", "duration": {"min": 1, "max": 10, "default": 5}}]},+}+TTS_INFO = {+ "is_audio": True,+ "audio_type": "audiocpp_tts",+ "audio_family": "qwen3_tts",+ "audio_workflows": ["clone", "speak"],+ "audio_reference_text": "required",+ "audio_required_inputs": [],+ "audio_clone": {"reference_text": "required", "emotion_audio": False},+}++# Shared fixtures, bound by assignment so pytest finds them in this module.+isolated = _media.isolated+++def wav_bytes(rate = 24000, frames = 2400) -> bytes:+ buf = io.BytesIO()+ with wave.open(buf, "wb") as w:+ w.setnchannels(1)+ w.setsampwidth(2)+ w.setframerate(rate)+ w.writeframes(b"\x01\x00" * frames)+ return buf.getvalue()+++class Backend:+ """The orchestrator as /audio/run sees it; ``run`` replaces the generation."""++ def __init__(+ self,+ name,+ info,+ ignored = (),+ run = None,+ ):+ self.active_model_name = name+ self.models = {name: info}+ self.ignored = list(ignored)+ self.run = run+ self.calls: list[dict] = []++ def generate_audio_response(self, **kwargs):+ self.calls.append(kwargs)+ if self.ignored:+ kwargs["stats_holder"]["stats"] = {"ignored_options": self.ignored}+ if self.run is not None:+ self.run(kwargs)+ return wav_bytes(), 24000+++def use_backend(monkeypatch):+ class _Llama:+ is_loaded = False+ _is_audio = False++ async def _noop_switch(*_args, **_kwargs):+ return None++ monkeypatch.setattr(inference, "get_llama_cpp_backend", lambda: _Llama())+ monkeypatch.setattr(inference, "_maybe_auto_switch_model", _noop_switch)++ def use(backend):+ monkeypatch.setattr(inference, "get_inference_backend", lambda: backend)+ return backend++ return use+++def app_for(account) -> FastAPI:+ app = FastAPI()++ async def subject():+ token = bind_account(account)+ try:+ yield account.username+ finally:+ reset_account(token)++ app.dependency_overrides[get_current_subject] = subject+ app.dependency_overrides[authenticated_via_api_key] = lambda: False+ for prefix in ("/api/inference", "/v1"):+ app.include_router(inference.router, prefix = prefix)+ app.include_router(inference.studio_router, prefix = "/api/inference")+ return app++[email protected]("path", ["/api/inference/audio/run", "/v1/audio/run"])+def test_a_clamped_music_length_is_reported_on_both_mounts(use_backend, path):+ backend = use_backend(Backend(MUSIC, MUSIC_INFO))+ with TestClient(app_for(ALICE)) as client:+ song = {"workflow": "music", "mode": "song", "text": "house"}+ long = client.post(path, json = {**song, "duration_s": 75})+ short = client.post(path, json = {**song, "duration_s": 5})+ assert long.status_code == 200, long.text+ assert backend.calls[0]["music"]["duration_s"] == 10.0+ assert long.json()["applied"] == {"duration_s": 10.0}+ assert long.json()["warnings"] == ["duration_s 75 clamped to 10"]+ assert (short.json()["applied"], short.json()["warnings"]) == ({"duration_s": 5.0}, [])+++def test_an_ignored_option_gives_exactly_one_warning(use_backend):+ use_backend(Backend(MUSIC, MUSIC_INFO, ignored = ["cfg_scale"]))+ with TestClient(app_for(ALICE)) as client:+ response = client.post(+ "/v1/audio/run",+ json = {+ "workflow": "music",+ "mode": "song",+ "text": "house",+ "options": {"cfg_scale": 3},+ },+ )+ body = response.json()+ assert body["ignored_options"] == ["cfg_scale"]+ assert body["warnings"] == ["Ignored option cfg_scale: the model does not take it."]+ # The route builds the response and FastAPI validates it again: still one line.+ again = AudioRunResponse.model_validate(body)+ assert again.warnings == body["warnings"]++[email protected]("kind, field", [("voice", "voice_id"), ("clip", "source_clip_id")])+def test_clip_rows_carry_their_lineage(use_backend, monkeypatch, tmp_path, kind, field):+ use_backend(Backend(TTS, TTS_INFO))+ ref = tmp_path / "ref.wav"+ ref.write_bytes(wav_bytes())+ source_id = "c" * 32+ monkeypatch.setattr(+ audio_inputs,+ "prepare_reference",+ lambda _ref: (audio_inputs.Source(kind, source_id, ref, "Alice"), ref),+ )+ with TestClient(app_for(ALICE)) as client:+ response = client.post(+ "/api/inference/audio/run",+ json = {+ "workflow": "clone",+ "text": "Hello.",+ "inputs": {"reference": {f"{kind}_id": source_id}, "reference_text": "Hi."},+ },+ )+ assert response.status_code == 200, response.text+ (clip,) = response.json()["clips"]+ other = ({"voice_id", "source_clip_id"} - {field}).pop()+ assert (clip[field], clip[other]) == (source_id, None)+++def test_a_length_the_model_never_receives_is_not_reported_as_applied(use_backend):+ info = {**MUSIC_INFO, "audio_family": "yue2"}+ use_backend(Backend("audio-cpp/audio.cpp-gguf/YuE2-GGUF", info))+ with TestClient(app_for(ALICE)) as client:+ song = {"workflow": "music", "mode": "song", "text": "house"}+ asked = client.post("/api/inference/audio/run", json = {**song, "duration_s": 10})+ plain = client.post("/api/inference/audio/run", json = song)+ assert asked.status_code == 200, asked.text+ assert asked.json()["applied"] == {}+ assert asked.json()["warnings"] == ["duration_s ignored: YuE2 sets its own length"]+ assert (plain.json()["applied"], plain.json()["warnings"]) == ({}, [])diff --git a/studio/backend/tests/test_audio_run_route.py b/studio/backend/tests/test_audio_run_route.pyindex d5b8bddd335..7374cc0a614 100644--- a/studio/backend/tests/test_audio_run_route.py+++ b/studio/backend/tests/test_audio_run_route.py@@ -36,6 +36,19 @@ QWEN3_BASE = "audio-cpp/audio.cpp-gguf/Qwen3-TTS-12Hz-0.6B-Base-GGUF"CLONE_FIELDS = {"workflow", "audio_inputs", "reference_text", "speed"}+_CLIP_KEYS = {+ "id",+ "role",+ "url",+ "sample_rate",+ "duration_s",+ "workflow",+ "seed",+ "voice_id",+ "source_clip_id",+}++def _wav(rate = 24000, frames = 2400) -> bytes:buf = io.BytesIO()with wave.open(buf, "wb") as w:@@ -181,7 +194,7 @@ def test_a_clone_run_hands_the_worker_an_account_path_and_saves_the_clip(stub, tassert all(not isinstance(v, (bytes, bytearray)) for v in call.values())assert body["audio"] is None(clip,) = body["clips"]- assert set(clip) == {"id", "role", "url", "sample_rate", "duration_s", "workflow", "seed"}+ assert set(clip) == _CLIP_KEYSassert (clip["role"], clip["workflow"], clip["sample_rate"]) == ("output", "clone", 24000)sidecar = _sidecar(tmp_path, clip["id"])meta = json.loads(sidecar)@@ -1042,7 +1055,7 @@ def test_a_separation_prepares_44k_and_saves_every_stem_as_one_group(sep, tmp_paassert body["audio"] is None and body["model"] and len(body["group_id"]) == 32assert stem_bytes.status_code == 200 and stem_bytes.content[:4] == b"RIFF"for clip in body["clips"]:- assert set(clip) == {"id", "role", "url", "sample_rate", "duration_s", "workflow", "seed"}+ assert set(clip) == _CLIP_KEYSassert (clip["workflow"], clip["sample_rate"], clip["duration_s"]) == ("separate",44100,diff --git a/studio/backend/tests/test_audio_runtime_stopped.py b/studio/backend/tests/test_audio_runtime_stopped.pynew file mode 100644index 00000000000..527c4b9e699--- /dev/null+++ b/studio/backend/tests/test_audio_runtime_stopped.py@@ -0,0 +1,185 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""An audio runtime that dies or goes silent mid-request is a 500 with code runtime_stopped, from+the backend through the worker and orchestrator to the route, instead of "Could not reach an+upstream service"."""++from __future__ import annotations++import asyncio+import queue+import sys+import threading+from pathlib import Path++import pytest+from fastapi import HTTPException+from fastapi.testclient import TestClient++from core.inference import audio_cpp_failures as af+from core.inference.audio_cpp_server import AudioCppUnavailableError+from core.inference.audio_errors import AUDIO_RUNTIME_ERROR_CODE, AudioRuntimeError+from core.inference.audio_cpp_backend import _runtime_stopped++_TESTS_DIR = str(Path(__file__).resolve().parent)+if _TESTS_DIR not in sys.path:+ sys.path.insert(0, _TESTS_DIR)++import test_audio_cpp_music_requests as music # noqa: E402+import test_audio_cpp_separation as sep # noqa: E402+import test_account_media_isolation as _media # noqa: E402+from test_account_media_isolation import ALICE # noqa: E402+from test_audio_run_cancel import KOKORO, SPEAK_INFO # noqa: E402+import test_audio_run_extras as _extras # noqa: E402+from test_audio_run_extras import Backend, app_for # noqa: E402+from test_audio_runtime_errors import _Nullcontext # noqa: E402++# Shared fixtures, bound by assignment so pytest finds them in this module.+isolated = _media.isolated+use_backend = _extras.use_backend++_DEAD = (+ "The audio runtime stopped while serving the request. Last output: abort at "+ "/home/alice/.unsloth/models/m.gguf token hf_AbCdEfGhIjKlMnOpQrStUvWxYz123456"+)+_PREFIX = (+ "The audio runtime stopped: it quit while serving the request. Last output: abort at m.gguf token "+ "hf_<redacted>"+)+_SUFFIX = ". Try again; if it fails again, load the model again."+++def _raise(error):+ def post(*_args, **_kwargs):+ raise error++ return post+++def _generate(error):+ b = music._backend(music._model("stable_audio"))+ b._server.post_json = _raise(error)+ return lambda: music._run(b, duration_s = 5)+++def _speak(error):+ from test_audio_cpp_models import _backend_with, _speech_model++ b, server = _backend_with(_speech_model("moss_tts_nano"), None)+ server.post_json = _raise(error)+ return lambda: b.generate_audio_response("hello")+++def _separate(error, tmp_path):+ model = sep._model("htdemucs", "HTDemucs-GGUF")+ b = sep._backend(model)+ b._server = sep._Recorder(model, error = error)+ b._served_session = dict((model.model_options or {}).get("session_options") or {})+ return lambda: b.separate_audio(sep.SOURCE, str(sep._out(tmp_path)))++[email protected](params = ["speak", "music", "separate"])+def call(request, tmp_path):+ if request.param == "separate":+ return lambda error: _separate(error, tmp_path)+ return {"speak": _speak, "music": _generate}[request.param]+++def test_a_dead_runtime_is_runtime_stopped_without_paths_or_tokens(call):+ with pytest.raises(AudioRuntimeError) as raised:+ call(AudioCppUnavailableError(_DEAD))()+ error = raised.value+ failure = af.failure_of(error)+ assert error.status == 500 and failure is not None+ assert (failure.code, failure.status) == ("runtime_stopped", 500)+ assert failure.message == str(error) == _PREFIX + _SUFFIX+ assert "alice" not in str(error) and "hf_AbCd" not in str(error)+++def test_a_silent_runtime_says_so():+ error = _runtime_stopped(+ AudioCppUnavailableError("The audio runtime did not answer: timed out")+ )+ assert str(error) == "The audio runtime stopped: did not answer: timed out" + _SUFFIX+ assert str(_runtime_stopped(AudioCppUnavailableError(""))).startswith(+ "The audio runtime stopped: no reason given."+ )+++def test_a_crash_already_named_keeps_its_code(call):+ oom = af.classify_audio_cpp_failure("cudaMalloc failed: out of memory", backend = "cuda")+ crash = af.attach_failure(AudioCppUnavailableError(_DEAD), oom)+ with pytest.raises(AudioCppUnavailableError) as raised:+ call(crash)()+ assert raised.value is crash and af.failure_of(raised.value).code == "out_of_memory"+++def test_the_worker_sends_the_code_and_the_orchestrator_rebuilds_it(monkeypatch):+ from core.inference.orchestrator import InferenceOrchestrator+ from core.inference.worker import _handle_generate_audio++ class _Dead:+ def generate_audio_response(self, **_kwargs):+ raise _runtime_stopped(AudioCppUnavailableError(_DEAD))++ responses: queue.Queue = queue.Queue()+ _handle_generate_audio(+ _Dead(), {"request_id": "r1", "text": "hi"}, responses, threading.Event()+ )+ sent = responses.get_nowait()+ assert (sent["code"], sent["status"]) == (AUDIO_RUNTIME_ERROR_CODE, 500)+ assert sent["failure"]["code"] == "runtime_stopped" and sent["failure"]["status"] == 500++ def rebuilt(payload):+ orchestrator = InferenceOrchestrator.__new__(InferenceOrchestrator)+ cmds = []+ reader = lambda *, timeout: {**payload, "request_id": cmds[0]["request_id"]}+ for name, value in {+ "_ensure_subprocess_alive": lambda: True,+ "_send_cmd": cmds.append,+ "_direct_reader": lambda _rid, _cancel = None: (reader, lambda **_k: None, lambda: None),+ "_reserve_worker": lambda _why: _Nullcontext(),+ "_wait_worker_idle": lambda **_k: True,+ "_claim_worker": lambda _cancel: None,+ "_release_worker": lambda *_a, **_k: None,+ }.items():+ monkeypatch.setattr(orchestrator, name, value, raising = False)+ orchestrator.active_model_name = "m"+ orchestrator.models = {"m": {}}+ orchestrator._gen_lock = threading.Lock()+ orchestrator._send_order_lock = threading.Lock()+ orchestrator._unload_pending = False+ with pytest.raises(AudioRuntimeError) as raised:+ orchestrator.generate_audio_response("hello")+ return raised.value++ error = rebuilt(sent)+ assert error.status == 500 and af.failure_of(error).code == "runtime_stopped"+ assert af.failure_of(rebuilt({**sent, "failure": {**sent["failure"], "code": "bogus"}})) is None+ # Without the code in the known set the parent drops it (the commit this test pins).+ monkeypatch.setattr(af, "_CODES", af._CODES - {"runtime_stopped"})+ assert af.failure_of(rebuilt(sent)) is None+++def test_the_route_answers_500_with_the_code_and_no_retry_header(use_backend):+ import routes.inference as ri+ from models.inference import ChatCompletionRequest+ from utils.api_errors import install_api_error_handlers++ def dead(_kwargs):+ raise _runtime_stopped(AudioCppUnavailableError(_DEAD))++ use_backend(Backend(KOKORO, SPEAK_INFO, run = dead))+ payload = ChatCompletionRequest(messages = [{"role": "user", "content": "hello"}])+ with pytest.raises(HTTPException) as raised:+ asyncio.run(ri._generate_tts_wav("hello", payload, request = None, current_subject = "t"))+ assert raised.value.status_code == 500 and raised.value.headers is None+ assert raised.value.detail == {"code": "runtime_stopped", "message": _PREFIX + _SUFFIX}++ app = app_for(ALICE)+ install_api_error_handlers(app)+ with TestClient(app) as client:+ response = client.post("/v1/audio/run", json = {"workflow": "speak", "text": "hello"})+ assert response.status_code == 500 and "retry-after" not in response.headers+ assert response.json()["error"]["code"] == "runtime_stopped"diff --git a/studio/backend/tests/test_mcp_audio_hints.py b/studio/backend/tests/test_mcp_audio_hints.pynew file mode 100644index 00000000000..c72545aa5be--- /dev/null+++ b/studio/backend/tests/test_mcp_audio_hints.py@@ -0,0 +1,92 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import json++import httpx+import pytest+from fastmcp.exceptions import ToolError++from studio_mcp.errors import audio_code, raise_for_audio++from .mcp_harness import openai_error++OOM = "Kokoro ran out of memory. Free GPU memory, or set the audio device to CPU."+VARIANTS = [{"variant": "Q4_K_M", "label": "Q4_K_M", "size_bytes": 1}, {"variant": "Q3_K_M"}]+++def _api(status, detail, **headers):+ content = json.dumps({"detail": detail}).encode()+ return httpx.Response(status, content = content, headers = headers)+++def _v1(status, message, **fields):+ sent = openai_error(message, status, **fields)+ return httpx.Response(status, content = sent.body, headers = sent.raw_headers)+++def _message(resp, **kwargs):+ with pytest.raises(ToolError) as caught:+ raise_for_audio(resp, **kwargs)+ return str(caught.value)+++def test_out_of_memory_names_cpu_and_the_smaller_variants():+ detail = {"code": "out_of_memory", "message": OOM, "smaller_variants": VARIANTS}+ message = _message(_api(503, detail))+ assert message.startswith("[out_of_memory] Kokoro ran out of memory.")+ assert "device='cpu'" in message+ assert message.endswith("load_model(variant=...): Q4_K_M, Q3_K_M.")+++def test_the_v1_envelope_code_gets_the_tag_and_the_generic_hint():+ message = _message(_v1(503, "Out of memory.", type = "server_error", code = "out_of_memory"))+ assert message.startswith("[out_of_memory] Out of memory.")+ assert message.endswith("Load the audio model again with device='cpu'.")+ # The envelope's "(out_of_memory)" suffix is dropped; the tag names the code once.+ assert message.count("out_of_memory") == 1+++def test_gpu_unavailable_points_at_the_cpu():+ detail = {"code": "gpu_unavailable", "message": "The audio runtime could not use the GPU."}+ message = _message(_api(503, detail))+ assert message.startswith("[gpu_unavailable] ")+ assert message.endswith("Load the audio model again with device='cpu'.")+++def test_runtime_stopped_says_retry_once():+ detail = {"code": "runtime_stopped", "message": "The audio runtime stopped: exit 9."}+ message = _message(_api(500, detail))+ assert message.startswith("[runtime_stopped] The audio runtime stopped")+ assert "Retry once" in message+++def test_an_already_tagged_message_is_not_tagged_twice():+ detail = {"code": "out_of_memory", "message": "[out_of_memory] Out of memory."}+ message = _message(_api(503, detail))+ assert message.startswith("[out_of_memory] Out of memory.")+ assert message.count("[out_of_memory]") == 1+++def test_a_gpu_busy_answer_keeps_its_retry_and_gets_no_audio_tag():+ resp = _v1(409, "Busy.", code = "gpu_busy", headers = {"Retry-After": "5"})+ assert _message(resp) == "GPU busy: Busy. Retry after 5 s."+++ "resp",+ [+ _api(400, "No model loaded."),+ _api(500, {"code": "something_else", "message": "Broke."}),+ _v1(400, "No model loaded."),+ ],+)+def test_other_errors_are_unchanged(resp):+ message = _message(resp, hints = {400: "Load one."})+ assert not message.startswith("[")+ assert audio_code(json.loads(resp.content)) is None+++def test_a_success_is_returned_as_is():+ resp = httpx.Response(200, content = b'{"clips": []}')+ assert raise_for_audio(resp) == {"clips": []}diff --git a/studio/backend/tests/test_mcp_audio_reports.py b/studio/backend/tests/test_mcp_audio_reports.pynew file mode 100644index 00000000000..65818d16a6b--- /dev/null+++ b/studio/backend/tests/test_mcp_audio_reports.py@@ -0,0 +1,128 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import pytest+from fastapi.responses import JSONResponse++from .mcp_harness import WAV_INPUT, bodies, run_tool+from .test_mcp_tools_audio import (+ COMPLETE,+ PAYLOADS as AUDIO_PAYLOADS,+ STT_STATUS,+ STT_STATUS_ROUTE,+ TRANSCRIPT,+ TRANSCRIPTIONS,+ UPLOADED,+ _from_source,+ _memo,+)+from .test_mcp_tools_status import PAYLOADS as STATUS_PAYLOADS++ANALYZE = ("POST", "/api/inference/audio/analyze")+ANALYSIS = {+ "duration_s": 2.5,+ "sample_rate": 24000,+ "channels": 1,+ "integrated_lufs": -23.0,+ "true_peak_dbtp": -20.0,+ "clipped_samples": 0,+ "clipping_ratio": 0.0,+ "speech_start_s": 0.5,+ "speech_end_s": 1.5,+ "transcript": "hello",+ "match_score": 1.0,+ "stt_model": "openai/whisper-small",+}+UPLOAD = ("POST", "/v1/audio/inputs")+ANALYZE_PAYLOADS = {ANALYZE: ANALYSIS, UPLOAD: AUDIO_PAYLOADS[UPLOAD]}+++def test_status_lists_the_active_music_models_duration_bounds(monkeypatch):+ chat = STATUS_PAYLOADS[("GET", "/api/inference/status")]+ music = {+ "modes": [+ {"id": "song", "duration": {"min": 10, "max": 240, "default": 30}},+ {"id": "edit", "actions": ["repaint"]},+ {"id": "sfx", "duration": {"min": 1, "max": 10.5, "default": True}},+ ]+ }+ routes = {**STATUS_PAYLOADS, ("GET", "/api/inference/status"): {**chat, "audio_music": music}}+ result, _studio = run_tool(monkeypatch, routes, "studio_status")+ active, other = result["structuredContent"]["chat"]["loaded"]+ assert active["audio_music"] == [+ {"id": "song", "min_s": 10.0, "max_s": 240.0, "default_s": 30.0},+ {"id": "sfx", "min_s": 1.0, "max_s": 10.5, "default_s": None},+ ]+ assert other["audio_music"] is None+++DIARIZED = {+ **COMPLETE,+ "segments": [+ {"start": 0.0, "end": 2.0, "text": "Hi.", "speaker": "S0"},+ {"start": 2.0, "end": 5.0, "text": "Hello.", "speaker": "S1"},+ ],+ "speakers": [{"id": "S0", "label": "Speaker 1"}, {"id": "S1", "label": "Speaker 2"}],+}+++def test_speakers_label_segments_and_small_audio_goes_by_id(monkeypatch):+ result, studio = _from_source(monkeypatch, {**_memo(), "speakers": True}, [DIARIZED])+ out = result["structuredContent"]+ assert [(s["speaker"], s["text"]) for s in out["segments"]] == [+ ("Speaker 1", "Hi."),+ ("Speaker 2", "Hello."),+ ]+ (body,) = bodies(studio, "/api/inference/audio/transcribe/source")+ assert body["source"] == {"input_id": UPLOADED["id"]}+ assert body["speakers"] is True and body["timestamps"] is False+ assert "/v1/audio/transcriptions" not in [c[1] for c in studio.state.calls]+++def test_speakers_with_translate_is_refused_before_any_route(monkeypatch):+ args = {**_memo(), "translate": True, "speakers": True}+ result, studio = run_tool(monkeypatch, {}, "transcribe", args, fallback = {})+ assert result["isError"] is True+ assert "leave out timestamps and speakers" in result["content"][0]["text"]+ assert studio.state.calls == []+++ "expected,score", [("Hello from Studio", 1.0), ("Goodbye from Studio.", 2 / 3)]+)+def test_expected_text_scores_a_multipart_transcript(monkeypatch, expected, score):+ routes = {TRANSCRIPTIONS: TRANSCRIPT, STT_STATUS_ROUTE: {}}+ args = {**_memo(), "model": "openai/whisper-small", "expected_text": expected}+ result, _studio = run_tool(monkeypatch, routes, "transcribe", args)+ assert result["structuredContent"]["match_score"] == pytest.approx(score, abs = 1e-4)+++def test_expected_text_scores_a_stored_clip(monkeypatch):+ args = {"audio": {"clip_id": "clip-7"}, "expected_text": "A long meeting"}+ result, _studio = _from_source(monkeypatch, args, status = STT_STATUS)+ assert result["structuredContent"]["match_score"] == 1.0+++def test_analyze_audio_forwards_the_clip_and_returns_the_metrics(monkeypatch):+ args = {"audio": {"clip_id": "clip-7"}, "expected_text": "hello"}+ result, studio = run_tool(monkeypatch, ANALYZE_PAYLOADS, "analyze_audio", args)+ assert result["structuredContent"] == ANALYSIS+ assert bodies(studio, ANALYZE[1]) == [{"clip_id": "clip-7", "expected_text": "hello"}]+++def test_analyze_audio_uploads_inline_audio_and_leaves_out_unasked_text(monkeypatch):+ silent = {**ANALYSIS, "integrated_lufs": None, "transcript": None, "match_score": None}+ routes = {**ANALYZE_PAYLOADS, ANALYZE: silent}+ result, studio = run_tool(monkeypatch, routes, "analyze_audio", {"audio": WAV_INPUT})+ assert result["structuredContent"]["integrated_lufs"] is None+ assert bodies(studio, ANALYZE[1]) == [{"input_id": UPLOADED["id"]}]+++def test_analyze_audio_without_the_stt_model_says_how_to_get_it(monkeypatch):+ missing = JSONResponse(+ {"detail": "STT model 'openai/whisper-small' is not downloaded."}, status_code = 409+ )+ args = {"audio": {"clip_id": "clip-7"}, "expected_text": "hello"}+ result, _studio = run_tool(monkeypatch, {ANALYZE: missing}, "analyze_audio", args)+ assert result["isError"] is True+ assert result["content"][0]["text"].endswith("or leave out expected_text.")diff --git a/studio/backend/tests/test_mcp_audio_takes.py b/studio/backend/tests/test_mcp_audio_takes.pynew file mode 100644index 00000000000..0cfd1111973--- /dev/null+++ b/studio/backend/tests/test_mcp_audio_takes.py@@ -0,0 +1,200 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import json++import pytest+from fastapi.responses import JSONResponse++from models.inference import AudioRunRequest+from studio_mcp.tools import audio++from .mcp_harness import bodies, fake_studio, run_tool, sequence+from .test_mcp_tools_audio import GALLERY, _clip++RUN = ("POST", "/v1/audio/run")+STATUS = ("GET", "/api/inference/status")+CANCEL = "/api/inference/audio/cancel"+SONG = {"text": "lo-fi", "mode": "song", "lyrics": "la", "variations": 3, "cancel_id": "run-abc12"}+++def _status(how):+ mode = {"id": "song", "duration": {"min": 10, "max": 240, "default": 30}}+ return {+ "model_identifier": "ace-step",+ "audio_music": {"modes": [{**mode, "variations": {"max": 4, "how": how}}]},+ }+++def _take(+ clip_id,+ seed = 7,+ **fields,+):+ return {"clips": [_clip(clip_id, seed = seed)], "model": "ace-step", **fields}++[email protected](autouse = True)+def fresh_runs():+ audio._RUNS.clear()+ audio._CANCELLED.clear()+ yield+ audio._RUNS.clear()+ audio._CANCELLED.clear()+++def _music(+ monkeypatch,+ run,+ how = "sequential",+ args = SONG,+ studio = None,+):+ studio = studio or fake_studio(+ {RUN: run, STATUS: _status(how), ("POST", CANCEL): {"cancelled": 1}, **GALLERY}+ )+ return run_tool(monkeypatch, studio, "generate_audio", {"workflow": "music", **args})+++def test_a_cancel_during_take_two_keeps_take_one_and_sends_no_third(monkeypatch):+ def run(request, body):+ if json.loads(body)["take"] == 1:+ return _take("var-1", seed = 41)+ assert audio.stop_run("owner", "run-abc12") is False+ return JSONResponse({"detail": "Audio generation cancelled"}, status_code = 499)++ result, studio = _music(monkeypatch, run)+ assert result["isError"] is False, result+ out = result["structuredContent"]+ assert [c["id"] for c in out["clips"]] == ["var-1"]+ assert out["warnings"] == ["Stopped after take 1 of 3: cancelled."]+ assert out["cancel_id"] == "run-abc12"+ sent = bodies(studio, "/v1/audio/run")+ assert [b["take"] for b in sent] == [1, 2]+ assert len({b["group_id"] for b in sent}) == 1 == len({out["group_id"], sent[0]["group_id"]})+ # Take 2 reuses the seed take 1 drew; neither asks for variations.+ assert "seed" not in sent[0] and sent[1]["seed"] == 41+ for body in sent:+ assert "variations" not in body and body["cancel_id"] == "run-abc12"+ AudioRunRequest.model_validate(body)+++def test_a_cancel_as_a_take_ends_stops_before_the_next(monkeypatch):+ def run(request, body):+ audio.stop_run("owner", "run-abc12")+ return _take("var-1")++ result, studio = _music(monkeypatch, run)+ assert [c["id"] for c in result["structuredContent"]["clips"]] == ["var-1"]+ assert len(bodies(studio, "/v1/audio/run")) == 1++ # The id stays spent as long as the route keeps an early cancel for it.+ again, studio = _music(monkeypatch, _take("var-2"))+ assert again["isError"] is True+ assert "pass a new cancel_id" in again["content"][0]["text"]+ assert bodies(studio, "/v1/audio/run") == []++[email protected]("how", ["batch", None])+def test_a_batch_model_gets_one_request_with_variations(monkeypatch, how):+ two = {"clips": [_clip("var-1"), _clip("var-2")], "group_id": "grp-9", "model": "ace-step"}+ args = {key: value for key, value in SONG.items() if key != "cancel_id"}+ result, studio = _music(monkeypatch, two, how = how, args = args)+ (body,) = bodies(studio, "/v1/audio/run")+ assert body["variations"] == 3 and "take" not in body+ assert body["cancel_id"].startswith("mcp-")+ assert result["structuredContent"]["cancel_id"] == body["cancel_id"]+ assert result["structuredContent"]["group_id"] == "grp-9"+++def test_takes_report_ignored_options_applied_and_warnings_once(monkeypatch):+ ignored = "Ignored option temperature: the model does not take it."+ clamped = "duration_s 75 clamped to 10"+ extra = {"ignored_options": ["temperature"], "applied": {"duration_s": 10.0, "flag": True}}+ answers = [_take(f"var-{n}", warnings = [ignored, clamped], **extra) for n in (1, 2)]+ answers.append(JSONResponse({"detail": "Broke."}, status_code = 500))+ result, _studio = _music(monkeypatch, sequence(*answers))+ out = result["structuredContent"]+ assert [c["id"] for c in out["clips"]] == ["var-1", "var-2"]+ assert out["ignored_options"] == ["temperature"]+ assert out["applied"] == {"duration_s": 10.0}+ assert out["warnings"][:2] == [ignored, clamped]+ assert out["warnings"][2].startswith("Take 3 of 3 failed: Broke.")+++def test_more_than_four_variations_is_refused_before_any_take(monkeypatch):+ result, studio = _music(monkeypatch, _take("c1"), args = {**SONG, "variations": 6})+ assert result["isError"] is True+ assert "variations must be 1 to 4" in result["content"][0]["text"]+ assert bodies(studio, "/v1/audio/run") == []+++def test_a_failed_first_take_is_the_tool_error(monkeypatch):+ stopped = {"code": "runtime_stopped", "message": "The audio runtime stopped: exit 9."}+ run = JSONResponse({"detail": stopped}, status_code = 500)+ result, studio = _music(monkeypatch, run)+ assert result["isError"] is True+ assert result["content"][0]["text"].startswith("[runtime_stopped] The audio runtime stopped")+ assert len(bodies(studio, "/v1/audio/run")) == 1+++def test_a_dead_runtime_answer_tells_the_agent_to_retry_once(monkeypatch):+ stopped = {"code": "runtime_stopped", "message": "The audio runtime stopped: exit 9."}+ failed = JSONResponse({"detail": stopped}, status_code = 500)+ studio = fake_studio({RUN: sequence(failed, _take("clip-1")), **GALLERY})+ args = {"workflow": "speak", "text": "Hi"}+ first, _ = run_tool(monkeypatch, studio, "generate_audio", args)+ assert first["isError"] is True and "Retry once" in first["content"][0]["text"]+ retried, _ = run_tool(monkeypatch, studio, "generate_audio", args)+ assert retried["isError"] is False+ assert [c["id"] for c in retried["structuredContent"]["clips"]] == ["clip-1"]+++CANCEL_PAYLOADS = {("POST", CANCEL): {"cancelled": 1}}+++def test_cancel_audio_with_no_mcp_run_asks_the_route(monkeypatch):+ args = {"kind": "audio", "id": "run-x"}+ result, studio = run_tool(monkeypatch, CANCEL_PAYLOADS, "cancel", args)+ assert result["structuredContent"]["cancelled"] is True+ assert bodies(studio, CANCEL) == [{"cancel_id": "run-x"}]+++def test_cancel_audio_between_takes_stops_the_run_without_the_route(monkeypatch):+ run = audio._RUNS[("owner", "run-abc12")] = audio._Run()+ args = {"kind": "audio", "id": "run-abc12"}+ result, studio = run_tool(monkeypatch, CANCEL_PAYLOADS, "cancel", args)+ assert result["structuredContent"]["cancelled"] is True+ assert "before its next take" in result["structuredContent"]["message"]+ assert run.cancelled is True+ assert studio.state.calls == []+++def test_cancel_audio_during_a_take_stops_it_through_the_route(monkeypatch):+ audio._RUNS[("owner", "run-abc12")] = audio._Run(in_flight = True)+ routes = {("POST", CANCEL): {"cancelled": 0}}+ args = {"kind": "audio", "id": "run-abc12"}+ result, studio = run_tool(monkeypatch, routes, "cancel", args)+ assert result["structuredContent"]["cancelled"] is True+ assert bodies(studio, CANCEL) == [{"cancel_id": "run-abc12"}]+++def test_cancel_audio_needs_an_id(monkeypatch):+ result, studio = run_tool(monkeypatch, {}, "cancel", {"kind": "audio"}, fallback = {})+ assert result["isError"] is True and "needs id" in result["content"][0]["text"]+ assert studio.state.calls == []+++def test_an_unsaved_oversized_later_take_keeps_the_saved_takes(monkeypatch):+ from studio_mcp.media import INLINE_CAP++ from .mcp_harness import WAV, b64++ big = WAV + b"\x00" * INLINE_CAP+ unsaved = {"clips": [], "model": "ace-step", "audio": {"data": b64(big), "format": "wav"}}+ result, studio = _music(monkeypatch, sequence(_take("var-1"), unsaved))+ assert result["isError"] is False, result+ out = result["structuredContent"]+ assert [c["id"] for c in out["clips"]] == ["var-1"] and out["group_id"]+ assert "too large to return inline" in out["warnings"][-1]+ assert len(bodies(studio, "/v1/audio/run")) == 2diff --git a/studio/backend/tests/test_mcp_no_host_paths.py b/studio/backend/tests/test_mcp_no_host_paths.pyindex e54afff521c..4f1f19adf8a 100644--- a/studio/backend/tests/test_mcp_no_host_paths.py+++ b/studio/backend/tests/test_mcp_no_host_paths.py@@ -30,6 +30,7 @@ from .mcp_harness import (poison,served,)+from .test_mcp_audio_reports import ANALYZE_PAYLOADSfrom .test_mcp_tools_audio import PAYLOADS as AUDIO_PAYLOADSfrom .test_mcp_tools_audio import TRANSCRIPT, VERBOSEfrom .test_mcp_tools_cancel import PAYLOADS as CANCEL_PAYLOADS@@ -103,12 +104,18 @@ CASES: dict[str, Any] = {(DATASETS_PAYLOADS, {"action": "check_format", "name": "a/b"}),(DATASETS_PAYLOADS, {"action": "status", "repo_id": "a/b"}),],+ "analyze_audio": (ANALYZE_PAYLOADS, {"audio": WAV_INPUT, "expected_text": "hello"}),"transcribe": (TRANSCRIBE_PAYLOADS, {"audio": WAV_INPUT, "timestamps": True, "language": "en"}),"generate_audio": (AUDIO_PAYLOADS, {"workflow": "clone", "text": "hi", "reference": WAV_INPUT}),}# Output keys that carry model-written text, which is the model's to say and is never rewritten.-MODEL_TEXT = {"chat": {"text"}, "transcribe": {"text"}, "get_job": {"data_rows"}}+MODEL_TEXT = {+ "chat": {"text"},+ "transcribe": {"text"},+ "get_job": {"data_rows"},+ "analyze_audio": {"transcript"},+}def leaks(result: dict, ignore: frozenset = frozenset()) -> Optional[str]:@@ -134,7 +141,7 @@ def run_case(monkeypatch, mcp, name: str, routes: dict, args: dict) -> dict:def test_every_registered_tool_has_a_host_path_case():names = {tool.name for tool in asyncio.run(create_studio_mcp().list_tools())}assert set(MODEL_TEXT) <= set(CASES) == names- assert len(names) == 18+ assert len(names) == len(CASES)@pytest.mark.parametrize("name", sorted(CASES))diff --git a/studio/backend/tests/test_mcp_server.py b/studio/backend/tests/test_mcp_server.pyindex eccb3d31154..0ca00609e57 100644--- a/studio/backend/tests/test_mcp_server.py+++ b/studio/backend/tests/test_mcp_server.py@@ -7,12 +7,31 @@ import pytestfrom mcp_server import INSTRUCTIONS, create_studio_mcp-READ_ONLY = {"studio_status", "list_models", "embed", "system_one", "get_job", "list_training_runs"}-DESTRUCTIVE = {"unload_model", "cancel"}-OTHERS = set(- "load_model chat generate_image generate_audio transcribe generate_video run_recipe datasets"- " start_training export_model".split()-)+READ_ONLY = {+ "embed",+ "get_job",+ "list_models",+ "list_training_runs",+ "studio_status",+ "system_one",+}+DESTRUCTIVE = {+ "cancel",+ "unload_model",+}+OTHERS = {+ "analyze_audio",+ "chat",+ "datasets",+ "export_model",+ "generate_audio",+ "generate_image",+ "generate_video",+ "load_model",+ "run_recipe",+ "start_training",+ "transcribe",+}def _tools():@@ -22,7 +41,7 @@ def _tools():def test_studio_mcp_registers_control_plane_tools():tools = _tools()assert READ_ONLY | DESTRUCTIVE | OTHERS == set(tools)- assert len(tools) == 18+ assert len(tools) == len(READ_ONLY | DESTRUCTIVE | OTHERS)for name, tool in tools.items():hints = tool.annotationsassert hints is not None, name@@ -62,7 +81,18 @@ SCHEMA_FACTS = [("cancel","parameters/properties/kind/enum",- lambda kinds: "audio" not in kinds and len(kinds) == 9,+ lambda kinds: {+ "training",+ "training_start",+ "diffusion_training",+ "export",+ "recipe",+ "image",+ "video",+ "chat",+ "dataset_download",+ }+ <= set(kinds),),("get_job", "parameters/properties/kind/enum", lambda kinds: {"video", "recipe"} <= set(kinds)),]diff --git a/studio/backend/tests/test_mcp_tool_contract.py b/studio/backend/tests/test_mcp_tool_contract.pyindex 0c262600c14..3fa1ff1e498 100644--- a/studio/backend/tests/test_mcp_tool_contract.py+++ b/studio/backend/tests/test_mcp_tool_contract.py@@ -339,6 +339,16 @@ PATH_CALLS = [and form(studio, "/v1/audio/transcriptions")["file"] == ("mcp-input.wav", WAV)),),+ (+ "analyze_audio",+ lambda path: {"audio": {"path": path}},+ {+ ("POST", "/v1/audio/inputs"): JSONResponse({"id": "in-1"}, status_code = 201),+ ("POST", "/api/inference/audio/analyze"): {"duration_s": 1.0},+ },+ ["/v1/audio/inputs", "/api/inference/audio/analyze"],+ lambda result, studio: call_to(studio, "/v1/audio/inputs")[3] == WAV,+ ),("generate_image",lambda path: {"prompt": "x", "init_image": {"path": path}},@@ -357,7 +367,7 @@ PATH_CALLS = [def _input_file(tmp_path, tool):- audio = tool in ("generate_audio", "transcribe")+ audio = tool in ("generate_audio", "transcribe", "analyze_audio")path = tmp_path / ("mcp-input.wav" if audio else "mcp-input.png")path.write_bytes(WAV if audio else PNG)return str(path)diff --git a/studio/backend/tests/test_mcp_tools_audio.py b/studio/backend/tests/test_mcp_tools_audio.pyindex 1adc6d9b34f..45684bf1be3 100644--- a/studio/backend/tests/test_mcp_tools_audio.py+++ b/studio/backend/tests/test_mcp_tools_audio.py@@ -310,8 +310,8 @@ def test_small_audio_runs_on_the_loaded_non_transformers_model(monkeypatch):SEGMENTS = [- {"start": 0.0, "end": 1.2, "text": "Hello"},- {"start": 1.2, "end": 2.0, "text": "from Studio."},+ {"start": 0.0, "end": 1.2, "text": "Hello", "speaker": None},+ {"start": 1.2, "end": 2.0, "text": "from Studio.", "speaker": None},]@@ -453,7 +453,10 @@ def test_a_stored_clip_is_transcribed_by_id_with_the_loaded_model("text": "A long meeting.","language": "en","model": model,- "segments": [{"start": 0.0, "end": 5.0, "text": "A long meeting."}] if timestamps else None,+ "segments": [{"start": 0.0, "end": 5.0, "text": "A long meeting.", "speaker": None}]+ if timestamps+ else None,+ "match_score": None,"saved_to_history": True,}extra = {"language": "en"} if "language" in args else {}diff --git a/studio/backend/tests/test_mcp_tools_status.py b/studio/backend/tests/test_mcp_tools_status.pyindex 89b167a6456..48c6fb90952 100644--- a/studio/backend/tests/test_mcp_tools_status.py+++ b/studio/backend/tests/test_mcp_tools_status.py@@ -127,12 +127,14 @@ def test_every_slot_is_reported(monkeypatch):"display_name": "Llama 3.2 1B","is_gguf": True,"audio_options": None,+ "audio_music": None,},{"id": "unsloth/Qwen3-0.6B","display_name": None,"is_gguf": None,"audio_options": None,+ "audio_music": None,},],"loading": ["unsloth/gemma-3-1b-it"],