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: keep audio gallery operations in the folder they resolved (#15875)
Daniel Hanf031e8bab77b
diff --git a/studio/backend/core/inference/audio_edit.py b/studio/backend/core/inference/audio_edit.pyindex ccec156e682..15aa1678890 100644--- a/studio/backend/core/inference/audio_edit.py+++ b/studio/backend/core/inference/audio_edit.py@@ -234,9 +234,9 @@ def save_source_clip(and meta.get("role") == "source"and meta.get("workflow") == "edit"and meta.get("source_sha256") == sha- and audio_gallery.owned_audio_path(path.stem) is not None+ and audio_gallery.owned_audio_path(path.stem, directory) is not None):- return audio_gallery._record(path.stem, meta)+ return audio_gallery._record(path.stem, meta, directory = directory)with wave.open(str(prepared_path)) as w:sample_rate, duration_s = w.getframerate(), round(w.getnframes() / w.getframerate(), 3)meta: dict[str, Any] = {diff --git a/studio/backend/core/inference/audio_gallery.py b/studio/backend/core/inference/audio_gallery.pyindex d1d2d885286..1333f72cc58 100644--- a/studio/backend/core/inference/audio_gallery.py+++ b/studio/backend/core/inference/audio_gallery.py@@ -51,7 +51,7 @@ def save(wav_tmp = directory / f".{audio_id}.wav.tmp"sidecar = directory / f"{audio_id}.json"sidecar_tmp = directory / f".{audio_id}.json.tmp"- source_path = _source_path(audio_id)+ source_path = _source_path(audio_id, directory)source_tmp = directory / f".{audio_id}.source.wav.tmp"if source_wav is not None:meta = {**meta, "source_saved": True}@@ -72,7 +72,7 @@ def save(passraise_prune_to_cap()- return _record(audio_id, meta)+ return _record(audio_id, meta, directory = directory)def save_file(@@ -107,7 +107,7 @@ def save_file(raiseif prune:_prune_to_cap()- return _record(audio_id, meta)+ return _record(audio_id, meta, directory = directory)# /v1/audio/speech persists every call, so cap the gallery to avoid filling the disk.@@ -138,9 +138,9 @@ def _env_limit(name: str, default: int) -> int:return 0-def _clip_bytes(audio_id: str) -> int:+def _clip_bytes(audio_id: str, directory: Path) -> int:total = 0- for path in (gallery_dir() / f"{audio_id}.wav", _source_path(audio_id)):+ for path in (directory / f"{audio_id}.wav", _source_path(audio_id, directory)):try:total += path.stat().st_sizeexcept OSError:@@ -160,15 +160,17 @@ def _prune_to_cap() -> int:# Select AND delete under one lock, as clear() does; choosing victims from a snapshot lets an archive land and be deleted anyway.with gallery_flags.exclusive(directory, require_file_lock = True):# By age, not display order: a clip dragged down is not older. Pinned clips are exempt.- entries = [e for e in _list_audio_entries() if not e[0].get("pinned")]- entries.sort(key = lambda e: _mtime(gallery_dir() / f"{e[0]['id']}.wav"), reverse = True)+ entries = [+ e for e in _list_audio_entries(directory = directory) if not e[0].get("pinned")+ ]+ entries.sort(key = lambda e: _mtime(directory / f"{e[0]['id']}.wav"), reverse = True)# Newest first, so the cut point drops the oldest; the newest is always kept, never a silent failurekeep = len(entries) if cap <= 0 else min(cap, len(entries))if byte_cap > 0:running = 0for index, (record, _cursor) in enumerate(entries[:keep]):- running += _clip_bytes(record["id"])+ running += _clip_bytes(record["id"], directory)if running > byte_cap and index > 0:keep = indexbreak@@ -191,8 +193,8 @@ def _prune_to_cap() -> int:for audio_id in victims:if audio_id in in_use:continue- path = audio_path(audio_id)- if path is None or _read_meta(_sidecar_path(audio_id)) is None:+ path = audio_path(audio_id, directory)+ if path is None or _read_meta(_sidecar_path(audio_id, directory)) is None:continuetry:path.unlink()@@ -201,9 +203,9 @@ def _prune_to_cap() -> int:continueremoved += 1pruned.append(audio_id)- _remove_source(audio_id)+ _remove_source(audio_id, directory)try:- _sidecar_path(audio_id).unlink()+ _sidecar_path(audio_id, directory).unlink()except OSError:passif pruned:@@ -228,9 +230,12 @@ def _record(audio_id: str,meta: dict[str, Any],flags: Optional[dict[str, dict[str, Any]]] = None,+ directory: Optional[Path] = None,) -> dict[str, Any]:+ if directory is None:+ directory = gallery_dir()if flags is None:- flags = gallery_flags.read(gallery_dir())+ flags = gallery_flags.read(directory)return {**meta,"workflow": _workflow(meta),@@ -239,7 +244,7 @@ def _record(**gallery_flags.flags_for(flags, audio_id),# The listing's own sort key, so a client re-sort agrees with it."order_at": gallery_flags.order_rank(- flags, audio_id, _mtime(gallery_dir() / f"{audio_id}.wav")+ flags, audio_id, _mtime(directory / f"{audio_id}.wav")),}@@ -251,29 +256,31 @@ def _workflow(meta: dict[str, Any]) -> str:return workflow_for_audio_type(meta.get("audio_type"))-def audio_path(audio_id: str) -> Optional[Path]:+def audio_path(audio_id: str, directory: Optional[Path] = None) -> Optional[Path]:"""Resolve an id to its on-disk WAV, or None if missing or unsafe."""if not _ID_RE.match(audio_id):return None- path = gallery_dir() / f"{audio_id}.wav"+ if directory is None:+ directory = gallery_dir()+ path = directory / f"{audio_id}.wav"try:- path.resolve().relative_to(gallery_dir().resolve())+ path.resolve().relative_to(directory.resolve())except ValueError:return Nonereturn path if path.is_file() else None-def _sidecar_path(audio_id: str) -> Path:- return gallery_dir() / f"{audio_id}.json"+def _sidecar_path(audio_id: str, directory: Optional[Path] = None) -> Path:+ return (gallery_dir() if directory is None else directory) / f"{audio_id}.json"-def _source_path(audio_id: str) -> Path:- return gallery_dir() / f"{audio_id}.source.wav"+def _source_path(audio_id: str, directory: Optional[Path] = None) -> Path:+ return (gallery_dir() if directory is None else directory) / f"{audio_id}.source.wav"-def _remove_source(audio_id: str) -> None:+def _remove_source(audio_id: str, directory: Optional[Path] = None) -> None:try:- _source_path(audio_id).unlink(missing_ok = True)+ _source_path(audio_id, directory).unlink(missing_ok = True)except OSError:pass@@ -291,7 +298,7 @@ def _sources_in_use(directory: Path, leaving: set[str]) -> set[str]:sources: set[str] = set()used: set[str] = set()for path in paths:- meta = _read_meta(_sidecar_path(path.stem))+ meta = _read_meta(_sidecar_path(path.stem, directory))if meta is None:continueif meta.get("role") == "source":@@ -327,20 +334,23 @@ def _read_meta(sidecar: Path) -> Optional[dict[str, Any]]:return meta-def owned_audio_path(audio_id: str) -> Optional[Path]:+def owned_audio_path(audio_id: str, directory: Optional[Path] = None) -> Optional[Path]:"""Serve routes use this, not audio_path(), so a guessed stem for an orphan WAV cannot stream out."""- path = audio_path(audio_id)- if path is None or _read_meta(_sidecar_path(audio_id)) is None:+ if directory is None:+ directory = gallery_dir()+ path = audio_path(audio_id, directory)+ if path is None or _read_meta(_sidecar_path(audio_id, directory)) is None:return Nonereturn pathdef owned_source_path(audio_id: str) -> Optional[Path]:- if owned_audio_path(audio_id) is None:+ directory = gallery_dir()+ if owned_audio_path(audio_id, directory) is None:return None- path = _source_path(audio_id)+ path = _source_path(audio_id, directory)try:- path.resolve().relative_to(gallery_dir().resolve())+ path.resolve().relative_to(directory.resolve())except ValueError:return Nonereturn path if path.is_file() else None@@ -377,12 +387,15 @@ def _list_audio_entries(before: Optional[GalleryCursor] = None,valid: Optional[Callable[[dict[str, Any]], bool]] = None,archived: bool = False,+ directory: Optional[Path] = None,) -> list[tuple[dict[str, Any], GalleryCursor]]:+ if directory is None:+ directory = gallery_dir()try:- paths = _clip_wavs(gallery_dir())+ paths = _clip_wavs(directory)except OSError:return []- flags = gallery_flags.read(gallery_dir())+ flags = gallery_flags.read(directory)paths = [p for p in paths if gallery_flags.is_archived(flags, p.stem) == archived]keyed_paths = [(_sort_key(flags, path), path) for path in paths]keyed_paths.sort(key = lambda item: item[0], reverse = True)@@ -391,10 +404,10 @@ def _list_audio_entries(for cursor, path in keyed_paths:if before is not None and cursor >= before:continue- meta = _read_meta(_sidecar_path(path.stem))+ meta = _read_meta(_sidecar_path(path.stem, directory))if meta is None:continue- record = _record(path.stem, meta, flags)+ record = _record(path.stem, meta, flags, directory)if valid is not None and not valid(record):continueentries.append((record, cursor))@@ -439,40 +452,42 @@ def set_flags(archived: Optional[bool] = None,) -> Optional[dict[str, Any]]:"""Pin or archive one owned clip; None when the id is not an Unsloth-owned clip."""- with gallery_flags.exclusive(gallery_dir()):- if owned_audio_path(audio_id) is None:+ directory = gallery_dir()+ with gallery_flags.exclusive(directory):+ if owned_audio_path(audio_id, directory) is None:return None- gallery_flags.set_flags_locked(gallery_dir(), audio_id, pinned = pinned, archived = archived)- meta = _read_meta(_sidecar_path(audio_id))+ gallery_flags.set_flags_locked(directory, audio_id, pinned = pinned, archived = archived)+ meta = _read_meta(_sidecar_path(audio_id, directory))if meta is None:return None- return _record(audio_id, meta)+ return _record(audio_id, meta, directory = directory)def move(audio_id: str, after_id: Optional[str]) -> Optional[dict[str, Any]]:"""Move an active clip to just after ``after_id`` (None = front) and return its record.None if the id is not an owned, active clip. Raises KeyError if ``after_id`` is not in history."""- with gallery_flags.exclusive(gallery_dir()):- if owned_audio_path(audio_id) is None:+ directory = gallery_dir()+ with gallery_flags.exclusive(directory):+ if owned_audio_path(audio_id, directory) is None:return None- flags = gallery_flags.read(gallery_dir())+ flags = gallery_flags.read(directory)if gallery_flags.is_archived(flags, audio_id):return None# The whole shelf in listing order, so neighbours past the client's loaded window are known.try:paths = [- p for p in _clip_wavs(gallery_dir()) if not gallery_flags.is_archived(flags, p.stem)+ p for p in _clip_wavs(directory) if not gallery_flags.is_archived(flags, p.stem)]except OSError:paths = []paths.sort(key = lambda p: _sort_key(flags, p), reverse = True)keyed = [(p.stem, _mtime(p)) for p in paths]- gallery_flags.place_locked(gallery_dir(), audio_id, keyed, after_id = after_id)- meta = _read_meta(_sidecar_path(audio_id))+ gallery_flags.place_locked(directory, audio_id, keyed, after_id = after_id)+ meta = _read_meta(_sidecar_path(audio_id, directory))if meta is None:return None- return _record(audio_id, meta)+ return _record(audio_id, meta, directory = directory)def delete(audio_id: str, *, cascade: bool = True) -> bool:@@ -481,11 +496,15 @@ def delete(audio_id: str, *, cascade: bool = True) -> bool:WAV first: sidecar-first then failing would lose a clip with no retry, while thisleaves at worst an orphan sidecar list_audio ignores."""- path = audio_path(audio_id)+ return _delete_in(gallery_dir(), audio_id, cascade)+++def _delete_in(directory: Path, audio_id: str, cascade: bool) -> bool:+ path = audio_path(audio_id, directory)if path is None:return False# only delete a pair we own; a foreign or orphan wav must not be destroyed by a guessed id- meta = _read_meta(_sidecar_path(audio_id))+ meta = _read_meta(_sidecar_path(audio_id, directory))if meta is None:return Falsetry:@@ -494,20 +513,20 @@ def delete(audio_id: str, *, cascade: bool = True) -> bool:logger.warning("audio_gallery.delete_failed: %s", exc)return Falsetry:- _sidecar_path(audio_id).unlink()+ _sidecar_path(audio_id, directory).unlink()except OSError:pass- _remove_source(audio_id)- gallery_flags.forget(gallery_dir(), [audio_id])+ _remove_source(audio_id, directory)+ gallery_flags.forget(directory, [audio_id])# An Edit's hidden Original goes with the last clip that plays it.original = str(meta.get("source_clip_id") or "")if (cascadeand _ID_RE.match(original)- and (_read_meta(_sidecar_path(original)) or {}).get("role") == "source"- and original not in _sources_in_use(gallery_dir(), set())+ and (_read_meta(_sidecar_path(original, directory)) or {}).get("role") == "source"+ and original not in _sources_in_use(directory, set())):- delete(original)+ _delete_in(directory, original, True)return True@@ -525,7 +544,7 @@ def delete_group(group_id: str) -> int:return 0removed: list[str] = []for path in paths:- if (_read_meta(_sidecar_path(path.stem)) or {}).get(+ if (_read_meta(_sidecar_path(path.stem, directory)) or {}).get("group_id") != group_id or gallery_flags.is_archived(flags, path.stem):continue@@ -536,10 +555,10 @@ def delete_group(group_id: str) -> int:continueremoved.append(path.stem)try:- _sidecar_path(path.stem).unlink()+ _sidecar_path(path.stem, directory).unlink()except OSError:pass- _remove_source(path.stem)+ _remove_source(path.stem, directory)gallery_flags.forget_locked(directory, removed)return len(removed)@@ -556,7 +575,7 @@ def clear(include_archived: bool = False, workflow: Optional[str] = None) -> intreturn 0doomed = []for path in paths:- meta = _read_meta(_sidecar_path(path.stem))+ meta = _read_meta(_sidecar_path(path.stem, directory))if meta is None:continueif workflow is not None and _workflow(meta) != workflow:@@ -575,9 +594,9 @@ def clear(include_archived: bool = False, workflow: Optional[str] = None) -> intcontinueremoved += 1cleared.append(path.stem)- _remove_source(path.stem)+ _remove_source(path.stem, directory)try:- _sidecar_path(path.stem).unlink()+ _sidecar_path(path.stem, directory).unlink()except OSError:passif include_archived and workflow is None and not gallery_flags.is_trusted(directory):diff --git a/studio/backend/core/inference/audio_inputs.py b/studio/backend/core/inference/audio_inputs.pyindex 0c55421ae53..d912a6b1865 100644--- a/studio/backend/core/inference/audio_inputs.py+++ b/studio/backend/core/inference/audio_inputs.py@@ -630,7 +630,7 @@ def resolve_source(ref: dict[str, Any]) -> Source:path = audio_gallery.owned_audio_path(source_id) if _valid_id(source_id) else Noneif path is None:raise AudioInputError(404, "That clip is no longer in your history.")- meta = audio_gallery._read_meta(audio_gallery._sidecar_path(source_id)) or {}+ meta = audio_gallery._read_meta(audio_gallery._sidecar_path(source_id, path.parent)) or {}prompt = " ".join(str(meta.get("prompt") or "").split())return Source("clip", source_id, path, (prompt[:60] or "Clip"))from core.inference import audio_voicesdiff --git a/studio/backend/tests/test_audio_gallery.py b/studio/backend/tests/test_audio_gallery.pyindex ca1b9b0aba6..9c15c745470 100644--- a/studio/backend/tests/test_audio_gallery.py+++ b/studio/backend/tests/test_audio_gallery.py@@ -1040,3 +1040,88 @@ def test_an_archive_racing_a_group_delete_is_never_lost(monkeypatch):threading.Event().wait(0.1)# The archive either lands first and spares the stem, or waits and finds it gone.assert archived and (archived[0] is None) == (gallery.audio_path(target) is None)+++def _relocate_after_first_call(monkeypatch, tmp_path, records):+ """Settings relocation mid-call: later gallery_dir() calls see a folder with same-id pairs to leave alone."""+ old = gallery.gallery_dir()+ new = tmp_path / "relocated"+ new.mkdir()+ for record in records:+ (new / f"{record['id']}.wav").write_bytes(_wav())+ (new / f"{record['id']}.json").write_text(+ json.dumps(_meta(prompt = "new folder")), encoding = "utf-8"+ )+ calls = []++ def _moving_dir():+ calls.append(1)+ return old if len(calls) == 1 else new++ monkeypatch.setattr(gallery, "gallery_dir", _moving_dir)+ return old, new+++def _gone_from_old_kept_in_new(old, new, audio_id):+ assert not (old / f"{audio_id}.wav").exists()+ assert not (old / f"{audio_id}.json").exists()+ assert (new / f"{audio_id}.wav").exists()+ assert (new / f"{audio_id}.json").exists()+++def test_clear_uses_the_locked_folder_when_the_folder_moves_mid_clear(monkeypatch, tmp_path):+ record = gallery.save(_wav(), _meta(prompt = "old folder"))+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [record])+ assert gallery.clear(include_archived = True) == 1+ _gone_from_old_kept_in_new(old, new, record["id"])+++def test_delete_group_uses_the_locked_folder_when_the_folder_moves(monkeypatch, tmp_path):+ record = gallery.save(_wav(), _meta(prompt = "stem", group_id = "run-1"))+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [record])+ (new / f"{record['id']}.json").write_text(+ json.dumps(_meta(prompt = "new folder", group_id = "run-1")),+ encoding = "utf-8",+ )+ assert gallery.delete_group("run-1") == 1+ _gone_from_old_kept_in_new(old, new, record["id"])+++def test_delete_uses_one_folder_when_the_folder_moves(monkeypatch, tmp_path):+ record = gallery.save(_wav(), _meta(prompt = "old folder"))+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [record])+ assert gallery.delete(record["id"]) is True+ _gone_from_old_kept_in_new(old, new, record["id"])+++def test_prune_uses_the_locked_folder_when_the_folder_moves(monkeypatch, tmp_path):+ oldest = _save_with_mtime("oldest", 100.0)+ newest = _save_with_mtime("newest", 200.0)+ monkeypatch.setenv("UNSLOTH_AUDIO_GALLERY_MAX_CLIPS", "1")+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [oldest])+ assert gallery._prune_to_cap() == 1+ _gone_from_old_kept_in_new(old, new, oldest["id"])+ assert (old / f"{newest['id']}.wav").exists()+++def test_delete_takes_the_hidden_original_from_the_same_folder_when_the_folder_moves(+ monkeypatch, tmp_path+):+ source = gallery.save(_wav(), _meta(workflow = "edit", role = "source"))+ edit = gallery.save(_wav(), _meta(workflow = "edit", role = "output", source_clip_id = source["id"]))+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [edit, source])+ (new / f"{source['id']}.json").write_text(+ json.dumps(_meta(workflow = "edit", role = "source")), encoding = "utf-8"+ )+ assert gallery.delete(edit["id"]) is True+ _gone_from_old_kept_in_new(old, new, edit["id"])+ _gone_from_old_kept_in_new(old, new, source["id"])+++def test_owned_source_path_stays_in_the_folder_it_checked(monkeypatch, tmp_path):+ source = tmp_path / "source.wav"+ source.write_bytes(_wav())+ kept = gallery.save(_wav(), _meta(), source)+ old, new = _relocate_after_first_call(monkeypatch, tmp_path, [kept])+ (new / f"{kept['id']}.source.wav").write_bytes(_wav())+ assert gallery.owned_source_path(kept["id"]) == old / f"{kept['id']}.source.wav"