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
# 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 asyncio
import json
from core.inference import transcript_stream
def test_progress_and_saved_result(monkeypatch):
async def scenario():
async def transcribe(progress):
progress({"text": "hello"})
await asyncio.sleep(0.01)
return {"text": "hello world", "model": "tiny", "duration": 2}
monkeypatch.setattr(
transcript_stream.transcript_gallery,
"save",
lambda result, title: {"id": "saved", **result},
)
events = [
json.loads(line)
async for line in transcript_stream.stream_transcript(transcribe, "clip")
]
assert any(event.get("text") == "hello" for event in events)
assert events[-1]["record"]["id"] == "saved"
assert events[-1]["text"] == "hello world"
asyncio.run(scenario())
def test_phases_are_never_throttled(monkeypatch):
monkeypatch.setattr(transcript_stream.transcript_gallery, "save", lambda result, title: {})
async def transcribe(progress):
# 50 ms apart: a text update this close to the last one would be throttled away.
for phase in ("loading", "transcribing"):
progress({"text": "", "phase": phase})
await asyncio.sleep(0.05)
return {"text": "done", "model": "moss"}
async def collect(stream):
return [json.loads(line) async for line in stream]
events = asyncio.run(collect(transcript_stream.stream_transcript(transcribe, "clip")))
assert [e["phase"] for e in events if "phase" in e] == ["loading", "transcribing"]
def test_back_to_back_phases_all_reach_the_client(monkeypatch):
"""Back-to-back phases must all reach the client in order; a latest-wins queue evicted loading."""
monkeypatch.setattr(transcript_stream.transcript_gallery, "save", lambda result, title: {})
def worker(progress):
progress({"text": "", "phase": "loading"})
progress({"text": "", "phase": "transcribing"})
async def transcribe(progress):
await asyncio.to_thread(worker, progress)
return {"text": "done", "model": "moss"}
async def collect(stream):
return [json.loads(line) async for line in stream]
for _ in range(20):
events = asyncio.run(collect(transcript_stream.stream_transcript(transcribe, "clip")))
assert [e["phase"] for e in events if "phase" in e] == ["loading", "transcribing"]
assert events[-1]["type"] == "complete"
def test_every_queued_phase_reaches_a_client_that_yields_between_chunks(monkeypatch):
"""StreamingResponse awaits each chunk's send, so the generator is suspended between yields. A
reader still running then could take a queued update off the deque mid-drain and drop it."""
monkeypatch.setattr(transcript_stream.transcript_gallery, "save", lambda result, title: {})
phases = ("loading", "downloading_aligner", "transcribing")
def worker(progress):
for phase in phases:
progress({"text": "", "phase": phase})
async def transcribe(progress):
await asyncio.to_thread(worker, progress)
return {"text": "done", "model": "moss"}
async def collect(stream):
events = []
async for line in stream:
events.append(json.loads(line))
await asyncio.sleep(0)
return events
for _ in range(20):
events = asyncio.run(collect(transcript_stream.stream_transcript(transcribe, "clip")))
assert [e["phase"] for e in events if "phase" in e] == list(phases)
assert events[-1]["type"] == "complete"
def test_save_failure_returns_complete_text(monkeypatch):
async def transcribe(progress):
return {"text": "keep this", "model": "tiny"}
def fail(*args):
raise OSError("disk full")
monkeypatch.setattr(transcript_stream.transcript_gallery, "save", fail)
async def scenario():
events = [
json.loads(line)
async for line in transcript_stream.stream_transcript(transcribe, "clip")
]
assert events[-1]["text"] == "keep this"
assert events[-1]["record"] is None
asyncio.run(scenario())
def test_closing_stream_cancels_inference_without_saving(monkeypatch):
async def scenario():
started, cancelled = asyncio.Event(), asyncio.Event()
async def transcribe(progress):
started.set()
try:
await asyncio.Event().wait()
finally:
cancelled.set()
monkeypatch.setattr(
transcript_stream.transcript_gallery,
"save",
lambda *args: (_ for _ in ()).throw(AssertionError("saved cancelled request")),
)
stream = transcript_stream.stream_transcript(transcribe, "clip")
await anext(stream)
await started.wait()
await stream.aclose()
assert cancelled.is_set()
asyncio.run(scenario())
def test_transformers_progress_uses_existing_audio_windows(monkeypatch):
import numpy as np
from core.inference.stt_sidecar import WhisperSttSidecar
class Worker:
generation_config = None
calls = 0
def transcribe_window(self, pcm, kwargs, cancel):
self.calls += 1
previous = kwargs.get("_stt_previous_text")
text = f"{previous} part {self.calls}" if previous else f"part {self.calls}"
return text, len(pcm) // 4
worker = Worker()
sidecar = WhisperSttSidecar()
monkeypatch.setattr(sidecar, "load", lambda model: worker)
updates = []
result = sidecar._transcribe_decoded(
"tiny", np.zeros(35 * 16000), {}, on_progress = updates.append
)
assert result == "part 1 part 2"
assert [event["processed_seconds"] for event in updates] == [30, 35]
assert updates[-1]["text"] == result
def test_mtmd_stream_cleans_metadata_and_requires_completion(monkeypatch):
import io
import pytest
from core.inference import stt_mtmd_sidecar as mtmd
marker = mtmd.MTMD_STT_MODELS["qwen3-asr-0.6b"].transcript_marker
wire = [
{"choices": [{"delta": {"content": f"language English{marker}hello"}}]},
{"choices": [{"delta": {"content": " world"}}]},
]
payloads = []
class Response(io.BytesIO):
status = 200
class Connection:
def __init__(self, *args, **kwargs):
pass
def request(self, *args, **kwargs):
payloads.append(json.loads(kwargs["body"]))
def getresponse(self):
return Response(
b"".join(b"data: " + json.dumps(event).encode() + b"\n\n" for event in wire)
)
def close(self):
pass
monkeypatch.setattr(mtmd.http.client, "HTTPConnection", Connection)
updates = []
sidecar = mtmd.MtmdSttSidecar()
with pytest.raises(RuntimeError, match = "before finishing"):
sidecar._post_transcribe(1234, "qwen3-asr-0.6b", b"wav", on_progress = updates.append)
assert updates[-1]["text"] == "hello world"
wire.append({"choices": [{"delta": {}, "finish_reason": "stop"}]})
assert (
sidecar._post_transcribe(1234, "qwen3-asr-0.6b", b"wav", on_progress = updates.append)
== "hello world"
)
assert payloads[-1]["stream"] is True
def test_diffusion_status_retains_the_exact_checkpoint_filename():
from models.inference import DiffusionStatusResponse
status = DiffusionStatusResponse(
loaded = True, model_kind = "gguf", gguf_filename = "model-Q8_0.gguf"
).model_dump()
assert status["gguf_filename"] == "model-Q8_0.gguf"