Download astra/ClothWipe-v1/worker_api.py from Linslab/ManiLadder_Models: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/Linslab/ManiLadder_Models/resolve/main/astra/ClothWipe-v1/worker_api.py
- Command line
-
hf download hf://Linslab/ManiLadder_Models/astra/ClothWipe-v1/worker_api.py
-
curl -L -o worker_api.py https://huggingface.co/Linslab/ManiLadder_Models/resolve/main/astra/ClothWipe-v1/worker_api.py
4.08 kB
| """Standalone worker client; safe to mount without the runner package.""" | |
| import base64 | |
| import json | |
| import socket | |
| import struct | |
| import uuid | |
| import os | |
| from pathlib import Path | |
| import numpy as np | |
| def array_hook(value): | |
| if value.get('__array__') is True: | |
| dtype=np.dtype(value['dtype']) | |
| if dtype.kind not in 'bifu':raise ValueError('non-numeric array from service') | |
| raw=base64.b64decode(value['data'],validate=True) | |
| return np.frombuffer(raw,dtype=dtype).reshape(value['shape']).copy() | |
| return value | |
| class API: | |
| def __init__(self,socket_path='/rpc/control.sock',demo_root='/demonstrations',manifest='/demo_manifest.json'): | |
| self.socket_path=socket_path | |
| self.demo_root=Path(demo_root) | |
| self.manifest=Path(manifest) | |
| def call(self,method,arguments=None,request_id=None): | |
| # Caller can persist a request ID before sending. Never automatically retry. | |
| request_id=request_id or str(uuid.uuid4()) | |
| request={'request_id':request_id,'method':method,'arguments':arguments or {}} | |
| if os.environ.get('AGENT_E20_RPC_TOKEN'): | |
| request['token']=os.environ['AGENT_E20_RPC_TOKEN'] | |
| payload=json.dumps(request,allow_nan=False).encode() | |
| if len(payload)>65536:raise ValueError('request too large') | |
| with socket.socket(socket.AF_UNIX,socket.SOCK_STREAM) as s: | |
| s.settimeout(1810);s.connect(self.socket_path) | |
| s.sendall(struct.pack('!I',len(payload))+payload) | |
| def exact(n): | |
| chunks=[] | |
| while n: | |
| chunk=s.recv(min(n,1024*1024)) | |
| if not chunk:raise RuntimeError('RPC interrupted; do not replay action with a new ID') | |
| chunks.append(chunk);n-=len(chunk) | |
| return b''.join(chunks) | |
| length=struct.unpack('!I',exact(4))[0] | |
| if length>64*1024*1024:raise RuntimeError('response too large') | |
| result=json.loads(exact(length),object_hook=array_hook) | |
| if not result['ok']:raise RuntimeError(result['error']) | |
| return result['result'] | |
| def next_episode(self,request_id=None):return self.call('next_episode',request_id=request_id) | |
| def latest_observation(self):return self.call('latest_observation') | |
| def execute_actions(self,actions,request_id=None): | |
| return self.call('execute_actions',{'actions':np.asarray(actions).tolist()},request_id) | |
| def finish_episode(self):return self.call('finish_episode') | |
| def budget_status(self):return self.call('budget_status') | |
| def rehearse(self,candidate='candidate',request_id=None): | |
| return self.call('rehearse',{'candidate':candidate},request_id) | |
| def score_rehearsal(self,episode,visible_success,evidence): | |
| return self.call('score_rehearsal',{'episode':episode,'visible_success':visible_success,'evidence':evidence}) | |
| def freeze(self):return self.call('freeze') | |
| def read_demo(self,demo_id,start=0,stop=None,fields=None): | |
| import h5py | |
| manifest=json.loads(self.manifest.read_text()) | |
| if manifest.get('status') not in ('released','released_for_offline_analysis'): | |
| raise RuntimeError('demonstrations not released by trusted preflight') | |
| entries={e['demo_id']:e for e in manifest['episodes']} | |
| if demo_id not in entries:raise ValueError('unauthorized demo ID') | |
| entry=entries[demo_id] | |
| allowed={f['path'] for f in entry['fields']} | |
| fields=sorted(allowed) if fields is None else fields | |
| if not set(fields)<=allowed:raise ValueError('unauthorized demo field') | |
| stop=entry['length']+1 if stop is None else stop | |
| if not isinstance(start,int) or not isinstance(stop,int) or not 0<=start<stop<=entry['length']+1: | |
| raise ValueError('invalid frame interval') | |
| path=(self.demo_root/entry['file']).resolve() | |
| if path.parent!=self.demo_root.resolve():raise ValueError('invalid demo file') | |
| with h5py.File(path,'r') as f: | |
| return {field:f[field][start:min(stop,len(f[field]))] for field in fields} | |