meg-huggingface commited on
Commit
7adeb33
·
1 Parent(s): c6ba6c5

Adding summaries

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  from PIL import Image
5
  import plotly.graph_objects as go
6
  import plotly.express as px
 
7
 
8
  TITLE = "Diffusion Faces Cluster Explorer"
9
  clusters_12 = json.load(open("clusters/id_all_blip_clusters_12.json"))
@@ -16,6 +17,17 @@ clusters_by_size = {
16
  48: clusters_48,
17
  }
18
 
 
 
 
 
 
 
 
 
 
 
 
19
  def show_cluster(cl_id, num_clusters):
20
  if not cl_id:
21
  cl_id = 0
@@ -29,16 +41,20 @@ def show_cluster(cl_id, num_clusters):
29
  model_fig = go.Figure()
30
  model_fig.add_trace(go.Pie(labels=list(dict(cl_dct["labels_model"]).keys()),
31
  values=list(dict(cl_dct["labels_model"]).values())))
 
 
32
  gender_fig = go.Figure()
33
  gender_fig.add_trace(go.Pie(labels=list(dict(cl_dct["labels_gender"]).keys()),
34
  values=list(dict(cl_dct["labels_gender"]).values())))
 
 
35
  ethnicity_fig = go.Figure()
36
  ethnicity_fig.add_trace(go.Bar(x=list(dict(cl_dct["labels_ethnicity"]).keys()),
37
  y=list(dict(cl_dct["labels_ethnicity"]).values()),
38
  marker_color=px.colors.qualitative.G10))
39
  return (len(cl_dct['img_path_list']),
40
- gender_fig,
41
- model_fig,
42
  ethnicity_fig,
43
  images)
44
 
@@ -46,8 +62,9 @@ with gr.Blocks(title=TITLE) as demo:
46
  gr.Markdown(f"# {TITLE}")
47
  gr.Markdown("## This Space lets you explore the data generated from [DiffusionBiasExplorer](https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer).")
48
  gr.HTML("""<span style="color:red" font-size:smaller>⚠️ DISCLAIMER: the images displayed by this tool were generated by text-to-image models and may depict offensive stereotypes or contain explicit content.</span>""")
49
- num_clusters = gr.Radio([12,24,48], value=12, label="How many clusters do you want to make from the data?")
50
 
 
51
  with gr.Row():
52
  with gr.Column(scale=4):
53
  gallery = gr.Gallery(label="Most representative images in cluster").style(grid=(3,3))
@@ -56,14 +73,16 @@ with gr.Blocks(title=TITLE) as demo:
56
  a = gr.Text(label="Number of images")
57
  with gr.Row():
58
  with gr.Column(scale=1):
59
- c = gr.Plot(label="Model makeup of cluster")
 
60
  with gr.Column(scale=1):
61
  b = gr.Plot(label="Gender label makeup of cluster")
 
62
  with gr.Column(scale=2):
63
  d = gr.Plot(label="Ethnicity label makeup of cluster")
64
- demo.load(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a,b,c,d, gallery])
65
- num_clusters.change(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a,b,c,d, gallery])
66
- cluster_id.change(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a,b,c,d, gallery])
67
 
68
  if __name__ == "__main__":
69
  demo.queue().launch(debug=True)
 
4
  from PIL import Image
5
  import plotly.graph_objects as go
6
  import plotly.express as px
7
+ import operator
8
 
9
  TITLE = "Diffusion Faces Cluster Explorer"
10
  clusters_12 = json.load(open("clusters/id_all_blip_clusters_12.json"))
 
17
  48: clusters_48,
18
  }
19
 
20
+ def describe_cluster(cl_dict, block="label"):
21
+ labels_values = sorted(cl_dict.items(), key=operator.itemgetter(1))
22
+ labels_values.reverse()
23
+ total = float(sum(cl_dict.values()))
24
+ lv_prcnt = list((item[0], round(item[1] * 100/total, 0)) for item in labels_values)
25
+ description_string = "The most represented %s is %s, making up about %d%% of the cluster.\n" % (block, lv_prcnt[0][0], lv_prcnt[0][1])
26
+ description_string += "This is followed by: "
27
+ for label_value_tuple in lv_prcnt[1:]:
28
+ description_string += "\n%s: %d%%" % label_value_tuple
29
+ return description_string
30
+
31
  def show_cluster(cl_id, num_clusters):
32
  if not cl_id:
33
  cl_id = 0
 
41
  model_fig = go.Figure()
42
  model_fig.add_trace(go.Pie(labels=list(dict(cl_dct["labels_model"]).keys()),
43
  values=list(dict(cl_dct["labels_model"]).values())))
44
+ model_description = describe_cluster(dict(cl_dct["labels_model"]), "model")
45
+
46
  gender_fig = go.Figure()
47
  gender_fig.add_trace(go.Pie(labels=list(dict(cl_dct["labels_gender"]).keys()),
48
  values=list(dict(cl_dct["labels_gender"]).values())))
49
+ gender_description = describe_cluster(dict(cl_dct["labels_gender"]), "gender")
50
+
51
  ethnicity_fig = go.Figure()
52
  ethnicity_fig.add_trace(go.Bar(x=list(dict(cl_dct["labels_ethnicity"]).keys()),
53
  y=list(dict(cl_dct["labels_ethnicity"]).values()),
54
  marker_color=px.colors.qualitative.G10))
55
  return (len(cl_dct['img_path_list']),
56
+ gender_fig,gender_description,
57
+ model_fig, model_description,
58
  ethnicity_fig,
59
  images)
60
 
 
62
  gr.Markdown(f"# {TITLE}")
63
  gr.Markdown("## This Space lets you explore the data generated from [DiffusionBiasExplorer](https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer).")
64
  gr.HTML("""<span style="color:red" font-size:smaller>⚠️ DISCLAIMER: the images displayed by this tool were generated by text-to-image models and may depict offensive stereotypes or contain explicit content.</span>""")
65
+ num_clusters = gr.Radio([12,24,48], value=12, labels="How many clusters do you want to make from the data?")
66
 
67
+
68
  with gr.Row():
69
  with gr.Column(scale=4):
70
  gallery = gr.Gallery(label="Most representative images in cluster").style(grid=(3,3))
 
73
  a = gr.Text(label="Number of images")
74
  with gr.Row():
75
  with gr.Column(scale=1):
76
+ c = gr.Plot(label="Model makeup of clssuster")
77
+ c_desc = gr.Text(label="")
78
  with gr.Column(scale=1):
79
  b = gr.Plot(label="Gender label makeup of cluster")
80
+ b_desc = gr.Text(label="")
81
  with gr.Column(scale=2):
82
  d = gr.Plot(label="Ethnicity label makeup of cluster")
83
+ demo.load(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a, b, b_desc, c, c_desc, d, gallery])
84
+ num_clusters.change(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a, b, b_desc, c, c_desc, d, gallery])
85
+ cluster_id.change(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a, b, b_desc, c, c_desc, d, gallery])
86
 
87
  if __name__ == "__main__":
88
  demo.queue().launch(debug=True)