修复后端声纹模型加载失败问题

main
Bifang 2026-10-14 10:51:50 +08:00
parent fdd9ae20a4
commit 400068692e
2 changed files with 43 additions and 6 deletions

View File

@ -33,12 +33,21 @@ def _get_model() -> Any:
kwargs = dict(settings.FUNASR_AUTOMODEL_KWARGS)
kwargs["disable_update"] = settings.FUNASR_DISABLE_UPDATE
logger.info(
"Loading FunASR realtime speaker model: model=%s device=%s disable_update=%s",
"Loading FunASR realtime speaker model: model_id=%s model_path=%s device=%s disable_update=%s",
settings.REALTIME_SV_MODEL,
model_path,
device,
settings.FUNASR_DISABLE_UPDATE,
)
_model = AutoModel(model=model_path, device=device, **kwargs)
# Keep the registered ModelScope ID as ``model`` so FunASR can
# resolve ERes2NetV2's model class; pass the local cache path
# separately to load its config and weights without downloading.
_model = AutoModel(
model=settings.REALTIME_SV_MODEL,
model_path=model_path,
device=device,
**kwargs,
)
logger.info("FunASR realtime speaker model loaded: %s", model_path)
return _model

View File

@ -4,6 +4,7 @@
from __future__ import annotations
import logging
import threading
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
@ -17,6 +18,8 @@ from app.services.asr.funasr_speaker import extract_realtime_speaker_embeddings
from app.services.speaker_registry import get_speaker_registry_service
logger = logging.getLogger(__name__)
_funasr_embedding_fallback_logged = False
_funasr_embedding_fallback_log_lock = threading.Lock()
class RealtimeSpeakerClusterer:
@ -291,10 +294,35 @@ class RealtimeSpeakerClusterer:
# 与 FunASR 实时跟踪器保持一致:整批声纹窗口通过 AutoModel.generate
# 提取,确保先执行 ERes2Net 所需的音频特征预处理,再进入模型。
embeddings = await run_sync(
extract_realtime_speaker_embeddings,
[item["audio"] for item in chunk_items],
)
try:
embeddings = await run_sync(
extract_realtime_speaker_embeddings,
[item["audio"] for item in chunk_items],
)
except Exception:
# Keep speaker IDs and live turn cuts available if this checkpoint
# cannot be loaded through FunASR AutoModel in the deployed runtime.
# The warning makes the fallback visible so it is not mistaken for
# a fully FunASR-aligned embedding path.
global _funasr_embedding_fallback_logged
with _funasr_embedding_fallback_log_lock:
if not _funasr_embedding_fallback_logged:
logger.exception(
"FunASR realtime speaker embedding failed; falling back to the "
"configured ModelScope speaker model for this batch"
)
_funasr_embedding_fallback_logged = True
registry = get_speaker_registry_service()
embeddings = [
await run_sync(
registry.extract_embedding_from_audio,
item["audio"],
16000,
model_id=settings.REALTIME_SV_MODEL,
model_revision=settings.REALTIME_SV_MODEL_REVISION or None,
)
for item in chunk_items
]
results: List[Dict[str, Any]] = []
for item, embedding in zip(chunk_items, embeddings):
results.append(