f4b4f1fc4f
CI/CD Pipeline / Validate Code Quality And Tests (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Frontend Lint (push) Has been cancelled
CI/CD Pipeline / Build & Push Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production Runtime Images (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
feat: ASR自动字幕能力
48 lines
1.3 KiB
Python
Executable File
48 lines
1.3 KiB
Python
Executable File
"""ASR 服务工厂 — 根据环境配置创建对应 ASR 服务实例。
|
||
|
||
支持的后端:
|
||
- mock: MockASRService(测试/开发用)
|
||
- 后续可扩展:whisper / aliyun / tencent 等
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from functools import lru_cache
|
||
|
||
from packages.ports.asr_service import ASRService
|
||
|
||
|
||
@lru_cache(maxsize=1)
|
||
def get_asr_service() -> ASRService | None:
|
||
"""获取全局 ASR 服务实例(单例)。
|
||
|
||
根据环境变量 ASR_PROVIDER 决定使用哪个后端:
|
||
- mock / 空 / 未设置: 返回 None(不启用 ASR)
|
||
- mock: 使用 MockASRService
|
||
|
||
Returns:
|
||
ASRService 实例,未配置或不启用时返回 None
|
||
"""
|
||
provider = os.environ.get("ASR_PROVIDER", "").lower().strip()
|
||
|
||
if not provider:
|
||
return None
|
||
|
||
if provider == "mock":
|
||
from packages.adapters.asr.mock_asr_service import MockASRService
|
||
|
||
return MockASRService()
|
||
|
||
# 未知 provider,记录日志并返回 None(不启用 ASR,不阻断主流程)
|
||
import logging
|
||
|
||
logger = logging.getLogger(__name__)
|
||
logger.warning("未知的 ASR provider: %s,ASR 自动字幕功能未启用", provider)
|
||
return None
|
||
|
||
|
||
def reset_asr_service_cache() -> None:
|
||
"""重置 ASR 服务缓存(测试用)。"""
|
||
get_asr_service.cache_clear()
|