aboutsummaryrefslogtreecommitdiff
path: root/app/tests/test_instruction_capabilities.py
blob: 440ee08a489863af97ffef1d1f064e897bde1817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Instruction-support and guidance regressions across the TTS backends.

Covers the capabilities the models actually implement (verified against
each backend's serving code) and what the clients send for them:
Breeze-TTS 2's recommended guidance strength with instructions, the
audio.cpp Qwen3-TTS variant split (CustomVoice reads instructions, the
Base cloner does not), the SGLang models that consume a separate style
instruction alongside their voice conditioning, and the Qwen demo's
CustomVoice instruction parameter.
"""

import io
import json
import tempfile
import unittest
import wave
from pathlib import Path
from unittest.mock import MagicMock, patch

from converter.clients import (
    BACKEND_QWEN, AudioCppTTSClient, QwenTTSClient,
    VOICE_MODE_CLONE, VOICE_MODE_CUSTOM, VOICE_MODE_DESIGN,
)
from converter.clients.audiocpp import (
    AUDIOCPP_FAMILY_BREEZE_TTS,
    AUDIOCPP_FAMILY_PROFILES,
    AUDIOCPP_VOICE_OPTIONAL,
    audiocpp_entry_supports_instructions,
    audiocpp_family_voice_policy,
)


_WAV_BYTES = b"RIFF\x18\x00\x00\x00WAVEfmt \x10\x00\x00\x00"


# ---------------------------------------------------------------------------
# Pure helpers
# ---------------------------------------------------------------------------

class VoicePolicyKnownFamiliesTests(unittest.TestCase):
    """Families whose verified policy must survive a stale local spec."""

    def test_breeze_is_tts_plus_clone_even_without_a_local_spec(self):
        # Remote Breeze entries against older local checkouts carry no
        # breeze_tts spec at all: the fallback keeps instructions-only
        # voice direction connectable instead of demanding a reference.
        self.assertEqual(
            audiocpp_family_voice_policy(AUDIOCPP_FAMILY_BREEZE_TTS),
            AUDIOCPP_VOICE_OPTIONAL)

    def test_vibevoice_accepts_reference_audio_despite_its_spec(self):
        # vibevoice.json declares only "tts", but the implementation
        # accepts reference audio: a mixed tts+clone family, so a picked
        # voice must not be silently dropped.
        self.assertEqual(audiocpp_family_voice_policy("vibevoice"),
                         AUDIOCPP_VOICE_OPTIONAL)


class EntryInstructionSupportTests(unittest.TestCase):
    """audiocpp_entry_supports_instructions: True/False/None per entry."""

    def test_breeze_supports_instructions(self):
        self.assertIs(
            audiocpp_entry_supports_instructions(
                AUDIOCPP_FAMILY_BREEZE_TTS, "tts", "Breeze-TTS-2-GGUF"),
            True)

    def test_qwen_customvoice_and_design_support_instructions(self):
        self.assertIs(
            audiocpp_entry_supports_instructions(
                "qwen3_tts", "tts", "Qwen3-TTS-12Hz-1.7B-CustomVoice-GGUF"),
            True)
        self.assertIs(
            audiocpp_entry_supports_instructions(
                "qwen3_tts", "vdes", "Qwen3-TTS-12Hz-1.7B-VoiceDesign"),
            True)

    def test_qwen_base_cloner_provably_does_not(self):
        self.assertIs(
            audiocpp_entry_supports_instructions(
                "qwen3_tts", "tts", "Qwen3-TTS-12Hz-1.7B-Base-GGUF"),
            False)

    def test_unknown_families_are_unknown_not_unsupported(self):
        self.assertIsNone(
            audiocpp_entry_supports_instructions("chatterbox",
                                                 "tts", "x"))


# ---------------------------------------------------------------------------
# audio.cpp request payloads (instructed Breeze runs)
# ---------------------------------------------------------------------------

def _breeze_client(instructions=None, request_options=None,
                   voice="narrator", seed=-1):
    """A fully-initialized Breeze client (no HTTP machinery touched)."""
    with patch.object(AudioCppTTSClient, "_connect"):
        client = AudioCppTTSClient(
            Path("."), voice=voice, instructions=instructions,
            request_options=request_options)
    client.api_url = "http://127.0.0.1:8080"
    client.model_id = "Breeze-TTS-2-GGUF"
    client.family = AUDIOCPP_FAMILY_BREEZE_TTS
    client.task = "tts"
    client.profile = AUDIOCPP_FAMILY_PROFILES[AUDIOCPP_FAMILY_BREEZE_TTS]
    client.design_mode = False
    client.instruction_voice = False
    client.plain_mode = False
    client.preset_mode = True
    client.speaker_mode = False
    client._seed = seed
    client._resolve_auto_guidance()
    return client


def _captured_payload(client):
    """The JSON body _request_wav sends, via a stubbed urlopen."""
    response = MagicMock()
    response.read.return_value = _WAV_BYTES
    response.__enter__ = lambda self: response
    response.__exit__ = lambda self, *exc: None
    with patch("converter.clients.audiocpp.urllib.request.urlopen") \
            as urlopen:
        urlopen.return_value = response
        client._request_wav("Hello there.")
    request = urlopen.call_args[0][0]
    return json.loads(request.data.decode("utf-8"))


class BreezeGuidanceDefaultTests(unittest.TestCase):
    """Breeze guidance: recommended 4 with instructions, otherwise none."""

    def test_instructed_clone_carries_guidance_4_and_the_instruction(self):
        client = _breeze_client(instructions="Screaming, crazed, yelling")
        self.assertEqual(client._auto_guidance_scale, 4.0)
        payload = _captured_payload(client)
        self.assertEqual(payload["guidance_scale"], 4.0)
        self.assertEqual(payload["voice"], "narrator")
        self.assertEqual(
            payload["options"],
            {"instruction": "Screaming, crazed, yelling"})
        self.assertNotIn("instructions", payload)

    def test_option_instruction_also_gets_the_guidance_default(self):
        client = _breeze_client(request_options={
            "instruction": "Read slowly and warmly."})
        self.assertEqual(client._auto_guidance_scale, 4.0)
        payload = _captured_payload(client)
        self.assertEqual(payload["guidance_scale"], 4.0)
        self.assertEqual(payload["options"]["instruction"],
                         "Read slowly and warmly.")

    def test_explicit_guidance_option_is_preserved(self):
        client = _breeze_client(
            instructions="Screaming, crazed, yelling",
            request_options={"guidance_scale": "2.5"})
        self.assertIsNone(client._auto_guidance_scale)
        payload = _captured_payload(client)
        self.assertNotIn("guidance_scale", payload)
        self.assertEqual(payload["options"]["guidance_scale"], "2.5")

    def test_guidance_0_override_still_counts_as_explicit(self):
        # 0 selects the instruction-free branch: a deliberate setting.
        client = _breeze_client(
            instructions="Screaming",
            request_options={"guidance_scale": "0"})
        payload = _captured_payload(client)
        self.assertNotIn("guidance_scale", payload)

    def test_plain_clone_without_instructions_uses_the_backend_default(self):
        client = _breeze_client()
        self.assertIsNone(client._auto_guidance_scale)
        payload = _captured_payload(client)
        self.assertNotIn("guidance_scale", payload)
        self.assertNotIn("options", payload)
        self.assertEqual(payload["voice"], "narrator")


class InstructionConflictTests(unittest.TestCase):
    """Two different instruction sources are refused before connecting."""

    def test_conflicting_instructions_and_option_raise_without_a_server(self):
        with self.assertRaises(RuntimeError) as ctx:
            _breeze_client(instructions="calm narration",
                           request_options={"instruction": "screaming"})
        self.assertIn("Two conflicting instructions",
                      str(ctx.exception))

    def test_identical_instructions_from_both_sources_are_accepted(self):
        client = _breeze_client(
            instructions="calm narration",
            request_options={"instruction": "calm narration"})
        self.assertEqual(client.instructions, "calm narration")

    def test_option_only_instruction_is_folded_into_the_reports(self):
        client = _breeze_client(
            request_options={"instruction": "calm narration"})
        self.assertEqual(client.instructions, "calm narration")


class SeedPrecisionTests(unittest.TestCase):
    """Full-range uint64 seeds travel as decimal strings (audio.cpp docs)."""

    def test_seed_above_2_pow_53_is_sent_as_a_string(self):
        seed = 2 ** 53 + 3  # beyond the exact JSON-number integer range
        client = _breeze_client(seed=seed)
        payload = _captured_payload(client)
        self.assertEqual(payload["seed"], str(seed))

    def test_ordinary_seeds_stay_numbers(self):
        client = _breeze_client(seed=42)
        payload = _captured_payload(client)
        self.assertEqual(payload["seed"], 42)


# ---------------------------------------------------------------------------
# SGLang-Omni: instructions on supported pipelines
# ---------------------------------------------------------------------------

class SgOmniInstructionTests(unittest.TestCase):
    """instructions reach the payload only where the serving code reads it."""

    _tmp_dir = None
    _REF = None

    @classmethod
    def setUpClass(cls):
        buffer = io.BytesIO()
        with wave.open(buffer, "wb") as wav_file:
            wav_file.setnchannels(1)
            wav_file.setsampwidth(2)
            wav_file.setframerate(24000)
            wav_file.writeframes(b"\x01\x00" * 16)
        cls._tmp_dir = tempfile.TemporaryDirectory()
        cls._REF = Path(cls._tmp_dir.name) / "narrator.wav"
        cls._REF.write_bytes(buffer.getvalue())
        cls.addClassCleanup(cls._tmp_dir.cleanup)

    def _client(self, model, **kwargs):
        from converter.clients import SgOmniTTSClient
        with patch.object(SgOmniTTSClient, "_connect"):
            client = SgOmniTTSClient(
                Path("."), model=model, ref_audio=str(self._REF),
                ref_text="Hello transcript.", instructions="screaming",
                **kwargs)
        entry = client.entry
        payload = client._request_payload("Hello there.")
        return entry, payload

    def test_qwen_base_clone_carries_ref_and_instruction(self):
        entry, payload = self._client("qwen3_tts_1_7b_base")
        self.assertIn("ref_audio", payload)
        self.assertNotEqual(payload.get("task_type"), "VoiceDesign")
        self.assertEqual(payload["instructions"], "screaming")

    def test_moss_clone_carries_ref_and_instruction(self):
        entry, payload = self._client("moss_tts")
        self.assertIn("ref_audio", payload)
        self.assertEqual(payload["instructions"], "screaming")

    def test_customvoice_speaker_with_instruction(self):
        entry, payload = self._client("qwen3_tts_0_6b_customvoice",
                                      voice="Vivian")
        self.assertEqual(payload["voice"], "Vivian")
        self.assertEqual(payload["instructions"], "screaming")
        self.assertNotIn("task_type", payload)

    def test_design_remains_voice_design_with_instruction(self):
        from converter.clients import SgOmniTTSClient
        with patch.object(SgOmniTTSClient, "_connect"):
            client = SgOmniTTSClient(
                Path("."), model="qwen3_tts_1_7b_voicedesign",
                instructions="a warm narrator")
        payload = client._request_payload("Hello there.")
        self.assertEqual(payload["task_type"], "VoiceDesign")
        self.assertEqual(payload["instructions"], "a warm narrator")
        self.assertNotIn("ref_audio", payload)

    def test_unsupported_model_refuses_instructions_at_connect(self):
        with self.assertRaises(RuntimeError) as ctx:
            self._client("higgs_audio_v3_tts")
        self.assertIn("does not consume style instructions",
                      str(ctx.exception))


# ---------------------------------------------------------------------------
# Qwen demo: CustomVoice instruction parameter
# ---------------------------------------------------------------------------

class QwenCustomVoiceInstructionTests(unittest.TestCase):
    """The run_instruct endpoint takes an ``instruct`` delivery control."""

    def test_run_instruct_sends_instruct_alongside_the_speaker(self):
        client = QwenTTSClient.__new__(QwenTTSClient)
        client.voice_mode = VOICE_MODE_CUSTOM
        client.speaker = "Vivian"
        client.language = "Auto"
        client.instructions = "screaming, crazed"
        client._seed = -1
        client.client = MagicMock()
        client._resolve_api_name = lambda *names: names[0]
        client._endpoint_accepts_param = MagicMock(return_value=True)
        client._generate_custom_voice("Hello there.")
        predict = client.client.predict
        predict.assert_called_once_with(
            text="Hello there.", lang_disp="Auto",
            spk_disp="Vivian", instruct="screaming, crazed",
            api_name="/run_instruct")

    def test_custom_voice_without_instructions_is_unchanged(self):
        client = QwenTTSClient.__new__(QwenTTSClient)
        client.voice_mode = VOICE_MODE_CUSTOM
        client.speaker = "Vivian"
        client.language = "Auto"
        client.instructions = ""
        client._seed = -1
        client.client = MagicMock()
        client._resolve_api_name = lambda *names: names[0]
        client._endpoint_accepts_param = MagicMock(return_value=True)
        client._generate_custom_voice("Hello there.")
        _, kwargs = client.client.predict.call_args
        self.assertNotIn("instruct", kwargs)


# ---------------------------------------------------------------------------
# audiobook voice-mode routing (qwen)
# ---------------------------------------------------------------------------

class CatalogInstructionFlagsTests(unittest.TestCase):
    """Only the verified pipelines carry supports_instructions."""

    def test_catalog_marks_only_the_verified_pipelines(self):
        from backends.sglomni.catalog import ENTRIES
        supported = {"qwen3_tts_0_6b_customvoice", "qwen3_tts_0_6b_base",
                     "qwen3_tts_1_7b_base", "qwen3_tts_1_7b_voicedesign",
                     "moss_tts", "moss_tts_local"}
        for entry in ENTRIES:
            with self.subTest(entry=entry.key):
                self.assertEqual(entry.supports_instructions,
                                 entry.key in supported)


class GradioPrefixProbeTests(unittest.TestCase):
    """Qwen demos under modern Gradio sit behind /gradio_api."""

    def _identify(self, modern_payload, legacy_payload=None):
        import backends.probe as probe
        seen = []

        def fake_get_json(url, timeout):
            seen.append(url)
            if url == "http://x/gradio_api/info":
                return modern_payload
            if url == "http://x/info":
                return legacy_payload
            return None

        with patch.object(probe, "_get_json", side_effect=fake_get_json):
            with patch.object(probe.common, "server_running",
                              return_value=True):
                identity = probe._identify_gradio("http://x", 1.0)
        return identity, seen

    def test_modern_prefix_is_probed_first_and_identifies(self):
        payload = {"named_endpoints": {"/run_instruct": {}}}
        identity, seen = self._identify(payload)
        self.assertEqual(identity, probe_identity("qwen-custom"))
        self.assertEqual(seen, ["http://x/gradio_api/info"])

    def test_legacy_info_still_identifies_older_gradio(self):
        payload = {"named_endpoints": {"/run_voice_clone": {}}}
        identity, seen = self._identify(None, payload)
        self.assertEqual(identity, probe_identity("qwen-clone"))
        self.assertEqual(seen, ["http://x/gradio_api/info",
                                "http://x/info"])

    def test_neither_prefix_answers_none(self):
        identity, _ = self._identify(None)
        self.assertIsNone(identity)


def probe_identity(name):
    """The probe's IDENTITY_* constant for a backend NAME (local import)."""
    import backends.probe as probe
    return {"qwen-custom": probe.IDENTITY_QWEN_CUSTOM,
            "qwen-clone": probe.IDENTITY_QWEN_CLONE,
            }[name]


# ---------------------------------------------------------------------------
# audiobook voice-mode routing (qwen)
# ---------------------------------------------------------------------------

class QwenVoiceModeRoutingTests(unittest.TestCase):
    """speaker + instructions is a directed CustomVoice run, not Design."""

    def test_voice_mode_for_qwen_combinations(self):
        from converter.converter import voice_mode_for
        cases = [
            (dict(voice=None, clone=None, instructions=None),
             VOICE_MODE_CUSTOM),
            (dict(voice=None, clone=None, instructions="screaming"),
             VOICE_MODE_DESIGN),
            (dict(voice="Vivian", clone=None, instructions="screaming"),
             VOICE_MODE_CUSTOM),
            (dict(voice=None, clone="ref.wav", instructions=None),
             VOICE_MODE_CLONE),
        ]
        for kwargs, expected in cases:
            with self.subTest(**kwargs):
                self.assertEqual(
                    voice_mode_for(BACKEND_QWEN, voice=kwargs["voice"],
                                   clone=kwargs["clone"],
                                   instructions=kwargs["instructions"]),
                    expected)


if __name__ == "__main__":
    unittest.main()