Use openai compatible as vendor
This commit is contained in:
@@ -13,20 +13,26 @@ from ..schemas import VoiceCreate, VoiceOut, VoicePreviewRequest, VoicePreviewRe
|
||||
|
||||
router = APIRouter(prefix="/voices", tags=["Voices"])
|
||||
|
||||
SILICONFLOW_DEFAULT_MODEL = "FunAudioLLM/CosyVoice2-0.5B"
|
||||
OPENAI_COMPATIBLE_DEFAULT_MODEL = "FunAudioLLM/CosyVoice2-0.5B"
|
||||
|
||||
|
||||
def _is_siliconflow_vendor(vendor: str) -> bool:
|
||||
return vendor.strip().lower() in {"siliconflow", "硅基流动"}
|
||||
def _is_openai_compatible_vendor(vendor: str) -> bool:
|
||||
normalized = (vendor or "").strip().lower()
|
||||
return normalized in {
|
||||
"openai compatible",
|
||||
"openai-compatible",
|
||||
"siliconflow", # backward compatibility
|
||||
"硅基流动", # backward compatibility
|
||||
}
|
||||
|
||||
|
||||
def _default_base_url(vendor: str) -> Optional[str]:
|
||||
if _is_siliconflow_vendor(vendor):
|
||||
if _is_openai_compatible_vendor(vendor):
|
||||
return "https://api.siliconflow.cn/v1"
|
||||
return None
|
||||
|
||||
|
||||
def _build_siliconflow_voice_key(voice: Voice, model: str) -> str:
|
||||
def _build_openai_compatible_voice_key(voice: Voice, model: str) -> str:
|
||||
if voice.voice_key:
|
||||
return voice.voice_key
|
||||
if ":" in voice.id:
|
||||
@@ -65,8 +71,8 @@ def create_voice(data: VoiceCreate, db: Session = Depends(get_db)):
|
||||
model = data.model
|
||||
voice_key = data.voice_key
|
||||
|
||||
if _is_siliconflow_vendor(vendor):
|
||||
model = model or SILICONFLOW_DEFAULT_MODEL
|
||||
if _is_openai_compatible_vendor(vendor):
|
||||
model = model or OPENAI_COMPATIBLE_DEFAULT_MODEL
|
||||
if not voice_key:
|
||||
raw_id = (data.id or data.name).strip()
|
||||
voice_key = raw_id if ":" in raw_id else f"{model}:{raw_id}"
|
||||
@@ -115,11 +121,11 @@ def update_voice(id: str, data: VoiceUpdate, db: Session = Depends(get_db)):
|
||||
update_data["vendor"] = update_data["vendor"].strip()
|
||||
|
||||
vendor_for_defaults = update_data.get("vendor", voice.vendor)
|
||||
if _is_siliconflow_vendor(vendor_for_defaults):
|
||||
model = update_data.get("model") or voice.model or SILICONFLOW_DEFAULT_MODEL
|
||||
if _is_openai_compatible_vendor(vendor_for_defaults):
|
||||
model = update_data.get("model") or voice.model or OPENAI_COMPATIBLE_DEFAULT_MODEL
|
||||
voice_key = update_data.get("voice_key") or voice.voice_key
|
||||
update_data["model"] = model
|
||||
update_data["voice_key"] = voice_key or _build_siliconflow_voice_key(voice, model)
|
||||
update_data["voice_key"] = voice_key or _build_openai_compatible_voice_key(voice, model)
|
||||
|
||||
for field, value in update_data.items():
|
||||
setattr(voice, field, value)
|
||||
@@ -152,7 +158,7 @@ def preview_voice(id: str, data: VoicePreviewRequest, db: Session = Depends(get_
|
||||
raise HTTPException(status_code=400, detail="Preview text cannot be empty")
|
||||
|
||||
api_key = (data.api_key or "").strip() or (voice.api_key or "").strip()
|
||||
if not api_key and _is_siliconflow_vendor(voice.vendor):
|
||||
if not api_key and _is_openai_compatible_vendor(voice.vendor):
|
||||
api_key = os.getenv("SILICONFLOW_API_KEY", "").strip()
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=400, detail=f"API key is required for voice: {voice.name}")
|
||||
@@ -161,11 +167,11 @@ def preview_voice(id: str, data: VoicePreviewRequest, db: Session = Depends(get_
|
||||
if not base_url:
|
||||
raise HTTPException(status_code=400, detail=f"Base URL is required for voice: {voice.name}")
|
||||
|
||||
model = voice.model or SILICONFLOW_DEFAULT_MODEL
|
||||
model = voice.model or OPENAI_COMPATIBLE_DEFAULT_MODEL
|
||||
payload = {
|
||||
"model": model,
|
||||
"input": text,
|
||||
"voice": voice.voice_key or _build_siliconflow_voice_key(voice, model),
|
||||
"voice": voice.voice_key or _build_openai_compatible_voice_key(voice, model),
|
||||
"response_format": "mp3",
|
||||
"speed": data.speed if data.speed is not None else voice.speed,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user