dariuscty's picture
Restore RSI-Anything inference resources and LabHorizon dataset
672ecc3 verified
Raw History Blame Contribute Delete
2.38 kB
import fcntl,hashlib,json,os,sys,time,uuid
if hasattr(sys,'set_int_max_str_digits'):sys.set_int_max_str_digits(0)
from pathlib import Path
ROOT=Path(os.environ.get('TRANSFER_ROOT',str(Path(__file__).resolve().parents[1])))
BASE=ROOT.parent
BASE_MODELS=Path(os.environ.get('RSI_BASE_MODELS_DIR', str(Path.home() / '.cache/rsi_anything/base_models'))).expanduser()
STATUS=['AC','WA','RE','TLE','CE']
def read(p):return json.loads(Path(p).read_text())
def lines(p):return [json.loads(x) for x in Path(p).open() if x.strip()]
def identity(x):return hashlib.sha256(json.dumps(x,sort_keys=True,ensure_ascii=False).encode()).hexdigest()
def seed(*parts):return int(identity(parts)[:8],16)%2147483647
def sha(p):
h=hashlib.sha256()
with Path(p).open('rb') as f:
for b in iter(lambda:f.read(2**20),b''):h.update(b)
return h.hexdigest()
def atomic(p,value):
p=Path(p);p.parent.mkdir(parents=True,exist_ok=True)
tmp=p.with_name(p.name+'.'+str(os.getpid())+'.'+uuid.uuid4().hex+'.tmp');tmp.write_text(json.dumps(value,ensure_ascii=False));tmp.replace(p)
def write_lines(p,rows):
p=Path(p);p.parent.mkdir(parents=True,exist_ok=True)
p.write_text(''.join(json.dumps(x,ensure_ascii=False)+'\n' for x in rows))
def cached(namespace,key,fn):
p=Path(os.environ.get('RSI_CODE_CACHE',str(Path.home()/'.cache/rsi_anything/code')))/namespace/(identity(key)+'.json');p.parent.mkdir(parents=True,exist_ok=True)
with p.with_suffix('.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX)
if p.exists():
item=read(p);assert item['key']==key;return item['value']
value=fn();atomic(p,{'key':key,'value':value});return value
def summary(feedback):
results=feedback['results']
return dict(all_pass=bool(results) and all(x['status']=='AC' for x in results),pass_fraction=sum(x['status']=='AC' for x in results)/max(1,len(results)))
def format_feedback(feedback):
return json.dumps([{k:r[k] for k in ('test_id','status','actual','stderr')} for r in feedback['results']],ensure_ascii=False)
def context(task,current,feedback,candidate):
return ('Whole-training-suite execution forecast. Only public observations are supplied.\n'
f"Problem:\n{task['problem']}\nCurrent program:\n{current}\n"
f"Trusted previous public execution:\n{format_feedback(feedback)}\nCandidate action:\n{candidate}\nOfficial public test suite:\n"
+json.dumps(task['public_tests'],ensure_ascii=False))