Xmaster6y commited on
Commit
405b0bd
1 Parent(s): ed12cc6

vote interface

Browse files
app.py CHANGED
@@ -5,19 +5,22 @@ Main Gradio module.
5
  import gradio as gr
6
 
7
  from src import (
8
- explore_interface,
9
  label_interface,
 
 
10
  )
11
 
12
 
13
  demo = gr.TabbedInterface(
14
  [
15
- explore_interface.interface,
16
  label_interface.interface,
 
 
17
  ],
18
  [
19
- "Explore",
20
  "Label",
 
 
21
  ],
22
  title="Explore & Label Concepts",
23
  analytics_enabled=True,
 
5
  import gradio as gr
6
 
7
  from src import (
 
8
  label_interface,
9
+ sample_interface,
10
+ vote_interface,
11
  )
12
 
13
 
14
  demo = gr.TabbedInterface(
15
  [
 
16
  label_interface.interface,
17
+ sample_interface.interface,
18
+ vote_interface.interface,
19
  ],
20
  [
 
21
  "Label",
22
+ "Sample",
23
+ "Vote",
24
  ],
25
  title="Explore & Label Concepts",
26
  analytics_enabled=True,
src/explore_interface.py DELETED
@@ -1,74 +0,0 @@
1
- """Interface to explore images and labels.
2
- """
3
-
4
- import random
5
-
6
- import gradio as gr
7
-
8
- from src import global_variables
9
- from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
10
-
11
- def get_next_image(
12
- split: str,
13
- concepts: list,
14
- current_metadata: dict,
15
- selected_concepts: list,
16
- ):
17
- if concepts != selected_concepts:
18
- for split, values in global_variables.all_metadata.items():
19
- current_metadata[split] = [x for x in values if all([c in x["concepts"] for c in concepts])]
20
- selected_concepts = concepts
21
- try:
22
- if split == "all":
23
- sample = random.choice(
24
- current_metadata["train"] + current_metadata["validation"] + current_metadata["test"]
25
- )
26
- else:
27
- sample = random.choice(current_metadata[split])
28
- image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
29
- return image_path, sample["class"], sample["concepts"], current_metadata, selected_concepts
30
- except IndexError:
31
- gr.Warning("No image found for the selected filter.")
32
- return None, None, None, current_metadata, selected_concepts
33
-
34
- with gr.Blocks() as interface:
35
- with gr.Row():
36
- with gr.Column():
37
- with gr.Group():
38
- gr.Markdown(
39
- "## # Image Selection",
40
- )
41
- split = gr.Radio(
42
- label="Split",
43
- choices=["train", "validation", "test", "all"],
44
- value="train",
45
- )
46
- concepts = gr.Dropdown(
47
- label="Concepts",
48
- multiselect=True,
49
- choices=CONCEPTS,
50
- )
51
- button = gr.Button(
52
- value="Next",
53
- )
54
- with gr.Group():
55
- gr.Markdown(
56
- "## # Image Info",
57
- )
58
- im_class = gr.Textbox(
59
- label="Class",
60
- )
61
- im_concepts = gr.JSON(
62
- label="Concepts",
63
- )
64
- with gr.Column():
65
- image = gr.Image(
66
- label="Image",
67
- )
68
- current_metadata = gr.State(global_variables.all_metadata)
69
- selected_concepts = gr.State([])
70
- button.click(
71
- get_next_image,
72
- outputs=[image, im_class, im_concepts, current_metadata, selected_concepts],
73
- inputs=[split, concepts, current_metadata, selected_concepts]
74
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/label_interface.py CHANGED
@@ -12,11 +12,18 @@ from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
12
 
13
  def get_next_image(
14
  split: str,
 
 
 
15
  profile: gr.OAuthProfile
16
  ):
17
  username = profile.username
 
 
 
 
18
  try:
19
- sample_idx = random.choice(range(len(global_variables.all_metadata[split])))
20
  sample = global_variables.all_metadata[split][sample_idx]
21
  image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
22
  try:
@@ -25,37 +32,56 @@ def get_next_image(
25
  except KeyError:
26
  voted_concepts = []
27
 
28
- return image_path, voted_concepts, f"{split}:{sample_idx}"
 
 
 
 
 
 
 
 
29
  except IndexError:
30
  gr.Warning("No image found for the selected filter.")
31
- return None, None, None
32
 
33
  def submit_label(
34
  voted_concepts: list,
35
  current_image: Optional[str],
 
 
 
 
36
  profile: gr.OAuthProfile
37
  ):
38
  username = profile.username
39
  if current_image is None:
40
  gr.Warning("No image selected.")
41
- return None, None, None
42
- split, idx = current_image.split(":")
43
  idx = int(idx)
44
- global_variables.get_metadata(split)
45
- if "votes" not in global_variables.all_metadata[split][idx]:
46
- global_variables.all_metadata[split][idx]["votes"] = {}
47
- global_variables.all_metadata[split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
48
  vote_sum = {c: 0 for c in CONCEPTS}
49
  concepts = {}
50
  for c in CONCEPTS:
51
- for vote in global_variables.all_metadata[split][idx]["votes"].values():
52
  if c not in vote:
53
  continue
54
  vote_sum[c] += 2 * vote[c] - 1
55
  concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
56
- global_variables.all_metadata[split][idx]["concepts"] = concepts
57
- global_variables.save_metadata(split)
58
- return get_next_image(split, profile)
 
 
 
 
 
 
 
59
 
60
 
61
  with gr.Blocks() as interface:
@@ -67,14 +93,20 @@ with gr.Blocks() as interface:
67
  )
68
  split = gr.Radio(
69
  label="Split",
70
- choices=["train", "validation", "test",],
71
  value="train",
72
  )
 
 
 
 
 
73
  with gr.Group():
74
  voted_concepts = gr.CheckboxGroup(
75
- label="Concepts",
76
  choices=CONCEPTS,
77
  )
 
78
  with gr.Row():
79
  next_button = gr.Button(
80
  value="Next",
@@ -83,19 +115,42 @@ with gr.Blocks() as interface:
83
  submit_button = gr.Button(
84
  value="Submit",
85
  )
86
-
 
 
 
 
 
 
 
 
 
87
  with gr.Column():
88
  image = gr.Image(
89
  label="Image",
90
  )
91
  current_image = gr.State(None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  next_button.click(
93
  get_next_image,
94
- inputs=[split],
95
- outputs=[image, voted_concepts, current_image],
96
  )
97
  submit_button.click(
98
  submit_label,
99
- inputs=[voted_concepts, current_image],
100
- outputs=[image, voted_concepts, current_image],
101
  )
 
12
 
13
  def get_next_image(
14
  split: str,
15
+ concepts: list,
16
+ filtered_indices: dict,
17
+ selected_concepts: list,
18
  profile: gr.OAuthProfile
19
  ):
20
  username = profile.username
21
+ if concepts != selected_concepts:
22
+ for key, values in global_variables.all_metadata.items():
23
+ filtered_indices[key] = [i for i in range(len(values)) if all([values[i]["concepts"].get(c, False) for c in concepts])]
24
+ selected_concepts = concepts
25
  try:
26
+ sample_idx = random.choice(filtered_indices[split])
27
  sample = global_variables.all_metadata[split][sample_idx]
28
  image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
29
  try:
 
32
  except KeyError:
33
  voted_concepts = []
34
 
35
+ return (
36
+ image_path,
37
+ voted_concepts,
38
+ f"{split}:{sample_idx}",
39
+ sample["class"],
40
+ sample["concepts"],
41
+ filtered_indices,
42
+ selected_concepts,
43
+ )
44
  except IndexError:
45
  gr.Warning("No image found for the selected filter.")
46
+ return None, None, None, None, None, filtered_indices, selected_concepts
47
 
48
  def submit_label(
49
  voted_concepts: list,
50
  current_image: Optional[str],
51
+ split,
52
+ concepts,
53
+ filtered_indices,
54
+ selected_concepts,
55
  profile: gr.OAuthProfile
56
  ):
57
  username = profile.username
58
  if current_image is None:
59
  gr.Warning("No image selected.")
60
+ return None, None, None, None, None, filtered_indices, selected_concepts
61
+ current_split, idx = current_image.split(":")
62
  idx = int(idx)
63
+ global_variables.get_metadata(current_split)
64
+ if "votes" not in global_variables.all_metadata[current_split][idx]:
65
+ global_variables.all_metadata[current_split][idx]["votes"] = {}
66
+ global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
67
  vote_sum = {c: 0 for c in CONCEPTS}
68
  concepts = {}
69
  for c in CONCEPTS:
70
+ for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
71
  if c not in vote:
72
  continue
73
  vote_sum[c] += 2 * vote[c] - 1
74
  concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
75
+ global_variables.all_metadata[current_split][idx]["concepts"] = concepts
76
+ global_variables.save_metadata(current_split)
77
+ gr.Info("Submit success")
78
+ return get_next_image(
79
+ split,
80
+ concepts,
81
+ filtered_indices,
82
+ selected_concepts,
83
+ profile
84
+ )
85
 
86
 
87
  with gr.Blocks() as interface:
 
93
  )
94
  split = gr.Radio(
95
  label="Split",
96
+ choices=["train", "validation", "test"],
97
  value="train",
98
  )
99
+ concepts = gr.Dropdown(
100
+ label="Concepts",
101
+ multiselect=True,
102
+ choices=CONCEPTS,
103
+ )
104
  with gr.Group():
105
  voted_concepts = gr.CheckboxGroup(
106
+ label="Voted Concepts",
107
  choices=CONCEPTS,
108
  )
109
+
110
  with gr.Row():
111
  next_button = gr.Button(
112
  value="Next",
 
115
  submit_button = gr.Button(
116
  value="Submit",
117
  )
118
+ with gr.Group():
119
+ gr.Markdown(
120
+ "## # Image Info",
121
+ )
122
+ im_class = gr.Textbox(
123
+ label="Class",
124
+ )
125
+ im_concepts = gr.JSON(
126
+ label="Concepts",
127
+ )
128
  with gr.Column():
129
  image = gr.Image(
130
  label="Image",
131
  )
132
  current_image = gr.State(None)
133
+ filtered_indices = gr.State({
134
+ split: list(range(len(global_variables.all_metadata[split])))
135
+ for split in global_variables.all_metadata
136
+ })
137
+ selected_concepts = gr.State([])
138
+ common_output = [
139
+ image,
140
+ voted_concepts,
141
+ current_image,
142
+ im_class,
143
+ im_concepts,
144
+ filtered_indices,
145
+ selected_concepts
146
+ ]
147
  next_button.click(
148
  get_next_image,
149
+ inputs=[split, concepts, filtered_indices, selected_concepts],
150
+ outputs=common_output
151
  )
152
  submit_button.click(
153
  submit_label,
154
+ inputs=[voted_concepts, current_image, split, concepts, filtered_indices, selected_concepts],
155
+ outputs=common_output
156
  )
src/sample_interface.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Interface for labeling concepts in images.
2
+ """
3
+
4
+ from typing import Optional
5
+
6
+ import gradio as gr
7
+
8
+ from src import global_variables
9
+ from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
10
+
11
+ def get_image(
12
+ step: int,
13
+ split: str,
14
+ index: str,
15
+ filtered_indices: dict,
16
+ profile: gr.OAuthProfile
17
+ ):
18
+ username = profile.username
19
+ try:
20
+ int_index = int(index)
21
+ except:
22
+ gr.Warning("Error parsing index using 0")
23
+ int_index = 0
24
+ sample_idx = int_index + step
25
+ if sample_idx < 0:
26
+ gr.Warning("No previous image.")
27
+ sample_idx = 0
28
+ if sample_idx >= len(global_variables.all_metadata[split]):
29
+ gr.Warning("No next image.")
30
+ sample_idx = len(global_variables.all_metadata[split]) - 1
31
+ sample = global_variables.all_metadata[split][sample_idx]
32
+ image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
33
+ try:
34
+ username_votes = sample["votes"][username]
35
+ voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
36
+ except KeyError:
37
+ voted_concepts = []
38
+
39
+ return (
40
+ image_path,
41
+ voted_concepts,
42
+ f"{split}:{sample_idx}",
43
+ sample["class"],
44
+ sample["concepts"],
45
+ str(sample_idx),
46
+ filtered_indices,
47
+ )
48
+
49
+ def make_get_image(step):
50
+ def f(
51
+ split: str,
52
+ index: str,
53
+ filtered_indices: dict,
54
+ profile: gr.OAuthProfile
55
+ ):
56
+ return get_image(step, split, index, filtered_indices, profile)
57
+ return f
58
+
59
+ get_next_image = make_get_image(1)
60
+ get_prev_image = make_get_image(-1)
61
+ get_current_image = make_get_image(0)
62
+
63
+ def submit_label(
64
+ voted_concepts: list,
65
+ current_image: Optional[str],
66
+ split,
67
+ index,
68
+ filtered_indices,
69
+ profile: gr.OAuthProfile
70
+ ):
71
+ username = profile.username
72
+ if current_image is None:
73
+ gr.Warning("No image selected.")
74
+ return None, None, None, None, None, index, filtered_indices
75
+ current_split, idx = current_image.split(":")
76
+ idx = int(idx)
77
+ global_variables.get_metadata(current_split)
78
+ if "votes" not in global_variables.all_metadata[current_split][idx]:
79
+ global_variables.all_metadata[current_split][idx]["votes"] = {}
80
+ global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
81
+ vote_sum = {c: 0 for c in CONCEPTS}
82
+ concepts = {}
83
+ for c in CONCEPTS:
84
+ for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
85
+ if c not in vote:
86
+ continue
87
+ vote_sum[c] += 2 * vote[c] - 1
88
+ concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
89
+ global_variables.all_metadata[current_split][idx]["concepts"] = concepts
90
+ global_variables.save_metadata(current_split)
91
+ gr.Info("Submit success")
92
+ return get_next_image(
93
+ split,
94
+ index,
95
+ filtered_indices,
96
+ profile
97
+ )
98
+
99
+
100
+ with gr.Blocks() as interface:
101
+ with gr.Row():
102
+ with gr.Column():
103
+ with gr.Group():
104
+ gr.Markdown(
105
+ "## # Image Selection",
106
+ )
107
+ split = gr.Radio(
108
+ label="Split",
109
+ choices=["train", "validation", "test"],
110
+ value="train",
111
+ )
112
+ index = gr.Textbox(
113
+ value="0",
114
+ label="Index",
115
+ max_lines=1,
116
+ )
117
+ with gr.Group():
118
+ voted_concepts = gr.CheckboxGroup(
119
+ label="Voted Concepts",
120
+ choices=CONCEPTS,
121
+ )
122
+
123
+ with gr.Row():
124
+ prev_button = gr.Button(
125
+ value="Prev",
126
+ )
127
+ next_button = gr.Button(
128
+ value="Next",
129
+ )
130
+ gr.LoginButton()
131
+ submit_button = gr.Button(
132
+ value="Submit",
133
+ )
134
+ with gr.Group():
135
+ gr.Markdown(
136
+ "## # Image Info",
137
+ )
138
+ im_class = gr.Textbox(
139
+ label="Class",
140
+ )
141
+ im_concepts = gr.JSON(
142
+ label="Concepts",
143
+ )
144
+ with gr.Column():
145
+ image = gr.Image(
146
+ label="Image",
147
+ )
148
+ current_image = gr.State(None)
149
+ filtered_indices = gr.State({
150
+ split: list(range(len(global_variables.all_metadata[split])))
151
+ for split in global_variables.all_metadata
152
+ })
153
+ common_output = [
154
+ image,
155
+ voted_concepts,
156
+ current_image,
157
+ im_class,
158
+ im_concepts,
159
+ index,
160
+ filtered_indices,
161
+ ]
162
+ common_input = [split, index, filtered_indices]
163
+ prev_button.click(
164
+ get_prev_image,
165
+ inputs=common_input,
166
+ outputs=common_output
167
+ )
168
+ next_button.click(
169
+ get_next_image,
170
+ inputs=common_input,
171
+ outputs=common_output
172
+ )
173
+ submit_button.click(
174
+ submit_label,
175
+ inputs=[voted_concepts, current_image, split, index, filtered_indices],
176
+ outputs=common_output
177
+ )
178
+ index.submit(
179
+ get_current_image,
180
+ inputs=common_input,
181
+ outputs=common_output,
182
+ )
183
+ interface.load(
184
+ get_current_image,
185
+ inputs=common_input,
186
+ outputs=common_output,
187
+ )
src/vote_interface.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Interface for labeling concepts in images.
2
+ """
3
+
4
+ import gradio as gr
5
+
6
+ from src import global_variables
7
+
8
+
9
+ def get_votes(
10
+ split,
11
+ profile: gr.OAuthProfile
12
+ ):
13
+ username = profile.username
14
+ vote_list = []
15
+ for i,s in enumerate(global_variables.all_metadata[split]):
16
+ if "votes" in s and username in s["votes"]:
17
+ vote_list.append(f'[{i}]: {s["votes"][username]}')
18
+
19
+ return "\n".join(vote_list)
20
+
21
+
22
+ with gr.Blocks() as interface:
23
+ with gr.Row():
24
+ with gr.Column():
25
+ with gr.Group():
26
+ gr.Markdown(
27
+ "## # Selection",
28
+ )
29
+ split = gr.Radio(
30
+ label="Split",
31
+ choices=["train", "validation", "test"],
32
+ value="train",
33
+ )
34
+
35
+ with gr.Row():
36
+ gr.LoginButton()
37
+ get_button = gr.Button(
38
+ value="Get My Votes",
39
+ )
40
+ with gr.Column():
41
+ votes = gr.TextArea(
42
+ label="Votes",
43
+ lines=1,
44
+ )
45
+ get_button.click(
46
+ get_votes,
47
+ inputs=[split],
48
+ outputs=[votes]
49
+ )