fix: 预置音色 preview 改为顺序生成,避免 DashScope 429 限流 #1223
@@ -7,7 +7,6 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from typing import Literal, Optional
|
||||
|
||||
from app.api.routes._helpers import get_user_plan
|
||||
@@ -97,23 +96,20 @@ def _resolve_all_preset_preview_urls(
|
||||
presets: list,
|
||||
cosyvoice: CosyVoiceService,
|
||||
) -> dict[str, str]:
|
||||
"""并行解析所有预置音色的 preview_url.
|
||||
"""顺序解析所有预置音色的 preview_url.
|
||||
|
||||
采用顺序调用(而非并行)以避免触发 DashScope API 速率限制。
|
||||
首次调用后结果缓存 7 天,后续请求直接命中缓存。
|
||||
|
||||
Returns:
|
||||
voice_id -> preview_url 映射
|
||||
"""
|
||||
result_map: dict[str, str] = {}
|
||||
with ThreadPoolExecutor(max_workers=4) as executor:
|
||||
future_to_voice = {
|
||||
executor.submit(_resolve_preset_preview_url, p.voice_id, p.preview_url, cosyvoice): p.voice_id
|
||||
for p in presets
|
||||
}
|
||||
for future in as_completed(future_to_voice):
|
||||
vid = future_to_voice[future]
|
||||
try:
|
||||
result_map[vid] = future.result()
|
||||
except Exception:
|
||||
result_map[vid] = ""
|
||||
for p in presets:
|
||||
try:
|
||||
result_map[p.voice_id] = _resolve_preset_preview_url(p.voice_id, p.preview_url, cosyvoice)
|
||||
except Exception:
|
||||
result_map[p.voice_id] = p.preview_url
|
||||
return result_map
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user