[Bugfix] Validate speaker in chat endpoint and fix case-insensitive lookup (#2407)

Signed-off-by: reidliu41 <reid201711@gmail.com>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
ReidandHongsheng Liu authored and GitHub committed 2026-04-12 00:14:16 -04:00
1 parent c20cac86ce
commit 38dfe56fdf
4 files changed
+169 -6

No files matched your search

+30
View File
@@ -53,3 +53,33 @@ def parse_lora_request(lora_body: Any) -> tuple[LoRARequest | None, float | None
scale = float(lora_scale) if lora_scale is not None else None
return LoRARequest(str(lora_name), int(lora_int_id), str(lora_path)), scale
def get_supported_speakers_from_hf_config(hf_config: Any) -> set[str]:
"""Extract supported speaker names from a model hf_config."""
config = (
hf_config.get("talker_config") if isinstance(hf_config, dict) else getattr(hf_config, "talker_config", None)
)
if config is None:
return set()
for spk_attr in ("speaker_id", "spk_id"):
speakers_dict = config.get(spk_attr) if isinstance(config, dict) else getattr(config, spk_attr, None)
if speakers_dict and isinstance(speakers_dict, dict):
return {speaker.lower() for speaker in speakers_dict}
return set()
def validate_requested_speaker(speaker: str | None, supported_speakers: set[str]) -> str | None:
"""Normalize and validate an optional speaker value.
Returns the normalized speaker string when provided, otherwise ``None``.
Raises ``ValueError`` when the speaker is not in the supported list.
"""
if not isinstance(speaker, str) or not speaker.strip():
return None
normalized = speaker.lower().strip()
if supported_speakers and normalized not in supported_speakers:
raise ValueError(f"Invalid speaker '{speaker}'. Supported: {', '.join(sorted(supported_speakers))}")
return normalized