nex_animation/backend/tests/test_index_tts_instruct.py

191 lines
9.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""回归用例:音频模型不许有「mac 本地」,IndexTTS2 必须能用「音色描述」直出。
用户口径(2026-11 第六轮):
1. 部署服务器是 Linux,没有系统 ``say`` 命令——「mac 本地(macos-say)」这条音频模型
在这台机器上永远跑不通,登记在库里只会让人选中一个不可用的模型;
2. IndexTTS2 除了参考音频克隆,还要支持**直接用描述词生成语音**:
只填「音色描述」(例:年幼小女孩、低沉老年男性)就该出对应的嗓子,
而不是必须先上传一段参考音频。
实测依据(直连 10.100.53.199:9200 提交图验证):
``easy indexTTSGenerateSimple`` 没有 emotions 输入,描述词写进去不生效;
描述词必须走 ``easy indexTTSGenerate``(全参)+ ``easy indexTTSEmotionText``
(reference_audio / voice_name / description / use_random → 输出 emotions)。
同一句台词用两组描述词实测中位基频 241.4 Hz ↔ 189.2 Hz,音色确实在变。
"""
from __future__ import annotations
import importlib
from app.models import ModelSpec, PlatformVoice
from app.providers.base import VoiceGenRequest
from app.providers.comfy_assets import ComfyTTSProvider
from app.services import timbre
CFG = {"base_url": "http://127.0.0.1:9200"}
def _req(**kw) -> VoiceGenRequest:
base = dict(text="你好,我是这条声线。", label="声线A", output_path="/tmp/x.wav",
provider_voice_id="")
base.update(kw)
return VoiceGenRequest(**base)
# ------------------------------------------------------------------ 描述词图
def test_instruct_switches_graph_to_full_node_with_emotion():
"""填了音色描述 → 换全参生成节点,并挂上描述词节点(Simple 节点不接 emotions)。"""
graph = ComfyTTSProvider.graph(_req(), CFG, 1234, "nex_animation/ref.wav",
instruct="低沉老年男性")
assert graph["3"]["class_type"] == "easy indexTTSGenerate"
assert graph["5"]["class_type"] == "easy indexTTSEmotionText"
assert graph["5"]["inputs"]["description"] == "低沉老年男性"
assert graph["5"]["inputs"]["reference_audio"] == ["2", 0]
assert graph["3"]["inputs"]["emotions"] == ["5", 0]
# 全参节点的必填项一个都不能少,缺一项 ComfyUI 会在提交阶段直接拒绝整张图
for key, value in ComfyTTSProvider.FULL_DEFAULTS.items():
assert graph["3"]["inputs"][key] == value, key
assert graph["3"]["inputs"]["text"] == "你好,我是这条声线。"
assert graph["3"]["inputs"]["seed"] == 1234
def test_instruct_keeps_clone_reference_when_present():
"""有真人参考音频时克隆优先:描述词只做偏移,不该把克隆来的嗓子丢掉。"""
graph = ComfyTTSProvider.graph(_req(), CFG, 7, "nex_animation/ref.wav",
instruct="年幼小女孩")
assert graph["3"]["inputs"]["reference_audio"] == ["2", 0]
def test_description_only_mode_still_loads_the_anchor_audio():
"""只填描述词时仍要一段底声锚点:ComfyUI-Index-TTS 的 emotion 节点
``reference_audio`` 是必填项,缺了它整张图提交就被拒。
锚点用模型配置的默认参考音频,音色由描述词决定——实测同一句台词两组描述词
中位基频 241.4 Hz ↔ 189.2 Hz,锚点相同而嗓子不同。
"""
graph = ComfyTTSProvider.graph(_req(), CFG, 7, "nex_default.wav",
instruct="年幼小女孩")
assert graph["2"]["inputs"]["audio"] == "nex_default.wav"
assert graph["5"]["inputs"]["reference_audio"] == ["2", 0]
assert graph["3"]["inputs"]["reference_audio"] == ["2", 0]
assert graph["3"]["inputs"]["emotions"] == ["5", 0]
def test_plain_clone_graph_is_unchanged():
"""没填描述词时仍是原来的 zero-shot 克隆图,老行为不受影响。"""
graph = ComfyTTSProvider.graph(_req(), CFG, 7, "nex_animation/ref.wav", instruct="")
assert graph["3"]["class_type"] == "easy indexTTSGenerateSimple"
assert "emotions" not in graph["3"]["inputs"]
assert "5" not in graph
def test_capabilities_declare_voice_instruct():
caps = ComfyTTSProvider().capabilities(CFG)
assert "voice_instruct" in caps.modes
assert "voice_clone" in caps.modes
# ------------------------------------------------------------------ mac 本地下线
def test_macos_say_adapter_is_gone():
assert importlib.util.find_spec("app.providers.macos_say") is None
def test_registry_no_longer_seeds_macos_say(db):
from app.providers import registry
registry.seed_specs(db)
db.commit()
assert db.query(ModelSpec).filter(ModelSpec.provider == "macos-say").count() == 0
rows = list(db.query(ModelSpec).filter(ModelSpec.media_type == "tts").all())
assert rows, "音频模型整类不能因为下线 macos-say 而被清空"
assert "macos-say" not in {r.provider for r in rows}
indextts = [r for r in rows if r.adapter == "comfyui_tts"]
assert indextts, "IndexTTS2 必须仍在册"
assert "voice_instruct" in (indextts[0].modes or [])
def test_legacy_macos_say_row_is_retired_and_default_promoted(db):
"""库里已经登记过 macos-say(开发机残留):启动时删掉,默认值顺延给后继模型。"""
from app.providers import registry
stale = ModelSpec(media_type="tts", provider="macos-say", adapter="macos_say",
display_name="mac 本地", model="system-voice",
model_code="macos-say/system-voice", is_default=True, enabled=True)
keeper = ModelSpec(media_type="tts", provider="comfyui_tts", adapter="comfyui_tts",
display_name="IndexTTS2", model="IndexTTS-2",
model_code="comfyui/indextts2", is_default=False, enabled=True,
modes=["voice", "voice_clone", "voice_instruct"])
db.add_all([stale, keeper])
db.commit()
assert registry._retire_macos_say(db) is True
db.commit()
assert db.query(ModelSpec).filter(ModelSpec.provider == "macos-say").count() == 0
db.refresh(keeper)
assert keeper.is_default is True
# 幂等:第二次没有 macos-say 行可删,不再动默认标记
assert registry._retire_macos_say(db) is False
# ------------------------------------------------------------------ 音色解析
def test_instruct_model_is_detected_from_modes():
spec = ModelSpec(media_type="tts", provider="comfyui_tts", adapter="comfyui_tts",
display_name="IndexTTS2", model="IndexTTS-2",
modes=["voice", "voice_clone", "voice_instruct"])
assert timbre.instruct_model(spec) is True
clone_only = ModelSpec(media_type="tts", provider="comfyui_tts", adapter="comfyui_tts",
display_name="只认参考音频的模型", model="x",
modes=["voice", "voice_clone"])
assert timbre.instruct_model(clone_only) is False
def test_description_only_voice_resolves_to_instruct(db):
"""没参考音频、没性别的声线:以前只能拿模型默认参考(音色不受控),现在走描述词。"""
voice = PlatformVoice(name="描述词声线", code="instruct-only", gender="",
description="年幼小女孩,音色清亮,语速偏快",
is_active=True, is_default=False, provider_voice_id="")
db.add(voice)
db.commit()
assert timbre.instruct_text_of(voice) == "年幼小女孩,音色清亮,语速偏快"
resolved = timbre.resolve_reference(db, voice, instruct=True)
assert resolved["source"] == "instruct"
assert resolved["path"] == ""
assert "音色描述" in resolved["note"]
status = timbre.reference_status(db, voice, instruct=True)
assert status["instruct"] is True
assert status["ready"] is True
assert status["instruct_text"] == "年幼小女孩,音色清亮,语速偏快"
# 关掉描述词能力(换回只认参考音频的模型)时,预告回到原来的口径
plain = timbre.reference_status(db, voice, instruct=False)
assert plain["instruct"] is False
assert plain["source"] == "model_default"
def test_instruct_needs_a_description(db):
"""描述词是空的:不能谎称「按描述词生成」,仍按模型默认参考处理。"""
voice = PlatformVoice(name="空描述", code="instruct-empty", gender="",
description="", is_active=True, is_default=False,
provider_voice_id="")
db.add(voice)
db.commit()
assert timbre.resolve_reference(db, voice, instruct=True)["source"] == "model_default"
def test_reference_audio_still_wins_over_description(db, tmp_path):
"""参考音频优先于描述词:克隆出来的嗓子比描述词精确得多。"""
anchor = tmp_path / "anchor.wav"
anchor.write_bytes(b"RIFF" + b"\0" * 40)
voice = PlatformVoice(name="有参考", code="instruct-ref", gender="male",
description="低沉老年男性", is_active=True, is_default=False,
provider_voice_id=str(anchor))
db.add(voice)
db.commit()
resolved = timbre.resolve_reference(db, voice, instruct=True)
assert resolved["source"] == "self"
assert resolved["path"] == str(anchor)