Spaces:
Sleeping
Sleeping
modified: inference.py
Browse files- debug_env/server/grader.py +22 -17
- inference.py +4 -1
debug_env/server/grader.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def grade(pass_rate: float) -> float:
|
| 2 |
"""Baseline reward — direct pass rate (used as fallback)."""
|
| 3 |
return float(pass_rate)
|
|
@@ -5,24 +8,26 @@ def grade(pass_rate: float) -> float:
|
|
| 5 |
|
| 6 |
def grade_by_comparison(submitted: str, reference: str) -> float:
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def grade_with_steps(pass_rate: float, step_count: int, max_steps: int = 40) -> float:
|
|
|
|
| 1 |
+
import ast
|
| 2 |
+
import difflib
|
| 3 |
+
|
| 4 |
def grade(pass_rate: float) -> float:
|
| 5 |
"""Baseline reward — direct pass rate (used as fallback)."""
|
| 6 |
return float(pass_rate)
|
|
|
|
| 8 |
|
| 9 |
def grade_by_comparison(submitted: str, reference: str) -> float:
|
| 10 |
"""
|
| 11 |
+
Grades submitted code against reference code, prioritizing semantic exactness.
|
| 12 |
+
- First attempts AST parsing: if both codes produce identical ASTs, returns 1.0.
|
| 13 |
+
- If AST parsing fails or differs, falls back to difflib SequenceMatcher on sanitized lines.
|
| 14 |
"""
|
| 15 |
+
try:
|
| 16 |
+
sub_ast = ast.unparse(ast.parse(submitted))
|
| 17 |
+
ref_ast = ast.unparse(ast.parse(reference))
|
| 18 |
+
if sub_ast == ref_ast:
|
| 19 |
+
return 1.0
|
| 20 |
+
except Exception:
|
| 21 |
+
pass # Fall back to token comparison if syntax is invalid
|
| 22 |
+
|
| 23 |
+
sub_lines = [line.strip() for line in submitted.splitlines() if line.strip()]
|
| 24 |
+
ref_lines = [line.strip() for line in reference.splitlines() if line.strip()]
|
| 25 |
+
|
| 26 |
+
if not ref_lines:
|
| 27 |
+
return 0.0 if sub_lines else 1.0
|
| 28 |
+
|
| 29 |
+
matcher = difflib.SequenceMatcher(None, sub_lines, ref_lines)
|
| 30 |
+
return round(matcher.ratio(), 4)
|
| 31 |
|
| 32 |
|
| 33 |
def grade_with_steps(pass_rate: float, step_count: int, max_steps: int = 40) -> float:
|
inference.py
CHANGED
|
@@ -316,7 +316,10 @@ async def execute_run(
|
|
| 316 |
await env_step(env_url, "edit_file", {"path": fname, "content": original}, delay_ms=0)
|
| 317 |
|
| 318 |
elapsed_ms = int((datetime.now(timezone.utc) - start_ts).total_seconds() * 1000)
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 321 |
|
| 322 |
print(
|
|
|
|
| 316 |
await env_step(env_url, "edit_file", {"path": fname, "content": original}, delay_ms=0)
|
| 317 |
|
| 318 |
elapsed_ms = int((datetime.now(timezone.utc) - start_ts).total_seconds() * 1000)
|
| 319 |
+
|
| 320 |
+
eval_rewards = [s["reward"] for s in steps_detail if s["tool"] in ("edit_file", "run_tests")]
|
| 321 |
+
final_reward = sum(eval_rewards) / len(eval_rewards) if eval_rewards else 0.0
|
| 322 |
+
|
| 323 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 324 |
|
| 325 |
print(
|