CoolFace
Apppublic

NILESH1003/codeguardian-env

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
grader.py104 linesDownload Raw Back to server
1from typing import List, Dict, Any2from models import CodeAction, RewardDetail3 4MIN_STRICT_VALUE = 0.0015MAX_STRICT_VALUE = 0.9996LOW_REWARD = 0.17PARTIAL_REWARD = 0.38MEDIUM_REWARD = 0.59HIGH_REWARD = 0.910 11 12def clamp_strict(value: float) -> float:13    return max(MIN_STRICT_VALUE, min(MAX_STRICT_VALUE, float(value)))14 15 16def compute_step_reward(action: CodeAction, task: Dict[str, Any], actions_taken: List[CodeAction] = None) -> RewardDetail:17    actions_taken = actions_taken or []18    19    if action.action in ["flag_bug", "suggest_fix"]:20        if action.line is None:21            return RewardDetail(22                step_reward=LOW_REWARD,23                reason="Action missing line number",24                partial=True,25            )26            27        matched_bug = None28        for bug in task["bugs"]:29            if abs(action.line - bug["line"]) <= 2:30                matched_bug = bug31                break32                33        if matched_bug:34            if action.action == "flag_bug" and action.bug_type == matched_bug["bug_type"]:35                return RewardDetail(36                    step_reward=HIGH_REWARD,37                    reason="Correctly flagged a real bug.",38                    partial=True,39                )40            elif action.action == "suggest_fix":41                return RewardDetail(42                    step_reward=MEDIUM_REWARD,43                    reason="Good fix suggestion.",44                    partial=True,45                )46            else:47                return RewardDetail(48                    step_reward=PARTIAL_REWARD,49                    reason="Flagged bug on correct line but wrong type.",50                    partial=True,51                )52        else:53            return RewardDetail(54                step_reward=LOW_REWARD,55                reason="Wrong action: flagged bug on clean line.",56                partial=True,57            )58            59    elif action.action in ["approve", "reject"]:60        found_bugs = set()61        for past_action in actions_taken:62            if past_action.action in ["flag_bug", "suggest_fix"] and past_action.line is not None:63                for idx, bug in enumerate(task["bugs"]):64                    if abs(past_action.line - bug["line"]) <= 2:65                        found_bugs.add(idx)66                        67        if len(found_bugs) < len(task["bugs"]):68            return RewardDetail(69                step_reward=LOW_REWARD,70                reason=f"{action.action} when critical bugs remain undetected.",71                partial=True,72            )73        else:74            return RewardDetail(75                step_reward=MEDIUM_REWARD,76                reason=f"{action.action} when all bugs found (correct!).",77                partial=True,78            )79            80    return RewardDetail(step_reward=LOW_REWARD, reason="Unknown action.", partial=True)81 82import numpy as np83 84def evaluate_score(task: Dict[str, Any], actions_taken: List[CodeAction]) -> float:85    bugs = task.get("bugs", [])86    if not bugs:87        score = MAX_STRICT_VALUE88    else:89        found_bugs = set()90        for action in actions_taken:91            if action.action in ["flag_bug", "suggest_fix"] and action.line is not None:92                for idx, bug in enumerate(bugs):93                    if abs(action.line - bug["line"]) <= 2:94                        if action.bug_type == bug["bug_type"] or action.action == "suggest_fix":95                            found_bugs.add(idx)96                            97        score = len(found_bugs) / len(bugs)98        99    if score == 0.0 or score == 1.0:100        print(f"WARNING: Task score is exactly {score}, which is out of range. Clipping to strictly between 0 and 1.")101        102    clipped_score = float(np.clip(score, MIN_STRICT_VALUE, MAX_STRICT_VALUE))103    return clamp_strict(clipped_score)104