Files
operating-room-monitor-server/app/services/voice_file_log.py
Kevin 0c05463617 feat: 语音确认、联调与运维增强
- 语音:序数解析(第一个/第二个等)、解析失败计数与 API detail.retry_remaining;
  百度 ASR 固定 dev_pid 为普通话;SurgeryPipelineError 支持 extra 并入 HTTP detail。
- Demo:demo 路由与假 RTSP、客户端 index 与 README;BackendResolver 与配置调整。
- 可观测:消耗 TSV 日志、语音文件日志、终端 Markdown 辅助;相关测试与依赖更新。
- 注意:.env 仍被 gitignore,本地密钥不会进入本提交。

Made-with: Cursor
2026-04-23 14:24:20 +08:00

168 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""语音确认ASR/解析/审计)的终端 loguru 行 + 每手术 TSV 落盘,与 `consumption_tsv_log` 并列。"""
from __future__ import annotations
import re
import threading
from datetime import datetime, timezone
from pathlib import Path
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from loguru import logger
from app.config import Settings
_lock = threading.Lock()
HEADER = (
"时间戳(ISO,UTC)\t来源\t状态\tconfirmation_id\tasr/识别文本\t"
"resolved_label\trejected\terror\taudio_object_key\n"
)
def _ts_iso_utc() -> str:
return datetime.now(timezone.utc).isoformat(timespec="milliseconds")
def _encode_cell(value: str) -> str:
return (value or "").replace("\r", " ").replace("\n", " ").replace("\t", " ")
def _log_tz_info(settings: Settings) -> object:
raw = (settings.consumption_log_timezone or "").strip()
if not raw:
lt = datetime.now().astimezone().tzinfo
return lt if lt is not None else timezone.utc
try:
return ZoneInfo(raw)
except ZoneInfoNotFoundError:
return timezone.utc
def _ts_local_for_display(settings: Settings) -> str:
tz = _log_tz_info(settings)
return datetime.now(tz).isoformat(timespec="milliseconds")
def _safe_surgery_path_segment(surgery_id: str) -> str:
s = (surgery_id or "unknown").strip() or "unknown"
s = re.sub(r"[^\w\-.@]", "_", s)
return s[:200] if len(s) > 200 else s
def resolved_voice_log_path(surgery_id: str, settings: Settings) -> Path:
raw = (settings.voice_file_log_path or "logs/voice_{surgery_id}.txt").strip()
safe = _safe_surgery_path_segment(surgery_id)
if "{surgery_id}" in raw:
raw = raw.replace("{surgery_id}", safe)
else:
p0 = Path(raw)
if p0.suffix:
raw = str(p0.with_name(f"{p0.stem}_{safe}{p0.suffix}"))
else:
raw = f"{raw.rstrip('/')}_{safe}.txt"
p = Path(raw).expanduser()
if not p.is_absolute():
p = Path.cwd() / p
return p
def init_voice_log_file(surgery_id: str, settings: Settings) -> None:
"""与 `init_consumption_log_file` 同生命周期:`start_surgery` 时截断并写表头。"""
if not settings.voice_file_log_enabled:
return
path = resolved_voice_log_path(surgery_id, settings)
path.parent.mkdir(parents=True, exist_ok=True)
with _lock:
with path.open("w", encoding="utf-8") as f:
f.write(HEADER)
def append_voice_tsv_line(surgery_id: str, line: str, settings: Settings) -> None:
if not settings.voice_file_log_enabled:
return
path = resolved_voice_log_path(surgery_id, settings)
path.parent.mkdir(parents=True, exist_ok=True)
with _lock:
with path.open("a", encoding="utf-8") as f:
f.write(line)
def emit_voice_event(
settings: Settings,
*,
surgery_id: str,
source: str,
status: str,
confirmation_id: str,
asr_text: str | None = None,
resolved_label: str | None = None,
rejected: str | bool | None = None,
error_message: str | None = None,
audio_object_key: str | None = None,
) -> None:
"""
终端:单条可 grep 的 VoiceConfirm 行文件TSV 一行(与启用的 `voice_file_log_enabled` 一致)。
:param source: `wav` | `text` | `n/a`
:param status: 与审计 `status` 或 `minio_not_configured` 等说明型状态一致
"""
rj: str
if rejected is None:
rj = ""
elif isinstance(rejected, bool):
rj = "true" if rejected else "false"
else:
rj = str(rejected)
ts_utc = _ts_iso_utc()
local_hint = _ts_local_for_display(settings)
if status in ("recognized", "rejected"):
logger.info(
"VoiceConfirm local_ts={!r} surgery_id={} source={} status={} "
"confirmation_id={} asr_text={!r} resolved_label={!r} rejected={} "
"error={!r} audio_key={!r}",
local_hint,
surgery_id,
source,
status,
confirmation_id,
asr_text,
resolved_label,
rj,
error_message,
audio_object_key,
)
else:
logger.warning(
"VoiceConfirm local_ts={!r} surgery_id={} source={} status={} "
"confirmation_id={} asr_text={!r} resolved_label={!r} rejected={} "
"error={!r} audio_key={!r}",
local_hint,
surgery_id,
source,
status,
confirmation_id,
asr_text,
resolved_label,
rj,
error_message,
audio_object_key,
)
if not settings.voice_file_log_enabled:
return
row = [
_encode_cell(ts_utc),
_encode_cell(source),
_encode_cell(status),
_encode_cell(confirmation_id),
_encode_cell("" if asr_text is None else asr_text),
_encode_cell("" if resolved_label is None else resolved_label),
_encode_cell(rj),
_encode_cell("" if error_message is None else error_message),
_encode_cell("" if audio_object_key is None else audio_object_key),
]
line = "\t".join(row) + "\n"
append_voice_tsv_line(surgery_id, line, settings)