Spaces:
Runtime error
Runtime error
Emily McMilin
commited on
Commit
·
50cb70b
1
Parent(s):
874e98e
adding documenation/examples and cleaning imports
Browse files
app.py
CHANGED
@@ -1,15 +1,11 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
from typing import Optional
|
5 |
import gradio as gr
|
|
|
|
|
|
|
6 |
import torch
|
|
|
7 |
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
8 |
from transformers import pipeline
|
9 |
-
import pandas as pd
|
10 |
-
import numpy as np
|
11 |
-
import matplotlib.pyplot as plt
|
12 |
-
from matplotlib.ticker import MaxNLocator
|
13 |
|
14 |
|
15 |
# DATASETS
|
@@ -189,6 +185,7 @@ def get_gendered_token_ids(tokenizer):
|
|
189 |
male_gendered_token_ids.append(subword_man_token_id)
|
190 |
female_gendered_token_ids.append(subword_woman_token_id)
|
191 |
|
|
|
192 |
assert tokenizer.unk_token_id not in male_gendered_token_ids
|
193 |
assert tokenizer.unk_token_id not in female_gendered_token_ids
|
194 |
|
@@ -460,6 +457,87 @@ def predict_gender_pronouns(
|
|
460 |
)
|
461 |
|
462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
gr.Interface(
|
464 |
fn=predict_gender_pronouns,
|
465 |
inputs=[
|
@@ -510,4 +588,8 @@ gr.Interface(
|
|
510 |
label="Table of softmax probability pronouns predicted male",
|
511 |
),
|
512 |
],
|
|
|
|
|
|
|
|
|
513 |
).launch(debug=True, share=True)
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
import torch
|
6 |
+
from matplotlib.ticker import MaxNLocator
|
7 |
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
8 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
# DATASETS
|
|
|
185 |
male_gendered_token_ids.append(subword_man_token_id)
|
186 |
female_gendered_token_ids.append(subword_woman_token_id)
|
187 |
|
188 |
+
# Confirming all tokens are in vocab
|
189 |
assert tokenizer.unk_token_id not in male_gendered_token_ids
|
190 |
assert tokenizer.unk_token_id not in female_gendered_token_ids
|
191 |
|
|
|
457 |
)
|
458 |
|
459 |
|
460 |
+
title = "Causing Gender Pronouns"
|
461 |
+
description = """
|
462 |
+
## Intro
|
463 |
+
This work investigates how we can cause LLMs to change their gender pronoun predictions.
|
464 |
+
|
465 |
+
We do this by first considering plausible data generating processes for the type of datasets upon which the LLMs were pretrained. The data generating process is usually not revealed by the dataset alone, and instead requires (ideally well-informed) assumptions about what may have caused both the features and the labels to appear in the dataset.
|
466 |
+
|
467 |
+
An example of an assumed data generating process for the [wiki-bio dataset](https://huggingface.co/datasets/wiki_bio) is shown in the form of a causal DAG in [causing_gender_pronouns](https://huggingface.co/spaces/emilylearning/causing_gender_pronouns), an earlier but better documented version of this Space.
|
468 |
+
|
469 |
+
Once we have a causal DAG, we can identify likely confounding variables that have causal influences on both the features and the labels in a model. We can include those variables in our model train-time and/or at inference-time to produce spurious correlations, exposing potentially surprising learned relationships between the features and labels.
|
470 |
+
|
471 |
+
## This demo
|
472 |
+
Here we can experiment with these spurious correlations in both BERT and BERT-like pre-trained models as well as two types of fine-tuned models. These fine-tuned models were trained with a specific gender-pronoun-predicting task, and with potentially confounding metadata either excluded (`none_metadata` variants) or included (`birth_date_metadata` and `subreddit_metadata` variants) in the text samples at train time.
|
473 |
+
See [source code](https://github.com/2dot71mily/causing_gendering_pronouns_two) for more details.
|
474 |
+
|
475 |
+
For the gender-pronoun-predicting task, the following non-gender-neutral terms are `[MASKED]` for gender-prediction.
|
476 |
+
```
|
477 |
+
gendered_lists = [
|
478 |
+
['he', 'she'],
|
479 |
+
['him', 'her'],
|
480 |
+
['his', 'hers'],
|
481 |
+
["himself", "herself"],
|
482 |
+
['male', 'female'],
|
483 |
+
['man', 'woman'],
|
484 |
+
['men', 'women'],
|
485 |
+
["husband", "wife"],
|
486 |
+
['father', 'mother'],
|
487 |
+
['boyfriend', 'girlfriend'],
|
488 |
+
['brother', 'sister'],
|
489 |
+
["actor", "actress"],
|
490 |
+
["##man", "##woman"]]
|
491 |
+
```
|
492 |
+
|
493 |
+
What we are looking for in this demo is a dose-response relationship, where a larger intervention in the treatment (the text injected in the inference sample, displayed on the x-axis) produces a larger response in the output (the average softmax probability of a gendered pronoun, displayed on the y-axis).
|
494 |
+
|
495 |
+
For the `wiki-bio` models the x-axis is simply the `date`, ranging from 1800 - 1999, which is injected into the text. For the `reddit` models, it is the `subreddit` name, which is prepended to the inference text samples, with subreddits that have a larger percentage of self-reported female commentors increasing to the right (following the methodology in http://bburky.com/subredditgenderratios/, we just copied over the entire list of subreddits that had a Minimum subreddit size of 400,000).
|
496 |
+
|
497 |
+
|
498 |
+
## What you can do:
|
499 |
+
|
500 |
+
- Pick a fine-tuned model type.
|
501 |
+
- Pick optional BERT, and/or BERT-like model.
|
502 |
+
- Decide if you want to see BERT-like model’s predictions normalized to only those predictions that are gendered (ignoring their gender-neutral predictions).
|
503 |
+
- Note, DistilBERT in particular does a great job at predicting gender-neutral terms, so this normalization can look pretty noisy.
|
504 |
+
- This normalization is not required for our fine-tuned models, which are forced to make a binary prediction.
|
505 |
+
- Decide if you want to see the baseline prediction (from neutral or no text injection into your text sample) in the plot.
|
506 |
+
- Come up with a text sample!
|
507 |
+
- Any term included that is from the `gendered_lists` above will be masked out for prediction.
|
508 |
+
- In the case of `wiki-bio`, any appearance of the word `DATE` will be replaced with the year shown on the x-axis.
|
509 |
+
- If no `DATE` is included, the phrase `Born in DATE…` will be prepended to your text sample.
|
510 |
+
- In the case of `reddit`, the `subreddit` names shown on the x-axis (or shown more clearly in the associated dataframe) will be prepended to your text sample).
|
511 |
+
"""
|
512 |
+
|
513 |
+
article = "The source code to generate the fine-tuned models can be found/reproduced here: https://github.com/2dot71mily/causing_gendering_pronouns_two"
|
514 |
+
|
515 |
+
scientist_example = [
|
516 |
+
REDDIT,
|
517 |
+
[BERT_LIKE_MODELS[0]],
|
518 |
+
"True",
|
519 |
+
"True",
|
520 |
+
'She was a very well regarded scientist and her work won many awards.',
|
521 |
+
]
|
522 |
+
|
523 |
+
death_date_example = [
|
524 |
+
WIKIBIO,
|
525 |
+
[BERT_LIKE_MODELS],
|
526 |
+
"False",
|
527 |
+
"True",
|
528 |
+
'Died in DATE, she was recognized for her great accomplishments to the field of teaching.'
|
529 |
+
]
|
530 |
+
|
531 |
+
|
532 |
+
neg_reddit_example = [
|
533 |
+
REDDIT,
|
534 |
+
[BERT_LIKE_MODELS[0]],
|
535 |
+
"False",
|
536 |
+
"True",
|
537 |
+
'She is not good at anything. The work she does is always subpar.'
|
538 |
+
]
|
539 |
+
|
540 |
+
|
541 |
gr.Interface(
|
542 |
fn=predict_gender_pronouns,
|
543 |
inputs=[
|
|
|
588 |
label="Table of softmax probability pronouns predicted male",
|
589 |
),
|
590 |
],
|
591 |
+
title=title,
|
592 |
+
description=description,
|
593 |
+
article=article,
|
594 |
+
examples=[scientist_example, death_date_example, neg_reddit_example]
|
595 |
).launch(debug=True, share=True)
|