File size: 6,702 Bytes
0321f34 da9c0a0 5da6a2b 6fc6046 0321f34 e0db39e 0946447 da9c0a0 e0db39e 5da6a2b 6d2d9db 38ba037 0321f34 e0db39e 6d2d9db 0321f34 e0db39e 38ba037 0946447 5da6a2b 0946447 e0db39e 0946447 e0db39e 6d2d9db e0db39e 6d2d9db e0db39e 6d2d9db e0db39e 6d2d9db e0db39e 6d2d9db a365da6 6d2d9db 5da6a2b e0db39e 38ba037 e0db39e 38ba037 e0db39e 0321f34 38ba037 da9c0a0 6fc6046 5da6a2b 6d2d9db 5da6a2b da9c0a0 0321f34 6fc6046 da9c0a0 e0db39e 38ba037 6fc6046 6d2d9db 0321f34 e0db39e 6fc6046 38ba037 0321f34 da9c0a0 e0db39e 0321f34 38ba037 0321f34 38ba037 0321f34 38ba037 6fc6046 5da6a2b 38ba037 e0db39e 5da6a2b da9c0a0 e0db39e da9c0a0 0321f34 5da6a2b 6fc6046 0946447 5da6a2b 0946447 5da6a2b 38ba037 e0db39e 38ba037 e0db39e 38ba037 5da6a2b 0321f34 e0db39e 0321f34 38ba037 6fc6046 da9c0a0 0321f34 da9c0a0 e0db39e da9c0a0 38ba037 0321f34 e0db39e 0321f34 0946447 38ba037 da9c0a0 6fc6046 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import json
import gradio as gr
import pandas as pd
import os
from scripts.genbit import *
from scripts.gender_profession_bias import *
from scripts.gender_distribution import *
methodologies = json.load(open("config/methodologies.json", "r"))
MAX_THRESHOLD = 5000
DATASET_CACHE = {}
def evaluate(dataset, sampling_method, sampling_size, column, methodology):
try:
print(
f"[{dataset.name.split('/')[-1]}::{column}] - {sampling_method} {sampling_size} entries"
)
data = DATASET_CACHE.setdefault(dataset.name, pd.read_csv(dataset.name))[
[column]
]
if sampling_method == "First":
data = data.head(sampling_size)
elif sampling_method == "Last":
data = data.tail(sampling_size)
elif sampling_method == "Random":
data = data.sample(n=sampling_size, random_state=42)
result_df, result_plot, result_conclusion = globals()[
methodologies.get(methodology).get("fx")
](data)
return (
gr.Markdown.update(
f"## {methodology} Results\nResult Summary", visible=True
),
gr.Plot.update(result_plot, visible=True),
gr.Dataframe.update(result_df, visible=True),
)
except Exception as e:
return (
gr.Markdown.update(visible=False),
gr.Plot.update(visible=False),
gr.Dataframe.update(visible=False),
)
def display_dataset_config(dataset):
try:
data = DATASET_CACHE.setdefault(dataset.name, pd.read_csv(dataset.name))
columns = data.select_dtypes(include=["object"]).columns.tolist()
corpus = data[columns[0]].tolist()[0:5]
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=f"Number of Entries",
info=f"Determines the number of entries to be analyzed. Due to computational constraints, the maximum number of entries that can be analyzed is {MAX_THRESHOLD}.",
minimum=1,
maximum=min(data.shape[0], MAX_THRESHOLD),
value=min(data.shape[0], MAX_THRESHOLD),
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,
),
gr.DataFrame.update(
value=pd.DataFrame({f"{columns[0]}": corpus}), visible=True
),
)
except:
return (
gr.Radio.update(visible=False),
gr.Slider.update(visible=False),
gr.Radio.update(visible=False),
gr.DataFrame.update(visible=False),
)
def update_column_metadata(dataset, column):
data = DATASET_CACHE.setdefault(dataset.name, pd.read_csv(dataset.name))
corpus = data[column].tolist()[0:5]
return gr.Dataframe.update(value=pd.DataFrame({f"{column}": corpus}), visible=True)
def get_methodology_metadata(methodology):
title = "## " + methodology
description = methodologies.get(methodology).get("description")
metadata = f"{title}\n\n{description}"
return (
gr.Markdown.update(metadata, visible=True),
gr.Button.update(interactive=True, visible=True),
)
BiasAware = gr.Blocks(title="BiasAware: Dataset Bias Detection")
with BiasAware:
gr.Markdown(
"""
# BiasAware: Dataset Bias Detection
BiasAware is a specialized tool for detecting and quantifying biases within datasets used for Natural Language Processing (NLP) tasks. NLP training datasets frequently mirror the inherent biases of their source materials, resulting in AI models that unintentionally perpetuate stereotypes, exhibit underrepresentation, and showcase skewed perspectives.
"""
)
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## Dataset")
dataset_file = gr.File(label="Dataset", file_types=["csv"])
dataset_examples = gr.Examples(
[
os.path.join(os.path.dirname(__file__), "data/imdb_100.csv"),
os.path.join(os.path.dirname(__file__), "data/z_employee.csv"),
os.path.join(os.path.dirname(__file__), "data/z_sentences.csv"),
],
inputs=dataset_file,
label="Example Datasets",
)
dataset_sampling_method = gr.Radio(visible=False)
dataset_sampling_size = gr.Slider(visible=False)
dataset_column = gr.Radio(visible=False)
dataset_corpus = gr.Dataframe(
row_count=(5, "fixed"), col_count=(1, "fixed"), visible=False
)
with gr.Column(scale=2):
gr.Markdown("## Methodology")
methodology = gr.Radio(
label="Methodology",
info="Determines the methodology to be used for bias detection",
choices=methodologies.keys(),
)
evalButton = gr.Button(value="Run Evaluation", interactive=False)
methodology_metadata = gr.Markdown(visible=False)
with gr.Column(scale=4):
result = gr.Markdown("## Result")
result_plot = gr.Plot(show_label=False, container=False, visible=False)
result_df = gr.DataFrame(visible=False)
dataset_file.change(
fn=display_dataset_config,
inputs=[dataset_file],
outputs=[
dataset_sampling_method,
dataset_sampling_size,
dataset_column,
dataset_corpus,
],
)
dataset_column.change(
fn=update_column_metadata,
inputs=[dataset_file, dataset_column],
outputs=[dataset_corpus],
)
methodology.change(
fn=get_methodology_metadata,
inputs=[methodology],
outputs=[methodology_metadata, evalButton],
)
evalButton.click(
fn=evaluate,
inputs=[
dataset_file,
dataset_sampling_method,
dataset_sampling_size,
dataset_column,
methodology,
],
outputs=[result, result_plot, result_df],
)
BiasAware.launch()
|