Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
94 lines
3.7 KiB
Python
94 lines
3.7 KiB
Python
"""Run the labeled moderation eval set live against the configured safeguard model.
|
|
|
|
Usage (needs the chosen hop's credentials in the env: RAILYARD_RELAY_URL/_API_KEY,
|
|
GROQ_API_KEY, OPENROUTER_API_KEY or ANTHROPIC_API_KEY)::
|
|
|
|
python -m tools.ops.moderation_eval [path/to/moderation_eval.tsv] \
|
|
[--transport relay|groq|openrouter|claude] [--delay SECONDS]
|
|
|
|
Positive class = "deny". A benign prompt counts as a false positive only if it is
|
|
denied (flag passes through); a violating prompt counts as caught only if denied.
|
|
Prints per-row results (label, category, decision, latency; never the model's
|
|
rationale) plus precision/recall and latency percentiles. Bypasses the cache.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import statistics
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from app.core import moderation_claude
|
|
from app.core import moderation_safeguard as sg
|
|
from app.core.moderation_types import ModerationUnavailable
|
|
|
|
TRANSPORTS = {
|
|
"relay": (sg._via_relay, sg.relay_model_id),
|
|
"groq": (sg._via_groq, sg.model_id),
|
|
"openrouter": (sg._via_openrouter, sg.openrouter_model_id),
|
|
"claude": (sg._via_claude, moderation_claude.model_id),
|
|
}
|
|
DEFAULT_SET = Path(__file__).resolve().parents[4] / "tests" / "fixtures" / "moderation_eval.tsv"
|
|
|
|
|
|
def load(path: Path) -> list[tuple[str, str, str]]:
|
|
rows = []
|
|
for line in path.read_text(encoding="utf-8").splitlines():
|
|
if not line.strip() or line.startswith("#"):
|
|
continue
|
|
label, category, prompt = line.split("\t", 2)
|
|
rows.append((label, category, prompt))
|
|
return rows
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
transport = "relay"
|
|
if "--transport" in argv:
|
|
transport = argv[argv.index("--transport") + 1]
|
|
delay = float(argv[argv.index("--delay") + 1]) if "--delay" in argv else 0.0
|
|
paths = [a for a in argv if a.endswith(".tsv")]
|
|
rows = load(Path(paths[0]) if paths else DEFAULT_SET)
|
|
if transport == "relay" and not sg.relay_url():
|
|
transport = "groq"
|
|
if transport not in TRANSPORTS:
|
|
print(f"unknown transport {transport!r}; choose one of {sorted(TRANSPORTS)}")
|
|
return 2
|
|
call, model_of = TRANSPORTS[transport]
|
|
tp = fp = fn = tn = errors = 0
|
|
latencies: list[float] = []
|
|
for index, (label, category, prompt) in enumerate(rows):
|
|
if delay and index:
|
|
time.sleep(delay)
|
|
start = time.perf_counter()
|
|
try:
|
|
result = call(prompt, 15.0)
|
|
decision, cats = result.decision, ",".join(result.categories) or "-"
|
|
except ModerationUnavailable as exc:
|
|
decision, cats = "error", str(exc)[:60]
|
|
errors += 1
|
|
ms = (time.perf_counter() - start) * 1000
|
|
latencies.append(ms)
|
|
denied = decision == "deny"
|
|
if label == "violating":
|
|
tp, fn = tp + denied, fn + (not denied)
|
|
else:
|
|
fp, tn = fp + denied, tn + (not denied)
|
|
ok = denied == (label == "violating")
|
|
print(f"{'ok ' if ok else 'BAD'} {label:9} {category:22} {decision:5} {cats:30} {ms:6.0f}ms")
|
|
precision = tp / (tp + fp) if tp + fp else 0.0
|
|
recall = tp / (tp + fn) if tp + fn else 0.0
|
|
lat = sorted(latencies)
|
|
p95 = lat[max(0, int(len(lat) * 0.95) - 1)]
|
|
print(
|
|
f"\ntransport={transport} model={model_of()} n={len(rows)} TP={tp} FP={fp} "
|
|
f"FN={fn} TN={tn} errors={errors}\nprecision={precision:.3f} recall={recall:.3f} "
|
|
f"benign_denied={fp}/{fp + tn} violations_caught={tp}/{tp + fn}\n"
|
|
f"latency_ms p50={statistics.median(lat):.0f} p95={p95:.0f} max={lat[-1]:.0f}"
|
|
)
|
|
return 0 if fp <= 1 and fn <= 1 and not errors else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main(sys.argv[1:]))
|