feat: 新增腾讯云ASR服务,支持按配置切换ASR提供商

- 新增tencent_asr_service.py腾讯云一句话识别
- 优化asr_service.py
- 更新services/__init__.py按ASR_PROVIDER切换whisper/tencent
- 更新requirements.txt

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
iammm0
2026-02-11 16:06:06 +08:00
parent 550de8d157
commit 240a184da8
4 changed files with 150 additions and 8 deletions

View File

@@ -16,11 +16,16 @@ logger = logging.getLogger(__name__)
ASR_MODEL_SIZE = os.getenv("ASR_MODEL_SIZE", "small")
ASR_DEVICE = os.getenv("ASR_DEVICE", "auto") # auto, cpu, cuda
ASR_COMPUTE_TYPE = os.getenv("ASR_COMPUTE_TYPE", "auto") # auto, int8, float16, float32
# 镜像内预置模型目录,设置后直接使用本地模型不联网下载(与 Dockerfile 中 download_root 一致
# 模型缓存目录每次启动优先从该目录加载不设置则使用默认本地路径api/models/whisper
# 设置 ASR_MODEL_CACHE_DIR 时仅使用本地模型不联网(与 Dockerfile 中 download_root 一致)
ASR_MODEL_CACHE_DIR = os.getenv("ASR_MODEL_CACHE_DIR")
# 默认本地缓存目录(相对 api 目录),确保每次启动都先从本地加载
_DEFAULT_ASR_CACHE_DIR = os.path.normpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "models", "whisper")
)
class ASRService:
class WhisperASRService:
"""
ASR 服务(语音转文字)
使用 faster-whisper 本地模型
@@ -60,8 +65,12 @@ class ASRService:
else:
compute_type = "int8" # CPU 使用 int8 量化,速度更快
download_root = ASR_MODEL_CACHE_DIR if ASR_MODEL_CACHE_DIR else None
local_files_only = bool(ASR_MODEL_CACHE_DIR)
# 每次启动都先从本地目录加载:优先用环境变量,否则用默认 api/models/whisper
download_root = ASR_MODEL_CACHE_DIR if ASR_MODEL_CACHE_DIR else _DEFAULT_ASR_CACHE_DIR
local_files_only = bool(ASR_MODEL_CACHE_DIR) # 仅当显式设置缓存目录时禁止联网(如 Docker
if not os.path.isdir(download_root):
os.makedirs(download_root, exist_ok=True)
logger.info(f"Whisper 模型从本地加载: download_root={download_root}, local_files_only={local_files_only}")
self.model = WhisperModel(
ASR_MODEL_SIZE,
device=device,
@@ -156,4 +165,4 @@ class ASRService:
# 全局实例
asr_service = ASRService()
asr_service = WhisperASRService()