Spaces:
Build error
Build error
Download tests/test_exam_difficulty_relabel.py from anwarkxc/trackademi: direct link, hf CLI and curl.
- Browser
- Download file 7.49 kB
-
https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/tests/test_exam_difficulty_relabel.py
- Command line
-
hf download hf://spaces/anwarkxc/trackademi/tests/test_exam_difficulty_relabel.py
-
curl -L -o test_exam_difficulty_relabel.py https://huggingface.co/spaces/anwarkxc/trackademi/resolve/main/tests/test_exam_difficulty_relabel.py
7.49 kB
| """Rank-assigned difficulty labels. | |
| The model will not distribute difficulty within one response. Measured across | |
| many production runs: given "4 easy, 3 medium, 3 hard" it returns a section at | |
| one level, and *which* level is decided by the question type rather than the | |
| instruction — true_false collapses to easy and essay to hard on the same exam | |
| with the same target. Three prompt rewrites (ratio wording, a scoped recipe | |
| block, explicit per-question slots) moved the delivered mix by 0.0pp on a full | |
| paper, while the same slot instruction is exact on a section generated alone. | |
| So the labels are assigned after generation, in measured-difficulty order. This | |
| delivers the requested counts and makes the label ordering reflect real | |
| difficulty — it does NOT make the questions harder, and these tests pin that | |
| distinction so nobody later mistakes a full `difficulty_balance` for quality. | |
| """ | |
| from app.services.gateway import ( | |
| _assign_difficulty_by_rank, | |
| _measured_difficulty, | |
| ) | |
| def _q(question, answer="إجابة", objective="هدف", **extra): | |
| base = { | |
| "type": "mcq", "difficulty": "hard", "question": question, | |
| "answer": answer, "learning_objective": objective, | |
| "choices": ["أ الخيار", "ب الخيار", "ج الخيار", "د الخيار"], | |
| } | |
| base.update(extra) | |
| return base | |
| _RECALL = _q("متى وُلد البيروني؟", "سنة ٣٦٢ هجرية") | |
| _HARDEST = _q( | |
| "قال طالب إن «مدارس» تُجر بالكسرة لأنها اسم مجرور. حدّد موضع الخطأ في تحليله.", | |
| "الخطأ أنه خلط بين الوظيفة الإعرابية وعلامة الإعراب", | |
| source_chunk_ids=["a", "b"], | |
| ) | |
| _MIDDLE = _q("ما سبب اختلاف الحكم في الموضعين؟", "دخول الإضافة على الاسم") | |
| def test_the_hardest_measured_question_gets_the_hard_label(): | |
| questions = [_RECALL.copy(), _HARDEST.copy(), _MIDDLE.copy(), _RECALL.copy()] | |
| _assign_difficulty_by_rank(questions, {"easy": 2, "medium": 1, "hard": 1}) | |
| assert questions[1]["difficulty"] == "hard" | |
| assert questions[2]["difficulty"] == "medium" | |
| assert questions[0]["difficulty"] == "easy" | |
| def test_requested_counts_are_delivered_exactly(): | |
| """The whole point: a uniformly-labelled section becomes the requested mix.""" | |
| questions = [_q(f"سؤال رقم {i}") for i in range(10)] | |
| _assign_difficulty_by_rank(questions, {"easy": 4, "medium": 3, "hard": 3}) | |
| counts = {} | |
| for q in questions: | |
| counts[q["difficulty"]] = counts.get(q["difficulty"], 0) + 1 | |
| assert counts == {"easy": 4, "medium": 3, "hard": 3} | |
| def test_a_uniformly_hard_section_is_broken_up(): | |
| """The exact observed failure — mcq 20/20 hard for a 33% request.""" | |
| questions = [_q(f"سؤال {i}") for i in range(20)] | |
| assert all(q["difficulty"] == "hard" for q in questions) | |
| _assign_difficulty_by_rank(questions, {"easy": 7, "medium": 7, "hard": 6}) | |
| assert sum(1 for q in questions if q["difficulty"] == "hard") == 6 | |
| def test_relabelling_does_not_change_the_questions(): | |
| """It reorders labels, nothing else. A section of easy questions still has | |
| easy questions in it — the label is a rank within this exam, not a claim | |
| that the question is objectively hard.""" | |
| questions = [_RECALL.copy() for _ in range(4)] | |
| before_scores = [_measured_difficulty(q) for q in questions] | |
| _assign_difficulty_by_rank(questions, {"easy": 2, "medium": 1, "hard": 1}) | |
| assert [_measured_difficulty(q) for q in questions] == before_scores | |
| # ...and the question labelled hard is still measurably not hard. | |
| hardest = next(q for q in questions if q["difficulty"] == "hard") | |
| assert _measured_difficulty(hardest) < 56 | |
| def test_it_is_deterministic_for_tied_scores(): | |
| questions_a = [_q(f"سؤال {i}") for i in range(6)] | |
| questions_b = [_q(f"سؤال {i}") for i in range(6)] | |
| _assign_difficulty_by_rank(questions_a, {"easy": 2, "medium": 2, "hard": 2}) | |
| _assign_difficulty_by_rank(questions_b, {"easy": 2, "medium": 2, "hard": 2}) | |
| assert [q["difficulty"] for q in questions_a] == [q["difficulty"] for q in questions_b] | |
| def test_no_target_means_no_relabelling(): | |
| """Without an AssessmentIntent there are no counts to honour, so the | |
| model's own labels stand.""" | |
| questions = [_RECALL.copy(), _HARDEST.copy()] | |
| assert _assign_difficulty_by_rank(questions, None) is None | |
| assert _assign_difficulty_by_rank(questions, {"easy": 0, "medium": 0, "hard": 0}) is None | |
| assert all(q["difficulty"] == "hard" for q in questions) | |
| def test_extra_questions_beyond_the_target_are_labelled_easy(): | |
| """A section that over-delivered must not keep a stale label on the tail.""" | |
| questions = [_q(f"سؤال {i}") for i in range(6)] | |
| _assign_difficulty_by_rank(questions, {"easy": 1, "medium": 1, "hard": 1}) | |
| assert all(q["difficulty"] in {"easy", "medium", "hard"} for q in questions) | |
| assert sum(1 for q in questions if q["difficulty"] == "easy") == 4 | |
| def test_stats_report_what_actually_changed(): | |
| questions = [_RECALL.copy(), _HARDEST.copy(), _MIDDLE.copy(), _RECALL.copy()] | |
| stats = _assign_difficulty_by_rank(questions, {"easy": 2, "medium": 1, "hard": 1}) | |
| assert stats["before"] == {"hard": 4} | |
| assert stats["after"] == {"easy": 2, "medium": 1, "hard": 1} | |
| assert stats["relabelled"] == 3 | |
| # Range of measured scores, so a section where everything scored the same | |
| # is visible as such rather than looking like a meaningful ranking. | |
| assert stats["measured_range"][0] <= stats["measured_range"][1] | |
| def test_every_section_carries_the_same_proportion(): | |
| """Difficulty must be spread across the paper, not front-loaded. | |
| Before rank assignment a 33%-hard exam came back with mcq 20/20 hard, | |
| true_false 10/10 hard and short_answer 0/10 hard — the first sections were | |
| the difficult exam and the last ones were a different, easy exam. Because | |
| each section is allocated from the same ratio, the spread is even by | |
| construction; this pins that so a future change to allocation can't | |
| reintroduce the lumpiness. | |
| """ | |
| sections = [[_q(f"سؤال {s}-{i}") for i in range(10)] for s in range(4)] | |
| for questions in sections: | |
| _assign_difficulty_by_rank(questions, {"easy": 4, "medium": 3, "hard": 3}) | |
| hard_per_section = [ | |
| sum(1 for q in questions if q["difficulty"] == "hard") for questions in sections | |
| ] | |
| assert hard_per_section == [3, 3, 3, 3] | |
| def test_family_tags_never_reach_the_question_text(): | |
| """The recipe block labels build orders `[error_diagnosis] ...` and the | |
| model sometimes copies the tag onto the question — 17 of 952 generated | |
| questions carried one, which would print on the student's paper.""" | |
| from app.services.gateway import _strip_family_tag | |
| assert _strip_family_tag("[transformation] لو افترضنا أن الشعراء التزموا") == "لو افترضنا أن الشعراء التزموا" | |
| assert _strip_family_tag("[error_diagnosis]: يقول طالب") == "يقول طالب" | |
| # Legitimate brackets inside a question are untouched. | |
| assert _strip_family_tag("ما دور [الجهاز العصبي] في التنسيق؟") == "ما دور [الجهاز العصبي] في التنسيق؟" | |
| assert _strip_family_tag("لماذا رفض البيروني الجوائز؟") == "لماذا رفض البيروني الجوائز؟" | |