Xmaster6y commited on
Commit
ed12cc6
1 Parent(s): 1f64946

label interface

Browse files
src/explore_interface.py CHANGED
@@ -18,10 +18,13 @@ def get_next_image(
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
- if split == "all":
22
- split = random.choice(["train", "validation", "test"])
23
  try:
24
- sample = random.choice(current_metadata[split])
 
 
 
 
 
25
  image_path = f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/{sample['file_name']}"
26
  return image_path, sample["class"], sample["concepts"], current_metadata, selected_concepts
27
  except IndexError:
@@ -40,8 +43,9 @@ with gr.Blocks() as interface:
40
  choices=["train", "validation", "test", "all"],
41
  value="train",
42
  )
43
- concepts = gr.CheckboxGroup(
44
  label="Concepts",
 
45
  choices=CONCEPTS,
46
  )
47
  button = gr.Button(
 
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:
 
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(
src/global_variables.py CHANGED
@@ -28,17 +28,32 @@ def setup():
28
  for row in reader:
29
  all_metadata[split].append(row)
30
 
31
- def save_metadata():
32
  global all_metadata
33
- for split, values in all_metadata.items():
34
- with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl", mode='w') as writer:
35
- writer.write_all(values)
36
- hf_api.upload_file(
37
- path_or_fileobj=f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl",
38
- path_in_repo=f"data/{split}/metadata.jsonl",
39
- repo_id=DATASET_NAME,
40
- repo_type="dataset",
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if gr.NO_RELOAD:
44
  setup()
 
28
  for row in reader:
29
  all_metadata[split].append(row)
30
 
31
+ def get_metadata(split):
32
  global all_metadata
33
+ global hf_api
34
+ hf_api.hf_hub_download(
35
+ repo_id=DATASET_NAME,
36
+ filename="metadata.jsonl",
37
+ subfolder=f"data/{split}",
38
+ repo_type="dataset",
39
+ local_dir=f"{ASSETS_FOLDER}/{DATASET_NAME}",
40
+ )
41
+ all_metadata[split] = []
42
+ with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl") as reader:
43
+ for row in reader:
44
+ all_metadata[split].append(row)
45
+
46
+ def save_metadata(split):
47
+ global all_metadata
48
+ values = all_metadata[split]
49
+ with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl", mode='w') as writer:
50
+ writer.write_all(values)
51
+ hf_api.upload_file(
52
+ path_or_fileobj=f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl",
53
+ path_in_repo=f"data/{split}/metadata.jsonl",
54
+ repo_id=DATASET_NAME,
55
+ repo_type="dataset",
56
+ )
57
 
58
  if gr.NO_RELOAD:
59
  setup()
src/label_interface.py CHANGED
@@ -1,18 +1,101 @@
1
  """Interface for labeling concepts in images.
2
  """
3
 
 
 
 
 
4
  import gradio as gr
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  with gr.Blocks() as interface:
8
  with gr.Row():
9
  with gr.Column():
10
- gr.Image(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  label="Image",
12
  )
13
- gr.Textbox(
14
- label="Label",
15
- )
16
- gr.Button(
17
- value="Submit",
18
- )
 
 
 
 
 
 
1
  """Interface for labeling concepts in images.
2
  """
3
 
4
+ from typing import Optional
5
+
6
+ import random
7
+
8
  import gradio as gr
9
 
10
+ from src import global_variables
11
+ 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:
23
+ username_votes = sample["votes"][username]
24
+ voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
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:
62
  with gr.Row():
63
  with gr.Column():
64
+ with gr.Group():
65
+ gr.Markdown(
66
+ "## # Image Selection",
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",
81
+ )
82
+ gr.LoginButton()
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
+ )