yonatanbitton commited on
Commit
f15f93d
·
1 Parent(s): 17d712b
Files changed (2) hide show
  1. app.py +41 -27
  2. app_two_screens.py +62 -0
app.py CHANGED
@@ -8,23 +8,31 @@ print(f"Loaded WMTIS identify, first example:")
8
  print(wmtis[0])
9
  dataset_size = len(wmtis) - 1
10
 
 
11
  NORMAL_IMAGE = 'normal_image'
12
  STRANGE_IMAGE = 'strange_image'
 
13
  def func(index):
14
  example = wmtis[index]
15
  outputs = []
16
- for normal_key in ['normal_image', 'normal_hash', 'normal_image_caption', 'rating_normal', 'comments_normal']:
17
- if normal_key == 'comments_normal':
18
- outputs.append(get_empty_comment_if_needed(example[normal_key]))
19
- else:
20
- outputs.append(example[normal_key])
21
- for strange_key in ['strange_image', 'strange_hash', 'strange_image_caption', 'rating_strange', 'comments_strange']:
22
- if normal_key == 'comments_normal':
23
- outputs.append(get_empty_comment_if_needed(example[strange_key]))
24
- else:
25
- outputs.append(example[strange_key])
26
  return outputs
27
 
 
 
 
 
 
 
 
 
 
 
 
28
  demo = gr.Blocks()
29
 
30
  def get_empty_comment_if_needed(item):
@@ -32,28 +40,34 @@ def get_empty_comment_if_needed(item):
32
  return '-'
33
  return item
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  with demo:
36
  gr.Markdown("# Slide to iterate WMTIS: Normal vs. Strange Images")
37
 
38
  with gr.Column():
39
  slider = gr.Slider(minimum=0, maximum=dataset_size)
40
  with gr.Row():
41
- index = random.choice(range(0, dataset_size))
42
- with gr.Column():
43
- i1 = gr.Image(value=wmtis[index]["normal_image"], label='Normal Image')
44
- t1 = gr.Textbox(value=wmtis[index]["normal_hash"], label='Image ID')
45
- p1 = gr.Textbox(value=wmtis[index]["normal_image_caption"], label='BLIP2 Predicted Caption')
46
- r1 = gr.Textbox(value=wmtis[index]["rating_normal"], label='Rating')
47
- c1 = gr.Textbox(value=get_empty_comment_if_needed(wmtis[index]["comments_normal"]), label='Comments')
48
- normal_outputs = [i1, t1, p1, r1, c1]
49
- with gr.Column():
50
- i2 = gr.Image(value=wmtis[index]["strange_image"], label='Strange Image')
51
- t2 = gr.Textbox(value=wmtis[index]["strange_hash"], label='Image ID')
52
- p2 = gr.Textbox(value=wmtis[index]["strange_image_caption"], label='BLIP2 Predicted Caption')
53
- r2 = gr.Textbox(value=wmtis[index]["rating_strange"], label='Rating')
54
- c2 = gr.Textbox(value=get_empty_comment_if_needed(wmtis[index]["comments_strange"]), label='Comments')
55
- strange_outputs = [i2, t2, p2, r2, c2]
56
-
57
- slider.change(func, inputs=[slider], outputs=normal_outputs + strange_outputs)
58
 
59
  demo.launch()
 
8
  print(wmtis[0])
9
  dataset_size = len(wmtis) - 1
10
 
11
+ NATURAL_IMAGE = 'natural_image'
12
  NORMAL_IMAGE = 'normal_image'
13
  STRANGE_IMAGE = 'strange_image'
14
+
15
  def func(index):
16
  example = wmtis[index]
17
  outputs = []
18
+ target_size = example['normal_image'].size
19
+ add_outputs_for_key(example, outputs, target_size, 'natural')
20
+ add_outputs_for_key(example, outputs, target_size, 'normal')
21
+ add_outputs_for_key(example, outputs, target_size, 'strange')
22
+
 
 
 
 
 
23
  return outputs
24
 
25
+
26
+ def add_outputs_for_key(example, outputs, target_size, item):
27
+ for item_key in [f'{item}_image', f'{item}_hash', f'{item}_image_caption', f'rating_{item}', f'comments_{item}']:
28
+ if item_key == f'comments_{item}':
29
+ outputs.append(get_empty_comment_if_needed(example[item_key]))
30
+ elif item_key == f'{item}_image':
31
+ outputs.append(example[item_key].resize(target_size))
32
+ else:
33
+ outputs.append(example[item_key])
34
+
35
+
36
  demo = gr.Blocks()
37
 
38
  def get_empty_comment_if_needed(item):
 
40
  return '-'
41
  return item
42
 
43
+
44
+ def add_column_by_key(item, target_size):
45
+ with gr.Column():
46
+ img = wmtis[index][f"{item}_image"]
47
+ img_resized = img.resize(target_size)
48
+ i1 = gr.Image(value=img_resized, label=f'{item.capitalize()} Image')
49
+ p1 = gr.Textbox(value=wmtis[index][f"{item}_image_caption"], label='BLIP2 Predicted Caption')
50
+ r1 = gr.Textbox(value=wmtis[index][f"rating_{item}"], label='Rating')
51
+ t1 = gr.Textbox(value=wmtis[index][f"{item}_hash"], label='Image ID')
52
+ c1 = gr.Textbox(value=get_empty_comment_if_needed(wmtis[index][f"comments_{item}"]), label='Comments')
53
+ item_outputs = [i1, t1, p1, r1, c1]
54
+ return item_outputs
55
+
56
+
57
  with demo:
58
  gr.Markdown("# Slide to iterate WMTIS: Normal vs. Strange Images")
59
 
60
  with gr.Column():
61
  slider = gr.Slider(minimum=0, maximum=dataset_size)
62
  with gr.Row():
63
+ index = slider.value
64
+ if index > dataset_size:
65
+ index = 0
66
+ target_size = wmtis[index]['normal_image'].size
67
+ natural_outputs = add_column_by_key('natural', target_size)
68
+ normal_outputs = add_column_by_key('normal', target_size)
69
+ strange_outputs = add_column_by_key('strange', target_size)
70
+
71
+ slider.change(func, inputs=[slider], outputs=natural_outputs + normal_outputs + strange_outputs)
 
 
 
 
 
 
 
 
72
 
73
  demo.launch()
app_two_screens.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import gradio as gr
3
+ import os
4
+ import random
5
+
6
+ wmtis = load_dataset("nlphuji/wmtis-identify")['test']
7
+ print(f"Loaded WMTIS identify, first example:")
8
+ print(wmtis[0])
9
+ dataset_size = len(wmtis) - 1
10
+
11
+ NORMAL_IMAGE = 'normal_image'
12
+ STRANGE_IMAGE = 'strange_image'
13
+ def func(index):
14
+ example = wmtis[index]
15
+ outputs = []
16
+ for normal_key in ['normal_image', 'normal_hash', 'normal_image_caption', 'rating_normal', 'comments_normal']:
17
+ if normal_key == 'comments_normal':
18
+ outputs.append(get_empty_comment_if_needed(example[normal_key]))
19
+ else:
20
+ outputs.append(example[normal_key])
21
+ for strange_key in ['strange_image', 'strange_hash', 'strange_image_caption', 'rating_strange', 'comments_strange']:
22
+ if normal_key == 'comments_normal':
23
+ outputs.append(get_empty_comment_if_needed(example[strange_key]))
24
+ else:
25
+ outputs.append(example[strange_key])
26
+ return outputs
27
+
28
+ demo = gr.Blocks()
29
+
30
+ def get_empty_comment_if_needed(item):
31
+ if item == 'nan':
32
+ return '-'
33
+ return item
34
+
35
+ with demo:
36
+ gr.Markdown("# Slide to iterate WMTIS: Normal vs. Strange Images")
37
+
38
+ with gr.Column():
39
+ slider = gr.Slider(minimum=0, maximum=dataset_size)
40
+ with gr.Row():
41
+ # index = random.choice(range(0, dataset_size))
42
+ index = slider.value
43
+ if index > dataset_size:
44
+ index = 0
45
+ with gr.Column():
46
+ i1 = gr.Image(value=wmtis[index]["normal_image"], label='Normal Image')
47
+ t1 = gr.Textbox(value=wmtis[index]["normal_hash"], label='Image ID')
48
+ p1 = gr.Textbox(value=wmtis[index]["normal_image_caption"], label='BLIP2 Predicted Caption')
49
+ r1 = gr.Textbox(value=wmtis[index]["rating_normal"], label='Rating')
50
+ c1 = gr.Textbox(value=get_empty_comment_if_needed(wmtis[index]["comments_normal"]), label='Comments')
51
+ normal_outputs = [i1, t1, p1, r1, c1]
52
+ with gr.Column():
53
+ i2 = gr.Image(value=wmtis[index]["strange_image"], label='Strange Image')
54
+ t2 = gr.Textbox(value=wmtis[index]["strange_hash"], label='Image ID')
55
+ p2 = gr.Textbox(value=wmtis[index]["strange_image_caption"], label='BLIP2 Predicted Caption')
56
+ r2 = gr.Textbox(value=wmtis[index]["rating_strange"], label='Rating')
57
+ c2 = gr.Textbox(value=get_empty_comment_if_needed(wmtis[index]["comments_strange"]), label='Comments')
58
+ strange_outputs = [i2, t2, p2, r2, c2]
59
+
60
+ slider.change(func, inputs=[slider], outputs=normal_outputs + strange_outputs)
61
+
62
+ demo.launch()