|
import gradio as gr |
|
import pandas as pd |
|
import os |
|
from scripts.genbit_metrics import * |
|
from scripts.gender_profession_tagging import * |
|
from scripts.gender_tagging import * |
|
from utils.load_csv import * |
|
from utils.read_config import get_args |
|
|
|
dummy_data = [ |
|
["Category", "Value", "Percentage"], |
|
["Total Reviews", 50000, None], |
|
["Total Sentences", 621647, None], |
|
["Pronouns in Sentences", None, None], |
|
["Male Pronouns", 85615, None], |
|
["Female Pronouns", 39372, None], |
|
["Both Male and Female Pronouns", 7765, None], |
|
["Exclusive Usage of Pronouns", None, None], |
|
["Only Male Pronouns", 77860, 13.77], |
|
["Only Female Pronouns", 31617, 6.33], |
|
["Pronouns and Professions in Sentences", None, None], |
|
["Male Pronouns with Professions", 5580, 0.9], |
|
["Female Pronouns with Professions", 2618, 0.42], |
|
["Exclusive Usage of Pronouns with Professions", None, None], |
|
["Only Male Pronouns with Professions", 5011, 0.81], |
|
["Only Female Pronouns with Professions", 2049, 0.33], |
|
["Pronouns and Professions in Combination", None, None], |
|
["Male or Female Pronouns with Professions", 7629, 1.23], |
|
["Male and Female Pronouns with Professions", 569, 0.09] |
|
] |
|
|
|
|
|
def display_methodology(methodology): |
|
title = "### " + methodology |
|
description = "" |
|
|
|
if methodology == "Term Identity Diversity Analysis": |
|
description = "333" |
|
elif methodology == "Gender Label Evaluation": |
|
description = "This approach to addressing gender bias in language places a strong emphasis on a fundamental shift in detection and mitigation strategies.\n- Instead of solely relying on traditional frequency-based methods, this approach adopts a more nuanced perspective, prioritizing features within the text that consider contextual and semantic cues. It recognizes that gender bias extends beyond mere word frequency and delves into how language is structured and how it reinforces gender stereotypes.\n- Even with advanced models like Word Embedding and Contextual Word Embedding, which capture more complex language features, there's still a risk of inheriting biases from training data.\n- To tackle this, this approach advocates for a data-driven strategy, involving the collection and labeling of datasets encompassing various subtypes of bias, using a comprehensive taxonomy for precise categorization." |
|
elif methodology == "Microsoft Genbit": |
|
description = "GenBiT is a versatile tool designed to address gender bias in language datasets by utilizing word co-occurrence statistical methods to measure bias. It introduces a novel approach to mitigating gender bias by combining contextual data augmentation, random sampling, sentence classification, and targeted gendered data filtering.\n- The primary goal is to reduce historical gender biases within conversational parallel multilingual datasets, ultimately enhancing the fairness and inclusiveness of machine learning model training and its subsequent applications.\n- What sets GenBiT apart is its adaptability to various forms of bias, not limited to gender alone. It can effectively address biases related to race, religion, or other dimensions, making it a valuable generic tool for bias mitigation in language datasets.\n- GenBiT's impact extends beyond bias reduction metrics; it has shown positive results in improving the performance of machine learning classifiers like Support Vector Machine(SVM). Augmented datasets produced by GenBiT yield significant enhancements in f1-score when compared to the original datasets, underlining its practical benefits in machine learning applications." |
|
|
|
return ( |
|
gr.Markdown.update(title, visible=True), |
|
gr.Markdown.update(description, visible=True) |
|
) |
|
|
|
|
|
def run_evaluation(dataset_file, dataset_scope, dataset_scope_n, dataset_columns, methodology): |
|
|
|
status = {} |
|
|
|
dataset = check_csv(dataset_file.name) |
|
sample_method = dataset_scope |
|
col_name = dataset_columns |
|
num_sample_records = dataset_scope_n |
|
|
|
|
|
if methodology == "Term Identity Diversity Analysis": |
|
status = load_dataset_and_analyze_gender_tag( |
|
dataset, sample_method, col_name, num_sample_records) |
|
if methodology == "Gender Label Evaluation": |
|
status = load_dataset_and_analyze_gender_profession( |
|
dataset, sample_method, col_name, num_sample_records) |
|
if methodology == "Microsoft Genbit": |
|
status = load_dataset_and_get_genbit_metrics( |
|
dataset, sample_method, col_name, num_sample_records) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return gr.JSON.update(status, visible=True) |
|
|
|
if methodology == "A": |
|
run_a(dataset) |
|
elif methodology == "B": |
|
run_b(dataset) |
|
elif methodology == "C": |
|
run_c(dataset) |
|
|
|
|
|
def process_dataset(dataset): |
|
|
|
data = pd.read_csv(dataset.name) |
|
|
|
|
|
|
|
|
|
columns = data.columns.tolist() |
|
columns = [x for x in columns if data[x].dtype == "object"] |
|
|
|
return ( |
|
gr.Radio.update( |
|
label="Scope", |
|
info="Determines the scope of the dataset to be analyzed", |
|
choices=["First", "Last", "Random"], |
|
value="First", |
|
visible=True, |
|
interactive=True, |
|
), |
|
gr.Slider.update( |
|
label="Number of Entries", |
|
info=f"Determines the number of entries to be analyzed. The dataset has {data.shape[0]} entries.", |
|
minimum=1, |
|
maximum=data.shape[0], |
|
value=data.shape[0] // 2, |
|
visible=True, |
|
interactive=True |
|
), |
|
gr.Radio.update( |
|
label="Column", |
|
info="Determines the column to be analyzed. These are the columns with text data.", |
|
choices=columns, |
|
value=columns[0], |
|
visible=True, |
|
interactive=True, |
|
) |
|
) |
|
|
|
|
|
def process_column(dataset, column): |
|
data = pd.read_csv(dataset.name) |
|
corpus = data[column].to_list()[:10] |
|
|
|
return gr.Dataframe.update(value=pd.DataFrame({"Data Corpus": corpus}), max_rows=5, visible=True) |
|
|
|
|
|
BiasAware = gr.Blocks(title="BiasAware: Dataset Bias Detection") |
|
|
|
with BiasAware: |
|
gr.Markdown("# BiasAware: Dataset Bias Detection") |
|
gr.Markdown( |
|
"Natural Language Processing (NLP) training datasets often reflect the biases present in the data sources they are compiled from, leading to the **perpetuation of stereotypes, underrepresentation, and skewed perspectives in AI models**. BiasAware is designed to **identify and quantify biases present in text data**, making it an invaluable resource for data scientists, machine learning practitioners, and organizations committed to **mitigating bias in AI systems**." |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
gr.Markdown("## Dataset") |
|
|
|
dataset_file = gr.File() |
|
dataset_examples = gr.Examples( |
|
[ |
|
os.path.join(os.path.dirname(__file__), |
|
"data/z_animal.csv"), |
|
os.path.join(os.path.dirname(__file__), |
|
"data/z_employee.csv"), |
|
os.path.join(os.path.dirname( |
|
__file__), "data/z_house.csv"), |
|
|
|
], |
|
inputs=dataset_file, |
|
) |
|
|
|
dataset_scope = gr.Radio(visible=False) |
|
dataset_scope_n = gr.Slider(visible=False) |
|
dataset_columns = gr.Radio(visible=False) |
|
|
|
dataset_corpus = gr.Dataframe(visible=False) |
|
|
|
with gr.Column(scale=1): |
|
gr.Markdown("## Methodology") |
|
|
|
methodology = gr.Radio( |
|
label="Methodology", |
|
info="Determines the methodology to be used for bias detection", |
|
choices=[ |
|
"Term Identity Diversity Analysis", |
|
"Gender Label Evaluation", |
|
"Microsoft Genbit", |
|
], |
|
value="Term Identity Diversity Analysis", |
|
) |
|
|
|
evalButton = gr.Button("Run Evaluation") |
|
|
|
methodology_title = gr.Markdown(visible=False) |
|
methodology_description = gr.Markdown(visible=False) |
|
|
|
with gr.Column(scale=4): |
|
gr.Markdown("## Result") |
|
|
|
result_status = gr.JSON(visible=False) |
|
result = gr.DataFrame() |
|
|
|
dataset_file.change( |
|
fn=process_dataset, |
|
inputs=[dataset_file], |
|
outputs=[ |
|
dataset_scope, |
|
dataset_scope_n, |
|
dataset_columns |
|
] |
|
) |
|
|
|
dataset_columns.change( |
|
fn=process_column, |
|
inputs=[dataset_file, dataset_columns], |
|
outputs=[dataset_corpus], |
|
) |
|
|
|
methodology.change( |
|
fn=display_methodology, |
|
inputs=[methodology], |
|
outputs=[methodology_title, methodology_description], |
|
) |
|
|
|
evalButton.click( |
|
fn=run_evaluation, |
|
inputs=[dataset_file, dataset_scope, |
|
dataset_scope_n, dataset_columns, methodology], |
|
outputs=[result_status] |
|
) |
|
|
|
BiasAware.launch() |
|
|