Spaces:
Running on Zero
Running on Zero
Sync from GitHub via hub-sync
Browse files- learning_engine.py +10 -0
- test_grade_answer.py +13 -0
learning_engine.py
CHANGED
|
@@ -56,6 +56,16 @@ def next_card(session: Session) -> Card | None:
|
|
| 56 |
# ---- Grading ---------------------------------------------------------------
|
| 57 |
|
| 58 |
def grade_answer(card: Card, user_answer: str) -> GradeResult:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if llm.STUB:
|
| 60 |
# Trivial heuristic so the stub demo "feels" responsive.
|
| 61 |
ans = (user_answer or "").strip().lower()
|
|
|
|
| 56 |
# ---- Grading ---------------------------------------------------------------
|
| 57 |
|
| 58 |
def grade_answer(card: Card, user_answer: str) -> GradeResult:
|
| 59 |
+
# An empty answer is unambiguously a miss — short-circuit before any model
|
| 60 |
+
# call. (The grader otherwise ignores the blank input and "grades" the
|
| 61 |
+
# reference answer itself, hallucinating a 4/5 "correct".) Also saves a call.
|
| 62 |
+
if not (user_answer or "").strip():
|
| 63 |
+
return new_grade(
|
| 64 |
+
0,
|
| 65 |
+
f"No answer given. The reference answer is: {card['answer']}",
|
| 66 |
+
card["topic"],
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
if llm.STUB:
|
| 70 |
# Trivial heuristic so the stub demo "feels" responsive.
|
| 71 |
ans = (user_answer or "").strip().lower()
|
test_grade_answer.py
CHANGED
|
@@ -88,9 +88,22 @@ def test_out_of_range_score_rejected():
|
|
| 88 |
print("ok out-of-range score rejected -> safe default")
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
test_clean_json_first_try()
|
| 93 |
test_repair_retry_recovers()
|
| 94 |
test_safe_default_when_never_valid()
|
| 95 |
test_out_of_range_score_rejected()
|
|
|
|
| 96 |
print("\nAll NAH-8 grade_answer tests passed.")
|
|
|
|
| 88 |
print("ok out-of-range score rejected -> safe default")
|
| 89 |
|
| 90 |
|
| 91 |
+
def test_empty_answer_short_circuits_to_zero():
|
| 92 |
+
# An empty answer is a miss — score 0 with no model call (the model otherwise
|
| 93 |
+
# ignores the blank input and hallucinates a 4/5 "correct").
|
| 94 |
+
llm.chat, calls = _fake_chat(['{"score": 5, "explanation": "x"}'])
|
| 95 |
+
for blank in ("", " ", "\n\t"):
|
| 96 |
+
g = le.grade_answer(_card(), blank)
|
| 97 |
+
assert g["score"] == 0 and g["correct"] is False, (blank, g)
|
| 98 |
+
assert "reference answer" in g["explanation"].lower()
|
| 99 |
+
assert calls["n"] == 0, "empty answer must not call the model"
|
| 100 |
+
print("ok empty answer short-circuits to score 0 (no model call)")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
if __name__ == "__main__":
|
| 104 |
test_clean_json_first_try()
|
| 105 |
test_repair_retry_recovers()
|
| 106 |
test_safe_default_when_never_valid()
|
| 107 |
test_out_of_range_score_rejected()
|
| 108 |
+
test_empty_answer_short_circuits_to_zero()
|
| 109 |
print("\nAll NAH-8 grade_answer tests passed.")
|