Oskar van der Wal commited on
Commit
97a1414
1 Parent(s): 6e551eb

First initial commit

Browse files
Files changed (6) hide show
  1. README.md +6 -7
  2. app.py +148 -0
  3. contrastive_pair.md +1 -0
  4. description.md +9 -0
  5. notice.md +8 -0
  6. simple_translation.md +5 -0
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
- title: MT Bias Demo
3
- emoji: 💩
4
- colorFrom: green
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.16.0
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Bias in MT
3
+ emoji: 🌍
4
+ colorFrom: yellow
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.3
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ A demo showing how gender bias could manifest in MT models when translating from Hungarian to English.
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio
2
+ import inseq
3
+ from inseq.data.aggregator import AggregatorPipeline, SubwordAggregator, SequenceAttributionAggregator, PairAggregator
4
+ import torch
5
+
6
+ if torch.cuda.is_available():
7
+ DEVICE = "cuda"
8
+ else:
9
+ DEVICE = "cpu"
10
+
11
+ def swap_pronoun(sentence):
12
+ if "He" in sentence:
13
+ return sentence.replace("He", "She")
14
+ elif "She" in sentence:
15
+ return sentence.replace("She", "He")
16
+ else:
17
+ return sentence
18
+
19
+ def run_counterfactual(occupation):
20
+ occupation = occupation.split(" (")[0]
21
+
22
+ model_name = f"Helsinki-NLP/opus-mt-hu-en"
23
+
24
+ # "egy" means something like "a", but is used less frequently than in English.
25
+ #source = f"Ő egy {occupation}."
26
+ source = f"Ő {occupation}."
27
+
28
+ model = inseq.load_model(model_name, "integrated_gradients")
29
+ model.device = DEVICE
30
+ target = model.generate(source)[0]
31
+ #target_modified = swap_pronoun(target)
32
+
33
+ out = model.attribute(
34
+ [
35
+ source,
36
+ source,
37
+ ],
38
+ [
39
+ #target,
40
+ #target_modified,
41
+ target.replace("She", "He"),
42
+ target.replace("He", "She"),
43
+ ],
44
+ n_steps=150,
45
+ return_convergence_delta=False,
46
+ attribute_target=False,
47
+ step_scores=["probability"],
48
+ internal_batch_size=100,
49
+ include_eos_baseline=False,
50
+ device=DEVICE,
51
+ )
52
+ #out = model.attribute(source, attribute_target=False, n_steps=150, device=DEVICE, return_convergence_delta=False, step_scores=["probability"])
53
+
54
+ squeezesum = AggregatorPipeline([SubwordAggregator, SequenceAttributionAggregator])
55
+ masculine = out.sequence_attributions[0].aggregate(aggregator=squeezesum)
56
+ feminine = out.sequence_attributions[1].aggregate(aggregator=squeezesum)
57
+
58
+ return masculine.show(aggregator=PairAggregator, paired_attr=feminine, return_html=True, display=True)
59
+ #return out.show(return_html=True, display=True)
60
+
61
+ def run_simple(occupation, lang, aggregate):
62
+ occupation = occupation.split(" (")[0]
63
+
64
+ model_name = f"Helsinki-NLP/opus-mt-hu-{lang}"
65
+
66
+ # "egy" means something like "a", but is used less frequently than in English.
67
+ #source = f"Ő egy {occupation}."
68
+ source = f"Ő {occupation}."
69
+
70
+ model = inseq.load_model(model_name, "integrated_gradients")
71
+ out = model.attribute([source], attribute_target=True, n_steps=150, device=DEVICE, return_convergence_delta=False)
72
+
73
+ if aggregate:
74
+ squeezesum = AggregatorPipeline([SubwordAggregator, SequenceAttributionAggregator])
75
+ return out.show(return_html=True, display=True, aggregator=squeezesum)
76
+ else:
77
+ return out.show(return_html=True, display=True)
78
+
79
+ with open("description.md") as fh:
80
+ desc = fh.read()
81
+
82
+ with open("simple_translation.md") as fh:
83
+ simple_translation = fh.read()
84
+
85
+ with open("contrastive_pair.md") as fh:
86
+ contrastive_pair = fh.read()
87
+
88
+ with open("notice.md") as fh:
89
+ notice = fh.read()
90
+
91
+ OCCUPATIONS = [
92
+ "nő (woman)",
93
+ "férfi (man)",
94
+ "nővér (nurse)",
95
+ "tudós (scientist)",
96
+ "mérnök (engineer)",
97
+ "pék (baker)",
98
+ "tanár (teacher)",
99
+ "esküvőszervező (wedding organizer)",
100
+ "vezérigazgató (CEO)",
101
+ ]
102
+
103
+ LANGS = [
104
+ "en",
105
+ "fr",
106
+ "de",
107
+ ]
108
+
109
+ with gradio.Blocks(title="Gender Bias in MT: Hungarian to English") as iface:
110
+ gradio.Markdown(desc)
111
+
112
+ print(simple_translation)
113
+ with gradio.Accordion("Simple translation", open=True):
114
+ gradio.Markdown(simple_translation)
115
+
116
+ with gradio.Accordion("Contrastive pair", open=False):
117
+ gradio.Markdown(contrastive_pair)
118
+
119
+ gradio.Markdown("**Does the model seem to rely on gender stereotypes in its translations?**")
120
+
121
+ with gradio.Tab("Simple translation"):
122
+ with gradio.Row(equal_height=True):
123
+ with gradio.Column(scale=4):
124
+ occupation_sel = gradio.Dropdown(label="Occupation", choices=OCCUPATIONS, value=OCCUPATIONS[0])
125
+ with gradio.Column(scale=4):
126
+ target_lang = gradio.Dropdown(label="Target Language", choices=LANGS, value=LANGS[0])
127
+ aggregate_subwords = gradio.Radio(
128
+ ["yes", "no"], label="Aggregate subwords?", value="yes"
129
+ )
130
+ but = gradio.Button("Translate & Attribute")
131
+ out = gradio.HTML()
132
+ args = [occupation_sel, target_lang, aggregate_subwords]
133
+ but.click(run_simple, inputs=args, outputs=out)
134
+
135
+ with gradio.Tab("Contrastive pair"):
136
+ with gradio.Row(equal_height=True):
137
+ with gradio.Column(scale=4):
138
+ occupation_sel = gradio.Dropdown(label="Occupation", choices=OCCUPATIONS, value=OCCUPATIONS[0])
139
+ but = gradio.Button("Translate & Attribute")
140
+ out = gradio.HTML()
141
+ args = [occupation_sel]
142
+ but.click(run_counterfactual, inputs=args, outputs=out)
143
+
144
+ with gradio.Accordion("Notes & References", open=False):
145
+ gradio.Markdown(notice)
146
+
147
+
148
+ iface.launch()
contrastive_pair.md ADDED
@@ -0,0 +1 @@
 
 
1
+ This example is very similar to the **Simple translation** example, but now we ask how the model's behaviour would change if we change the translation of “ő” from “he” to “she”? The `probability` row at the bottom shows the difference in the probability between both versions of the translation.
description.md ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Gender Bias in MT: Hungarian to English
2
+
3
+ The Hungarian language has no grammatical gender and words like “he” and “she” are both translated as “ő”.
4
+ This makes it an interesting language to study gender bias in machine translation (MT) models, when translating to another language that does distinguish between “he” and “she”.
5
+ In this demo, we will test the OPUS-MT models (Tiedemann & Thottingal, 2020) from the *Language Technology Research Group at the University of Helsinki* ([Helsinki-NLP](https://github.com/Helsinki-NLP)).
6
+
7
+ For each translation, we also use the [Inseq library](https://github.com/inseq-team/inseq) to compute the feature attributions with integrated gradients: How important is each token in the source (Hungarian) for the translation of the target tokens (English)?
8
+
9
+ ⚠️ Please note that this demo is just an illustration of how gender bias could manifest in MT models, but an actual assessment of its bias requires a more rigourous experiment.
notice.md ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ The idea for testing the gender bias in translations from Hungarian to English comes from Farkas and Németh (2022).
2
+
3
+ ### References:
4
+ [Inseq: Intepretability for Sequence Generation Models 🔍](https://github.com/inseq-team/inseq). GitHub.
5
+
6
+ Tiedemann, J., & Thottingal, S. (2020). [OPUS-MT — Building open translation services for the World](https://helda.helsinki.fi/handle/10138/327852). Proceedings of the 22nd Annual Conferenec of the European Association for Machine Translation (EAMT).
7
+
8
+ Farkas, A., & Németh, R. (2022). [How to measure gender bias in machine translation: Real-world oriented machine translators, multiple reference points](https://www.sciencedirect.com/science/article/pii/S2590291121001352). Social Sciences & Humanities Open, 5(1), 100239.
simple_translation.md ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Select an occupation (or the word “woman”/“man”) from the dropdown menu and press `Translate & Attribute` to translate a sentence like “He/She is a nurse” from Hungarian:
2
+
3
+ > Ő nővér.
4
+
5
+ Which pronouns (“she”/“he”) do the MT models go for? Does it change depending on the occupation term you choose? And can we find a difference between the target languages (you can change it in the other dropdown menu on the right)?