File size: 1,961 Bytes
4c01182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import numpy as np
from src.core.logger import logging
from src.core.exception import AppException
from src.app.monitoring.service_metrics import EXPLAINER_REQUEST_SUCCESS, EXPLAINER_REQUEST_FAILED


class ExplainerService:
    def __init__(self, explainer, model_booster, vectorizer):
        """

        Initializes an instance of LimeExplainer explaination service.



        Args:

            explainer (LimeTextExplainer) : An instance of LimeTextExplainer.

            model_booster (xgb.XGBClassifier) : An instance of the XGBoost model.

            vectorizer : An instance of the vectorizer to transform input text into numerical features.

        """
        self.explainer = explainer
        self.booster = model_booster
        self.vectorizer = vectorizer

    def _get_prediction(self, text) -> np.ndarray:
        """

        Internal function to get class probability scores for lime explainer.

        """
        X = self.vectorizer.transform(text)
        prob = self.booster.inplace_predict(X)
    
        if len(prob.shape) == 1:
            prob = np.vstack([1 - prob, prob]).T
        return prob    
    
    def explain(self, text: str):
        """

        Generate an explanation of the prediction made by the model for a given text.



        Returns:

            HTML content of the explanation which is rendered in the UI

        """
        try:
            explanation = self.explainer.explain_instance(
                text,
                self._get_prediction,
                num_features=10,
                num_samples=20
            )
            html_content = explanation.as_html()
            EXPLAINER_REQUEST_SUCCESS.inc()

            return html_content
        
        except Exception as e:
            EXPLAINER_REQUEST_FAILED.inc()
            logging.error(f"Explainer service failed: {e}", exc_info=True)
            raise AppException(e, sys)