Spaces:
Sleeping
Sleeping
Update grader.py
Browse files12345673 unmodified lines1112131415161718192021222324252627282930313233343536373839401 unmodified line424344454647484950515253545556575859606162636465666768697071727374757677import numpy as npfrom sentence_transformers import SentenceTransformer, util# Load model once globally_model = Nonedef _get_model():3 unmodified linesreturn _modeldef grade_comment(comment: str, expected_keywords: list, expert_comment: str) -> float:"""Returns a score in [0,1] based on:- semantic similarity with expert comment (70%)- keyword coverage (30%)- length bonus/penalty"""if not comment:return 0.0# 1. Semantic similaritymodel = _get_model()emb_comment = model.encode(comment, convert_to_tensor=True)emb_expert = model.encode(expert_comment, convert_to_tensor=True)sim = util.pytorch_cos_sim(emb_comment, emb_expert).item() # in [0,1]# 2. Keyword coveragecomment_lower = comment.lower()matched = sum(1 for kw in expected_keywords if kw in comment_lower)kw_score = min(1.0, matched / max(1, len(expected_keywords) // 2))# 3. Combine (70% semantic, 30% keywords)combined = 0.7 * sim + 0.3 * kw_score# 4. Length bonus/penaltywords = comment.split()if len(words) >= 15:length_bonus = 0.11 unmodified linelength_bonus = -0.2else:length_bonus = 0.0# 5. Final score, clampedfinal = combined + length_bonusreturn max(0.0, min(1.0, final))def grade_question(question: str) -> float:"""Simple heuristic for question quality."""words = question.split()if len(words) < 3:return 0.0# Check for question wordsif any(q in question.lower() for q in ["what", "how", "why", "where", "when", "does", "is"]):return min(1.0, len(words) / 20)return 0.2def grade_fix(proposed_fix: str, expected_fix_keywords: list, hidden_test: callable) -> float:"""Evaluates a code fix. Hidden_test can be a function that runs unit tests."""# Keyword check (simplified)matched = sum(1 for kw in expected_fix_keywords if kw in proposed_fix.lower())kw_score = min(1.0, matched / max(1, len(expected_fix_keywords) // 2))# If we have a test function, run ittest_score = 0.0if hidden_test is not None:try:test_score = hidden_test(proposed_fix)except Exception:test_score = 0.0# Weighted: 60% test, 40% keywordsreturn 0.6 * test_score + 0.4 * kw_score1234567891011121314153 unmodified lines192021222324252627282930313233341 unmodified line363738394041424344454647484950515253545556575859606162import numpy as npfrom sentence_transformers import SentenceTransformer, utilEPS = 0.001def clamp_score(score):if score <= 0.0:return EPSif score >= 1.0:return 1.0 - EPSreturn score_model = Nonedef _get_model():3 unmodified linesreturn _modeldef grade_comment(comment: str, expected_keywords: list, expert_comment: str) -> float:if not comment:return clamp_score(0.0)model = _get_model()emb_comment = model.encode(comment, convert_to_tensor=True)emb_expert = model.encode(expert_comment, convert_to_tensor=True)sim = util.pytorch_cos_sim(emb_comment, emb_expert).item()comment_lower = comment.lower()matched = sum(1 for kw in expected_keywords if kw in comment_lower)kw_score = min(1.0, matched / max(1, len(expected_keywords) // 2))combined = 0.7 * sim + 0.3 * kw_scorewords = comment.split()if len(words) >= 15:length_bonus = 0.11 unmodified linelength_bonus = -0.2else:length_bonus = 0.0final = combined + length_bonus# Clamp to (0,1) using EPSreturn clamp_score(final)def grade_question(question: str) -> float:words = question.split()if len(words) < 3:return clamp_score(0.0)if any(q in question.lower() for q in ["what", "how", "why", "where", "when", "does", "is"]):score = min(1.0, len(words) / 20)return clamp_score(score)return clamp_score(0.2)def grade_fix(proposed_fix: str, expected_fix_keywords: list, hidden_test: callable) -> float:matched = sum(1 for kw in expected_fix_keywords if kw in proposed_fix.lower())kw_score = min(1.0, matched / max(1, len(expected_fix_keywords) // 2))test_score = 0.0if hidden_test is not None:try:test_score = hidden_test(proposed_fix)except Exception:test_score = 0.0score = 0.6 * test_score + 0.4 * kw_scorereturn clamp_score(score)
grader.py+19-34