Datasets:
Create validate_preview.py
Browse files- validate_preview.py +39 -0
validate_preview.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Run reviewed reference checks only inside a disposable CPU sandbox."""
|
| 2 |
+
import json, os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from cwsandbox import AuthStrategy, Sandbox
|
| 5 |
+
ROOT=Path(__file__).resolve().parent
|
| 6 |
+
os.environ['WANDB_API_KEY']=(Path.home()/'.config/forge-experiments/api-key').read_text().strip()
|
| 7 |
+
worker='''import json, subprocess, sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
rows=[json.loads(l) for l in Path('/work/reviewed-preview.jsonl').read_text().splitlines()]
|
| 10 |
+
tests=json.loads(Path('/work/review-tests.json').read_text())
|
| 11 |
+
results=[]
|
| 12 |
+
for row in rows:
|
| 13 |
+
check=tests[str(row['source_id'])]
|
| 14 |
+
source=row['prompt']+row['completion']+'\\n'+check['checks']
|
| 15 |
+
try:
|
| 16 |
+
r=subprocess.run([sys.executable,'-I','-S','-c',source],capture_output=True,text=True,timeout=3)
|
| 17 |
+
result=dict(source_id=row['source_id'],passed=r.returncode==0,checks=check['checks'].count('assert '),review_scope=check['review_scope'],stderr=r.stderr[:1000])
|
| 18 |
+
except subprocess.TimeoutExpired:
|
| 19 |
+
result=dict(source_id=row['source_id'],passed=False,error='timeout')
|
| 20 |
+
results.append(result)
|
| 21 |
+
Path('/work/validation.json').write_text(json.dumps(results,indent=2))
|
| 22 |
+
print(json.dumps(results,indent=2))
|
| 23 |
+
'''
|
| 24 |
+
with Sandbox.run(auth=AuthStrategy.WANDB,placement_mode='serverless',container_image='python:3.11-slim',resources={'cpu':'1','memory':'1Gi'},max_lifetime_seconds=180) as s:
|
| 25 |
+
s.exec(['mkdir','-p','/work'],check=True).result()
|
| 26 |
+
for name in ['reviewed-preview.jsonl','review-tests.json']: s.write_file('/work/'+name,(ROOT/name).read_bytes()).result()
|
| 27 |
+
s.write_file('/work/validate.py',worker.encode()).result()
|
| 28 |
+
r=s.exec(['python','/work/validate.py'],timeout_seconds=30).result()
|
| 29 |
+
print(r.stdout); print(r.stderr)
|
| 30 |
+
(ROOT/'validation.json').write_bytes(s.read_file('/work/validation.json').result())
|
| 31 |
+
print('CPU sandbox stopped; no GPU used')
|
| 32 |
+
# Publish only examples whose reviewed contracts are sufficiently clear.
|
| 33 |
+
verdicts={r['source_id']:r for r in json.loads((ROOT/'validation.json').read_text())}
|
| 34 |
+
rows=[json.loads(line) for line in (ROOT/'reviewed-preview.jsonl').read_text().splitlines()]
|
| 35 |
+
for row in rows:
|
| 36 |
+
row['validation']=verdicts[row['source_id']]
|
| 37 |
+
row['validation_status']=('needs_contract_review' if row['source_id']==533341 else 'reviewed_targeted_checks_passed') if row['validation']['passed'] else 'checks_failed'
|
| 38 |
+
(ROOT/'reviewed-preview.jsonl').write_text(''.join(json.dumps(r)+'\n' for r in rows))
|
| 39 |
+
(ROOT/'approved-preview.jsonl').write_text(''.join(json.dumps(r)+'\n' for r in rows if r['validation_status']=='reviewed_targeted_checks_passed'))
|