Skip to content

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.

Studio: list saved voices with one folder lookup and one sidecar read each (#16354)

Daniel Han65829fc43ae5
diff --git a/studio/backend/core/inference/audio_voices.py b/studio/backend/core/inference/audio_voices.py
index 68f627fc028..12892eeb44f 100644
--- a/studio/backend/core/inference/audio_voices.py
+++ b/studio/backend/core/inference/audio_voices.py
@@ -40,15 +40,15 @@ def voices_dir() -> Path:
return directory
-def _sidecar(voice_id: str) -> Path:
- return voices_dir() / f"{voice_id}.json"
+def _sidecar(voice_id: str, directory: Optional[Path] = None) -> Path:
+ return (directory or voices_dir()) / f"{voice_id}.json"
-def _read(voice_id: str) -> Optional[dict[str, Any]]:
+def _read(voice_id: str, directory: Optional[Path] = None) -> Optional[dict[str, Any]]:
if not _valid_id(voice_id):
return None
try:
- meta = json.loads(_sidecar(voice_id).read_text(encoding = "utf-8"))
+ meta = json.loads(_sidecar(voice_id, directory).read_text(encoding = "utf-8"))
except (OSError, ValueError, UnicodeError):
return None
if not isinstance(meta, dict) or any(k not in meta for k in _REQUIRED):
@@ -83,11 +83,22 @@ def _clean_name(value: Optional[str]) -> str:
return text
-def _ids() -> list[str]:
+def _scan() -> tuple[Optional[Path], list[tuple[str, dict[str, Any]]]]:
+ """The voices folder (resolved once: a settings lookup + mkdir) and each valid sidecar."""
try:
- return [p.stem for p in voices_dir().glob("*.json") if _read(p.stem) is not None]
+ directory = voices_dir()
+ found = []
+ for path in directory.glob("*.json"):
+ meta = _read(path.stem, directory)
+ if meta is not None:
+ found.append((path.stem, meta))
+ return directory, found
except OSError:
- return []
+ return None, []
+
+
+def _ids() -> list[str]:
+ return [voice_id for voice_id, _meta in _scan()[1]]
def create(src_path: Path, meta: dict[str, Any]) -> dict[str, Any]:
@@ -127,9 +138,9 @@ def create(src_path: Path, meta: dict[str, Any]) -> dict[str, Any]:
def list_voices() -> list[dict[str, Any]]:
"""Every saved voice in this account, newest first."""
entries = []
- for voice_id in _ids():
- meta = _read(voice_id)
- if meta is None or not (voices_dir() / f"{voice_id}.wav").is_file():
+ directory, found = _scan()
+ for voice_id, meta in found:
+ if not (directory / f"{voice_id}.wav").is_file():
continue
created = float(meta.get("created_at_epoch") or 0.0)
entries.append((created, meta["created_at"], voice_id, meta))
diff --git a/studio/backend/tests/test_audio_voices_scan.py b/studio/backend/tests/test_audio_voices_scan.py
new file mode 100644
index 00000000000..04a29749b7c
--- /dev/null
+++ b/studio/backend/tests/test_audio_voices_scan.py
@@ -0,0 +1,76 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""list_voices resolves the folder once, with the same skips and order as before."""
+
+from __future__ import annotations
+
+import json
+import uuid
+
+import pytest
+
+import core.inference.audio_gallery as gallery
+from core.inference import audio_voices
+
+
[email protected](autouse = True)
+def _tmp_gallery(monkeypatch, tmp_path):
+ monkeypatch.setattr(gallery, "studio_root", lambda: tmp_path)
+
+
+def _voice(
+ i,
+ *,
+ epoch = None,
+ created = "2026-01-01T00:00:00Z",
+ wav = True,
+ body = None,
+):
+ vid = uuid.UUID(int = i + 1).hex
+ folder = audio_voices.voices_dir()
+ meta = {"name": f"v{i}", "duration_s": 1.0, "sample_rate": 24000, "created_at": created}
+ if epoch is not None:
+ meta["created_at_epoch"] = epoch
+ (folder / f"{vid}.json").write_text(json.dumps(meta) if body is None else body, "utf-8")
+ if wav:
+ (folder / f"{vid}.wav").write_bytes(b"RIFF")
+ return vid
+
+
+def test_skips_corrupt_partial_and_lone_entries_and_keeps_order():
+ newest = _voice(0, epoch = 3.0)
+ tie_a = _voice(1, epoch = 2.0)
+ tie_b = _voice(2, epoch = 2.0)
+ no_epoch = _voice(3)
+ _voice(4, body = '{"name": "x", "duration_s"') # partially written
+ _voice(5, body = "[]")
+ _voice(6, body = json.dumps({"name": "x"}))
+ _voice(7, epoch = 9.0, wav = False)
+ (audio_voices.voices_dir() / f"{uuid.uuid4().hex}.wav").write_bytes(b"RIFF")
+ (audio_voices.voices_dir() / "not-an-id.json").write_text("{}", "utf-8")
+ (audio_voices.voices_dir() / f"{uuid.UUID(int = 99).hex}.json").write_bytes(b"\xff\xfe")
+
+ listed = [v["id"] for v in audio_voices.list_voices()]
+ assert listed == [newest, max(tie_a, tie_b), min(tie_a, tie_b), no_epoch]
+ # The count create() checks includes a sidecar whose WAV is gone, as before.
+ assert len(audio_voices._ids()) == 5
+
+
+def test_list_resolves_the_folder_once(monkeypatch):
+ for i in range(20):
+ _voice(i, epoch = float(i))
+ calls = []
+ real = audio_voices.voices_dir
+ monkeypatch.setattr(audio_voices, "voices_dir", lambda: calls.append(1) or real())
+ assert len(audio_voices.list_voices()) == 20
+ assert len(calls) == 1
+
+
+def test_unreadable_folder_lists_nothing(monkeypatch):
+ def boom():
+ raise OSError("drive gone")
+
+ monkeypatch.setattr(audio_voices, "voices_dir", boom)
+ assert audio_voices.list_voices() == []
+ assert audio_voices._ids() == []