dlp-lab / demo /src /explain.py
alirezaaminzadeh's picture
Publish AriaDLP synthetic Persian/English redaction lab
eef4eed verified
Raw History Blame Contribute Delete
2.47 kB
from __future__ import annotations
from src.domain import Verdict
from src.i18n import t
from src.schemas import Finding, Kpis, MethodScore, Span, ValidationReport
def decide_verdict(report: ValidationReport, n_docs: int, leftover: int, n_review: int) -> Verdict:
if not report.ok:
return Verdict.INVALID
if n_docs <= 0:
return Verdict.INSUFFICIENT
if leftover > 0 or n_review > 0:
return Verdict.REVIEW
return Verdict.HOLD
def status_label(verdict: Verdict, lang: str) -> str:
mapping = {
Verdict.REVIEW: t(lang, "status_review"),
Verdict.HOLD: t(lang, "status_hold"),
Verdict.INSUFFICIENT: t(lang, "status_insufficient"),
Verdict.INVALID: t(lang, "status_invalid"),
}
return mapping[verdict]
def reasons(policy: MethodScore, baseline: MethodScore, lang: str) -> list[str]:
return [
t(lang, "reason_policy").format(p=f"{policy.precision:.2f}", r=f"{policy.recall:.2f}"),
t(lang, "reason_baseline").format(p=f"{baseline.precision:.2f}", r=f"{baseline.recall:.2f}"),
t(lang, "reason_over").format(n=f"{policy.over_redaction:.3f}", base=f"{baseline.over_redaction:.3f}"),
t(lang, "reason_left").format(n=policy.leftover_exports),
t(lang, "reason_meta").format(n=policy.metadata_leaks),
t(lang, "reason_scope"),
t(lang, "reason_no_llm"),
]
def narrative(kpis: Kpis, lang: str) -> str:
return t(lang, "narrative").format(
docs=kpis.n_docs,
spans=kpis.n_spans,
precision=f"{100 * kpis.policy_precision:.0f}%",
recall=f"{100 * kpis.policy_recall:.0f}%",
leftover=kpis.leftover_exports,
)
def finding_from_span(doc_id: str, span: Span, lang: str) -> Finding:
preview = span.surface if len(span.surface) <= 6 else f"{span.surface[:2]}…{span.surface[-2:]}"
return Finding(
finding_id=span.finding_id,
doc_id=doc_id,
entity_type=span.entity_type.value,
start=span.start,
end=span.end,
surface_preview=preview,
confidence=span.confidence,
method=span.method,
rule_id=span.rule_id,
workflow=span.workflow.value,
reason=span.reason,
language=span.language,
observed=t(lang, "observed_span").format(start=span.start, end=span.end, rule=span.rule_id),
inference=span.reason,
action=t(lang, "action_span").format(kind=span.entity_type.value),
)