Update tools/explain_tool.py
Browse files- tools/explain_tool.py +43 -43
tools/explain_tool.py
CHANGED
|
@@ -1,44 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import shap
|
| 4 |
-
import base64
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from huggingface_hub import hf_hub_download
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
class ExplainTool:
|
| 11 |
-
def __init__(self, cfg: AppConfig, tracer: Tracer):
|
| 12 |
-
self.cfg = cfg
|
| 13 |
-
self.tracer = tracer
|
| 14 |
-
self._model = None
|
| 15 |
-
|
| 16 |
-
def _ensure_model(self):
|
| 17 |
-
if self._model is None:
|
| 18 |
-
import joblib
|
| 19 |
-
path = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="model.pkl", token=os.getenv("HF_TOKEN"))
|
| 20 |
-
self._model = joblib.load(path)
|
| 21 |
-
|
| 22 |
-
def _to_data_uri(self, fig) -> str:
|
| 23 |
-
buf = io.BytesIO()
|
| 24 |
-
fig.savefig(buf, format="png", bbox_inches="tight")
|
| 25 |
-
buf.seek(0)
|
| 26 |
-
return "data:image/png;base64," + base64.b64encode(buf.read()).decode()
|
| 27 |
-
|
| 28 |
-
def run(self, df: pd.DataFrame):
|
| 29 |
-
self._ensure_model()
|
| 30 |
-
# Use a small sample for speed on CPU Spaces
|
| 31 |
-
sample = df.sample(min(len(df), 500), random_state=42)
|
| 32 |
-
explainer = shap.Explainer(self._model, sample, feature_names=list(sample.columns))
|
| 33 |
-
shap_values = explainer(sample)
|
| 34 |
-
|
| 35 |
-
# Global summary plot
|
| 36 |
-
fig1 = shap.plots.bar(shap_values, show=False)
|
| 37 |
-
img1 = self._to_data_uri(fig1)
|
| 38 |
-
|
| 39 |
-
# Beeswarm (optional)
|
| 40 |
-
fig2 = shap.plots.beeswarm(shap_values, show=False)
|
| 41 |
-
img2 = self._to_data_uri(fig2)
|
| 42 |
-
|
| 43 |
-
self.tracer.trace_event("explain", {"rows": len(sample)})
|
| 44 |
return {"global_bar": img1, "beeswarm": img2}
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import shap
|
| 4 |
+
import base64
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from utils.config import AppConfig
|
| 8 |
+
from utils.tracing import Tracer
|
| 9 |
+
|
| 10 |
+
class ExplainTool:
|
| 11 |
+
def __init__(self, cfg: AppConfig, tracer: Tracer):
|
| 12 |
+
self.cfg = cfg
|
| 13 |
+
self.tracer = tracer
|
| 14 |
+
self._model = None
|
| 15 |
+
|
| 16 |
+
def _ensure_model(self):
|
| 17 |
+
if self._model is None:
|
| 18 |
+
import joblib
|
| 19 |
+
path = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="model.pkl", token=os.getenv("HF_TOKEN"))
|
| 20 |
+
self._model = joblib.load(path)
|
| 21 |
+
|
| 22 |
+
def _to_data_uri(self, fig) -> str:
|
| 23 |
+
buf = io.BytesIO()
|
| 24 |
+
fig.savefig(buf, format="png", bbox_inches="tight")
|
| 25 |
+
buf.seek(0)
|
| 26 |
+
return "data:image/png;base64," + base64.b64encode(buf.read()).decode()
|
| 27 |
+
|
| 28 |
+
def run(self, df: pd.DataFrame):
|
| 29 |
+
self._ensure_model()
|
| 30 |
+
# Use a small sample for speed on CPU Spaces
|
| 31 |
+
sample = df.sample(min(len(df), 500), random_state=42)
|
| 32 |
+
explainer = shap.Explainer(self._model, sample, feature_names=list(sample.columns))
|
| 33 |
+
shap_values = explainer(sample)
|
| 34 |
+
|
| 35 |
+
# Global summary plot
|
| 36 |
+
fig1 = shap.plots.bar(shap_values, show=False)
|
| 37 |
+
img1 = self._to_data_uri(fig1)
|
| 38 |
+
|
| 39 |
+
# Beeswarm (optional)
|
| 40 |
+
fig2 = shap.plots.beeswarm(shap_values, show=False)
|
| 41 |
+
img2 = self._to_data_uri(fig2)
|
| 42 |
+
|
| 43 |
+
self.tracer.trace_event("explain", {"rows": len(sample)})
|
| 44 |
return {"global_bar": img1, "beeswarm": img2}
|