Spaces:
Runtime error
Runtime error
Emily McMilin
commited on
Commit
·
d4b589f
1
Parent(s):
55392f1
fix bug causing add-own-model to fail
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
# %%
|
| 2 |
-
# from http.client import TEMPORARY_REDIRECT
|
| 3 |
import gradio as gr
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import numpy as np
|
|
@@ -9,18 +8,19 @@ from matplotlib.ticker import MaxNLocator
|
|
| 9 |
from transformers import pipeline
|
| 10 |
from winogender_sentences import get_sentences
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
MODEL_NAME_DICT = {
|
| 13 |
"roberta-large": "RoBERTa-large",
|
| 14 |
"bert-large-uncased": "BERT-large",
|
| 15 |
"roberta-base": "RoBERTa-base",
|
| 16 |
"bert-base-uncased": "BERT-base",
|
|
|
|
| 17 |
}
|
| 18 |
MODEL_NAMES = list(MODEL_NAME_DICT.keys())
|
| 19 |
|
| 20 |
|
| 21 |
-
OWN_MODEL_NAME = 'add-a-model'
|
| 22 |
-
PICK_YOUR_OWN_LABEL = 'pick-your-own'
|
| 23 |
-
|
| 24 |
DECIMAL_PLACES = 1
|
| 25 |
EPS = 1e-5 # to avoid /0 errors
|
| 26 |
NUM_PTS_TO_AVERAGE = 2
|
|
@@ -33,7 +33,6 @@ NUM_PTS = 30
|
|
| 33 |
DATES = np.linspace(START_YEAR, STOP_YEAR, NUM_PTS).astype(int).tolist()
|
| 34 |
DATES = [f'{d}' for d in DATES]
|
| 35 |
|
| 36 |
-
|
| 37 |
GENDERED_LIST = [
|
| 38 |
['he', 'she'],
|
| 39 |
['him', 'her'],
|
|
@@ -52,10 +51,7 @@ GENDERED_LIST = [
|
|
| 52 |
|
| 53 |
# %%
|
| 54 |
# Fire up the models
|
| 55 |
-
models =
|
| 56 |
-
|
| 57 |
-
for bert_like in MODEL_NAMES:
|
| 58 |
-
models[bert_like] = pipeline("fill-mask", model=bert_like)
|
| 59 |
|
| 60 |
# %%
|
| 61 |
# Get the winogender sentences
|
|
@@ -64,7 +60,6 @@ occs = sorted(list({sentence_id.split('_')[0]
|
|
| 64 |
for sentence_id in winogender_sentences}))
|
| 65 |
|
| 66 |
# %%
|
| 67 |
-
|
| 68 |
def get_gendered_token_ids():
|
| 69 |
male_gendered_tokens = [list[0] for list in GENDERED_LIST]
|
| 70 |
female_gendered_tokens = [list[1] for list in GENDERED_LIST]
|
|
@@ -112,8 +107,7 @@ def get_figure(df, model_name, occ):
|
|
| 112 |
ax.axis('tight')
|
| 113 |
ax.set_xlabel("Sentence number")
|
| 114 |
ax.set_ylabel("Uncertainty metric")
|
| 115 |
-
ax.set_title(
|
| 116 |
-
f"{MODEL_NAME_DICT[model_name]} gender pronoun uncertainty in '{occ}' sentences")
|
| 117 |
return fig
|
| 118 |
|
| 119 |
|
|
@@ -137,7 +131,7 @@ def predict_gender_pronouns(
|
|
| 137 |
if model_name is None or model_name == '':
|
| 138 |
model_name = MODEL_NAMES[0]
|
| 139 |
model = models[model_name]
|
| 140 |
-
elif model_name
|
| 141 |
model = pipeline("fill-mask", model=own_model_name)
|
| 142 |
else:
|
| 143 |
model = models[model_name]
|
|
@@ -200,11 +194,12 @@ def predict_gender_pronouns(
|
|
| 200 |
|
| 201 |
uncertain_df = uncertain_df.reset_index().rename(
|
| 202 |
columns={'index': 'Sentence number'})
|
|
|
|
| 203 |
return (
|
|
|
|
| 204 |
uncertain_df,
|
| 205 |
get_figure(uncertain_df, model_name, occ),
|
| 206 |
)
|
| 207 |
-
# %%
|
| 208 |
|
| 209 |
|
| 210 |
demo = gr.Blocks()
|
|
@@ -227,13 +222,13 @@ with demo:
|
|
| 227 |
|
| 228 |
with gr.Row():
|
| 229 |
model_name = gr.Radio(
|
| 230 |
-
MODEL_NAMES
|
| 231 |
type="value",
|
| 232 |
-
label="Pick a preloaded BERT-like model for uncertainty evaluation (note: BERT-base performance least
|
| 233 |
)
|
| 234 |
own_model_name = gr.Textbox(
|
| 235 |
label=f"...Or, if you selected an '{OWN_MODEL_NAME}' model, put any Hugging Face pipeline model name \
|
| 236 |
-
(that supports the
|
| 237 |
)
|
| 238 |
|
| 239 |
with gr.Row():
|
|
@@ -269,12 +264,15 @@ with demo:
|
|
| 269 |
female_fig = gr.Plot(type="auto")
|
| 270 |
with gr.Row():
|
| 271 |
female_df = gr.Dataframe()
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
uncertain_btn.click(
|
| 274 |
fn=predict_gender_pronouns,
|
| 275 |
inputs=[model_name, own_model_name, input_texts, occ_box],
|
| 276 |
# inputs=date_example,
|
| 277 |
-
outputs=[female_df, female_fig]
|
| 278 |
)
|
| 279 |
|
| 280 |
demo.launch(debug=True)
|
|
|
|
| 1 |
# %%
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
|
|
|
| 8 |
from transformers import pipeline
|
| 9 |
from winogender_sentences import get_sentences
|
| 10 |
|
| 11 |
+
OWN_MODEL_NAME = 'add-a-model'
|
| 12 |
+
PICK_YOUR_OWN_LABEL = 'pick-your-own'
|
| 13 |
+
|
| 14 |
MODEL_NAME_DICT = {
|
| 15 |
"roberta-large": "RoBERTa-large",
|
| 16 |
"bert-large-uncased": "BERT-large",
|
| 17 |
"roberta-base": "RoBERTa-base",
|
| 18 |
"bert-base-uncased": "BERT-base",
|
| 19 |
+
OWN_MODEL_NAME: "Your model's"
|
| 20 |
}
|
| 21 |
MODEL_NAMES = list(MODEL_NAME_DICT.keys())
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
DECIMAL_PLACES = 1
|
| 25 |
EPS = 1e-5 # to avoid /0 errors
|
| 26 |
NUM_PTS_TO_AVERAGE = 2
|
|
|
|
| 33 |
DATES = np.linspace(START_YEAR, STOP_YEAR, NUM_PTS).astype(int).tolist()
|
| 34 |
DATES = [f'{d}' for d in DATES]
|
| 35 |
|
|
|
|
| 36 |
GENDERED_LIST = [
|
| 37 |
['he', 'she'],
|
| 38 |
['him', 'her'],
|
|
|
|
| 51 |
|
| 52 |
# %%
|
| 53 |
# Fire up the models
|
| 54 |
+
models = {m : pipeline("fill-mask", model=m) for m in MODEL_NAMES if m != OWN_MODEL_NAME}
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# %%
|
| 57 |
# Get the winogender sentences
|
|
|
|
| 60 |
for sentence_id in winogender_sentences}))
|
| 61 |
|
| 62 |
# %%
|
|
|
|
| 63 |
def get_gendered_token_ids():
|
| 64 |
male_gendered_tokens = [list[0] for list in GENDERED_LIST]
|
| 65 |
female_gendered_tokens = [list[1] for list in GENDERED_LIST]
|
|
|
|
| 107 |
ax.axis('tight')
|
| 108 |
ax.set_xlabel("Sentence number")
|
| 109 |
ax.set_ylabel("Uncertainty metric")
|
| 110 |
+
ax.set_title(f"{MODEL_NAME_DICT[model_name]} gender pronoun uncertainty in '{occ}' sentences")
|
|
|
|
| 111 |
return fig
|
| 112 |
|
| 113 |
|
|
|
|
| 131 |
if model_name is None or model_name == '':
|
| 132 |
model_name = MODEL_NAMES[0]
|
| 133 |
model = models[model_name]
|
| 134 |
+
elif model_name == OWN_MODEL_NAME:
|
| 135 |
model = pipeline("fill-mask", model=own_model_name)
|
| 136 |
else:
|
| 137 |
model = models[model_name]
|
|
|
|
| 194 |
|
| 195 |
uncertain_df = uncertain_df.reset_index().rename(
|
| 196 |
columns={'index': 'Sentence number'})
|
| 197 |
+
|
| 198 |
return (
|
| 199 |
+
target_text,
|
| 200 |
uncertain_df,
|
| 201 |
get_figure(uncertain_df, model_name, occ),
|
| 202 |
)
|
|
|
|
| 203 |
|
| 204 |
|
| 205 |
demo = gr.Blocks()
|
|
|
|
| 222 |
|
| 223 |
with gr.Row():
|
| 224 |
model_name = gr.Radio(
|
| 225 |
+
MODEL_NAMES,
|
| 226 |
type="value",
|
| 227 |
+
label="Pick a preloaded BERT-like model for uncertainty evaluation (note: BERT-base performance least consistent)...",
|
| 228 |
)
|
| 229 |
own_model_name = gr.Textbox(
|
| 230 |
label=f"...Or, if you selected an '{OWN_MODEL_NAME}' model, put any Hugging Face pipeline model name \
|
| 231 |
+
(that supports the `fill-mask` task (see list at https://huggingface.co/models?pipeline_tag=fill-mask).",
|
| 232 |
)
|
| 233 |
|
| 234 |
with gr.Row():
|
|
|
|
| 264 |
female_fig = gr.Plot(type="auto")
|
| 265 |
with gr.Row():
|
| 266 |
female_df = gr.Dataframe()
|
| 267 |
+
with gr.Row():
|
| 268 |
+
display_text = gr.Textbox(
|
| 269 |
+
type="auto", label="Sample of text fed to model")
|
| 270 |
|
| 271 |
uncertain_btn.click(
|
| 272 |
fn=predict_gender_pronouns,
|
| 273 |
inputs=[model_name, own_model_name, input_texts, occ_box],
|
| 274 |
# inputs=date_example,
|
| 275 |
+
outputs=[display_text, female_df, female_fig]
|
| 276 |
)
|
| 277 |
|
| 278 |
demo.launch(debug=True)
|