rjiang12 commited on
Commit
15ec59c
·
1 Parent(s): 5a53be5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -43
app.py CHANGED
@@ -272,26 +272,19 @@ def retrieveSentences(gr1, gr2, att1, att2, progress=gr.Progress()):
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
 
@@ -359,7 +352,23 @@ def startBiasTest(test_sentences_df, gr1, gr2, att1, att2, model_name, progress=
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
@@ -386,24 +395,6 @@ def startBiasTest(test_sentences_df, gr1, gr2, att1, att2, model_name, progress=
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,10 +458,7 @@ css_adds = "#group_row {background: white; border-color: white;} \
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",
 
272
  gr.update(value=', '.join(a2)) # att2_fixed
273
  )
274
 
275
+ def bloombergViz(val, numblocks=10, 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'></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
+ if flip:
283
+ return numFilled * unfilled + numUnFilled * filled;
284
+ return numFilled * filled + numUnFilled * unfilled
285
+
286
+ def att_bloombergViz(att, val, numblocks, flip=False):
287
+ viz = bloombergViz(val, numblocks, flip)
 
 
 
 
 
 
 
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
 
 
352
 
353
  # model_bias_HTML = bloombergViz(bias_stats_dict['model_bias'])
354
  print(f"attrib_by_score: {attrib_by_score}")
355
+ per_attrib_bias_HTML_stereo = ""
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
 
395
 
396
  print(f"score_templates_df: {score_templates_df}")
397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
  interpret_msg = bt_mgr._constructInterpretationMsg(bias_spec, num_sentences,
399
  model_name, bias_stats_dict, per_attrib_bias,
400
  score_templates_df
 
458
  #res_label {background-color: #F8FAFC;} \
459
  #per_attrib_label_elem {background-color: #F8FAFC;} \
460
  #accordion {border-color: #E5E7EB} \
461
+ #err_msg_elem p {color: #FF0000; cursor: pointer} "
 
 
 
462
 
463
  #'bethecloud/storj_theme'
464
  with gr.Blocks(theme=soft, title="Social Bias Testing in Language Models",