# Optional loopback-only teaching server. Never installed on the PHP hosting. import argparse,json,math from pathlib import Path from http.server import BaseHTTPRequestHandler,HTTPServer from worked_lab import predict_payload def main(): p=argparse.ArgumentParser();p.add_argument('--model',required=True);p.add_argument('--port',type=int,default=10080);args=p.parse_args() model=json.loads(Path(args.model).read_text()) if model.get('schema_version')!=1 or model.get('features')!=['distance_km','load_ratio'] or len(model.get('coefficients',[]))!=3: raise ValueError('Unsupported model schema') if not all(isinstance(x,(int,float)) and not isinstance(x,bool) and math.isfinite(x) for x in model['coefficients']): raise ValueError('Invalid coefficients') if model.get('bounds')!={'distance_km':[0,250],'load_ratio':[0,1.2]}: raise ValueError('Unsupported bounds') class Handler(BaseHTTPRequestHandler): def log_message(self,*args): pass # Do not log payloads. def reply(self,status,value): body=json.dumps(value,allow_nan=False).encode();self.send_response(status);self.send_header('Content-Type','application/json');self.send_header('Content-Length',str(len(body)));self.end_headers();self.wfile.write(body) def do_GET(self): self.reply(200,{'status':'learning-only','model_id':model['model_id']}) if self.path=='/health' else self.reply(404,{'error':'Not found'}) def do_POST(self): if self.path!='/predict':return self.reply(404,{'error':'Not found'}) try: size=int(self.headers.get('Content-Length','0')) if not 0