📦 nlp-shap
Multimodal explainability for NLP and multimodal models based on Shapley-style cooperative game theory.
Installation
pip install nlp-shap
Optional backends and visualization:
pip install "nlp-shap[transformers]" # Hugging Face text backend
pip install "nlp-shap[lmstudio]" # LM Studio SDK
pip install "nlp-shap[api]" # OpenAI-compatible HTTP API
pip install "nlp-shap[viz]" # matplotlib token charts
The core package does not install PyTorch. See the extending guide for plugin entry points.
Quickstart
from nlp_shap import ExplainConfig, ExplainRunner
from nlp_shap.domain.conversation import ConversationSnapshot, Message, Turn
from nlp_shap.domain.enums import Role
snapshot = ConversationSnapshot.from_turns((
Turn(messages=(Message(role=Role.USER, text="refund my order"),)),
))
config = ExplainConfig.model_validate({
"backend": {"kind": "mock", "model_id": "stub"},
"explanation": {"estimator": "exact", "value_fn": "tfidf_cosine"},
})
output = ExplainRunner(config).explain_sync(snapshot)
print(output.result.values)
From source:
git clone https://github.com/Pawlo77/nlp-shap
cd nlp-shap
make install
Requires Python 3.12 and uv.
Documentation
Full documentation: pawlo77.github.io/nlp-shap
- Getting started
- User guide — estimands
- User guide — masking
- User guide — runtime
- User guide — exact estimation
- User guide — approximate estimation
- User guide — visualization
- Examples (notebooks)
- API reference
Examples
Runnable Jupyter notebooks in examples/:
| Notebook | What it demonstrates |
|---|---|
estimands_toy_game.ipynb |
Shapley vs Banzhaf aggregators, coalition weights, labelled results, manifests, plugin entry points |
masking_views.ipynb |
Token partitioning, absence policies, masked views, mask codec, mask space, plugin registry |
runtime_core.ipynb |
Run archive, coalition dedup, hot LRU cache, async scheduler with bounded concurrency |
exact_estimation.ipynb |
Exact coalition enumeration, estimand delegation, budget guard, plugin resolution |
estimator_comparison.ipynb |
Eight-player cap≤20 benchmark + five-player fraction sweep 0.1–0.5, MC bias analysis |
attribution_viz.ipynb |
Per-token Shapley visualization with token_text and token_bar renderers |
text_explain_e2e.ipynb |
Full explain pipeline on a refund FAQ prompt — LM Studio + transformers + charts |
Setup and details: examples/README.md. Embedded walkthroughs: docs/examples.
Development
make install
make check
Run make help for other targets.
License
Apache License 2.0. See LICENSE.
Copyright 2026 Paweł Pozorski.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nlp_shap-0.1.16.tar.gz.
File metadata
- Download URL: nlp_shap-0.1.16.tar.gz
- Upload date:
- Size: 55.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5eff05b19079e9073dd743a679621439cc0f27ca76a9dab7a6b2d710245e0bdf
|
|
| MD5 |
b28711926d39480317c0873807656485
|
|
| BLAKE2b-256 |
9154d812001cf4e99426c52930ddf4dc3832faa3082ebe195d09bd0a5ffa35f8
|
File details
Details for the file nlp_shap-0.1.16-py3-none-any.whl.
File metadata
- Download URL: nlp_shap-0.1.16-py3-none-any.whl
- Upload date:
- Size: 91.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e9dbf2476d976fea989ce9012fcbbd71e422a538f684a187262343599221e91
|
|
| MD5 |
1d6dcd65df85b398b1e9970e1f2b1730
|
|
| BLAKE2b-256 |
8a75151162a4dd80f1df7f63f23ce22b6c69941e854576b41b01710d2b21fb7e
|