yonatanbitton commited on
Commit
72fb101
1 Parent(s): e3151fe

Add application file

Browse files
Files changed (3) hide show
  1. .gitignore +0 -0
  2. app.py +63 -0
  3. requirements.txt +0 -0
.gitignore ADDED
File without changes
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import gradio as gr
3
+ import os
4
+ import random
5
+
6
+ wmtis = load_dataset("nlphuji/wmtis")['test']
7
+ dataset_size = len(wmtis)
8
+
9
+ IMAGE = 'image'
10
+ IMAGE_DESIGNER = 'image_designer'
11
+ DESIGNER_EXPLANATION = 'designer_explanation'
12
+ CROWD_CAPTIONS = 'crowd_captions'
13
+ CROWD_EXPLANATIONS = 'crowd_explanations'
14
+ CROWD_NEGATIVE_CAPTIONS = 'crowd_negative_captions'
15
+ CROWD_NEGATIVE_EXPLANATIONS = 'crowd_negative_explanations'
16
+ QA = 'question_answering_pairs'
17
+ IMAGE_ID = 'image_id'
18
+ dataset_cols = [x for x in wmtis.features.keys() if x not in [IMAGE]]
19
+ enumerate_cols = [CROWD_CAPTIONS, CROWD_EXPLANATIONS, CROWD_NEGATIVE_CAPTIONS, CROWD_NEGATIVE_EXPLANATIONS]
20
+ def func(index):
21
+ example = wmtis[index]
22
+ values = get_instance_values(example)
23
+ return values
24
+
25
+
26
+ def get_instance_values(example):
27
+ values = []
28
+ for k in [IMAGE] + dataset_cols:
29
+ if k in enumerate_cols:
30
+ value = list_to_string(example[k])
31
+ elif k == QA:
32
+ qa_list = [f"Q: {x[0]} A: {x[1]}" for x in example[k]]
33
+ value = list_to_string(qa_list)
34
+ else:
35
+ value = example[k]
36
+ values.append(value)
37
+ return values
38
+ def list_to_string(lst):
39
+ return '\n'.join(['{}. {}'.format(i+1, item) for i, item in enumerate(lst)])
40
+
41
+ demo = gr.Blocks()
42
+
43
+ with demo:
44
+ gr.Markdown("# Slide to iterate WMTIS")
45
+
46
+ with gr.Column():
47
+ slider = gr.Slider(minimum=0, maximum=dataset_size, step=1, label='index')
48
+ with gr.Row():
49
+ index = random.choice(range(0, dataset_size))
50
+ image_input = gr.Image(value=wmtis[index]["image"])
51
+ with gr.Column():
52
+ example = wmtis[index]
53
+ instance_values = get_instance_values(example)
54
+ text_inputs = []
55
+ assert len(dataset_cols) == len(instance_values[1:]) # excluding the image
56
+ for key, value in zip(dataset_cols, instance_values[1:]):
57
+ label = key.capitalize().replace("_", " ")
58
+ text_input_k = gr.Textbox(value=value, label=label)
59
+ text_inputs.append(text_input_k)
60
+
61
+ slider.change(func, inputs=[slider], outputs=[image_input] + text_inputs)
62
+
63
+ demo.launch()
requirements.txt ADDED
File without changes