save code
This commit is contained in:
+27
-16
@@ -15,25 +15,36 @@ class ASRService:
|
||||
self._ready = False
|
||||
self._attempted = False
|
||||
self._init_error: Optional[str] = None
|
||||
self._device: str = "uninitialized"
|
||||
|
||||
def _ensure_loaded(self) -> None:
|
||||
if self._ready or self._attempted:
|
||||
return
|
||||
self._attempted = True
|
||||
try:
|
||||
from funasr import AutoModel
|
||||
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
||||
from funasr import AutoModel
|
||||
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
||||
|
||||
self._model = AutoModel(
|
||||
model="FunAudioLLM/SenseVoiceSmall",
|
||||
device="cuda:0",
|
||||
hub="hf",
|
||||
)
|
||||
self._post = rich_transcription_postprocess
|
||||
self._ready = True
|
||||
except Exception as exc: # pragma: no cover
|
||||
self._init_error = str(exc)
|
||||
logger.warning("ASR fallback mode: %s", exc)
|
||||
errors: list[str] = []
|
||||
for device in ("cuda:0", "cpu"):
|
||||
try:
|
||||
self._model = AutoModel(
|
||||
model="FunAudioLLM/SenseVoiceSmall",
|
||||
device=device,
|
||||
hub="hf",
|
||||
)
|
||||
self._post = rich_transcription_postprocess
|
||||
self._ready = True
|
||||
self._init_error = None
|
||||
self._device = device
|
||||
if device != "cuda:0":
|
||||
logger.warning("ASR running in degraded mode on %s", device)
|
||||
return
|
||||
except Exception as exc: # pragma: no cover
|
||||
errors.append(f"{device}: {exc}")
|
||||
|
||||
self._init_error = " | ".join(errors)
|
||||
self._device = "unavailable"
|
||||
logger.warning("ASR unavailable: %s", self._init_error)
|
||||
|
||||
async def transcribe(self, audio_16k: np.ndarray) -> str:
|
||||
self._ensure_loaded()
|
||||
@@ -46,9 +57,9 @@ class ASRService:
|
||||
)
|
||||
return self._post(res[0]["text"])
|
||||
|
||||
# Fallback for initial bring-up
|
||||
return "(ASR降级)语音已接收"
|
||||
# When ASR is unavailable, avoid emitting fake user text that would trigger a bogus LLM reply.
|
||||
return ""
|
||||
|
||||
@property
|
||||
def health(self) -> dict:
|
||||
return {"ready": self._ready, "attempted": self._attempted, "error": self._init_error}
|
||||
return {"ready": self._ready, "attempted": self._attempted, "device": self._device, "error": self._init_error}
|
||||
|
||||
Reference in New Issue
Block a user