fix: Vidu 克隆 voice_id 长度校验,自动规范化用户输入名称
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
import tempfile
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
@@ -368,6 +369,35 @@ async def synthesize_to_file(request: TTSSynthesizeRequest, output_path: str):
|
||||
raise HTTPException(status_code=500, detail=f"保存失败: {str(e)}")
|
||||
|
||||
|
||||
def _normalize_voice_id(name: str | None) -> str:
|
||||
"""
|
||||
将用户输入的名称规范化为 Vidu 合法的 voice_id。
|
||||
|
||||
Vidu 要求:8~256 字符,首字符必须是字母。
|
||||
"""
|
||||
if not name:
|
||||
return f"vidu_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
# 只保留字母、数字、下划线
|
||||
cleaned = re.sub(r"[^a-zA-Z0-9_]", "", name)
|
||||
|
||||
# 确保首字符是字母
|
||||
if cleaned and not cleaned[0].isalpha():
|
||||
cleaned = "v" + cleaned
|
||||
elif not cleaned:
|
||||
cleaned = "voice"
|
||||
|
||||
# 长度不足 8,补足随机字符
|
||||
if len(cleaned) < 8:
|
||||
cleaned = cleaned + uuid.uuid4().hex[: (8 - len(cleaned))]
|
||||
|
||||
# 长度超过 256,截断
|
||||
if len(cleaned) > 256:
|
||||
cleaned = cleaned[:256]
|
||||
|
||||
return cleaned
|
||||
|
||||
|
||||
@router.post("/clone/submit", response_model=ApiResponse[VoiceCloneTaskResponse])
|
||||
async def submit_clone_task(request: VoiceCloneSubmitRequest):
|
||||
"""
|
||||
@@ -376,10 +406,11 @@ async def submit_clone_task(request: VoiceCloneSubmitRequest):
|
||||
Vidu 声音复刻是同步接口,直接返回结果。
|
||||
"""
|
||||
try:
|
||||
voice_id = _normalize_voice_id(request.voice_name)
|
||||
service = ViduTTSService()
|
||||
result = await service.clone_voice(
|
||||
audio_url=request.source_audio_url or "",
|
||||
voice_id=request.voice_name or f"vidu_{uuid.uuid4().hex[:8]}",
|
||||
voice_id=voice_id,
|
||||
)
|
||||
|
||||
# Vidu 同步返回,状态直接为 succeeded
|
||||
@@ -426,10 +457,11 @@ async def clone_and_wait(request: VoiceCloneSubmitRequest, poll_interval: float
|
||||
适用于需要等待克隆完成的场景。
|
||||
"""
|
||||
try:
|
||||
voice_id = _normalize_voice_id(request.voice_name)
|
||||
service = ViduTTSService()
|
||||
result = await service.clone_voice(
|
||||
audio_url=request.source_audio_url or "",
|
||||
voice_id=request.voice_name or f"vidu_{uuid.uuid4().hex[:8]}",
|
||||
voice_id=voice_id,
|
||||
)
|
||||
|
||||
return success_response(
|
||||
|
||||
Reference in New Issue
Block a user