Spaces:
Runtime error
Runtime error
meg-huggingface
commited on
Commit
·
5e01e13
1
Parent(s):
ea06e5a
Adding real app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
clusters_12 = json.load(open("clusters/id_all_blip_clusters_12.json"))
|
| 7 |
+
clusters_24 = json.load(open("clusters/id_all_blip_clusters_24.json"))
|
| 8 |
+
clusters_48 = json.load(open("clusters/id_all_blip_clusters_48.json"))
|
| 9 |
|
| 10 |
+
clusters_by_size = {
|
| 11 |
+
12: clusters_12,
|
| 12 |
+
24: clusters_24,
|
| 13 |
+
48: clusters_48,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
def show_cluster(cl_id, num_clusters):
|
| 17 |
+
cl_dct = clusters_by_size[num_clusters][cl_id]
|
| 18 |
+
images = []
|
| 19 |
+
for i in range(6):
|
| 20 |
+
img_path = "/".join([st.replace("/", "") for st in cl_dct['img_path_list'][i].split("//")][3:])
|
| 21 |
+
images.append((Image.open(os.path.join("identities-images", img_path)), "_".join([img_path.split("/")[0], img_path.split("/")[-1]])))
|
| 22 |
+
return (len(cl_dct['img_path_list']),
|
| 23 |
+
dict(cl_dct["labels_gender"]),
|
| 24 |
+
dict(cl_dct["labels_model"]),
|
| 25 |
+
dict(cl_dct["labels_ethnicity"]),
|
| 26 |
+
images)
|
| 27 |
+
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("# Cluster Explorer")
|
| 30 |
+
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>""")
|
| 31 |
+
with gr.Row():
|
| 32 |
+
num_clusters = gr.Radio([12, 24, 48], label="number of clusters")
|
| 33 |
+
cluster_id = gr.Number(precision=0, label="cluster id")
|
| 34 |
+
button = gr.Button(value="Go")
|
| 35 |
+
with gr.Column():
|
| 36 |
+
a = gr.Text(label="Number of items in cluster")
|
| 37 |
+
with gr.Row():
|
| 38 |
+
c = gr.Text(label="Model makeup of cluster")
|
| 39 |
+
b = gr.Text(label="Gender label makeup of cluster")
|
| 40 |
+
d = gr.Text(label="Ethnicity label makeup of cluster")
|
| 41 |
+
gallery = gr.Gallery(label="Most representative images in cluster").style(grid=6)
|
| 42 |
+
button.click(fn=show_cluster, inputs=[cluster_id, num_clusters], outputs=[a,b,c,d, gallery])
|
| 43 |
+
# demo = gr.Interface(fn=show_cluster, inputs=[gr.Slider(0, 50), gr.Radio([12, 24, 48])], outputs=["text", "text", "text", "text", gr.Gallery()])
|
| 44 |
+
demo.launch(debug=True)
|