lovodkin93 commited on
Commit
3b03418
β€’
1 Parent(s): 86c3edd

Create app_grid.py

Browse files
Files changed (1) hide show
  1. app_grid.py +88 -0
app_grid.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ import gradio as gr
4
+ from datasets import load_dataset
5
+
6
+ whoops = load_dataset("nlphuji/whoops")['test']
7
+ whoops = whoops.shuffle()
8
+
9
+ print(f"Loaded WMTIS, first example:")
10
+ print(whoops[0])
11
+ dataset_size = len(whoops)
12
+ print(f"dataset_size: {dataset_size}")
13
+
14
+ IMAGE = 'image'
15
+ IMAGE_DESIGNER = 'image_designer'
16
+ DESIGNER_EXPLANATION = 'designer_explanation'
17
+ CROWD_CAPTIONS = 'crowd_captions'
18
+ CROWD_EXPLANATIONS = 'crowd_explanations'
19
+ CROWD_UNDERSPECIFIED_CAPTIONS = 'crowd_underspecified_captions'
20
+ SELECTED_CAPTION = 'selected_caption'
21
+ COMMONSENSE_CATEGORY = 'commonsense_category'
22
+ QA = 'question_answering_pairs'
23
+ IMAGE_ID = 'image_id'
24
+ left_side_columns = [IMAGE]
25
+ right_side_columns = [x for x in whoops.features.keys() if x not in left_side_columns and x not in [QA]]
26
+ enumerate_cols = [CROWD_CAPTIONS, CROWD_EXPLANATIONS, CROWD_UNDERSPECIFIED_CAPTIONS]
27
+ emoji_to_label = {IMAGE_DESIGNER: '🎨, πŸ§‘β€πŸŽ¨, πŸ’»', DESIGNER_EXPLANATION: 'πŸ’‘, πŸ€”, πŸ§‘β€πŸŽ¨',
28
+ CROWD_CAPTIONS: 'πŸ‘₯, πŸ’¬, πŸ“', CROWD_EXPLANATIONS: 'πŸ‘₯, πŸ’‘, πŸ€”', CROWD_UNDERSPECIFIED_CAPTIONS: 'πŸ‘₯, πŸ’¬, πŸ‘Ž',
29
+ QA: '❓, πŸ€”, πŸ’‘', IMAGE_ID: 'πŸ”, πŸ“„, πŸ’Ύ', COMMONSENSE_CATEGORY: 'πŸ€”, πŸ“š, πŸ’‘', SELECTED_CAPTION: 'πŸ“, πŸ‘Œ, πŸ’¬'}
30
+ target_size = (1024, 1024)
31
+
32
+ columns_number = 3
33
+ rows_number = 10
34
+
35
+ def func(index):
36
+ example = whoops[index]
37
+ values = get_instance_values(example)
38
+ return values
39
+
40
+
41
+ def get_instance_values(example):
42
+ values = []
43
+ for k in left_side_columns + right_side_columns:
44
+ if k in enumerate_cols:
45
+ value = list_to_string(example[k])
46
+ elif k == QA:
47
+ qa_list = [f"Q: {x[0]} A: {x[1]}" for x in example[k]]
48
+ value = list_to_string(qa_list)
49
+ else:
50
+ value = example[k]
51
+ values.append(value)
52
+ return values
53
+
54
+
55
+ def list_to_string(lst):
56
+ return '\n'.join(['{}. {}'.format(i + 1, item) for i, item in enumerate(lst)])
57
+
58
+ def create_image_accordion_block(index):
59
+ example = whoops[index]
60
+ instance_values = get_instance_values(example)
61
+ assert len(left_side_columns) == len(
62
+ instance_values[:len(left_side_columns)]) # excluding the image & designer
63
+ for key, value in zip(left_side_columns, instance_values[:len(left_side_columns)]):
64
+ if key == IMAGE:
65
+ img = whoops[index]["image"]
66
+ img_resized = img.resize(target_size)
67
+ gr.Image(value=img_resized, label=whoops[index]['commonsense_category'])
68
+ else:
69
+ label = key.capitalize().replace("_", " ")
70
+ gr.Textbox(value=value, label=f"{label} {emoji_to_label[key]}")
71
+ with gr.Accordion("Click for details", open=False):
72
+ assert len(right_side_columns) == len(
73
+ instance_values[len(left_side_columns):]) # excluding the image & designer
74
+ for key, value in zip(right_side_columns, instance_values[len(left_side_columns):]):
75
+ label = key.capitalize().replace("_", " ")
76
+ gr.Textbox(value=value, label=f"{label} {emoji_to_label[key]}")
77
+
78
+
79
+
80
+ with gr.Blocks() as demo:
81
+ gr.Markdown(f"# Slide to iterate WHOOPS!")
82
+ for row_num in range(0, rows_number):
83
+ with gr.Row():
84
+ for col_num in range(0, columns_number):
85
+ with gr.Column():
86
+ index = random.choice(range(0, dataset_size))
87
+ create_image_accordion_block(index)
88
+ demo.launch()