Spaces:
Sleeping
Sleeping
Commit ·
147b2e1
1
Parent(s): a8a9661
debugged the script
Browse files- app/__pycache__/grader.cpython-313.pyc +0 -0
- app/grader.py +29 -8
- app/tasks.py +3 -0
- inference.py +1 -1
- validate_submission.py +28 -2
app/__pycache__/grader.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/grader.cpython-313.pyc and b/app/__pycache__/grader.cpython-313.pyc differ
|
|
|
app/grader.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import re
|
| 2 |
|
| 3 |
from app.models import GradeResult, TaskDefinition
|
|
@@ -16,6 +17,14 @@ _MIN_SCORE = 0.01
|
|
| 16 |
_MAX_SCORE = 0.99
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def _normalize(text: str) -> str:
|
| 20 |
return " ".join(re.findall(r"[a-z0-9_+.#-]+", text.lower()))
|
| 21 |
|
|
@@ -26,6 +35,15 @@ def _contains_phrase(text: str, phrase: str) -> bool:
|
|
| 26 |
|
| 27 |
def grade_review(task: TaskDefinition, review: str) -> GradeResult:
|
| 28 |
normalized_review = _normalize(review)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
matched_criteria: list[str] = []
|
| 31 |
missed_criteria: list[str] = []
|
|
@@ -38,7 +56,7 @@ def grade_review(task: TaskDefinition, review: str) -> GradeResult:
|
|
| 38 |
else:
|
| 39 |
missed_criteria.append(canonical)
|
| 40 |
|
| 41 |
-
coverage_score = len(matched_criteria) /
|
| 42 |
|
| 43 |
penalties: dict[str, float] = {}
|
| 44 |
|
|
@@ -60,15 +78,12 @@ def grade_review(task: TaskDefinition, review: str) -> GradeResult:
|
|
| 60 |
|
| 61 |
total_penalty = sum(penalties.values())
|
| 62 |
raw_score = coverage_score - total_penalty
|
| 63 |
-
|
| 64 |
-
final_score = max(_MIN_SCORE, min(_MAX_SCORE, raw_score))
|
| 65 |
-
rounded = round(final_score, 4)
|
| 66 |
-
# Guard float edge cases so reward never equals exactly 0.0 or 1.0 after rounding.
|
| 67 |
-
score = max(_MIN_SCORE, min(_MAX_SCORE, rounded))
|
| 68 |
|
|
|
|
| 69 |
rationale = (
|
| 70 |
-
f"Coverage={
|
| 71 |
-
f"Matched={len(matched_criteria)}/{
|
| 72 |
)
|
| 73 |
|
| 74 |
return GradeResult(
|
|
@@ -78,3 +93,9 @@ def grade_review(task: TaskDefinition, review: str) -> GradeResult:
|
|
| 78 |
penalties=penalties,
|
| 79 |
rationale=rationale,
|
| 80 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
import re
|
| 3 |
|
| 4 |
from app.models import GradeResult, TaskDefinition
|
|
|
|
| 17 |
_MAX_SCORE = 0.99
|
| 18 |
|
| 19 |
|
| 20 |
+
def normalize_score(score: float) -> float:
|
| 21 |
+
"""Clamp reward to strict open interval (0, 1) for hub / Phase-2 validators."""
|
| 22 |
+
x = float(score)
|
| 23 |
+
if not math.isfinite(x):
|
| 24 |
+
return _MIN_SCORE
|
| 25 |
+
return max(_MIN_SCORE, min(_MAX_SCORE, x))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
def _normalize(text: str) -> str:
|
| 29 |
return " ".join(re.findall(r"[a-z0-9_+.#-]+", text.lower()))
|
| 30 |
|
|
|
|
| 35 |
|
| 36 |
def grade_review(task: TaskDefinition, review: str) -> GradeResult:
|
| 37 |
normalized_review = _normalize(review)
|
| 38 |
+
n_groups = len(task.criteria)
|
| 39 |
+
if n_groups == 0:
|
| 40 |
+
return GradeResult(
|
| 41 |
+
score=normalize_score(0.5),
|
| 42 |
+
matched_criteria=[],
|
| 43 |
+
missed_criteria=[],
|
| 44 |
+
penalties={"no_criteria": 0.5},
|
| 45 |
+
rationale="No grading criteria configured; neutral partial score.",
|
| 46 |
+
)
|
| 47 |
|
| 48 |
matched_criteria: list[str] = []
|
| 49 |
missed_criteria: list[str] = []
|
|
|
|
| 56 |
else:
|
| 57 |
missed_criteria.append(canonical)
|
| 58 |
|
| 59 |
+
coverage_score = len(matched_criteria) / n_groups
|
| 60 |
|
| 61 |
penalties: dict[str, float] = {}
|
| 62 |
|
|
|
|
| 78 |
|
| 79 |
total_penalty = sum(penalties.values())
|
| 80 |
raw_score = coverage_score - total_penalty
|
| 81 |
+
score = normalize_score(round(raw_score, 4))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
cov_display = min(coverage_score, _MAX_SCORE)
|
| 84 |
rationale = (
|
| 85 |
+
f"Coverage={cov_display:.2f}; Penalty={total_penalty:.2f}; "
|
| 86 |
+
f"Matched={len(matched_criteria)}/{n_groups}"
|
| 87 |
)
|
| 88 |
|
| 89 |
return GradeResult(
|
|
|
|
| 93 |
penalties=penalties,
|
| 94 |
rationale=rationale,
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def grader(output: str, expected: TaskDefinition) -> float:
|
| 99 |
+
"""OpenEnv-style entry: ``output`` is the review; ``expected`` is the task rubric."""
|
| 100 |
+
result = grade_review(expected, output)
|
| 101 |
+
return normalize_score(result.score)
|
app/tasks.py
CHANGED
|
@@ -79,6 +79,9 @@ TASKS: dict[str, TaskDefinition] = {
|
|
| 79 |
|
| 80 |
ORDERED_TASK_IDS: list[str] = ["easy", "medium", "hard"]
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def get_task(task_id: str) -> TaskDefinition:
|
| 84 |
if task_id not in TASKS:
|
|
|
|
| 79 |
|
| 80 |
ORDERED_TASK_IDS: list[str] = ["easy", "medium", "hard"]
|
| 81 |
|
| 82 |
+
# Explicit curriculum ordering for multi-task episodes and static validators.
|
| 83 |
+
ORDERED_TASK_DEFINITIONS: list[TaskDefinition] = [TASKS[tid] for tid in ORDERED_TASK_IDS]
|
| 84 |
+
|
| 85 |
|
| 86 |
def get_task(task_id: str) -> TaskDefinition:
|
| 87 |
if task_id not in TASKS:
|
inference.py
CHANGED
|
@@ -94,7 +94,7 @@ def _dry_run_review(task_id: str) -> str:
|
|
| 94 |
)
|
| 95 |
return (
|
| 96 |
"Code review: handle empty list to prevent division by zero, avoid broad except Exception, "
|
| 97 |
-
"iterate directly over values, add type hints and tests. Overall score: 0.86
|
| 98 |
)
|
| 99 |
|
| 100 |
|
|
|
|
| 94 |
)
|
| 95 |
return (
|
| 96 |
"Code review: handle empty list to prevent division by zero, avoid broad except Exception, "
|
| 97 |
+
"iterate directly over values, add type hints and tests. Overall score: 0.86 (on a 0–1 scale)."
|
| 98 |
)
|
| 99 |
|
| 100 |
|
validate_submission.py
CHANGED
|
@@ -8,7 +8,9 @@ from pathlib import Path
|
|
| 8 |
|
| 9 |
from fastapi.testclient import TestClient
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
from app.main import app
|
| 13 |
from app.tasks import TASKS
|
| 14 |
|
|
@@ -187,7 +189,7 @@ def _check_tasks_and_graders() -> None:
|
|
| 187 |
"medium": "Use a list comprehension while preserving behavior for readability and concise code.",
|
| 188 |
"hard": (
|
| 189 |
"Handle empty list to avoid division by zero, avoid broad except Exception, "
|
| 190 |
-
"iterate directly, add type hints/tests, overall score: 0.85
|
| 191 |
),
|
| 192 |
}
|
| 193 |
|
|
@@ -197,6 +199,29 @@ def _check_tasks_and_graders() -> None:
|
|
| 197 |
assert 0.0 < result.score < 1.0, f"Score out of strict range for {task_id}: {result.score}"
|
| 198 |
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
def _check_inference_script(path: Path) -> None:
|
| 201 |
assert path.exists(), "Root inference.py is required"
|
| 202 |
content = path.read_text(encoding="utf-8")
|
|
@@ -261,6 +286,7 @@ def main() -> None:
|
|
| 261 |
_check_tasks_endpoint()
|
| 262 |
_check_api_contracts()
|
| 263 |
_check_tasks_and_graders()
|
|
|
|
| 264 |
_check_inference_script(root / "inference.py")
|
| 265 |
_run_inference_dry_run(root)
|
| 266 |
|
|
|
|
| 8 |
|
| 9 |
from fastapi.testclient import TestClient
|
| 10 |
|
| 11 |
+
import math
|
| 12 |
+
|
| 13 |
+
from app.grader import grade_review, grader, normalize_score
|
| 14 |
from app.main import app
|
| 15 |
from app.tasks import TASKS
|
| 16 |
|
|
|
|
| 189 |
"medium": "Use a list comprehension while preserving behavior for readability and concise code.",
|
| 190 |
"hard": (
|
| 191 |
"Handle empty list to avoid division by zero, avoid broad except Exception, "
|
| 192 |
+
"iterate directly, add type hints/tests, overall score: 0.85 (on a 0–1 scale)."
|
| 193 |
),
|
| 194 |
}
|
| 195 |
|
|
|
|
| 199 |
assert 0.0 < result.score < 1.0, f"Score out of strict range for {task_id}: {result.score}"
|
| 200 |
|
| 201 |
|
| 202 |
+
def _check_grader_edge_cases() -> None:
|
| 203 |
+
"""Stress grader outputs: no NaN/inf, scores never touch 0.0 or 1.0, grader() matches rubric."""
|
| 204 |
+
assert normalize_score(float("nan")) == 0.01
|
| 205 |
+
assert normalize_score(float("inf")) == 0.01
|
| 206 |
+
assert normalize_score(0.0) == 0.01
|
| 207 |
+
assert normalize_score(1.0) == 0.99
|
| 208 |
+
assert normalize_score(1.5) == 0.99
|
| 209 |
+
|
| 210 |
+
junk = (
|
| 211 |
+
"weather recipe movie travel football politics unrelated filler text "
|
| 212 |
+
"with enough tokens to avoid the too_short penalty entirely here"
|
| 213 |
+
)
|
| 214 |
+
emptyish = "a " * 30
|
| 215 |
+
|
| 216 |
+
for _task_id, task in TASKS.items():
|
| 217 |
+
for review in (junk, emptyish, "syntax error " * 20):
|
| 218 |
+
result = grade_review(task, review)
|
| 219 |
+
s = float(result.score)
|
| 220 |
+
assert math.isfinite(s), f"non-finite score for {task.id}: {s}"
|
| 221 |
+
assert 0.0 < s < 1.0, f"score out of range for {task.id}: {s}"
|
| 222 |
+
assert grader(review, task) == s
|
| 223 |
+
|
| 224 |
+
|
| 225 |
def _check_inference_script(path: Path) -> None:
|
| 226 |
assert path.exists(), "Root inference.py is required"
|
| 227 |
content = path.read_text(encoding="utf-8")
|
|
|
|
| 286 |
_check_tasks_endpoint()
|
| 287 |
_check_api_contracts()
|
| 288 |
_check_tasks_and_graders()
|
| 289 |
+
_check_grader_edge_cases()
|
| 290 |
_check_inference_script(root / "inference.py")
|
| 291 |
_run_inference_dry_run(root)
|
| 292 |
|