Spaces:
Sleeping
Sleeping
Martijn van Beers
commited on
Commit
•
99b20e7
1
Parent(s):
a80ec68
Add option to enter a pair manually
Browse files- app.py +30 -6
- descr-2.md +3 -0
- description.md +6 -2
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import torch
|
2 |
import datasets
|
3 |
import gradio
|
|
|
4 |
|
5 |
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
|
6 |
|
@@ -23,10 +24,9 @@ class CrowSPairsDataset(object):
|
|
23 |
return self.df.bias_type.unique().tolist()
|
24 |
|
25 |
|
26 |
-
def run(
|
27 |
-
sample = dataset.sample(bias_type)
|
28 |
result = "<table><tr style='color: white; background-color: #555'><th>index</th><th>more stereotypical</th><th>less stereotypical<th></tr>"
|
29 |
-
for i, row in
|
30 |
result += f"<tr><td>{i}</td>"
|
31 |
more = row["sent_more"]
|
32 |
|
@@ -55,6 +55,17 @@ def run(bias_type):
|
|
55 |
result += "</table>"
|
56 |
return result
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
if torch.cuda.is_available():
|
60 |
device = torch.device("cuda")
|
@@ -71,9 +82,12 @@ bias_type_sel = gradio.Dropdown(label="Bias Type", choices=dataset.bias_types())
|
|
71 |
with open("description.md") as fh:
|
72 |
desc = fh.read()
|
73 |
|
|
|
|
|
|
|
74 |
with open("notice.md") as fh:
|
75 |
notice = fh.read()
|
76 |
-
|
77 |
with open("results.md") as fh:
|
78 |
results = fh.read()
|
79 |
|
@@ -81,11 +95,21 @@ with gradio.Blocks(title="Detecting stereotypes in the GPT-2 language model usin
|
|
81 |
gradio.Markdown(desc)
|
82 |
with gradio.Row(equal_height=True):
|
83 |
with gradio.Column(scale=4):
|
84 |
-
|
85 |
with gradio.Column(scale=1):
|
86 |
but = gradio.Button("Sample")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
out = gradio.HTML()
|
88 |
-
but.click(
|
|
|
|
|
89 |
with gradio.Accordion("A note about explainability models"):
|
90 |
gradio.Markdown(notice)
|
91 |
with gradio.Accordion("Results for English and French BERT language models"):
|
|
|
1 |
import torch
|
2 |
import datasets
|
3 |
import gradio
|
4 |
+
import pandas
|
5 |
|
6 |
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
|
7 |
|
|
|
24 |
return self.df.bias_type.unique().tolist()
|
25 |
|
26 |
|
27 |
+
def run(df):
|
|
|
28 |
result = "<table><tr style='color: white; background-color: #555'><th>index</th><th>more stereotypical</th><th>less stereotypical<th></tr>"
|
29 |
+
for i, row in df.iterrows():
|
30 |
result += f"<tr><td>{i}</td>"
|
31 |
more = row["sent_more"]
|
32 |
|
|
|
55 |
result += "</table>"
|
56 |
return result
|
57 |
|
58 |
+
def sample_and_run(bias_type):
|
59 |
+
sample = dataset.sample(bias_type)
|
60 |
+
return run(sample)
|
61 |
+
|
62 |
+
def manual_run(more, less):
|
63 |
+
df = pandas.DataFrame.from_dict({
|
64 |
+
'sent_more': [more],
|
65 |
+
'sent_less': [less],
|
66 |
+
'bias_type': ["manual"],
|
67 |
+
})
|
68 |
+
return run(df)
|
69 |
|
70 |
if torch.cuda.is_available():
|
71 |
device = torch.device("cuda")
|
|
|
82 |
with open("description.md") as fh:
|
83 |
desc = fh.read()
|
84 |
|
85 |
+
with open("descr-2.md") as fh:
|
86 |
+
desc2 = fh.read()
|
87 |
+
|
88 |
with open("notice.md") as fh:
|
89 |
notice = fh.read()
|
90 |
+
|
91 |
with open("results.md") as fh:
|
92 |
results = fh.read()
|
93 |
|
|
|
95 |
gradio.Markdown(desc)
|
96 |
with gradio.Row(equal_height=True):
|
97 |
with gradio.Column(scale=4):
|
98 |
+
bias_sel = gradio.Dropdown(label="Bias Type", choices=dataset.bias_types())
|
99 |
with gradio.Column(scale=1):
|
100 |
but = gradio.Button("Sample")
|
101 |
+
gradio.Markdown(desc2)
|
102 |
+
with gradio.Row(equal_height=True):
|
103 |
+
with gradio.Column(scale=2):
|
104 |
+
more = gradio.Textbox(label="More stereotypical")
|
105 |
+
with gradio.Column(scale=2):
|
106 |
+
less = gradio.Textbox(label="Less stereotypical")
|
107 |
+
with gradio.Column(scale=1):
|
108 |
+
manual = gradio.Button("Run")
|
109 |
out = gradio.HTML()
|
110 |
+
but.click(sample_and_run, bias_sel, out)
|
111 |
+
manual.click(manual_run, [more, less], out)
|
112 |
+
|
113 |
with gradio.Accordion("A note about explainability models"):
|
114 |
gradio.Markdown(notice)
|
115 |
with gradio.Accordion("Results for English and French BERT language models"):
|
descr-2.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
Or you can enter a pair of sentences in the entries below, and click `Run` to get the result for your manual pair.
|
2 |
+
|
3 |
+
*The colors indicate whether the <font color=#00ffff>stereotypical</font> or the <font color=#ff00ff>less stereotypical</font> examples gets the higher score, the intensity of the color how strong the preference is.*
|
description.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
# Detecting stereotypes in the GPT-2 language model using CrowS-Pairs
|
2 |
|
3 |
-
*GPT-2* is a language model which can score how likely it is that some text is a valid English sentence: not only grammaticality, but also the 'meaning' of the sentence is part of this score. *CrowS-Pairs* is a dataset with pairs of more and less stereotypical examples for different social groups (e.g., gender and nationality stereotypes).
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
1 |
# Detecting stereotypes in the GPT-2 language model using CrowS-Pairs
|
2 |
|
3 |
+
*GPT-2* is a language model which can score how likely it is that some text is a valid English sentence: not only grammaticality, but also the 'meaning' of the sentence is part of this score. *CrowS-Pairs* is a dataset with pairs of more and less stereotypical examples for different social groups (e.g., gender and nationality stereotypes).
|
4 |
|
5 |
+
You can either select a CrowS-Pairs bias type from the drop-down below and click `Sample`, and then we
|
6 |
+
sample 10 random pairs from CrowS-Pairs and show whether the stereotypical example gets
|
7 |
+
a higher score ('is more likely').
|
8 |
+
|
9 |
+
**If GPT-2 systematically prefers the stereotypical examples, it has probably learnt these stereotypes from the training data.**
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
torch
|
2 |
transformers
|
3 |
datasets
|
|
|
|
1 |
torch
|
2 |
transformers
|
3 |
datasets
|
4 |
+
pandas
|