File size: 3,143 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
"""Independent authored inputs and candidate-order checks; no generative API."""
import argparse
import json
from pathlib import Path
import sys
import time

import numpy as np

ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'src'))
from decision_model.artifacts import sha256
from decision_model.data import read_jsonl, to_examples
from decision_model.learning import metrics
from decision_model.prediction import PredictionService
from decision_model.schemas import DecisionRequest, ChoiceQuestion

parser=argparse.ArgumentParser()
parser.add_argument('--model',type=Path,default=ROOT/'artifacts/model')
parser.add_argument('--input',type=Path,default=ROOT/'data/examples/tickets.jsonl')
args=parser.parse_args()
sys.stdout.reconfigure(encoding='utf-8')
import torch
torch.set_num_threads(2)
service=PredictionService.load(args.model)
records=read_jsonl(args.input)
scores, golds, permutations={}, {}, []
started=time.perf_counter()
for ticket in records:
    rows=to_examples(ticket)
    questions={row.question.type:row.question for row in rows}
    response=service.predict(DecisionRequest(state=ticket.text,questions=questions))
    for row in rows:
        answer=response.answers[row.question.type]
        p=[1-answer.noul,answer.noul] if answer.type=='noul' else list(answer.probabilities.values())
        key=f'{ticket.language}/{row.question.type}'
        scores.setdefault(key,[]).append(p)
        golds.setdefault(key,[]).append(row.gold)
    q=questions['choice']
    reversed_q=ChoiceQuestion(type='choice',instructions=q.instructions,criteria=dict(reversed(list(q.criteria.items()))))
    reversed_answer=service.predict(DecisionRequest(state=ticket.text,questions={'choice':reversed_q})).answers['choice']
    permutations.append({'language':ticket.language,'changed':reversed_answer.choice!=response.answers['choice'].choice,
                         'correct':reversed_answer.choice==ticket.department})
result={'model':service.model_version,'manifest_sha256':sha256(args.model/'manifest.json'),
    'reference_sha256':sha256(args.input),'reference_groups':len({t.group_id for t in records}),
    'reference_origin':'AI-authored, rubric-checked examples created before main training; not a human benchmark',
    'by_language_and_type':{key:metrics(np.array(value),np.array(golds[key]),ordinal=key.endswith('/score')) for key,value in scores.items()},
    'candidate_order':{lang:{'n':sum(p['language']==lang for p in permutations),
        'prediction_change_rate':float(np.mean([p['changed'] for p in permutations if p['language']==lang])),
        'reversed_order_accuracy':float(np.mean([p['correct'] for p in permutations if p['language']==lang]))} for lang in ['uk','en']},
    'elapsed_seconds':time.perf_counter()-started}
output=ROOT/'artifacts/challenges.json'
output.write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
print(json.dumps({'by_language_and_type':{key:{k:v[k] for k in ['n','accuracy','macro_f1','ece']} for key,v in result['by_language_and_type'].items()},
                  'candidate_order':result['candidate_order']},ensure_ascii=False,indent=2))