Skip to main content
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 custom models. The bias detection requires you to pass a predict_proba function similar to what you would get on scikit-learn models. You can find a tutorial on how to do that for pre-trained transformer models here.

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()

# launch the dashboard visualize the counterfactual scores
detection_res.dashboard(num_keywords=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 the concept files in concepts. A tutorial on how to create custom concepts and work with them can be found here.

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

Metadata

Release files for biaslyze 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for biaslyze 0.1.0
File Size Uploaded
biaslyze-0.1.0.tar.gz 29.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for biaslyze 0.1.0
File Interpreter ABI Platform
biaslyze-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 63.8 kB

Release files / biaslyze-0.1.0.tar.gz

Download URL biaslyze-0.1.0.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4b8fa8210a38019a7c7b3ac8ee1865f98c3ce94570a951aad2589c78bf223e42
BLAKE2b-256 checksum
How to use checksums
795748c3feec5c14dd6f5adf7bbfa03ac9cdded3e58eb4e1fc2c1faf63762db7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-1041-azure

Release files / biaslyze-0.1.0-py3-none-any.whl

Download URL biaslyze-0.1.0-py3-none-any.whl
Size 33.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b68ae16dec9f652d211fc6b2b0f63cdb872e358ac019ca3a3f8408cb8accb80
BLAKE2b-256 checksum
How to use checksums
be28dcea9b74bf8a98ccc1fd019b55fa87a7b8ed0ae336bf54cc048d714cb7d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/5.15.0-1041-azure
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page