Spaces:
Sleeping
Sleeping
File size: 3,355 Bytes
4df9b63 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """Apply explicit semantic-review decisions; never invent an audit or gold label."""
import argparse
import json
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'src'))
from decision_model.data import read_jsonl, write_jsonl, apply_reviews, Ticket, deduplicate, normalize
from decision_model.artifacts import sha256
parser = argparse.ArgumentParser()
parser.add_argument('--freeze-canonical', action='store_true')
args = parser.parse_args()
directory = ROOT / 'data/review'
directory.mkdir(parents=True, exist_ok=True)
decisions = json.loads((directory/'audit-decisions.json').read_text(encoding='utf-8'))
seeds = json.loads((ROOT/'data/generated/seeds.json').read_text(encoding='utf-8'))
indices = {s['id']: i for i, s in enumerate(seeds)}
canonical = directory/'canonical-base.jsonl'
base = canonical if canonical.exists() else ROOT/'data/generated/tickets.jsonl'
records = read_jsonl(base)
if not canonical.exists():
# Freeze family membership from unedited source/translation text. An audit
# correction must never separate previously correlated examples.
clustered = deduplicate(records, source_texts={s['id']:s['text'] for s in seeds})
by_id = {t.id:t.group_id for t in clustered}
by_text = {(t.language,normalize(t.text)):t.group_id for t in clustered}
family = {t.id:by_id.get(t.id) or by_text[(t.language,normalize(t.text))] for t in records}
records = apply_reviews(records, ROOT/'data/generated/review-overrides.jsonl')
records = [t.model_copy(update={'group_id':family[t.id]}) for t in records]
# The immutable base retains exclusions and original text, so subsequent
# corrections/exclusions only change annotations, never the population.
if args.freeze_canonical:
write_jsonl(canonical, records)
reviewed = set(decisions['reviewed_indices'])
excluded = set(map(int, decisions.get('excluded', {})))
result, checked, dropped = [], 0, []
for ticket in records:
index = indices[ticket.id.rsplit(':',1)[0]]
if index in excluded:
dropped.append(ticket.id)
continue
body = ticket.model_dump()
if index in reviewed:
changes = decisions.get('corrections', {}).get(str(index), {})
if not set(changes).issubset({'department','wants_refund','frustration','en','uk'}):
raise ValueError('Unexpected audit override field')
body.update({k:v for k,v in changes.items() if k not in {'en','uk'}})
body['text'] = changes.get(ticket.language, ticket.text)
body.update(reviewed=True, reviewer=decisions['reviewer'])
checked += 1
result.append(Ticket.model_validate(body))
if args.freeze_canonical and base == canonical:
raise ValueError('Canonical base already exists; do not overwrite a frozen review population')
write_jsonl(directory/'tickets.jsonl',result)
report = {'base_sha256':sha256(base),'decision_sha256':sha256(directory/'audit-decisions.json'),
'tickets':len(result),'reviewed_tickets':sum(t.reviewed for t in result),'excluded_ids':dropped,
'reviewer':decisions['reviewer'],'review_is_ai_assisted_not_human':True}
(directory/'audit-status.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8')
print(json.dumps({k:v for k,v in report.items() if k!='excluded_ids'},ensure_ascii=False))
|