File size: 1,708 Bytes
b9a3a58
 
734cbc0
 
b9a3a58
 
 
 
 
 
 
 
 
 
 
 
 
734cbc0
 
e66c262
734cbc0
 
 
e66c262
7c714e1
 
 
 
2fda5e0
5116490
7c714e1
 
 
 
 
 
 
3e7ca70
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
import json
import gradio as gr
import os
from PIL import Image

clusters_12 = json.load(open("clusters/id_all_blip_clusters_12.json"))
clusters_24 = json.load(open("clusters/id_all_blip_clusters_12.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:])
        img_path = os.path.join("identities-images", img_path)
        images.append(Image.open(img_path))
    return len(cl_dct['img_path_list']),cl_dct["labels_gender"], cl_dct["labels_model"], cl_dct["labels_ethnicity"], 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])
        cluster_id = gr.Number(precision=0)
        button = gr.Button(value="Go")
    with gr.Column():
        a = gr.Text()
        b = gr.Text()
        c = gr.Text()
        d = gr.Text()
    gallery = gr.Gallery().style(grid=4)
    button.click(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[gr.Text(),gr.Text(),gr.Text(),gr.Text(), 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)