Skip to main content

A library for bias detection and explainable AI methods in NLP models.

Project description

Bias Lens

Bias Lens is a Python library designed for bias detection and explainable AI (XAI) methods for NLP models. It provides a unified interface to perform token-level bias analysis using multiple state-of-the-art explanation techniques including Integrated Gradients, DeepLIFT, LIME, SHAP, attention-based methods, counterfactual explanations, LRP, and occlusion methods.

Features

  • Bias NER Classification:
    Leverage a token classification model (e.g., BERT) to identify bias-related tokens with customizable label mappings.

  • Explainability Methods:
    Integrated support for various XAI techniques:

    • LIME: Local, interpretable model-agnostic explanations.
    • SHAP: Shapley values for fair attribution of input tokens.
    • Integrated Gradients: Gradient-based attributions for deep models.
    • DeepLIFT: Explains predictions by comparing against a baseline.
    • Attention Explanations: Visualize attention scores.
    • Counterfactual Explanations: Generate alternative input versions to understand model decisions.
    • Layer-wise Relevance Propagation (LRP): Propagate relevance scores back to input features.
    • Occlusion: Identify important tokens by systematically masking parts of the input.
  • Modular Design:
    Easily extendable code structure to add new explanation methods or integrate with custom models.

  • Factory Pattern:
    Use a unified factory to access various explanation methods effortlessly.

Installation

You can install Bias Lens from PyPI (after publishing) with:

pip install bias-lens

Or install directly from the repository:

pip install git+https://github.com/bhavaniit24/bias-lens.git

Quick Start

Below is a simple example demonstrating how to use Bias Lens with your own model, tokenizer, and label mapping.

from transformers import BertForTokenClassification, BertTokenizerFast
from bias_lens import BiasNERClassifier, BiasModelExplainerFactory, ExplainerMethod

# User-defined model, tokenizer, and id2label mapping
model = BertForTokenClassification.from_pretrained("your_model_path")
tokenizer = BertTokenizerFast.from_pretrained("your_model_path")
id2label = {
    0: "O",
    1: "B-STEREO",
    2: "I-STEREO",
    3: "B-GEN",
    4: "I-GEN",
    5: "B-UNFAIR",
    6: "I-UNFAIR",
    7: "B-EXCL",
    8: "I-EXCL",
    9: "B-FRAME",
    10: "I-FRAME",
    11: "B-ASSUMP",
    12: "I-ASSUMP",
}

# Create a bias NER classifier instance
ner_classifier = BiasNERClassifier(model, tokenizer, id2label)
result = ner_classifier.predict("Women are bad drivers")
print("NER Prediction:", result)

# Create an explainer factory
explainer_factory = BiasModelExplainerFactory(model, tokenizer, id2label)

# Example: Get LIME explainer and generate explanation
lime_explainer = explainer_factory.get_explainer(ExplainerMethod.LIME)
lime_explanation = lime_explainer.explain("Women are bad drivers")
print("LIME Explanation:", lime_explanation)

Documentation

Modules and Classes

  • BiasNERClassifier: Provides a method to perform token-level bias prediction using a pre-trained token classification model.

  • BaseExplainer & ProbabilityExplainerMixin: Base classes that offer common functionality such as input preparation and probability prediction.

  • Explainer Implementations: Classes like LimeExplainer, ShapExplainer, IntegratedGradientsExplainer, DeepLiftExplainer, AttentionExplainer, CounterfactualExplainer, LRPExplainer, and OcclusionExplainer each encapsulate a different XAI method.

  • BiasModelExplainerFactory: A factory class to easily retrieve the desired explainer based on a string key.

  • ExplainerMethod: An enum class that defines the available explanation methods.

Explanation Methods

The available methods in the enum are:

  • LIME
  • SHAP
  • INTEGRATED_GRADIENTS
  • INTEGRATED_GAP_GRADIENTS
  • DEEPLIFT
  • ATTENTION
  • COUNTERFACTUAL
  • LRP
  • OCCLUSION
  • SALIENCY

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub with your ideas and improvements.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any questions or feedback, please contact bhavaniit24.


Bias Lens is designed to make bias detection and model interpretability more accessible and flexible. Whether you are researching fairness in NLP or developing production models, this library aims to provide robust tools to explain and mitigate biases.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bias_lens-0.0.6.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bias_lens-0.0.6-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file bias_lens-0.0.6.tar.gz.

File metadata

  • Download URL: bias_lens-0.0.6.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for bias_lens-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cf026918bc7626edada13c6288e3b81ad75eebf2d85480ec142765c4dd0a1352
MD5 ad4414885b2b903e6de640363506cb96
BLAKE2b-256 f05951261cdc026c2cebc1ade869bb871c45ae7b65bccf6af6cd69fdd1a88a9e

See more details on using hashes here.

File details

Details for the file bias_lens-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: bias_lens-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for bias_lens-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9e37f707b0fefe1748a26bfaa63e61fe59c963b1ff994f71cf04510531316c64
MD5 c7511fd2cea491a1854dd3260035e26e
BLAKE2b-256 955bfdc69cedd2b594b91806a752d6b0f8384c61607761ae5a7fd7c80fce045f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page