Download code_repair/codecontests/src/ranking.py from Gen-Verse/RSI-Anything: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/codecontests/src/ranking.py
- Command line
-
hf download hf://datasets/Gen-Verse/RSI-Anything/code_repair/codecontests/src/ranking.py
-
curl -L -o ranking.py https://huggingface.co/datasets/Gen-Verse/RSI-Anything/resolve/main/code_repair/codecontests/src/ranking.py
2.04 kB
| import hashlib | |
| import math | |
| import json | |
| from dataclasses import asdict | |
| from public_context import feedback_text | |
| def choose(predictions): | |
| if len(predictions) != 5: | |
| raise ValueError('Expected four candidates and one current program') | |
| utility = [row['utility'] for row in predictions] | |
| if not all((math.isfinite(v) and 0 <= v <= 1.25 for v in utility)): | |
| raise ValueError('Invalid utility') | |
| best = max(range(4), key=lambda i: utility[i]) | |
| return best if utility[best] > utility[4] else 4 | |
| class Predictor: | |
| def __init__(self, checkpoint, backbone): | |
| from semantic_world import SemanticExecutionWorld | |
| self.model = SemanticExecutionWorld.load(checkpoint, backbone, device='cuda').eval() | |
| self.identity = self.model.identity | |
| def predict(self, problem, code, feedback, tests, candidates): | |
| public = [asdict(test) for test in tests if test.test_id.startswith('public_')] | |
| observed = asdict(feedback) | |
| observed['results'] = [row for row in observed['results'] if row['test_id'].startswith('public_')] | |
| contexts = [f'Whole-training-suite execution forecast. Only public observations are supplied.\nProblem:\n{problem}\nCurrent program:\n{code}\nTrusted previous public execution:\n{feedback_text(observed)}\nCandidate action:\n{candidate.code}\nOfficial public test suite:\n' + json.dumps(public, ensure_ascii=False) for candidate in candidates] | |
| values = self.model.score_suite(contexts) | |
| return [dict(candidate_id=c.candidate_id, utility=float(values['utility'][i]), full_suite_pass_probability=float(values['full_suite_pass_probability'][i]), predicted_pass_fraction=float(values['predicted_pass_fraction'][i])) for (i, c) in enumerate(candidates)] | |
| def state_key(code, feedback, step): | |
| outcomes = [{k: getattr(row, k) for k in ('test_id', 'status', 'actual', 'expected', 'stderr')} for row in feedback.results] | |
| return hashlib.sha256(json.dumps(dict(code=code, feedback=outcomes, step=step), sort_keys=True).encode()).hexdigest() | |