Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -272,19 +272,26 @@ def retrieveSentences(gr1, gr2, att1, att2, progress=gr.Progress()):
|
|
272 |
gr.update(value=', '.join(a2)) # att2_fixed
|
273 |
)
|
274 |
|
275 |
-
def bloombergViz(val, numblocks
|
276 |
-
percent = round(val * 100)
|
277 |
-
percentStr = f"{percent}"
|
278 |
-
filled = "<div style='height:20px;width:20px;background-color:#555;display:inline-block'></div> "
|
279 |
-
unfilled = "<div style='height:20px;width:20px;background-color:#999;display:inline-block'></div> "
|
280 |
-
numFilled = round((percent/100) * numblocks)
|
281 |
-
numUnFilled = numblocks - numFilled
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
attHTML = f"<div style='border-style:solid;border-color:#999;border-radius:12px'>{att}: {round(val*100)}%<br>{viz}</div><br>"
|
289 |
return attHTML
|
290 |
|
@@ -351,24 +358,8 @@ def startBiasTest(test_sentences_df, gr1, gr2, att1, att2, model_name, progress=
|
|
351 |
attrib_by_score = dict(sorted(per_attrib_bias.items(), key=lambda item: item[1], reverse=True))
|
352 |
|
353 |
# model_bias_HTML = bloombergViz(bias_stats_dict['model_bias'])
|
354 |
-
|
355 |
-
|
356 |
-
num_atts = 0
|
357 |
-
for att, score in attrib_by_score.items():
|
358 |
-
if att in attributes_g1:
|
359 |
-
per_attrib_bias_HTML_stereo += att_bloombergViz(att, score, att_freqs[att])
|
360 |
-
num_atts += 1
|
361 |
-
if num_atts >= 8:
|
362 |
-
break
|
363 |
-
|
364 |
-
per_attrib_bias_HTML_antistereo = ""
|
365 |
-
num_atts = 0
|
366 |
-
for att, score in attrib_by_score.items():
|
367 |
-
if att in attributes_g2:
|
368 |
-
per_attrib_bias_HTML_antistereo += att_bloombergViz(att, score, att_freqs[att], True)
|
369 |
-
num_atts += 1
|
370 |
-
if num_atts >= 8:
|
371 |
-
break
|
372 |
|
373 |
# bias score
|
374 |
#test_pairs_df['bias_score'] = 0
|
@@ -393,6 +384,26 @@ def startBiasTest(test_sentences_df, gr1, gr2, att1, att2, model_name, progress=
|
|
393 |
score_templates_df = score_templates_df[['Stereotyped','Attribute','Groups','Template']]
|
394 |
num_sentences = score_templates_df.shape[0]
|
395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
interpret_msg = bt_mgr._constructInterpretationMsg(bias_spec, num_sentences,
|
397 |
model_name, bias_stats_dict, per_attrib_bias,
|
398 |
score_templates_df
|
@@ -456,7 +467,10 @@ css_adds = "#group_row {background: white; border-color: white;} \
|
|
456 |
#res_label {background-color: #F8FAFC;} \
|
457 |
#per_attrib_label_elem {background-color: #F8FAFC;} \
|
458 |
#accordion {border-color: #E5E7EB} \
|
459 |
-
#err_msg_elem p {color: #FF0000; cursor: pointer}
|
|
|
|
|
|
|
460 |
|
461 |
#'bethecloud/storj_theme'
|
462 |
with gr.Blocks(theme=soft, title="Social Bias Testing in Language Models",
|
|
|
272 |
gr.update(value=', '.join(a2)) # att2_fixed
|
273 |
)
|
274 |
|
275 |
+
def bloombergViz(att, val, numblocks, score_templates_df, flip=False):
|
276 |
+
# percent = round(val * 100)
|
277 |
+
# percentStr = f"{percent}"
|
278 |
+
# filled = "<div style='height:20px;width:20px;background-color:#555;display:inline-block'><span class='tooltiptext' style='color:#FFF'>{}</span></div> "
|
279 |
+
# unfilled = "<div style='height:20px;width:20px;background-color:#999;display:inline-block'><span class='tooltiptext' style='color:#FFF'>{}</span></div> "
|
280 |
+
# numFilled = round((percent/100) * numblocks)
|
281 |
+
# numUnFilled = numblocks - numFilled
|
282 |
+
|
283 |
+
res = ""
|
284 |
+
for sentence in score_templates_df[score_templates_df['Attribute'] == att, score_templates_df['Stereotyped'] == 'yes', 'Template']:
|
285 |
+
res += f"<div style='height:20px;width:20px;background-color:#555;display:inline-block' id='filled'><span class='tooltiptext' style='color:#FFF'>{sentence}</span></div> "
|
286 |
+
for sentence in score_templates_df[score_templates_df['Attribute'] == att, score_templates_df['Stereotyped'] == 'no', 'Template']:
|
287 |
+
res += f"<div style='height:20px;width:20px;background-color:#999;display:inline-block' id='empty'><span class='tooltiptext' style='color:#FFF'>{sentence}</span></div> "
|
288 |
+
return res
|
289 |
+
# if flip:
|
290 |
+
# return numFilled * unfilled + numUnFilled * filled;
|
291 |
+
# return numFilled * filled + numUnFilled * unfilled
|
292 |
+
|
293 |
+
def att_bloombergViz(att, val, numblocks, score_templates_df, flip=False):
|
294 |
+
viz = bloombergViz(att, val, numblocks, score_templates_df, flip)
|
295 |
attHTML = f"<div style='border-style:solid;border-color:#999;border-radius:12px'>{att}: {round(val*100)}%<br>{viz}</div><br>"
|
296 |
return attHTML
|
297 |
|
|
|
358 |
attrib_by_score = dict(sorted(per_attrib_bias.items(), key=lambda item: item[1], reverse=True))
|
359 |
|
360 |
# model_bias_HTML = bloombergViz(bias_stats_dict['model_bias'])
|
361 |
+
print(f"attrib_by_score: {attrib_by_score}")
|
362 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
|
364 |
# bias score
|
365 |
#test_pairs_df['bias_score'] = 0
|
|
|
384 |
score_templates_df = score_templates_df[['Stereotyped','Attribute','Groups','Template']]
|
385 |
num_sentences = score_templates_df.shape[0]
|
386 |
|
387 |
+
print(f"score_templates_df: {score_templates_df}")
|
388 |
+
|
389 |
+
per_attrib_bias_HTML_stereo = ""
|
390 |
+
num_atts = 0
|
391 |
+
for att, score in attrib_by_score.items():
|
392 |
+
if att in attributes_g1:
|
393 |
+
per_attrib_bias_HTML_stereo += att_bloombergViz(att, score, att_freqs[att], score_templates_df, False)
|
394 |
+
num_atts += 1
|
395 |
+
if num_atts >= 8:
|
396 |
+
break
|
397 |
+
|
398 |
+
per_attrib_bias_HTML_antistereo = ""
|
399 |
+
num_atts = 0
|
400 |
+
for att, score in attrib_by_score.items():
|
401 |
+
if att in attributes_g2:
|
402 |
+
per_attrib_bias_HTML_antistereo += att_bloombergViz(att, score, att_freqs[att], score_templates_df, True)
|
403 |
+
num_atts += 1
|
404 |
+
if num_atts >= 8:
|
405 |
+
break
|
406 |
+
|
407 |
interpret_msg = bt_mgr._constructInterpretationMsg(bias_spec, num_sentences,
|
408 |
model_name, bias_stats_dict, per_attrib_bias,
|
409 |
score_templates_df
|
|
|
467 |
#res_label {background-color: #F8FAFC;} \
|
468 |
#per_attrib_label_elem {background-color: #F8FAFC;} \
|
469 |
#accordion {border-color: #E5E7EB} \
|
470 |
+
#err_msg_elem p {color: #FF0000; cursor: pointer} \
|
471 |
+
.tooltiptext {visibility: hidden;max-width:100ch;min-width:60ch;top: 100%;left: 50%;background-color: #222;text-align: center;border-radius: 6px;padding: 5px 0;position: absolute;z-index: 1;} \
|
472 |
+
#filled:hover .tooltiptext {visibility: visible;} \
|
473 |
+
#empty:hover .tooltiptext {visibility: visible;}"
|
474 |
|
475 |
#'bethecloud/storj_theme'
|
476 |
with gr.Blocks(theme=soft, title="Social Bias Testing in Language Models",
|