Skip to main content

The NLP Bias Identification Toolkit

Project description

Biaslyze

The NLP Bias Identification Toolkit

Bias is often subtle and difficult to detect in NLP models, as the protected attributes are less obvious and can take many forms in language (e.g. proxies, double meanings, ambiguities etc.). Therefore, technical bias testing is a key step in avoiding algorithmically mediated discrimination. However, it is currently conducted too rarely due to the effort involved, missing resources or lack of awareness for the problem.

Biaslyze helps to get started with the analysis of bias within NLP models and offers a concrete entry point for further impact assessments and mitigation measures. Especially for developers, researchers and teams with limited resources, our toolbox offers a low-effort approach to bias testing in NLP use cases.

Supported Models

All text classification models with probability output are supported. This includes models from scikit-learn, tensorflow, pytorch, huggingface transformers and others. See the tutorials section for examples.

Installation

Installation can be done using pypi:

pip install biaslyze

Then you need to download the required spacy models:

python -m spacy download en_core_web_sm

Quickstart

from biaslyze.bias_detectors import CounterfactualBiasDetector

bias_detector = CounterfactualBiasDetector()

# detect bias in the model based on the given texts
# here, clf is a scikit-learn text classification pipeline trained for a binary classification task
detection_res = bias_detector.process(
    texts=texts,
    predict_func=clf.predict_proba
)

# see a summary of the detection
detection_res.report()

# visualize the counterfactual scores
detection_res.visualize_counterfactual_scores(concept="religion", top_n=10)

You will get results as Boxplots, among others, indicating the impact of keywords and concepts on the prediction of your model. Example output:

See more detailed examples in the tutorial.

Development setup

  • First you need to install poetry to manage your python environment: https://python-poetry.org/docs/#installation
  • Run make install to install the dependencies and get the spacy basemodels.
  • Now you can use biaslyze in your jupyter notebooks.

Adding concepts and keywords

You can add concepts and new keywords for existing concepts by editing concepts.py.

Preview/build the documentation with mkdocs

To preview the documentation run make doc-preview. This will launch a preview of the documentation on http://127.0.0.1:8000/. To build the documentation html run make doc.

Run the automated tests

make test

Style guide

We are using isort and black: make style For linting we are running ruff: make lint

Contributing

Follow the google style guide for python: https://google.github.io/styleguide/pyguide.html

This project uses black, isort and ruff to enforce style. Apply it by running make style and make lint.

Acknowledgements

  • Funded from March 2023 until August 2023 by logos of the "Bundesministerium für Bildung und Forschung", Prodotype Fund and OKFN-Deutschland

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

biaslyze-0.0.7a0.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

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

biaslyze-0.0.7a0-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file biaslyze-0.0.7a0.tar.gz.

File metadata

  • Download URL: biaslyze-0.0.7a0.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.10 Linux/5.15.0-76-generic

File hashes

Hashes for biaslyze-0.0.7a0.tar.gz
Algorithm Hash digest
SHA256 719f7709afdc377c39fc67eb507e22f947f0a305ccc6e0acee760b56ecc8d251
MD5 3a6776a4f6b1d75715cb6d032c8e354f
BLAKE2b-256 1dee0196f9f75d985eb749929e1fa06029ae962289c978bc1defbcb214e94054

See more details on using hashes here.

File details

Details for the file biaslyze-0.0.7a0-py3-none-any.whl.

File metadata

  • Download URL: biaslyze-0.0.7a0-py3-none-any.whl
  • Upload date:
  • Size: 29.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.10 Linux/5.15.0-76-generic

File hashes

Hashes for biaslyze-0.0.7a0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7de5902c088e55a8eceff6787c77e430a46eebd713cfb08025d8a7ba0a2dbb4
MD5 8c171acd505114da7451e1508eff5abb
BLAKE2b-256 d131177d056116fa241a165a03e198c39f859d86748a881e435e538ebfc0e504

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