Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(web): 人脸模型给活动中心总控块一份只读状态

longmeidao9eb06701b8d3
diff --git a/docs/REUSE.md b/docs/REUSE.md
index 7ab3fd784..d892e42d6 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -859,7 +859,7 @@ Python 源文件纳入 `src/peach/library_sharing.py` 与 `src/peach/library_pro
任务中心复用 `CodeBlock` 展示可复制的 JSON 技术详情。`task_results.result_counts` 将链接检查、资源扫描、追更、订阅、来源查找及浏览记录分析的明细折算为领域数量,供后台结算与读取投影共用;读取投影只补同一轮已结束的恢复检查点,不写 ledger。想要资料任务按作品保存未完整取得的结果,续跑时逐项更新数量。页面保留失败、访问失败、未能清理等结果,有问题的完成任务显示“发现问题”并独立展示;摘要里的能力缺失标记(`face_match_unavailable`,人脸模型取不到时退回旧判据的那一轮)同样算问题。原始任务执行状态仍由任务服务维护。未新增依赖。
-人脸模型(YuNet、SFace)是全进程共用的一次性能力。`face_detect.fetch_model` 每次联网取模型的结果经 `face_detect.observer` 报给 `face_models.Reporter`,它只按转变写 `face-models` 任务记录:从能用到取不到一条失败,取回来一条成功,同一次失败反复撞上不再写。失败卡上的「重新下载」调 `POST /api/face-models`,走普通 `BackgroundJob`;取到后 `face_detect.downloads` 加一,常驻的 `avatar_face.FaceProbe` 据此提前结束重试窗口。未新增依赖。
+人脸模型(YuNet、SFace)是全进程共用的一次性能力。`face_detect.fetch_model` 每次联网取模型的结果经 `face_detect.observer` 报给 `face_models.Reporter`,它只按转变写 `face-models` 任务记录:从能用到取不到一条失败,取回来一条成功,同一次失败反复撞上不再写。失败卡上的「重新下载」调 `POST /api/face-models`,走普通 `BackgroundJob`;取到后 `face_detect.downloads` 加一,常驻的 `avatar_face.FaceProbe` 据此提前结束重试窗口。活动中心总控块读 `face_models.status(contract.task_runs)`:只查最新一条记录和两个模型文件在不在,不联网,返回 `state`(`ready`/`missing`/`failed`/`downloading`)、失败时刻与原因、对应任务卡的 `run_id` 和 `retry_url`。未新增依赖。
普通 Toast 与扫描任务卡共用 Agent Progress 外框,16px 半径由通知栈内的既有 token 解析,细边框与接触阴影分别复用 `border-button-default`、`elevation-xs`,不改变内容排版。浏览器回归核对桌面、手机和明暗主题下的外框一致性,并以同一任务、同一内容回放外框基线生成对比图。
diff --git a/src/peach/face_models.py b/src/peach/face_models.py
index b78223b20..d84afd845 100644
--- a/src/peach/face_models.py
+++ b/src/peach/face_models.py
@@ -66,6 +66,45 @@ def attach(runs) -> None:
face_detect.observer = Reporter(runs)
+#: 「重新下载」的地址。任务卡和活动中心的总控块都用这一个。
+RETRY_URL = "/api/face-models"
+
+
+def status(runs) -> dict:
+ """人脸模型此刻的状态,给活动中心的总控块读。不联网、不算哈希,读一次就是一条查询。
+
+ 判据和任务卡同一个来源:最新那条 `face-models` 记录。`state` 取四个值:
+
+ - `downloading`:重新下载那一轮在跑。
+ - `failed`:最新一条是失败,`failed_at`/`error`/`run_id` 指向那张卡。
+ - `ready`:两个模型文件都在本机。哈希留到用的时候由 `fetch_model` 核对。
+ - `missing`:还没取过,下次用到时自动去取;这不是问题,不必提示。
+
+ 详细原因只在任务卡上写,这里的 `error` 供总控判断与排查,不另起一套说法。
+ """
+ from . import face_match
+
+ latest = runs.query(task_key=TASK_KEY, limit=1)
+ last = latest[0] if latest else None
+ present = face_detect.MODEL_PATH.is_file() and face_match.MODEL_PATH.is_file()
+ if last is not None and last.active:
+ state = "downloading"
+ elif last is not None and last.status == "failed":
+ state = "failed"
+ else:
+ state = "ready" if present else "missing"
+ failed = state == "failed"
+ return {
+ "task_key": TASK_KEY,
+ "state": state,
+ "available": state == "ready",
+ "failed_at": last.finished_at if failed else None,
+ "error": last.error if failed else "",
+ "run_id": last.id if last is not None and state in ("failed", "downloading") else None,
+ "retry_url": RETRY_URL,
+ }
+
+
def download_all() -> dict:
"""重新下载那一轮:两个模型各取一次,本地已有且校验对得上的直接算取到。
diff --git a/tests/test_face_models.py b/tests/test_face_models.py
index a610f1221..01189ffe1 100644
--- a/tests/test_face_models.py
+++ b/tests/test_face_models.py
@@ -121,8 +121,8 @@ class FetchObserverTests(FaceModelsCase):
self.assertEqual(face_detect.downloads, before + 1)
-class RetryEndpointTests(FaceModelsCase):
- """任务卡上的「重新下载」:一轮普通的后台任务,记录只有它自己那一行。"""
+class ModelFilesCase(FaceModelsCase):
+ """两个模型落在临时目录,重新下载走真实路由与后台任务,只有取字节那一步换掉。"""
def setUp(self):
super().setUp()
@@ -143,6 +143,10 @@ class RetryEndpointTests(FaceModelsCase):
web_router.dispatch_api_post(self.contract, "/api/face-models", {})
self.contract.face_models_job.thread.join(5)
+
+class RetryEndpointTests(ModelFilesCase):
+ """任务卡上的「重新下载」:一轮普通的后台任务,记录只有它自己那一行。"""
+
def test_a_failed_retry_is_one_failed_manual_row(self):
self.retry(side_effect=OSError("断网"))
[run] = self.rows()
@@ -160,6 +164,40 @@ class RetryEndpointTests(FaceModelsCase):
self.assertIn("/api/face-models", web_router.READ_ONLY_POST_ROUTES)
+class StatusTests(ModelFilesCase):
+ """活动中心总控块读的那一份:和任务卡同一个来源,读的时候不联网。"""
+
+ def status(self):
+ with mock.patch.object(face_detect, "read_url",
+ side_effect=AssertionError("读状态不该联网")):
+ return face_models.status(self.store)
+
+ def test_never_fetched_is_missing_not_a_problem(self):
+ self.assertEqual(self.status(), {
+ "task_key": TASK, "state": "missing", "available": False, "failed_at": None,
+ "error": "", "run_id": None, "retry_url": face_models.RETRY_URL})
+
+ def test_the_latest_failure_is_reported_with_its_card(self):
+ self.retry(side_effect=OSError("断网"))
+ [run] = self.rows()
+ current = self.status()
+ self.assertEqual((current["state"], current["available"]), ("failed", False))
+ self.assertEqual((current["failed_at"], current["error"], current["run_id"]),
+ (run.finished_at, run.error, run.id))
+ self.assertEqual(current["retry_url"], "/api/face-models")
+
+ def test_a_retry_in_progress_is_downloading(self):
+ run = self.store.start(TASK, trigger="manual", mutex_key=TASK)
+ self.assertEqual((self.status()["state"], self.status()["run_id"]), ("downloading", run.id))
+
+ def test_both_models_on_disk_after_a_recovery_is_ready(self):
+ self.retry(side_effect=OSError("断网"))
+ self.retry(return_value=self.payload)
+ current = self.status()
+ self.assertEqual((current["state"], current["available"], current["error"]),
+ ("ready", True, ""))
+
+
class LabelTests(unittest.TestCase):
def test_every_task_written_by_the_service_has_a_chinese_name(self):
self.assertEqual(task_label("face-models"), "下载人脸模型")