laya-devops-mlx / predict.py
tebebe's picture
Laya DevOps v29 early stage preview: weights, method, paired audit and integration
aa73ff9 verified
Raw History Blame Contribute Delete
1.57 kB
"""Apply preprocessing and the frozen permission policy; never execute input."""
import argparse, json, os, sys
from pathlib import Path
ROOT=Path(__file__).resolve().parent
sys.path.insert(0,str(ROOT/'runtime'))
os.environ.setdefault('HF_HOME',str(ROOT/'.hf-cache'))
os.environ['HF_HUB_OFFLINE']='1'
def main():
parser=argparse.ArgumentParser();parser.add_argument('state',type=Path);args=parser.parse_args()
from local_model_api import validate_state
from input_preprocessing import load_preprocessor
from operating_policy import load_allow_threshold
from evaluate import decision
from layastudio import engine
import laya_mlx
state=validate_state(json.loads(args.state.read_text()))
prepare=load_preprocessor(json.loads((ROOT/'input-preprocessor.json').read_text()))
state=prepare(state);engine.limit_mlx_cache();agent=laya_mlx.load(str(ROOT));questions=json.loads((ROOT/'questions.json').read_text())
prefix,_=engine.build_prefix(agent.tok,engine.internal_question(questions['permission']),agent.cfg['head_max_len'])
room=agent.cfg['max_len']-len(prefix)-1
if len(agent.tok(engine.serialize_state(state).replace(agent.tok.mask_token,' '))['input_ids'])>room:raise ValueError('Input exceeds model context budget')
p=agent.predict(state,questions)['answers']['permission']['probabilities'];threshold=load_allow_threshold(ROOT)
print(json.dumps(dict(decision=decision(p,threshold),probabilities=p,allow_threshold=threshold,executed=False,benchmark_qualified=False),indent=2))
if __name__=='__main__':main()