3c016af076
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
CI/CD Pipeline / Check push changed paths (push) Successful in 8s
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Build Staging Web Image (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Successful in 48s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 24s
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (push) Failing after 19s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 2m38s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Failing after 2m12s
CI/CD Pipeline / Staging API Integration Tests (push) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (push) Has been skipped
CI/CD Pipeline / Staging E2E Tests (push) Has been skipped
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / CI Gate (push) Has been cancelled
CI/CD Pipeline / Validate - Security (push) Has been cancelled
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Has been cancelled
CI/CD Pipeline / Validate - Style (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
- script_asr_service.py: 捕获ImportError(apps.worker未安装),抛ASRNotConfiguredError - scripts_ai.py B2回退路径: 504超时直接抛出,502/503 ASR/下载错误走desc兜底 - 修复API镜像未打包worker模块导致有旁白视频在MediaKit失败时直接502的问题 - 更新单元测试覆盖ASR失败→desc兜底场景
65 lines
2.1 KiB
Python
65 lines
2.1 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 调用失败
|
||
"""
|
||
# 延迟导入,避免循环依赖和启动时副作用
|
||
try:
|
||
from apps.worker.services.asr_service_factory import get_asr_service
|
||
except ImportError as exc:
|
||
# API 镜像未打包 worker 代码(本地 ASR 依赖 worker 的 asr_service_factory)
|
||
logger.warning("本地 ASR 不可用(apps.worker 未安装): %s", exc)
|
||
raise ASRNotConfiguredError("本地 ASR 服务不可用(worker 模块未安装)") from exc
|
||
|
||
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
|