File size: 1,199 Bytes
fc7db98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import annotations

import sys

from app.config import REPO_ROOT
from app.schemas import InterventionDetail


def _ensure_path() -> None:
    if str(REPO_ROOT) not in sys.path:
        sys.path.insert(0, str(REPO_ROOT))


def run_intervention(
    predictor,
    sentence: str,
    target_word: str,
    reason: str,
    use_llm: bool,
    predicted_reason: str | None = None,
) -> InterventionDetail:
    _ensure_path()
    from reason_interventions import apply_intervention

    base = predictor.predict(sentence, target_word)
    intervention = apply_intervention(reason, sentence, target_word, use_llm=use_llm)
    after = predictor.predict(intervention.edited_sentence, target_word)
    drop = base.difficult_class_prob - after.difficult_class_prob
    return InterventionDetail(
        reason=reason,
        original_sentence=intervention.original_sentence,
        edited_sentence=intervention.edited_sentence,
        edit_method=intervention.edit_method,
        before_level=base.complexity_level,
        after_level=after.complexity_level,
        hardness_drop=round(drop, 4),
        matches_predicted=reason == predicted_reason if predicted_reason else None,
    )