Metadata-Version: 2.4
Name: mlflow-explainable
Version: 1.0.0
Summary: Log explainable ML models (predictor + SHAP explainer) to MLflow as a single self-contained pyfunc artifact.
Author-email: Pongsakorn <prince.pongsakorn@outlook.com>
License: MIT License
        
        Copyright (c) 2026 Pongsakorn
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/princepongsakorn/mlflow-explainable
Project-URL: Repository, https://github.com/princepongsakorn/mlflow-explainable
Project-URL: Issues, https://github.com/princepongsakorn/mlflow-explainable/issues
Keywords: mlflow,shap,xai,explainable-ai,machine-learning,pyfunc
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mlflow<4,>=2.0
Requires-Dist: shap>=0.43
Requires-Dist: cloudpickle>=2.0
Requires-Dist: numpy>=1.23
Requires-Dist: pandas>=1.5
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: scikit-learn>=1.2; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=4.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Dynamic: license-file

# mlflow-explainable

Log explainable ML models (predictor + SHAP explainer) to MLflow as a single
self-contained `mlflow.pyfunc` artifact. Designed for a multi-model serving
runtime that should not need to know whether the underlying model is a sklearn
estimator, an XGBoost booster, or a custom torch network.

## Installation

```bash
pip install mlflow-explainable
```

## Quick start

### sklearn-style model

```python
import mlflow
from sklearn.ensemble import RandomForestClassifier
from mlflow_explainable import log_explainable_model

model = RandomForestClassifier().fit(X_train, y_train)

with mlflow.start_run():
    mlflow.log_metric("accuracy", acc)
    log_explainable_model(
        model,
        X_train,
        registered_name="sample-rf-crc",
    )
```

### Gradient Boosting (sklearn)

```python
from sklearn.ensemble import GradientBoostingClassifier
from mlflow_explainable import log_explainable_model

model = GradientBoostingClassifier().fit(X_train, y_train)
with mlflow.start_run():
    log_explainable_model(model, X_train, registered_name="sample-gb-crc")
```

### XGBoost

```python
import xgboost as xgb
from mlflow_explainable import log_explainable_model

model = xgb.XGBClassifier(eval_metric="logloss").fit(X_train, y_train)
with mlflow.start_run():
    log_explainable_model(
        model,
        X_train,
        registered_name="sample-xgboost-crc",
        extra_pip_requirements=["xgboost"],
    )
```

Note: as of `shap` 0.49 the meta-`Explainer` no longer auto-detects
`XGBClassifier`. `log_explainable_model` transparently retries with
`model.predict_proba` when this happens, so the user-facing API stays the same.

### Custom torch model (or any callable wrapper)

```python
from mlflow_explainable import log_explainable_model

wrapper = GCNTabularWrapper(gcn, edge_index, device, feature_names)

with mlflow.start_run():
    mlflow.log_metric("accuracy", acc)
    log_explainable_model(
        wrapper,
        X_train,
        registered_name="sample-gcn-crc",
        explainer_kwargs={"algorithm": "permutation"},
        extra_pip_requirements=["torch", "torch_geometric"],
    )
```

The library walks the predictor's object graph, collects source files of any
user-defined classes (skipping stdlib and well-known third-party prefixes),
and packs them into the artifact via MLflow's `code_path`. The serving runtime
never needs to import those classes from its own codebase.

## Loading at serving time

```python
loaded = mlflow.pyfunc.load_model(model_uri)
impl   = loaded._model_impl.python_model    # ExplainableModel instance

# 1) standard pyfunc predict — returns DataFrame[Y_proba, Y_class]
result = loaded.predict(X)

# 2) SHAP explanation — uniform shape across all explainer types
explanation = impl.shap_explain(X)
# explanation["values"]      shape (n, n_features) or (n, n_features, n_classes)
# explanation["base_values"] shape ()              or (n_classes,) or (n, n_classes)
# explanation["data"]        shape (n, n_features)
```

## Why a contract?

`mlflow.sklearn.load_model` ties the runtime to the sklearn API. Adding a
torch model means another branch, another set of attribute assumptions
(`predict_proba`, `expected_value`, ...), and another way for `kserve` to
break when the SHAP version changes.

`mlflow-explainable` standardises the runtime-facing surface to two methods —
`predict` and `shap_explain` — and pushes all framework-specific glue into the
artifact itself.

## License

MIT
