feat: 配置写死与 baked 模块,Alembic 建表,百度仅 BAIDU_*

- 新增 app/baked/algorithm|pipeline,非部署参数不再走 env;Settings 保留 DB/HTTP/RTSP/海康/百度/MinIO/Demo
- 移除 init_db_schema 与 reload 配置;main 仅 check_database;start*.sh 在 uvicorn 前执行 alembic upgrade head
- 依赖 psycopg[binary] 供 Alembic 同步 URL;alembic/env 注释与预发清单更新
- 撕段门控消费管线、各视频/语音/归档调用改为 baked
- 百度环境变量仅 BAIDU_APP_ID、BAIDU_API_KEY、BAIDU_SECRET_KEY 与 BAIDU_* 超时/ASR;人脸脚本与 baidu_speech 文案同步
- 全量单测与 .env.example 更新;.gitignore 忽略 refs/(本地权重/视频不入库)

Made-with: Cursor
This commit is contained in:
Kevin
2026-04-24 15:33:22 +08:00
parent b651364877
commit 8a4bad99d3
47 changed files with 1333 additions and 648 deletions

View File

@@ -26,6 +26,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
import app.db.models # noqa: F401 # register ORM tables on Base.metadata
import main as main_module
from app.baked import pipeline as bp
from app.db.base import Base
from app.dependencies import AppContainer, build_container
from app.domain.consumption import SurgeryConsumptionStored
@@ -165,7 +166,6 @@ def integration_client(
return None
monkeypatch.setattr(main_module, "check_database", _noop)
monkeypatch.setattr(main_module, "init_db_schema", _noop)
class _FakeEngine:
async def dispose(self) -> None:
@@ -176,11 +176,10 @@ def integration_client(
from app.config import settings as real_settings
monkeypatch.setattr(
real_settings,
"archive_persist_durable_fallback_dir",
bp,
"ARCHIVE_PERSIST_DURABLE_FALLBACK_DIR",
str(tmp_path / "pending_archive"),
)
monkeypatch.setattr(real_settings, "auto_create_schema", False)
def _stubbed_build_container(*args, **kwargs) -> AppContainer:
container = build_container(real_settings, session_factory=sqlite_factory)

View File

@@ -8,7 +8,7 @@ from datetime import datetime, timezone
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from app.config import Settings
from app.baked import pipeline as bp
from app.domain.consumption import SurgeryConsumptionStored
from app.repositories.surgery_results import SurgeryResultRepository
from app.services.video.archive_persister import ArchivePersister
@@ -39,12 +39,12 @@ def _detail(item_id: str = "纱布") -> SurgeryConsumptionStored:
async def test_persist_or_archive_writes_durable_fallback(
tmp_path,
sqlite_session_factory: async_sessionmaker[AsyncSession],
monkeypatch: pytest.MonkeyPatch,
) -> None:
fallback_dir = tmp_path / "pending_archive"
settings = Settings(archive_persist_durable_fallback_dir=str(fallback_dir))
monkeypatch.setattr(bp, "ARCHIVE_PERSIST_DURABLE_FALLBACK_DIR", str(fallback_dir))
repo = _AlwaysFailRepo()
persister = ArchivePersister(
settings=settings,
repository=repo,
session_factory=sqlite_session_factory,
)
@@ -62,6 +62,7 @@ async def test_persist_or_archive_writes_durable_fallback(
async def test_recover_from_durable_fallback_reloads_pending_archive(
tmp_path,
sqlite_session_factory: async_sessionmaker[AsyncSession],
monkeypatch: pytest.MonkeyPatch,
) -> None:
fallback_dir = tmp_path / "pending_archive"
fallback_dir.mkdir()
@@ -82,9 +83,8 @@ async def test_recover_from_durable_fallback_reloads_pending_archive(
(fallback_dir / "recov01.json").write_text(
json.dumps(payload, ensure_ascii=False), encoding="utf-8"
)
settings = Settings(archive_persist_durable_fallback_dir=str(fallback_dir))
monkeypatch.setattr(bp, "ARCHIVE_PERSIST_DURABLE_FALLBACK_DIR", str(fallback_dir))
persister = ArchivePersister(
settings=settings,
repository=SurgeryResultRepository(),
session_factory=sqlite_session_factory,
)

View File

@@ -20,6 +20,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
import app.db.models # noqa: F401 register ORM tables
import main as main_module
from app.baked import pipeline as bp
from app.db.base import Base
from app.dependencies import AppContainer, build_container
from app.domain.consumption import SurgeryConsumptionStored
@@ -79,7 +80,6 @@ def test_durable_fallback_recovers_on_startup_and_persists(
return None
monkeypatch.setattr(main_module, "check_database", _noop)
monkeypatch.setattr(main_module, "init_db_schema", _noop)
class _FakeEngine:
async def dispose(self) -> None:
@@ -90,10 +90,11 @@ def test_durable_fallback_recovers_on_startup_and_persists(
from app.config import settings as real_settings
monkeypatch.setattr(
real_settings, "archive_persist_durable_fallback_dir", str(durable_dir)
bp,
"ARCHIVE_PERSIST_DURABLE_FALLBACK_DIR",
str(durable_dir),
)
monkeypatch.setattr(real_settings, "auto_create_schema", False)
monkeypatch.setattr(real_settings, "archive_persist_retry_interval_seconds", 5.0)
monkeypatch.setattr(bp, "ARCHIVE_PERSIST_RETRY_INTERVAL_SECONDS", 5.0)
def _build(*_a, **_kw) -> AppContainer:
return build_container(real_settings, session_factory=sqlite_factory)

View File

@@ -0,0 +1,20 @@
"""百度凭据:仅从环境变量 BAIDU_APP_ID / BAIDU_API_KEY / BAIDU_SECRET_KEY 读入。"""
import os
from unittest.mock import patch
from app.config import Settings
def test_speech_creds_from_baidu_env_triplet() -> None:
extra = {
"BAIDU_APP_ID": "app-x",
"BAIDU_API_KEY": "key-x",
"BAIDU_SECRET_KEY": "sec-x",
}
with patch.dict(os.environ, extra, clear=False):
s = Settings()
assert s.baidu_speech_app_id == "app-x"
assert s.baidu_speech_api_key == "key-x"
assert s.baidu_speech_secret_key == "sec-x"
assert s.baidu_speech_configured is True

View File

@@ -2,7 +2,7 @@
import pytest
from app.config import settings
from app.baked import pipeline as bp
from app.services.consumable_vision_algorithm import ClsTop3
from app.services.consumption_tsv_log import (
HEADER,
@@ -25,7 +25,7 @@ def test_short_camera_label() -> None:
def test_build_tsv_line_matches_sample_shape(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(settings, "consumption_log_timezone", "UTC")
monkeypatch.setattr(bp, "CONSUMPTION_LOG_TIMEZONE", "UTC")
best = ClsTop3(
t1_name="一次性医用灭菌棉签",
t1_conf=0.9997,
@@ -90,11 +90,11 @@ def test_replace_pending_line_with_voice_resolution_rewrites_one_row(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""语音确认后应替换 pending 行,而不是再多一行。"""
monkeypatch.setattr(settings, "consumption_tsv_log_enabled", True)
monkeypatch.setattr(settings, "consumption_log_timezone", "UTC")
monkeypatch.setattr(bp, "CONSUMPTION_TSV_LOG_ENABLED", True)
monkeypatch.setattr(bp, "CONSUMPTION_LOG_TIMEZONE", "UTC")
monkeypatch.setattr(
settings,
"consumption_tsv_log_path",
bp,
"CONSUMPTION_TSV_LOG_PATH",
str(tmp_path / "{surgery_id}.txt"),
)
init_consumption_log_file("SURG01")
@@ -126,10 +126,10 @@ def test_per_surgery_file_init_and_append(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(settings, "consumption_tsv_log_enabled", True)
monkeypatch.setattr(bp, "CONSUMPTION_TSV_LOG_ENABLED", True)
monkeypatch.setattr(
settings,
"consumption_tsv_log_path",
bp,
"CONSUMPTION_TSV_LOG_PATH",
str(tmp_path / "{surgery_id}.txt"),
)
init_consumption_log_file("or-001")
@@ -145,10 +145,10 @@ def test_append_consumption_log_summary_appends_three_column_block(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(settings, "consumption_tsv_log_enabled", True)
monkeypatch.setattr(bp, "CONSUMPTION_TSV_LOG_ENABLED", True)
monkeypatch.setattr(
settings,
"consumption_tsv_log_path",
bp,
"CONSUMPTION_TSV_LOG_PATH",
str(tmp_path / "{surgery_id}.txt"),
)
init_consumption_log_file("s1")
@@ -167,7 +167,7 @@ def test_append_consumption_log_summary_appends_three_column_block(
def test_build_consumption_markdown_top123_columns(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(settings, "consumption_log_timezone", "UTC")
monkeypatch.setattr(bp, "CONSUMPTION_LOG_TIMEZONE", "UTC")
best = ClsTop3(
t1_name="一次性医用灭菌棉签",
t1_conf=0.9997,

View File

@@ -7,12 +7,11 @@ from unittest.mock import MagicMock
import pytest
from app.config import Settings
from app.services.consumable_vision_algorithm import ConsumableVisionAlgorithmService
def test_effective_preserves_non_empty_request() -> None:
svc = ConsumableVisionAlgorithmService(Settings())
svc = ConsumableVisionAlgorithmService()
got = svc.effective_candidate_consumables([" 纱布 ", "缝线", "纱布"])
assert got == ["纱布", "缝线"]
@@ -22,8 +21,7 @@ def test_effective_empty_uses_model_when_yaml_has_no_names(
) -> None:
yml = tmp_path / "empty.yaml"
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
s = Settings(consumable_classifier_labels_yaml_path=str(yml))
svc = ConsumableVisionAlgorithmService(s)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
mock_cls = MagicMock()
mock_cls.names = {0: "ban", 1: "apple"}
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
@@ -36,8 +34,7 @@ def test_effective_empty_prefers_yaml_class_names(tmp_path: Path) -> None:
"names:\n 0: 商品甲\n 1: 商品乙\nlabel_id:\n 0: a\n 1: b\n",
encoding="utf-8",
)
s = Settings(consumable_classifier_labels_yaml_path=str(yml))
svc = ConsumableVisionAlgorithmService(s)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
assert svc.effective_candidate_consumables([]) == ["商品甲", "商品乙"]
@@ -46,8 +43,7 @@ def test_effective_whitespace_only_treated_as_empty(
) -> None:
yml = tmp_path / "empty.yaml"
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
s = Settings(consumable_classifier_labels_yaml_path=str(yml))
svc = ConsumableVisionAlgorithmService(s)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
mock_cls = MagicMock()
mock_cls.names = {0: "x"}
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
@@ -60,8 +56,7 @@ def test_build_name_mapping_from_label_id(tmp_path: Path) -> None:
"names:\n 0: 商品A\nlabel_id:\n 0: y1/y2\n",
encoding="utf-8",
)
s = Settings(consumable_classifier_labels_yaml_path=str(yml))
svc = ConsumableVisionAlgorithmService(s)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
m = svc.build_name_mapping(["商品A"])
assert m["商品A"] == "y1/y2"
@@ -74,7 +69,6 @@ def test_build_name_mapping_uses_name_when_no_id_in_yaml(
"names:\n 0: 仅表内有的\nlabel_id: {}\n",
encoding="utf-8",
)
s = Settings(consumable_classifier_labels_yaml_path=str(yml))
svc = ConsumableVisionAlgorithmService(s)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
m = svc.build_name_mapping(["仅表内有的"])
assert m["仅表内有的"] == "仅表内有的"

View File

@@ -6,6 +6,7 @@ from unittest.mock import MagicMock
import pytest
from app.baked import pipeline as bp
from app.config import Settings
from app.services.consumable_vision_algorithm import (
PredictionCandidate,
@@ -178,9 +179,11 @@ async def test_archive_retry_loop_starts() -> None:
@pytest.mark.asyncio
async def test_handle_skips_below_voice_floor() -> None:
async def test_handle_skips_below_voice_floor(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.video_voice_confirm_min_confidence = 0.5
monkeypatch.setattr(bp, "VIDEO_VOICE_CONFIRM_MIN_CONFIDENCE", 0.5)
mgr = CameraSessionManager(
settings=settings,
vision_algorithm=MagicMock(),
@@ -248,10 +251,12 @@ async def test_handle_high_conf_top1_not_in_candidates_enqueues_pending() -> Non
@pytest.mark.asyncio
async def test_handle_mid_confidence_enqueues_pending() -> None:
async def test_handle_mid_confidence_enqueues_pending(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.video_auto_confirm_confidence = 0.8
settings.video_voice_confirm_min_confidence = 0.3
monkeypatch.setattr(bp, "VIDEO_AUTO_CONFIRM_CONFIDENCE", 0.8)
monkeypatch.setattr(bp, "VIDEO_VOICE_CONFIRM_MIN_CONFIDENCE", 0.3)
mgr = CameraSessionManager(
settings=settings,
vision_algorithm=MagicMock(),
@@ -274,10 +279,12 @@ async def test_handle_mid_confidence_enqueues_pending() -> None:
@pytest.mark.asyncio
async def test_handle_voice_disabled_no_pending_for_mid_conf() -> None:
async def test_handle_voice_disabled_no_pending_for_mid_conf(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.voice_confirmation_enabled = False
settings.video_auto_confirm_confidence = 0.8
monkeypatch.setattr(bp, "VOICE_CONFIRMATION_ENABLED", False)
monkeypatch.setattr(bp, "VIDEO_AUTO_CONFIRM_CONFIDENCE", 0.8)
mgr = CameraSessionManager(
settings=settings,
vision_algorithm=MagicMock(),
@@ -296,9 +303,11 @@ async def test_handle_voice_disabled_no_pending_for_mid_conf() -> None:
@pytest.mark.asyncio
async def test_handle_vision_cooldown_skips_duplicate() -> None:
async def test_handle_vision_cooldown_skips_duplicate(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.video_detail_cooldown_sec = 3600.0
monkeypatch.setattr(bp, "VIDEO_DETAIL_COOLDOWN_SEC", 3600.0)
mgr = CameraSessionManager(
settings=settings,
vision_algorithm=MagicMock(),
@@ -317,9 +326,11 @@ async def test_handle_vision_cooldown_skips_duplicate() -> None:
@pytest.mark.asyncio
async def test_handle_pending_dedupe_cooldown() -> None:
async def test_handle_pending_dedupe_cooldown(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.video_detail_cooldown_sec = 3600.0
monkeypatch.setattr(bp, "VIDEO_DETAIL_COOLDOWN_SEC", 3600.0)
mgr = CameraSessionManager(
settings=settings,
vision_algorithm=MagicMock(),

View File

@@ -9,6 +9,7 @@ from unittest.mock import MagicMock
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from app.baked import pipeline as bp
from app.config import Settings
from app.domain.consumption import SurgeryConsumptionStored
from app.repositories.surgery_results import SurgeryResultRepository
@@ -86,8 +87,11 @@ async def test_stop_surgery_failed_persist_goes_to_archive_then_retry_persists(
monkeypatch: pytest.MonkeyPatch,
) -> None:
repo = _FlakyResultRepo()
settings = Settings(
archive_persist_durable_fallback_dir=str(tmp_path / "pending_archive")
settings = Settings()
monkeypatch.setattr(
bp,
"ARCHIVE_PERSIST_DURABLE_FALLBACK_DIR",
str(tmp_path / "pending_archive"),
)
mgr = CameraSessionManager(
settings=settings,

View File

@@ -0,0 +1,70 @@
"""撕段门控 + 段合并的纯逻辑单测(不加载 YOLO"""
from __future__ import annotations
import numpy as np
import pytest
from app.services.tear_gated_segment_consumption.product_map import (
load_tear_segment_name_to_id,
resolve_tear_segment_labels_yaml_path,
)
from app.services.tear_gated_segment_consumption.runner import haocai_mean_topk
from app.services.tear_gated_segment_consumption.segments import merge_tear_segments
def test_merge_tear_segments_one_valid() -> None:
dt = 0.04
rows: list[tuple[int, float, bool, str]] = [
(i, i * dt, True, "A") for i in range(40)
]
segs = merge_tear_segments(
rows,
min_tear_sec=1.2,
min_gap_sec=1.0,
)
assert len(segs) == 1
assert segs[0]["start_frame"] == 0
assert segs[0]["end_frame"] == 39
def test_haocai_mean_topk() -> None:
names = {0: "A", 1: "B"}
a = np.array([0.2, 0.8, 0.0], dtype=np.float32)
b = np.array([0.2, 0.8, 0.0], dtype=np.float32)
t1, c1, t2, c2, t3, c3 = haocai_mean_topk([a, b], names)
assert t1 == "B"
assert abs(c1 - 0.8) < 1e-5
def test_load_tear_segment_name_to_id_uses_package_yaml() -> None:
p = resolve_tear_segment_labels_yaml_path()
assert p.name == "consumable_classifier_labels.yaml"
m = load_tear_segment_name_to_id()
assert "一次性使用灭菌橡胶外科手套" in m or len(m) >= 1
@pytest.mark.asyncio
async def test_append_confirmed_detail_tear_cooldown_keys() -> None:
"""同 item_id 多段在独立 cooldown_key 下应都能写入。"""
from app.services.video.session_registry import SurgerySessionRegistry, SurgerySessionState
reg = SurgerySessionRegistry()
st = SurgerySessionState(candidate_consumables=["X"], name_to_code={"X": "id1"})
await reg.append_confirmed_detail(
state=st,
item_id="SAME",
item_name="A",
doctor_id="d",
source="tear_segment",
cooldown_key="s1:seg:1",
)
await reg.append_confirmed_detail(
state=st,
item_id="SAME",
item_name="A",
doctor_id="d",
source="tear_segment",
cooldown_key="s1:seg:2",
)
assert len(st.details) == 2

View File

@@ -5,7 +5,9 @@ from __future__ import annotations
import tempfile
from pathlib import Path
from app.config import Settings
import pytest
from app.baked import pipeline as bp
from app.services.voice_file_log import (
append_voice_tsv_line,
emit_voice_event,
@@ -14,38 +16,46 @@ from app.services.voice_file_log import (
)
def test_resolved_voice_log_path_replaces_surgery_id() -> None:
s = Settings()
s.voice_file_log_path = "logs/voice_{surgery_id}.txt"
p = resolved_voice_log_path("123456", s)
def test_resolved_voice_log_path_replaces_surgery_id(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(bp, "VOICE_FILE_LOG_PATH", "logs/voice_{surgery_id}.txt")
p = resolved_voice_log_path("123456")
assert p.name == "voice_123456.txt"
assert "logs" in str(p)
def test_init_and_append_tsv() -> None:
def test_init_and_append_tsv(monkeypatch: pytest.MonkeyPatch) -> None:
with tempfile.TemporaryDirectory() as d:
base = Path(d)
s = Settings()
s.voice_file_log_enabled = True
s.voice_file_log_path = str((base / "v_{surgery_id}.txt").resolve())
init_voice_log_file("999999", s)
p = resolved_voice_log_path("999999", s)
monkeypatch.setattr(bp, "VOICE_FILE_LOG_ENABLED", True)
monkeypatch.setattr(
bp,
"VOICE_FILE_LOG_PATH",
str((base / "v_{surgery_id}.txt").resolve()),
)
init_voice_log_file("999999")
p = resolved_voice_log_path("999999")
assert p.exists()
h = p.read_text(encoding="utf-8")
assert "来源" in h and "confirmation_id" in h
line = "ts\ttest\trecognized\tcid1\t\t\tfalse\t\tk.wav\n"
append_voice_tsv_line("999999", line, s)
append_voice_tsv_line("999999", line)
assert p.read_text(encoding="utf-8").endswith(line)
def test_emit_voice_event_writes_when_enabled() -> None:
s = Settings()
s.voice_file_log_enabled = True
def test_emit_voice_event_writes_when_enabled(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(bp, "VOICE_FILE_LOG_ENABLED", True)
with tempfile.TemporaryDirectory() as d:
s.voice_file_log_path = str((Path(d) / "v_{surgery_id}.txt").resolve())
init_voice_log_file("111111", s)
monkeypatch.setattr(
bp,
"VOICE_FILE_LOG_PATH",
str((Path(d) / "v_{surgery_id}.txt").resolve()),
)
init_voice_log_file("111111")
emit_voice_event(
s,
surgery_id="111111",
source="wav",
status="recognized",
@@ -55,7 +65,7 @@ def test_emit_voice_event_writes_when_enabled() -> None:
rejected=False,
audio_object_key="k.wav",
)
p = resolved_voice_log_path("111111", s)
p = resolved_voice_log_path("111111")
body = p.read_text(encoding="utf-8")
assert "纱布" in body
assert "recognized" in body

View File

@@ -11,6 +11,7 @@ from unittest.mock import MagicMock
import pytest
from sqlalchemy import func, select
from app.baked import pipeline as bp
from app.config import Settings
from app.db.models import VoiceConfirmationAudit
from app.repositories.voice_audits import VoiceAuditRepository
@@ -105,7 +106,7 @@ async def test_resolve_recognized_appends_voice_detail_and_audit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.voice_upload_max_bytes = 10 * 1024 * 1024
monkeypatch.setattr(bp, "VOICE_UPLOAD_MAX_BYTES", 10 * 1024 * 1024)
sessions, cid = _active_session_with_pending()
minio = MagicMock()
minio.configured = True
@@ -245,7 +246,7 @@ async def test_audio_too_large_audit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
settings = Settings()
settings.voice_upload_max_bytes = 10
monkeypatch.setattr(bp, "VOICE_UPLOAD_MAX_BYTES", 10)
sessions, cid = _active_session_with_pending()
minio = MagicMock()
minio.configured = True