DS-20202 commited on
Commit
17897fe
1 Parent(s): 6c8a2b2

auto-debias

Browse files
Files changed (2) hide show
  1. app.py +42 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ import gc
5
+
6
+ # Download models
7
+ bert_debiased = pipeline('fill-mask', model='Daniel-Saeedi/auto-debias-gender-bert-base-uncased')
8
+ bert_original = pipeline('fill-mask', model='bert-base-uncased')
9
+
10
+ albert_debiased = pipeline('fill-mask', model='Daniel-Saeedi/auto-debias-albert-base-v2-race')
11
+ albert_original = pipeline('fill-mask', model='albert-base-v2')
12
+
13
+ def make_result(unmask):
14
+ html = '<div><ol>'
15
+
16
+ for word in unmask:
17
+ html += '<li><b>{}</b> - Score: {}<li>'.format(word['token_str'],word['score'])
18
+
19
+ html += '</ol></div>'
20
+ return html
21
+
22
+
23
+
24
+ def fill_mask(stmt,model):
25
+ if model == 'bert-base-uncased-gender-debiased':
26
+ return "<h2>Debiased:</h2>" + make_result(bert_debiased(stmt)) + "<h2>Original:</h2>" + make_result(bert_original(stmt))
27
+ elif model == 'albert-race-debiased':
28
+ return "<h2>Debiased:</h2>" + make_result(albert_debiased(stmt)) + "<h2>Original:</h2>" + make_result(albert_original(stmt))
29
+
30
+
31
+ demo = gr.Interface(
32
+ fill_mask,
33
+ inputs = [
34
+ gr.Textbox(placeholder="Fill Mask"),
35
+ gr.Radio(choices=['bert-base-uncased-gender-debiased','albert-race-debiased'],value='bert-base-uncased-gender-debiased')
36
+ ],
37
+ outputs = [gr.Markdown(
38
+ value="<h3>Examples: </h3> <p>The woman works as [MASK].</p> <p>The black woman works as [MASK].</p>")],
39
+ description = '<a href="https://aclanthology.org/2022.acl-long.72/">Auto-Debias: Debiasing Masked Language Models with Automated Biased Prompts</a>'
40
+ )
41
+ if __name__ == '__main__':
42
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ tensorflow
3
+ torch
4
+ transformers