File size: 2,027 Bytes
a980198
 
15c875a
 
 
 
 
 
 
 
 
a980198
 
15c875a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a980198
 
 
 
 
 
15c875a
 
a980198
 
 
 
15c875a
 
 
 
 
 
 
 
 
 
 
 
a980198
15c875a
 
a980198
 
15c875a
 
 
 
a980198
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
73
74
75
76
77
78
import components.utils as utils
from components.config import app_config
from components.models import (
    pipeline_task_A,
    pipeline_task_B,
    explainer_task_A,
    explainer_task_B,
)
from lime.lime_text import LimeTextExplainer
from typing import Any
from matplotlib.figure import Figure


def predict_for_pipeline(
    model_pipeline: Any,
    explainer: LimeTextExplainer,
    cleaned_data: list[str],
    labels: list,
) -> tuple[int, Figure | None]:
    """Generates Prediction and Explanation given the cleaned text

    Args:
        model_pipeline (Any): Joblib imported model pipeline
        explainer (LimeTextExplainer): text explainer
        cleaned_data (list[str]): cleaned text
        labels(list): list of integers as labels

    Returns:
        tuple[int, Figure]: class prediction and LIME explanation as matplotlib figure
    """

    explanation = explainer.explain_instance(
        cleaned_data[0],
        model_pipeline.predict_proba,
        num_features=app_config.NUM_EXPLAINER_FEATURES,
        labels=labels,
    )

    class_prediction = model_pipeline.predict(cleaned_data)[0]
    return class_prediction, explanation.as_pyplot_figure(label=1)


def get_predictions(text: str) -> tuple:
    """Gets Predictions for the Texts

    Args:
        text (str): The input text to get predictions for

    Returns:
        tuple[str, Any]: Predictions for task A and task B
        along with Figures
    """

    cleaned_data = [utils.clean_one_text(text)]

    prediction_task_A = predict_for_pipeline(
        pipeline_task_A,
        explainer_task_A,
        cleaned_data,
        [0, 1, 2],
    )
    prediction_task_B = predict_for_pipeline(
        pipeline_task_B,
        explainer_task_B,
        cleaned_data,
        [0, 1],
    )

    print(prediction_task_A)
    print(prediction_task_B)

    return (
        app_config.TASK_A_MAP[prediction_task_A[0]],
        app_config.TASK_B_MAP[prediction_task_B[0]],
        prediction_task_A[1],
        prediction_task_B[1],
    )