独立依赖
This commit is contained in:
+23
-3
@@ -1,13 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import numpy as np
|
||||
|
||||
from config import asr_bundle_dir
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _asr_model_path() -> Path:
|
||||
p = asr_bundle_dir().resolve()
|
||||
if not p.is_dir():
|
||||
raise FileNotFoundError(f"ASR model directory missing: {p}")
|
||||
if not (p / "configuration.json").is_file():
|
||||
raise FileNotFoundError(f"ASR bundle incomplete (no configuration.json): {p}")
|
||||
return p
|
||||
|
||||
|
||||
class ASRService:
|
||||
def __init__(self) -> None:
|
||||
self._model = None
|
||||
@@ -21,16 +33,26 @@ class ASRService:
|
||||
if self._ready or self._attempted:
|
||||
return
|
||||
self._attempted = True
|
||||
try:
|
||||
model_dir = _asr_model_path()
|
||||
except FileNotFoundError as exc:
|
||||
self._init_error = str(exc)
|
||||
self._device = "unavailable"
|
||||
logger.error("%s", exc)
|
||||
return
|
||||
|
||||
from funasr import AutoModel
|
||||
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
||||
|
||||
model_ref = str(model_dir)
|
||||
errors: list[str] = []
|
||||
for device in ("cuda:0", "cpu"):
|
||||
try:
|
||||
self._model = AutoModel(
|
||||
model="FunAudioLLM/SenseVoiceSmall",
|
||||
model=model_ref,
|
||||
device=device,
|
||||
hub="hf",
|
||||
disable_update=True,
|
||||
)
|
||||
self._post = rich_transcription_postprocess
|
||||
self._ready = True
|
||||
@@ -56,8 +78,6 @@ class ASRService:
|
||||
use_itn=True,
|
||||
)
|
||||
return self._post(res[0]["text"])
|
||||
|
||||
# When ASR is unavailable, avoid emitting fake user text that would trigger a bogus LLM reply.
|
||||
return ""
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user