Skip to content

BiasPipeline: Class

This class can be used to individually run the bias classification and NER for bias stages of the pipeline. See the example code snippets below on how to do this. It is also used by the run_pipeline_on_texts function to perform the entire pipeline on a list of texts. Alt text

To individually run the debiasing stage of the pipeline see the get_debiased_sequence function.

__init__() -> None

Called upon initialization. It calls load_resources() below.

load_resources() -> None

Loads the bias classifier, tokenizers and models.

predict_entities(text:str) -> list[str]

Retrieves biased entities from a text.

Parameters

text: The text to retrieve biased entities from.

Returns

A list of biased entities in the text.

Example

import UnBIAS.unbias as unbias

sentence = """Last Tuesday, Elon Musk announced a new project on Mars, 
            which would be funded with $6 billion from Tesla, and he discussed  
            this in New York during a United Nations meeting."""

bias_pipeline = unbias.BiasPipeline()
biased_entities = bias_pipeline.predict_entities(sentence)
print(biased_entities)

Output

['new-B-BIAS', 'project-I-BIAS', 'funded-B-BIAS', 'discussed-B-BIAS', 'this-I-BIAS', 'new-B-BIAS', 'united-B-BIAS', 'nations-I-BIAS', 'meeting-I-BIAS']

B-BIAS stands for beginning of entity and I-BIAS stands for inside of entity. This is because a biased entity can span multiple words.

Warning

If running the above multiple times in a Jupyter Notebook, you might have to do del bias_pipeline before running it again to avoid out-of-memory errors.

predict_bias(texts:str|list[str]) -> list[dict[str: str, str: float]]

Performs bias classification on a text or a list of texts.

Parameters

texts: The text or list of texts to classify.

Returns

An array of dictionaries where each dictionary is of the form: {'label': Non-biased|Biased, 'score': <bias_score>}.

Example

import UnBIAS.unbias as unbias

sentences = ["The weather is cold outside.", "Men are dumb."]
bias_pipeline = unbias.BiasPipeline()
sentence_classifications = bias_pipeline.predict_bias(sentences)
print(sentence_classifications)

Output

[{'label': 'Non-biased', 'score': 0.9980363249778748}, {'label': 'Biased', 'score': 0.9899910688400269}]