File size: 2,775 Bytes
2f74c56
 
 
 
 
 
 
 
 
 
 
0fe28e9
2f74c56
 
 
 
 
 
 
 
 
 
 
 
0fe28e9
 
4213665
 
0fe28e9
 
ada7257
0fe28e9
 
4213665
ada7257
 
0fe28e9
af0cf81
2f74c56
ada7257
2f74c56
 
0fe28e9
 
4213665
0fe28e9
4213665
 
0fe28e9
af0cf81
2f74c56
ada7257
 
0fe28e9
ada7257
2f74c56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Portions of this file were developed with assistance from OpenAI ChatGPT/Codex and reviewed/modified by the author.
"""Inference helpers for the Campus Triage application.

Portions of this file were developed with assistance from OpenAI ChatGPT/Codex and reviewed/modified by the author.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any

from campus_triage.config import CATEGORY_LABELS, ROUTING_RECOMMENDATIONS, URGENCY_LABELS
from campus_triage.features import keyword_explanation
from campus_triage.models import load_dual_classifier


EXAMPLE_MESSAGES = [
    "My FAFSA documents still say incomplete and tuition is due tomorrow. Can someone help?",
    "I cannot register for BIO 101 because there is a hold on my account.",
    "The portal keeps showing an error when I try to submit my housing form.",
    "I am worried about a student who said they might hurt themselves. Please call me ASAP.",
]


ROOT_DIR = Path(__file__).resolve().parents[2]
MODEL_PATH = ROOT_DIR / "models" / "tfidf_logistic_regression.joblib"


def resolve_deployed_model_path(model_path: Path = MODEL_PATH) -> Path | None:
    """Return the trained model path from the repository root."""

    if model_path.exists():
        return model_path
    return None


def model_available(model_path: Path = MODEL_PATH) -> bool:
    """Return whether the deployed model artifact exists."""

    return resolve_deployed_model_path(model_path) is not None


def model_search_diagnostics(model_path: Path = MODEL_PATH) -> str:
    """Return the absolute model path checked during deployment."""

    return str(model_path)


def load_deployed_model(model_path: Path = MODEL_PATH) -> Any:
    """Load the deployed TF-IDF Logistic Regression model."""

    resolved_model_path = resolve_deployed_model_path(model_path)
    if resolved_model_path is None:
        raise FileNotFoundError(f"No deployed model artifact found at {model_path}")
    return load_dual_classifier(str(resolved_model_path))


def predict_message(message_text: str, model: Any | None = None) -> dict[str, Any]:
    """Predict triage fields and attach routing guidance."""

    deployed_model = model or load_deployed_model()
    prediction = deployed_model.predict_one(message_text)
    prediction["routing_recommendation"] = ROUTING_RECOMMENDATIONS[prediction["category"]]
    prediction["explanation"] = keyword_explanation(
        message_text,
        prediction["category"],
        prediction["category_confidence"],
    )
    prediction["category_scores"] = {label: prediction["category_scores"].get(label, 0.0) for label in CATEGORY_LABELS}
    prediction["urgency_scores"] = {label: prediction["urgency_scores"].get(label, 0.0) for label in URGENCY_LABELS}
    return prediction