0c59f83a7f
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 2s
CI/CD Pipeline / Check push changed paths (push) Successful in 21s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 31s
CI/CD Pipeline / Build Staging API Image (push) Successful in 36s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 36s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 42s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 2m51s
CI/CD Pipeline / Integration Tests (push) Successful in 2m54s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 2m25s
CI/CD Pipeline / Frontend Unit Tests (push) Failing after 4m21s
CI/CD Pipeline / Validate - Style (push) Successful in 4m37s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 2m52s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 3m59s
CI/CD Pipeline / Validate - Security (push) Successful in 6m54s
CI/CD Pipeline / Unit Tests (push) Successful in 9m27s
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
Worker Base Image Build / Build Worker Base Image (push) Failing after 20m47s
API Base Image Build / Build API Base Image (push) Failing after 24m44s
CI/CD Pipeline / Deploy Production (push) Failing after 25h49m26s
CI/CD Pipeline / Build Production Web Image (push) Failing after 25h49m30s
CI/CD Pipeline / Frontend Lint (push) Failing after 25h58m58s
CI/CD Pipeline / PR Build API Image (push) Failing after 25h59m1s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 25h58m26s
CI/CD Pipeline / PR Build Worker Image (push) Failing after 25h58m23s
CI/CD Pipeline / PR Build Web Image (push) Failing after 25h58m23s
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Failing after 25h57m20s
CI/CD Pipeline / Retag skipped Staging API Image (push) Failing after 25h57m20s
CI/CD Pipeline / CI Gate (push) Failing after 25h48m52s
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 25h57m20s
CI/CD Pipeline / Canary Release to Production (push) Failing after 25h48m48s
CI/CD Pipeline / Build Production Worker Image (push) Failing after 25h48m52s
CI/CD Pipeline / Build Production API Image (push) Failing after 25h48m52s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""文案提取 ASR 服务封装 — Issue #1893.
|
||
|
||
将已有的 ASR 服务工厂封装为面向文案提取场景的简单接口:
|
||
- transcribe_to_text(video_path) -> str:将视频/音频转写为纯文本
|
||
- 未配置 ASR 时抛 ASRNotConfiguredError(路由层映射为 503)
|
||
- ASR 调用失败时抛 ASRTranscriptionError(路由层映射为 502)
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from pathlib import Path
|
||
|
||
from packages.ports.asr_service import ASRServiceError
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class ASRNotConfiguredError(Exception):
|
||
"""ASR 服务未配置."""
|
||
|
||
|
||
class ASRTranscriptionError(Exception):
|
||
"""ASR 转写失败."""
|
||
|
||
|
||
def transcribe_to_text(media_path: str | Path) -> str:
|
||
"""将视频/音频文件转写为纯文本.
|
||
|
||
Args:
|
||
media_path: 媒体文件路径
|
||
|
||
Returns:
|
||
转写出的文本
|
||
|
||
Raises:
|
||
ASRNotConfiguredError: ASR 服务未配置
|
||
ASRTranscriptionError: ASR 调用失败
|
||
"""
|
||
# 延迟导入,避免循环依赖和启动时副作用
|
||
from apps.worker.services.asr_service_factory import get_asr_service
|
||
|
||
asr = get_asr_service()
|
||
if asr is None:
|
||
raise ASRNotConfiguredError("ASR 服务未配置,请联系管理员配置火山 MediaKit 或阿里云 ASR 密钥")
|
||
|
||
try:
|
||
timeline = asr.transcribe(Path(media_path))
|
||
# 拼接所有分段的文本
|
||
text = "".join(seg.text for seg in timeline.segments)
|
||
return text.strip()
|
||
except ASRNotConfiguredError:
|
||
raise
|
||
except ASRServiceError as exc:
|
||
logger.error("ASR 转写失败: %s", exc)
|
||
raise ASRTranscriptionError(f"语音识别失败: {exc}") from exc
|
||
except Exception as exc:
|
||
logger.error("ASR 转写异常: %s", exc)
|
||
raise ASRTranscriptionError(f"语音识别失败: {exc}") from exc
|