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, )