e206778a5c
Cluster B, milestone #99. Backend for the allowlist tuning dashboard. #7a: AllowlistService.coverage(tag_id, threshold) counts distinct images with a prediction resolving to the tag (raw_name==tag.name OR (raw_name,category) in the tag's aliases) scoring >= threshold — the gross candidate pool, mirroring tasks.ml._confidence_for_tag resolution. list_all now carries applied_count (grouped image_tag count) + coverage_count (at the row's threshold). New GET /api/tags/<id>/allowlist/coverage?threshold= for the live what-if number. #7b: /suggestions/accept + /alias return {allowlisted, tag_id, tag_name, projected_count} (projection at the tag's threshold) instead of 204, so the UI can show a non-blocking 'auto-applying to ~N images' toast. Apply still runs async via apply_allowlist_tags — projected_count is an estimate. Tests: coverage by threshold (direct + alias-with-category), list applied vs coverage, coverage route (explicit/default/bad threshold), accept/alias payload (newly-allowlisted vs already-on-list). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XCUHUGQLrBrkgyk1t49kpX
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
"""Allowlist API: list, adjust threshold, remove."""
|
|
|
|
from quart import Blueprint, jsonify, request
|
|
|
|
from ..extensions import get_session
|
|
from ..models import TagAllowlist
|
|
from ..services.ml.allowlist import AllowlistService
|
|
|
|
allowlist_bp = Blueprint("allowlist", __name__, url_prefix="/api")
|
|
|
|
|
|
@allowlist_bp.route("/allowlist", methods=["GET"])
|
|
async def list_allowlist():
|
|
async with get_session() as session:
|
|
rows = await AllowlistService(session).list_all()
|
|
return jsonify(
|
|
[
|
|
{
|
|
"tag_id": r.tag_id,
|
|
"tag_name": r.tag_name,
|
|
"tag_kind": r.tag_kind,
|
|
"min_confidence": r.min_confidence,
|
|
"applied_count": r.applied_count,
|
|
"coverage_count": r.coverage_count,
|
|
}
|
|
for r in rows
|
|
]
|
|
)
|
|
|
|
|
|
@allowlist_bp.route("/tags/<int:tag_id>/allowlist/coverage", methods=["GET"])
|
|
async def coverage(tag_id: int):
|
|
"""Live "at threshold T, a sweep would cover ~N images" projection for the
|
|
allowlist tuning dashboard. Defaults to the tag's stored threshold."""
|
|
raw = request.args.get("threshold")
|
|
async with get_session() as session:
|
|
svc = AllowlistService(session)
|
|
if raw is not None:
|
|
try:
|
|
threshold = float(raw)
|
|
except ValueError:
|
|
return jsonify({"error": "threshold must be a float"}), 400
|
|
if not (0 < threshold <= 1):
|
|
return jsonify({"error": "threshold must be in (0, 1]"}), 400
|
|
else:
|
|
row = await session.get(TagAllowlist, tag_id)
|
|
if row is None:
|
|
return jsonify({"error": "not on allowlist"}), 404
|
|
threshold = row.min_confidence
|
|
count = await svc.coverage(tag_id, threshold)
|
|
return jsonify({"count": count, "threshold": threshold})
|
|
|
|
|
|
@allowlist_bp.route("/tags/<int:tag_id>/allowlist", methods=["GET"])
|
|
async def get_one(tag_id: int):
|
|
async with get_session() as session:
|
|
row = await session.get(TagAllowlist, tag_id)
|
|
if row is None:
|
|
return jsonify({"error": "not on allowlist"}), 404
|
|
return jsonify(
|
|
{"min_confidence": row.min_confidence, "added_at": row.added_at.isoformat()}
|
|
)
|
|
|
|
|
|
@allowlist_bp.route("/tags/<int:tag_id>/allowlist", methods=["PATCH"])
|
|
async def patch_threshold(tag_id: int):
|
|
body = await request.get_json()
|
|
if not body or "min_confidence" not in body:
|
|
return jsonify({"error": "min_confidence required"}), 400
|
|
mc = float(body["min_confidence"])
|
|
if not (0 < mc <= 1):
|
|
return jsonify({"error": "min_confidence must be in (0, 1]"}), 400
|
|
async with get_session() as session:
|
|
await AllowlistService(session).update_threshold(tag_id, mc)
|
|
await session.commit()
|
|
return "", 204
|
|
|
|
|
|
@allowlist_bp.route("/tags/<int:tag_id>/allowlist", methods=["DELETE"])
|
|
async def remove(tag_id: int):
|
|
async with get_session() as session:
|
|
await AllowlistService(session).remove(tag_id)
|
|
await session.commit()
|
|
return "", 204
|