Datasets:
Download runner/agentgridworld/preview.py from bytkim/AgentGridWorld-Hard: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/datasets/bytkim/AgentGridWorld-Hard/resolve/main/runner/agentgridworld/preview.py
- Command line
-
hf download hf://datasets/bytkim/AgentGridWorld-Hard/runner/agentgridworld/preview.py
-
curl -L -o preview.py https://huggingface.co/datasets/bytkim/AgentGridWorld-Hard/resolve/main/runner/agentgridworld/preview.py
2.87 kB
| """Render native solver demonstrations; never substitutes for model evaluation.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from . import authored | |
| from .gridworld import Episode, verify | |
| from .pool import verify_pool, sha | |
| ROOT=Path(__file__).resolve().parent | |
| def build_preview(pool, output): | |
| pools=[Path(p).resolve() for p in (pool if isinstance(pool,(list,tuple)) else [pool])];output=Path(output).resolve() | |
| if output.exists():raise FileExistsError('Use a new preview directory') | |
| members=[(p,row) for p in pools for row in verify_pool(p)['tasks']];cases=[] | |
| if not members or len({row['id'] for _,row in members})!=len(members):raise ValueError('Preview requires unique task membership') | |
| for source,row in members: | |
| task=source/row['id'];config=json.loads((task/'environment/task-config.json').read_text()) | |
| d=authored.design(config['environment']) | |
| if not d:raise ValueError('This level-design preview requires authored maps') | |
| proof=json.loads((task/'qualification.json').read_text()) | |
| witness=json.loads((task/'witness.json').read_text()) | |
| episode=Episode(config) | |
| frames=[{'observation':episode.observation(),'metrics':episode.all_metrics(),'action':None,'events':[]}] | |
| for action in witness['actions']: | |
| observation=episode.step(action) | |
| frames.append({'observation':observation,'metrics':episode.all_metrics(), | |
| 'action':action,'events':episode.events[-1]['events']}) | |
| outcome=verify(config,episode.receipt()) | |
| if outcome!=witness['outcome']:raise ValueError('Preview differs from packaged demonstration') | |
| cases.append({'config':config,'design':d,'proof':proof,'frames':frames,'outcome':outcome, | |
| 'difficulty':'medium' if authored.map_path(config['environment']).parent.parent.name=='medium' else 'easy', | |
| 'task_manifest_sha256':row['manifest_sha256'], | |
| 'trace_sha256':hashlib.sha256(json.dumps(episode.receipt(),sort_keys=True).encode()).hexdigest()}) | |
| data={'schema':'agentgridworld.level-preview.v2','origin':'native_solver_demonstrations', | |
| 'pools':[{'name':p.name,'manifest_sha256':sha(p/'pool.json')} for p in pools],'cases':cases} | |
| output.mkdir(parents=True) | |
| (output/'data.json').write_text(json.dumps(data,indent=2)+'\n') | |
| embedded=json.dumps(data).replace('<','\\u003c') | |
| (output/'index.html').write_text((ROOT/'preview.html').read_text().replace('__DATA__',embedded).replace('__SCRIPT__',(ROOT/'preview.js').read_text())) | |
| return output | |
| def main(): | |
| p=argparse.ArgumentParser();p.add_argument('--pool',type=Path,required=True,action='append');p.add_argument('--output',type=Path,required=True) | |
| a=p.parse_args();print(build_preview(a.pool,a.output)) | |
| if __name__=='__main__':main() | |