roopalgarg commited on
Commit
b069388
β€’
1 Parent(s): 62557b1

update app to support additional IIW data release sets.

Browse files
Files changed (1) hide show
  1. app.py +86 -32
app.py CHANGED
@@ -1,18 +1,55 @@
1
- import os
2
  import gradio as gr
3
- from datasets import load_dataset
4
  import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- auth_token = os.environ.get("auth_token")
7
- iiw_400 = load_dataset('google/imageinwords', token=auth_token, name="IIW-400")
8
 
9
- def display_iiw_data(index):
10
- data = iiw_400['test'][index]
11
- image_html = f'<img src="{data["image/url"]}" style="width:100%; max-width:800px; height:auto;">'
12
- iiw_text = f"<h2>IIW Human Descriptions:</h2><p style='font-size: 16px'>{data['IIW']}</p>"
13
- iiw_p5b_text = f"<h2>IIW PaLI 5B Predictions:</h2><p style='font-size: 16px'>{data['IIW-P5B']}</p>"
14
- ratings = "<h2>Ratings:</h2>"
15
- if data['iiw-human-sxs-iiw-p5b'] is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  for key, value in data['iiw-human-sxs-iiw-p5b'].items():
17
  key = key.split("metrics/")[-1]
18
  emoji = ""
@@ -27,30 +64,47 @@ def display_iiw_data(index):
27
  elif key == "Human Like":
28
  emoji = "πŸ‘€" # Bust in Silhouette
29
  ratings += f"<p style='font-size: 16px'>{emoji} <strong>{key}</strong>: {value}</p>"
30
- return image_html, iiw_text, iiw_p5b_text, ratings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- def random_index():
33
- while True:
34
- index = random.randint(0, len(iiw_400['test']) - 1)
35
- if iiw_400['test'][index]['iiw-human-sxs-iiw-p5b'] is not None:
36
- return index
37
 
38
- demo = gr.Blocks()
 
 
39
 
40
- with demo:
41
- gr.Markdown("# Slide across the slider to see various examples from IIW-400")
 
42
 
43
- with gr.Column():
44
- slider = gr.Slider(minimum=0, maximum=400)
45
- with gr.Row():
46
- index = random_index()
47
- with gr.Column():
48
- image_output = gr.HTML(display_iiw_data(index)[0])
49
- with gr.Column():
50
- iiw_text_output = gr.HTML(display_iiw_data(index)[1])
51
- iiw_p5b_text_output = gr.HTML(display_iiw_data(index)[2])
52
- ratings_output = gr.HTML(display_iiw_data(index)[3])
 
 
53
 
54
- slider.change(display_iiw_data, inputs=[slider], outputs=[image_output, iiw_text_output, iiw_p5b_text_output, ratings_output])
 
55
 
56
- demo.launch(debug=True)
 
 
1
  import gradio as gr
2
+ import logging
3
  import random
4
+ import os
5
+
6
+ from datasets import load_dataset
7
+ from huggingface_hub import login
8
+
9
+ try:
10
+ login()
11
+ except:
12
+ pass
13
+
14
+ auth_token = os.environ.get('HF_TOKEN', None)
15
+ if not auth_token:
16
+ raise ValueError("could not authenticate the user.")
17
+
18
+ iiw_400 = load_dataset('google/imageinwords', token=auth_token, trust_remote_code=True, name="IIW-400")
19
+ docci_test = load_dataset('google/imageinwords', token=auth_token, trust_remote_code=True, name="DOCCI_Test")
20
+ locnar_eval = load_dataset('google/imageinwords', token=auth_token, trust_remote_code=True, name="LocNar_Eval")
21
+ cm_3600 = load_dataset('google/imageinwords', token=auth_token, trust_remote_code=True, name="CM_3600")
22
+
23
+ _SELECTOR_TO_DATASET = {
24
+ "IIW-400": iiw_400,
25
+ "DOCCI_Test": docci_test,
26
+ "LocNar_Eval": locnar_eval,
27
+ "CM_3600": cm_3600
28
+ }
29
 
 
 
30
 
31
+ def display_iiw_data_with_slider_change(dataset_type, index):
32
+ dataset_split, image_key, image_url_key = "test", "image/key", "image/url"
33
+ if dataset_type == "LocNar_Eval":
34
+ dataset_split = "validation"
35
+ if dataset_type == "DOCCI_Test":
36
+ image_url_key = "image/thumbnail_url"
37
+ image_key = "image"
38
+
39
+ logging.warning(f"SELECTION: {dataset_type} : {dataset_split}: {index}")
40
+ data = _SELECTOR_TO_DATASET[dataset_type][dataset_split][index]
41
+ image_html = f'<img src="{data[image_url_key]}" style="width:100%; max-width:800px; height:auto;">'
42
+ image_key_html = f"<p style='font-size: 10px'>Image Key: {data[image_key]}</p>"
43
+
44
+ iiw_text, iiw_p5b_text, ratings = "", "", ""
45
+ if "IIW" in data:
46
+ iiw_text = f"<h2>IIW Human-Authored Descriptions</h2><p style='font-size: 16px'>{data['IIW']}</p>"
47
+
48
+ if "IIW-P5B" in data:
49
+ iiw_p5b_text = f"<h2>IIW PaLI-5B Generated Descriptions</h2><p style='font-size: 16px'>{data['IIW-P5B']}</p>"
50
+
51
+ if 'iiw-human-sxs-iiw-p5b' in data and data['iiw-human-sxs-iiw-p5b'] is not None:
52
+ ratings = "<h2>Ratings</h2>"
53
  for key, value in data['iiw-human-sxs-iiw-p5b'].items():
54
  key = key.split("metrics/")[-1]
55
  emoji = ""
 
64
  elif key == "Human Like":
65
  emoji = "πŸ‘€" # Bust in Silhouette
66
  ratings += f"<p style='font-size: 16px'>{emoji} <strong>{key}</strong>: {value}</p>"
67
+ return image_key_html, image_html, iiw_text, iiw_p5b_text, ratings
68
+
69
+
70
+ def display_iiw_data_with_dataset_change(dataset_type, index):
71
+ slider = gr.Slider(minimum=0, maximum=max_index(dataset_type)-1, label="Dataset Size", value=0)
72
+ image_key_html, image_html, iiw_text, iiw_p5b_text, ratings = display_iiw_data_with_slider_change(dataset_type, index=0)
73
+ return slider, image_key_html, image_html, iiw_text, iiw_p5b_text, ratings
74
+
75
+
76
+ def max_index(dataset_type):
77
+ dataset_split = "test"
78
+ if dataset_type == "LocNar_Eval":
79
+ dataset_split = "validation"
80
+
81
+ logging.warning(f"SELECTION: {dataset_type} : {dataset_split}")
82
+ dataset_instance =_SELECTOR_TO_DATASET[dataset_type][dataset_split]
83
+ return len(dataset_instance)
84
 
 
 
 
 
 
85
 
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("# ImageInWords: Unlocking Hyper-Detailed Image Descriptions")
88
+ gr.Markdown("Slide across the slider to see various examples across the different IIW datasets.")
89
 
90
+ with gr.Row():
91
+ dataset_selector = gr.Radio(["IIW-400", "DOCCI_Test", "LocNar_Eval", "CM_3600"], value="IIW-400", label="IIW Datasets")
92
+ slider, image_key_html, image_html, iiw_text, iiw_p5b_text, ratings = display_iiw_data_with_dataset_change(dataset_selector.value, index=0)
93
 
94
+ with gr.Row():
95
+ with gr.Column():
96
+ image_output = gr.HTML(image_html)
97
+
98
+ with gr.Column():
99
+ image_key_output = gr.HTML(image_key_html)
100
+ if iiw_text:
101
+ iiw_text_output = gr.HTML(iiw_text)
102
+ if iiw_p5b_text:
103
+ iiw_p5b_text_output = gr.HTML(iiw_p5b_text)
104
+ if ratings:
105
+ ratings_output = gr.HTML(ratings)
106
 
107
+ slider.change(display_iiw_data_with_slider_change, inputs=[dataset_selector, slider], outputs=[image_key_output, image_output, iiw_text_output, iiw_p5b_text_output, ratings_output])
108
+ dataset_selector.change(display_iiw_data_with_dataset_change, inputs=[dataset_selector, slider], outputs=[slider, image_key_output, image_output, iiw_text_output, iiw_p5b_text_output, ratings_output])
109
 
110
+ demo.launch(debug=True)