File size: 3,148 Bytes
b9a3a58
 
734cbc0
 
3dd4093
 
b9a3a58
 
1b3b48e
b9a3a58
 
 
 
 
 
 
 
 
 
734cbc0
 
e66c262
3dd4093
 
 
 
 
 
 
 
 
 
 
 
 
cece7ed
3dd4093
 
 
 
 
e66c262
7c714e1
 
 
 
a3da3e8
 
335988f
7c714e1
66dc83e
3dd4093
cece7ed
3dd4093
 
 
 
601c134
7c714e1
0eadf73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import gradio as gr
import os
from PIL import Image
import plotly.graph_objects as go
import plotly.express as px

clusters_12 = json.load(open("clusters/id_all_blip_clusters_12.json"))
clusters_24 = json.load(open("clusters/id_all_blip_clusters_24.json"))
clusters_48 = json.load(open("clusters/id_all_blip_clusters_48.json"))

clusters_by_size = {
    12: clusters_12,
    24: clusters_24,
    48: clusters_48,
}

def show_cluster(cl_id, num_clusters):
    cl_dct = clusters_by_size[num_clusters][cl_id]
    images = []
    for i in range(6):
        img_path = "/".join([st.replace("/", "") for st in cl_dct['img_path_list'][i].split("//")][3:])
        images.append((Image.open(os.path.join("identities-images", img_path)), "_".join([img_path.split("/")[0], img_path.split("/")[-1]]).replace('Photo_portrait_of_an_','').replace('Photo_portrait_of_a_','').replace('SD_v2_random_seeds_identity_','(SD v.2) ').replace('dataset-identities-dalle2_','(Dall-E 2) ').replace('SD_v1.4_random_seeds_identity_','(SD v.1.4) ').replace('_',' ')))
    model_fig = go.Figure()
    model_fig.add_trace(go.Bar(x=list(dict(cl_dct["labels_model"]).keys()), 
                               y=list(dict(cl_dct["labels_model"]).values()),
                               marker_color=px.colors.qualitative.G10))
    gender_fig = go.Figure()
    gender_fig.add_trace(go.Bar(x=list(dict(cl_dct["labels_gender"]).keys()), 
                                y=list(dict(cl_dct["labels_gender"]).values()),
                                marker_color=px.colors.qualitative.G10))
    ethnicity_fig = go.Figure()
    ethnicity_fig.add_trace(go.Bar(x=list(dict(cl_dct["labels_ethnicity"]).keys()), 
                                   y=list(dict(cl_dct["labels_ethnicity"]).values()), 
                                   marker_color=px.colors.qualitative.G10))
    return (len(cl_dct['img_path_list']),
           gender_fig,
           #dict(cl_dct["labels_model"]),
           model_fig,
           ethnicity_fig,
           images)

with gr.Blocks() as demo:
    gr.Markdown("# Cluster Explorer")
    gr.HTML("""<span style="color:red">⚠️ <b>DISCLAIMER: the images displayed by this tool were generated by text-to-image models and may depict offensive stereotypes or contain explicit content.</b></span>""")
    with gr.Row():
        num_clusters = gr.Radio([12, 24, 48], label="number of clusters")
        cluster_id = gr.Number(precision=0, label="cluster id")
        button = gr.Button(value="Go")
    with gr.Column():
        a = gr.Text(label="Number of items in cluster")
        gallery = gr.Gallery(label="Most representative images in cluster").style(grid=6)
        with gr.Row():
            c = gr.Plot(label="Model makeup of cluster")
            b = gr.Plot(label="Gender label makeup of cluster")
            d = gr.Plot(label="Ethnicity label makeup of cluster")
    
    button.click(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a,b,c,d, gallery])
# demo = gr.Interface(fn=show_cluster, inputs=[gr.Slider(0, 50), gr.Radio([12, 24, 48])], outputs=["text", "text", "text", "text", gr.Gallery()])
demo.launch(debug=True)