Spaces:
Sleeping
Sleeping
Fix inference.py to always output valid scores for HF validation
Browse files- agent.py +32 -0
- inference.py +13 -6
- test_graders.py +53 -0
agent.py
CHANGED
|
@@ -234,6 +234,38 @@ def mock_review(code: str) -> list:
|
|
| 234 |
return bugs
|
| 235 |
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
# ---------------------------------------------------------------------------
|
| 238 |
# Main entry point
|
| 239 |
# ---------------------------------------------------------------------------
|
|
|
|
| 234 |
return bugs
|
| 235 |
|
| 236 |
|
| 237 |
+
def mock_review_action(code: str) -> dict:
|
| 238 |
+
"""
|
| 239 |
+
Rule-based fallback that returns a single action dict (not a list).
|
| 240 |
+
Used when LLM is unavailable - provides heuristic-based scoring.
|
| 241 |
+
Returns format compatible with inference.py graders:
|
| 242 |
+
{
|
| 243 |
+
"category": "logic" | "security" | "style" | "approve",
|
| 244 |
+
"severity": int 1-5,
|
| 245 |
+
"line_hint": int | None,
|
| 246 |
+
"comment": str
|
| 247 |
+
}
|
| 248 |
+
"""
|
| 249 |
+
bugs = mock_review(code)
|
| 250 |
+
if not bugs:
|
| 251 |
+
return {
|
| 252 |
+
"category": "approve",
|
| 253 |
+
"severity": 1,
|
| 254 |
+
"line_hint": None,
|
| 255 |
+
"comment": "no_issues_found",
|
| 256 |
+
}
|
| 257 |
+
bug = bugs[0]
|
| 258 |
+
cat = str(bug.get("category", "logic"))
|
| 259 |
+
if cat not in ("logic", "security", "style", "approve"):
|
| 260 |
+
cat = "logic"
|
| 261 |
+
return {
|
| 262 |
+
"category": cat,
|
| 263 |
+
"severity": int(bug.get("severity", 3)),
|
| 264 |
+
"line_hint": None,
|
| 265 |
+
"comment": bug.get("comment", "")[:100],
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
|
| 269 |
# ---------------------------------------------------------------------------
|
| 270 |
# Main entry point
|
| 271 |
# ---------------------------------------------------------------------------
|
inference.py
CHANGED
|
@@ -15,6 +15,7 @@ import re
|
|
| 15 |
import sys
|
| 16 |
|
| 17 |
from openai import OpenAI
|
|
|
|
| 18 |
|
| 19 |
# Manifest graders (must match openenv.yaml `grader:` entries exactly)
|
| 20 |
from tasks.easy.grader import grade as grade_task_easy_001
|
|
@@ -182,16 +183,19 @@ def run_inference() -> None:
|
|
| 182 |
llm_key = (
|
| 183 |
os.environ.get("HF_TOKEN") or os.environ.get("OPENAI_API_KEY") or ""
|
| 184 |
).strip()
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
print(
|
| 187 |
-
"[
|
| 188 |
flush=True,
|
| 189 |
)
|
| 190 |
-
sys.exit(1)
|
| 191 |
|
| 192 |
-
client =
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
# Ensure grader.py rubric returns (0,1); does not change [STEP] scores (those use manifest graders).
|
| 195 |
_san = root_rubric_grade(
|
| 196 |
{
|
| 197 |
"type": "approve",
|
|
@@ -210,7 +214,10 @@ def run_inference() -> None:
|
|
| 210 |
for task in TASKS:
|
| 211 |
tid = task["task_id"]
|
| 212 |
try:
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
| 214 |
except Exception as exc:
|
| 215 |
action = {
|
| 216 |
"category": "approve",
|
|
|
|
| 15 |
import sys
|
| 16 |
|
| 17 |
from openai import OpenAI
|
| 18 |
+
from agent import mock_review_action
|
| 19 |
|
| 20 |
# Manifest graders (must match openenv.yaml `grader:` entries exactly)
|
| 21 |
from tasks.easy.grader import grade as grade_task_easy_001
|
|
|
|
| 183 |
llm_key = (
|
| 184 |
os.environ.get("HF_TOKEN") or os.environ.get("OPENAI_API_KEY") or ""
|
| 185 |
).strip()
|
| 186 |
+
|
| 187 |
+
has_api_key = bool(llm_key)
|
| 188 |
+
|
| 189 |
+
if not has_api_key:
|
| 190 |
print(
|
| 191 |
+
"[WARNING] HF_TOKEN or OPENAI_API_KEY not set — using rule-based fallback for scoring.",
|
| 192 |
flush=True,
|
| 193 |
)
|
|
|
|
| 194 |
|
| 195 |
+
client = None
|
| 196 |
+
if has_api_key:
|
| 197 |
+
client = OpenAI(api_key=llm_key, base_url=API_BASE_URL)
|
| 198 |
|
|
|
|
| 199 |
_san = root_rubric_grade(
|
| 200 |
{
|
| 201 |
"type": "approve",
|
|
|
|
| 214 |
for task in TASKS:
|
| 215 |
tid = task["task_id"]
|
| 216 |
try:
|
| 217 |
+
if client is not None:
|
| 218 |
+
action = run_task(client, task)
|
| 219 |
+
else:
|
| 220 |
+
action = mock_review_action(task["code"])
|
| 221 |
except Exception as exc:
|
| 222 |
action = {
|
| 223 |
"category": "approve",
|
test_graders.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
|
| 3 |
+
sys.path.insert(0, "C:/Users/vinit/Projects/CodeBug")
|
| 4 |
+
|
| 5 |
+
from tasks.easy.grader import grade as grade_easy
|
| 6 |
+
from tasks.medium.grader import grade as grade_medium
|
| 7 |
+
from tasks.hard.grader import grade as grade_hard
|
| 8 |
+
from tasks.extra1.grader import grade as grade_extra1
|
| 9 |
+
from tasks.extra2.grader import grade as grade_extra2
|
| 10 |
+
|
| 11 |
+
# Test with correct actions
|
| 12 |
+
correct_easy = {"category": "logic", "severity": 4, "line_hint": 2}
|
| 13 |
+
correct_medium = {"category": "logic", "severity": 3, "line_hint": 5}
|
| 14 |
+
correct_hard = {"category": "security", "severity": 5, "line_hint": 2}
|
| 15 |
+
correct_extra1 = {"category": "security", "severity": 5, "line_hint": 1}
|
| 16 |
+
correct_extra2 = {"category": "logic", "severity": 3, "line_hint": 2}
|
| 17 |
+
|
| 18 |
+
# Test with wrong actions
|
| 19 |
+
wrong_action = {"category": "approve", "severity": 1, "line_hint": None}
|
| 20 |
+
|
| 21 |
+
print("Testing graders:")
|
| 22 |
+
scores = {
|
| 23 |
+
"easy": grade_easy(correct_easy),
|
| 24 |
+
"medium": grade_medium(correct_medium),
|
| 25 |
+
"hard": grade_hard(correct_hard),
|
| 26 |
+
"extra1": grade_extra1(correct_extra1),
|
| 27 |
+
"extra2": grade_extra2(correct_extra2),
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
for name, score in scores.items():
|
| 31 |
+
print(f" {name}: {score}")
|
| 32 |
+
|
| 33 |
+
print("\nWith wrong action:")
|
| 34 |
+
wrong_scores = {
|
| 35 |
+
"easy": grade_easy(wrong_action),
|
| 36 |
+
"medium": grade_medium(wrong_action),
|
| 37 |
+
"hard": grade_hard(wrong_action),
|
| 38 |
+
"extra1": grade_extra1(wrong_action),
|
| 39 |
+
"extra2": grade_extra2(wrong_action),
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
for name, score in wrong_scores.items():
|
| 43 |
+
print(f" {name}: {score}")
|
| 44 |
+
|
| 45 |
+
# Check for issues
|
| 46 |
+
print("\nIssues:")
|
| 47 |
+
for name, score in scores.items():
|
| 48 |
+
if score <= 0 or score >= 1:
|
| 49 |
+
print(f" {name}: OUT OF RANGE ({score})")
|
| 50 |
+
|
| 51 |
+
for name, score in wrong_scores.items():
|
| 52 |
+
if score <= 0 or score >= 1:
|
| 53 |
+
print(f" {name}: OUT OF RANGE ({score})")
|