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: adopt the WAV save_from_media already transcoded instead of decoding it again (#15916)
Daniel Hane5acea16e8db
diff --git a/studio/backend/core/inference/audio_inputs.py b/studio/backend/core/inference/audio_inputs.pyindex 57e2e36cb17..0c55421ae53 100644--- a/studio/backend/core/inference/audio_inputs.py+++ b/studio/backend/core/inference/audio_inputs.py@@ -351,6 +351,7 @@ def save_from_media(rate = info["sample_rate"]with wave.open(str(full), "rb") as reader:total = reader.getnframes()+ channels = reader.getnchannels()first = int(start_s * rate)if first >= total:raise AudioInputError(@@ -359,14 +360,31 @@ def save_from_media("Unsloth Studio can only cut inside the track.",)last = total if end_s is None else min(total, max(first + 1, int(end_s * rate)))- reader.setpos(first)- data = reader.readframes(last - first)- with wave.open(str(cut), "wb") as writer:- writer.setparams(reader.getparams())- writer.writeframes(data)- payload = cut.read_bytes()+ whole = not first and last == total+ if not whole:+ reader.setpos(first)+ with wave.open(str(cut), "wb") as writer:+ writer.setparams(reader.getparams())+ writer.writeframes(reader.readframes(last - first))+ if whole:+ os.replace(full, cut)+ digest = hashlib.sha256()+ with open(cut, "rb") as f:+ for chunk in iter(lambda: f.read(1 << 20), b""):+ digest.update(chunk)+ # transcode() wrote this WAV, so transcoding it again would only rewrite the same bytes.+ prepared = {+ "duration_s": round((last - first) / rate, 3),+ "sample_rate": rate,+ "channels": channels,+ }return _finish_upload(- directory, cut, hashlib.sha256(payload).hexdigest(), _clean_name(name), len(payload)+ directory,+ cut,+ digest.hexdigest(),+ _clean_name(name),+ os.stat(cut).st_size,+ prepared = prepared,)finally:full.unlink(missing_ok = True)@@ -385,8 +403,15 @@ def _find_by_sha(directory: Path, sha: str) -> Optional[tuple[str, dict[str, Anydef _finish_upload(- directory: Path, tmp: Path, sha: str, name: str, size: int+ directory: Path,+ tmp: Path,+ sha: str,+ name: str,+ size: int,+ *,+ prepared: Optional[dict[str, Any]] = None,) -> tuple[dict[str, Any], bool]:+ """``prepared``: ``tmp`` is already a ``transcode`` output with this info, so it is moved in as is."""existing = _find_by_sha(directory, sha)if existing is not None:input_id, meta = existing@@ -396,7 +421,11 @@ def _finish_upload(input_id = uuid.uuid4().hexwav = directory / f"{input_id}.wav"try:- info = transcode(tmp, wav)+ if prepared is None:+ info = transcode(tmp, wav)+ else:+ os.replace(tmp, wav)+ info = preparednow = _now()meta = {"name": name,diff --git a/studio/backend/tests/test_audio_inputs.py b/studio/backend/tests/test_audio_inputs.pyindex cb97d9b344c..5672accb6b5 100644--- a/studio/backend/tests/test_audio_inputs.py+++ b/studio/backend/tests/test_audio_inputs.py@@ -4,7 +4,9 @@from __future__ import annotationsimport asyncio+import hashlibimport io+import jsonimport mathimport osimport time@@ -377,3 +379,47 @@ def test_the_sweep_reaps_a_prepared_upload_a_crash_left_behind():os.utime(path, (time.time() - age, time.time() - age))audio_inputs.sweep()assert not stale.exists() and waiting.exists()+++ "fmt, codec, rate, layout, start_s, end_s",+ [+ ("mp4", "aac", 44100, "stereo", 0.25, 1.1),+ ("wav", "pcm_s16le", 96000, "mono", 0.0, None),+ ("flac", "flac", 16000, "5.1", 0.5, None),+ ],+)+def test_a_media_cut_is_decoded_once_and_stored_as_cut(+ tmp_path, monkeypatch, fmt, codec, rate, layout, start_s, end_s+):+ src = tmp_path / f"clip.{fmt}"+ src.write_bytes(encode(fmt, codec, rate, layout, 1.5))+ whole = tmp_path / "whole.wav"+ audio_inputs.transcode(src, whole)+ with wave.open(str(whole)) as r:+ out_rate, channels, total = r.getframerate(), r.getnchannels(), r.getnframes()+ first = int(start_s * out_rate)+ last = total if end_s is None else min(total, int(end_s * out_rate))+ r.setpos(first)+ expected = io.BytesIO()+ with wave.open(expected, "wb") as w:+ w.setparams(r.getparams())+ w.writeframes(r.readframes(last - first))++ calls = []+ real = audio_inputs.transcode+ monkeypatch.setattr(audio_inputs, "transcode", lambda *a, **k: calls.append(a) or real(*a, **k))+ record, created = audio_inputs.save_from_media(src, "clip", start_s, end_s)+ assert created and len(calls) == 1+ stored = audio_inputs.inputs_dir() / f"{record['id']}.wav"+ assert stored.read_bytes() == expected.getvalue()+ assert record["sample_rate"] == out_rate and record["channels"] == channels+ assert record["duration_s"] == round((last - first) / out_rate, 3)+ meta = json.loads((audio_inputs.inputs_dir() / f"{record['id']}.json").read_text())+ assert meta["sha256"] == hashlib.sha256(expected.getvalue()).hexdigest()+ assert meta["bytes"] == len(expected.getvalue())++ again, created = audio_inputs.save_from_media(src, "clip", start_s, end_s)+ assert not created and again["id"] == record["id"]+ leftovers = [p.name for p in audio_inputs.inputs_dir().iterdir() if p.name.startswith(".")]+ assert leftovers == []