A library for detecting biases in machine learning models.
Project description
bias-scope
bias-scope is a Python library for measuring bias in language models across four complementary families of metrics:
- embedding-based metrics
- probability-based metrics
- generated-text metrics
- prompt-based benchmarks
The goal is a single, consistent API for bias evaluation whether you are working with sentence encoders, masked language models, generated completions, or dataset-driven benchmark suites.
Why bias-scope
- One package for multiple bias evaluation paradigms
- Consistent metric classes with
evaluate()entrypoints - Optional model adapters so users do not need to hand-wire every scorer
- Support for both raw-text convenience paths and precomputed inputs where appropriate
- Lightweight core install with optional extras for heavier ML dependencies
Installation
Core install:
pip install bias-scope
Optional extras:
pip install "bias-scope[torch]"
pip install "bias-scope[embeddings]"
pip install "bias-scope[datasets]"
pip install "bias-scope[llm]"
pip install "bias-scope[all]"
What each extra includes:
torch:torch,transformersfor probability-based masked-token metrics,BertPLLScorer, and transformer-backed generated-text metrics such asRegardScoreembeddings:sentence-transformersfor the built-in embedding helper used by embedding-based convenience pathsdatasets:datasetsfor prompt-based benchmark loadersllm:litellmfor prompt-based model callsall: everything above
Install from source:
git clone https://github.com/RAINLabLAU/bias_scope.git
cd bias_scope
pip install -e .
Quick Start
Embedding-Based Example
You can now pass raw text directly and let the metric embed it for you:
from bias_scope.embeddings_based import WEAT
weat = WEAT(model_name="sentence-transformers/all-MiniLM-L6-v2")
score = weat.evaluate(
target_embeddings=(
["John", "Paul", "Mike", "Kevin"],
["Amy", "Joan", "Lisa", "Sarah"],
),
attribute_embeddings=(
["executive", "management", "salary", "career"],
["home", "children", "marriage", "family"],
),
)
print(f"WEAT effect size: {score:.4f}")
If you already have embeddings, you can still pass precomputed arrays exactly as before.
Probability-Based Example
Masked-token metrics can use a built-in model adapter via model_name:
from bias_scope.probability_based import CrowSPairs
crows = CrowSPairs(model_name="bert-base-uncased")
score = crows.evaluate(
sentence_pairs=[
(
["Women", "are", "bad", "at", "math"],
["Men", "are", "bad", "at", "math"],
)
]
)
print(f"CrowS-Pairs score: {score:.4f}")
Advanced users can still provide a custom callback or scorer wrapper when needed.
Generated-Text Example
from bias_scope.generated_text_based import ScoreParity
parity = ScoreParity(classifier=lambda texts: [0.9 if "doctor" in t else 0.4 for t in texts])
result = parity.evaluate(
group_a_texts=[["The man is a doctor."]],
group_b_texts=[["The woman is a nurse."]],
)
print(result)
Prompt-Based Example
from bias_scope.prompts_based import BBQMetric
metric = BBQMetric(model_name="gpt-4o-mini")
result = metric.evaluate(return_details=True)
print(result)
Prompt-based metrics typically require bias-scope[datasets], bias-scope[llm], or both depending on the benchmark.
Metric Families
Embedding-Based
Use these when you want to measure association bias in vector spaces.
WEATSEATCEATSentenceBiasScoreembed()helper for built-in text embedding
Probability-Based
Use these with masked or token-prediction models.
CrowSPairsAULAULACATICATLMBLPBSCBSDisCoMetricBertPLLScorerTokenPredictionScorer
Generated-Text
Use these when you already have generations or want to score generated completions.
ToxicityFractionToxicityProbabilityRegardScoreScoreParitySocialGroupSubstitutionCoOccurrenceBiasScoreCounterfactualSentimentBiasDemographicRepresentationStereotypicalAssociationsMarkedPersonsEMTFGBGenderPolarityHONESTPGBPerspectiveAPIClientPsycholinguisticNorms
Prompt-Based
Use these for dataset-backed evaluation suites and benchmark-style audits.
AnalogicalReasoningBiasBBQMetricBOLDCounterfactualFairnessDemographicRepresentationBiasOpinionConsistencyAcrossPersonasRealToxicityPromptsStereoSetMetricTofNofTruthfulQAUnQoverMetric
API Notes
- Most metrics return a scalar by default.
- Metrics that support
return_details=Truereturn a richer dictionary of component scores. - Embedding metrics accept
model_nameon both__init__andevaluate(); the per-call value overrides the instance default. - Probability-based masked-token metrics support either a built-in
model_namepath or a backward-compatible custom callback path. - Metric objects now have informative
repr(...)output for notebook and REPL use.
Examples
The repository includes runnable examples for each metric family:
- examples/embeddings_based
- examples/probability_based
- examples/generated_text_based
- examples/prompts_based
- examples/metric_usage_examples.py
Documentation
Project docs live under docs/.
Good starting points:
Development
Install developer dependencies:
pip install -e .[dev]
Run tests:
python -m pytest
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
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 bias_scope-0.1.1.tar.gz.
File metadata
- Download URL: bias_scope-0.1.1.tar.gz
- Upload date:
- Size: 695.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d358e88e7ca264aadf16c7ff07f61dd57efe98ec293615d4329f8664c3074707
|
|
| MD5 |
8c3a7c616f3e35c8a8d0502e66b37f75
|
|
| BLAKE2b-256 |
e0a7aafa80b8ecc98ee05eb0fc47ce123bf7b2efa103cfa46fd98c2bac2028ae
|
File details
Details for the file bias_scope-0.1.1-py3-none-any.whl.
File metadata
- Download URL: bias_scope-0.1.1-py3-none-any.whl
- Upload date:
- Size: 142.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2484e3641153e89e2158135ce1cb38d6e7e0d463ef6f0d7e2a6855f9d3d22238
|
|
| MD5 |
60bc1875d90d1e30e97ece77b71cf59b
|
|
| BLAKE2b-256 |
673391469fb19379315563756061744249c0fdfbc76aba3e841953a483b7a533
|