Spaces:
Sleeping
Sleeping
Download app/services/explainer.py from Subi003/ToxicTweet-Tagger: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/Subi003/ToxicTweet-Tagger/resolve/main/app/services/explainer.py
- Command line
-
hf download hf://spaces/Subi003/ToxicTweet-Tagger/app/services/explainer.py
-
curl -L -o explainer.py https://huggingface.co/spaces/Subi003/ToxicTweet-Tagger/resolve/main/app/services/explainer.py
1.96 kB
| 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) |