Spaces:
Runtime error
Runtime error
vote not in metadata
Browse files- src/global_variables.py +71 -1
- src/label_interface.py +18 -20
- src/sample_interface.py +19 -20
src/global_variables.py
CHANGED
|
@@ -8,7 +8,7 @@ import jsonlines
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
-
from src.constants import DATASET_NAME, HF_TOKEN, ASSETS_FOLDER
|
| 12 |
|
| 13 |
hf_api: HfApi
|
| 14 |
all_metadata: dict
|
|
@@ -64,6 +64,76 @@ def save_metadata(split):
|
|
| 64 |
repo_type="dataset",
|
| 65 |
)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def get_votes(key):
|
| 68 |
global all_votes
|
| 69 |
global hf_api
|
|
|
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
+
from src.constants import DATASET_NAME, HF_TOKEN, ASSETS_FOLDER, CONCEPTS
|
| 12 |
|
| 13 |
hf_api: HfApi
|
| 14 |
all_metadata: dict
|
|
|
|
| 64 |
repo_type="dataset",
|
| 65 |
)
|
| 66 |
|
| 67 |
+
def update_votes(
|
| 68 |
+
username: str,
|
| 69 |
+
current_image: str,
|
| 70 |
+
voted_concepts: list,
|
| 71 |
+
):
|
| 72 |
+
global all_metadata
|
| 73 |
+
global all_votes
|
| 74 |
+
current_split, idx = current_image.split(":")
|
| 75 |
+
idx = int(idx)
|
| 76 |
+
s_id = all_metadata[current_split][idx]["id"]
|
| 77 |
+
if s_id not in all_votes:
|
| 78 |
+
all_votes[s_id] = {}
|
| 79 |
+
all_votes[s_id][username] = {c: c in voted_concepts for c in CONCEPTS}
|
| 80 |
+
new_concepts = compute_concepts(all_votes[s_id])
|
| 81 |
+
all_metadata[current_split][idx]["concepts"] = new_concepts
|
| 82 |
+
|
| 83 |
+
def compute_concepts(votes):
|
| 84 |
+
vote_sum = {c: 0 for c in CONCEPTS}
|
| 85 |
+
for vote in votes.values():
|
| 86 |
+
for c in CONCEPTS:
|
| 87 |
+
if c not in vote:
|
| 88 |
+
continue
|
| 89 |
+
vote_sum[c] += 2 * vote[c] - 1
|
| 90 |
+
return {c: vote_sum[c] > 0 if vote_sum[c] != 0 else None for c in CONCEPTS}
|
| 91 |
+
|
| 92 |
+
def save_current_work(
|
| 93 |
+
username: str,
|
| 94 |
+
):
|
| 95 |
+
global all_metadata
|
| 96 |
+
global all_votes
|
| 97 |
+
global hf_api
|
| 98 |
+
hf_api.snapshot_download(
|
| 99 |
+
local_dir=f"{ASSETS_FOLDER}/{DATASET_NAME}",
|
| 100 |
+
repo_id=DATASET_NAME,
|
| 101 |
+
repo_type="dataset",
|
| 102 |
+
allow_patterns=["*/metadata.jsonl", "votes/*"],
|
| 103 |
+
)
|
| 104 |
+
new_votes = {}
|
| 105 |
+
for file in os.listdir(f"{ASSETS_FOLDER}/{DATASET_NAME}/votes"):
|
| 106 |
+
with open(f"{ASSETS_FOLDER}/{DATASET_NAME}/votes/{file}") as f:
|
| 107 |
+
key = file.split(".")[0]
|
| 108 |
+
new_votes[key] = json.load(f)
|
| 109 |
+
for key in all_votes:
|
| 110 |
+
if username in all_votes[key]:
|
| 111 |
+
if key not in new_votes:
|
| 112 |
+
new_votes[key] = {}
|
| 113 |
+
new_votes[key][username] = all_votes[key][username]
|
| 114 |
+
for key in new_votes:
|
| 115 |
+
with open(f"{ASSETS_FOLDER}/{DATASET_NAME}/votes/{key}.json", "w") as f:
|
| 116 |
+
json.dump(new_votes[key], f)
|
| 117 |
+
all_votes = new_votes
|
| 118 |
+
new_metadata = {}
|
| 119 |
+
for split in ["train", "validation", "test"]:
|
| 120 |
+
new_metadata[split] = []
|
| 121 |
+
with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl") as reader:
|
| 122 |
+
for row in reader:
|
| 123 |
+
s_id = row["id"]
|
| 124 |
+
if s_id in all_votes:
|
| 125 |
+
row["concepts"] = compute_concepts(all_votes[s_id])
|
| 126 |
+
new_metadata[split].append(row)
|
| 127 |
+
with jsonlines.open(f"{ASSETS_FOLDER}/{DATASET_NAME}/data/{split}/metadata.jsonl", mode='w') as writer:
|
| 128 |
+
writer.write_all(new_metadata[split])
|
| 129 |
+
all_metadata = new_metadata
|
| 130 |
+
hf_api.upload_folder(
|
| 131 |
+
folder_path=f"{ASSETS_FOLDER}/{DATASET_NAME}",
|
| 132 |
+
repo_id=DATASET_NAME,
|
| 133 |
+
repo_type="dataset",
|
| 134 |
+
allow_patterns=["*/metadata.jsonl", "votes/*"],
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
def get_votes(key):
|
| 138 |
global all_votes
|
| 139 |
global hf_api
|
src/label_interface.py
CHANGED
|
@@ -86,25 +86,9 @@ def submit_label(
|
|
| 86 |
if current_image is None:
|
| 87 |
gr.Warning("No image selected.")
|
| 88 |
return None, None, None, None, None, None, None, filtered_indices, selected_concepts, selected_sample_type
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
s_id = global_variables.all_metadata[current_split][idx]["id"]
|
| 93 |
-
global_variables.get_votes(s_id)
|
| 94 |
-
if s_id not in global_variables.all_votes:
|
| 95 |
-
global_variables.all_votes[s_id] = {}
|
| 96 |
-
global_variables.all_votes[s_id][username] = {c: c in voted_concepts for c in CONCEPTS}
|
| 97 |
-
vote_sum = {c: 0 for c in CONCEPTS}
|
| 98 |
-
new_concepts = {}
|
| 99 |
-
for c in CONCEPTS:
|
| 100 |
-
for vote in global_variables.all_votes[s_id].values():
|
| 101 |
-
if c not in vote:
|
| 102 |
-
continue
|
| 103 |
-
vote_sum[c] += 2 * vote[c] - 1
|
| 104 |
-
new_concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
|
| 105 |
-
global_variables.all_metadata[current_split][idx]["concepts"] = new_concepts
|
| 106 |
-
global_variables.save_metadata(current_split)
|
| 107 |
-
global_variables.save_votes(s_id)
|
| 108 |
gr.Info("Submit success")
|
| 109 |
return get_next_image(
|
| 110 |
split,
|
|
@@ -116,6 +100,12 @@ def submit_label(
|
|
| 116 |
profile
|
| 117 |
)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
with gr.Blocks() as interface:
|
| 121 |
with gr.Row():
|
|
@@ -160,7 +150,11 @@ with gr.Blocks() as interface:
|
|
| 160 |
)
|
| 161 |
gr.LoginButton()
|
| 162 |
submit_button = gr.Button(
|
| 163 |
-
value="Submit",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
)
|
| 165 |
with gr.Group():
|
| 166 |
gr.Markdown(
|
|
@@ -205,3 +199,7 @@ with gr.Blocks() as interface:
|
|
| 205 |
inputs=[voted_concepts, current_image, split, concepts, sample_type, filtered_indices, selected_concepts, selected_sample_type],
|
| 206 |
outputs=common_output
|
| 207 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if current_image is None:
|
| 87 |
gr.Warning("No image selected.")
|
| 88 |
return None, None, None, None, None, None, None, filtered_indices, selected_concepts, selected_sample_type
|
| 89 |
+
|
| 90 |
+
global_variables.update_votes(username, current_image, voted_concepts)
|
| 91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
gr.Info("Submit success")
|
| 93 |
return get_next_image(
|
| 94 |
split,
|
|
|
|
| 100 |
profile
|
| 101 |
)
|
| 102 |
|
| 103 |
+
def save_current_work(
|
| 104 |
+
profile: gr.OAuthProfile,
|
| 105 |
+
):
|
| 106 |
+
username = profile.username
|
| 107 |
+
global_variables.save_current_work(username)
|
| 108 |
+
gr.Info("Save success")
|
| 109 |
|
| 110 |
with gr.Blocks() as interface:
|
| 111 |
with gr.Row():
|
|
|
|
| 150 |
)
|
| 151 |
gr.LoginButton()
|
| 152 |
submit_button = gr.Button(
|
| 153 |
+
value="Local Submit",
|
| 154 |
+
)
|
| 155 |
+
with gr.Row():
|
| 156 |
+
save_button = gr.Button(
|
| 157 |
+
value="Save",
|
| 158 |
)
|
| 159 |
with gr.Group():
|
| 160 |
gr.Markdown(
|
|
|
|
| 199 |
inputs=[voted_concepts, current_image, split, concepts, sample_type, filtered_indices, selected_concepts, selected_sample_type],
|
| 200 |
outputs=common_output
|
| 201 |
)
|
| 202 |
+
save_button.click(
|
| 203 |
+
save_current_work,
|
| 204 |
+
outputs=[image]
|
| 205 |
+
)
|
src/sample_interface.py
CHANGED
|
@@ -77,25 +77,9 @@ def submit_label(
|
|
| 77 |
if current_image is None:
|
| 78 |
gr.Warning("No image selected.")
|
| 79 |
return None, None, None, None, None, None, None, index, filtered_indices
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
s_id = global_variables.all_metadata[current_split][idx]["id"]
|
| 84 |
-
global_variables.get_votes(s_id)
|
| 85 |
-
if s_id not in global_variables.all_votes:
|
| 86 |
-
global_variables.all_votes[s_id] = {}
|
| 87 |
-
global_variables.all_votes[s_id][username] = {c: c in voted_concepts for c in CONCEPTS}
|
| 88 |
-
vote_sum = {c: 0 for c in CONCEPTS}
|
| 89 |
-
new_concepts = {}
|
| 90 |
-
for c in CONCEPTS:
|
| 91 |
-
for vote in global_variables.all_votes[s_id].values():
|
| 92 |
-
if c not in vote:
|
| 93 |
-
continue
|
| 94 |
-
vote_sum[c] += 2 * vote[c] - 1
|
| 95 |
-
new_concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
|
| 96 |
-
global_variables.all_metadata[current_split][idx]["concepts"] = new_concepts
|
| 97 |
-
global_variables.save_metadata(current_split)
|
| 98 |
-
global_variables.save_votes(s_id)
|
| 99 |
gr.Info("Submit success")
|
| 100 |
return get_next_image(
|
| 101 |
split,
|
|
@@ -104,6 +88,12 @@ def submit_label(
|
|
| 104 |
profile
|
| 105 |
)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
with gr.Blocks() as interface:
|
| 109 |
with gr.Row():
|
|
@@ -145,7 +135,11 @@ with gr.Blocks() as interface:
|
|
| 145 |
)
|
| 146 |
gr.LoginButton()
|
| 147 |
submit_button = gr.Button(
|
| 148 |
-
value="Submit",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
)
|
| 150 |
with gr.Group():
|
| 151 |
gr.Markdown(
|
|
@@ -198,3 +192,8 @@ with gr.Blocks() as interface:
|
|
| 198 |
inputs=common_input,
|
| 199 |
outputs=common_output,
|
| 200 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if current_image is None:
|
| 78 |
gr.Warning("No image selected.")
|
| 79 |
return None, None, None, None, None, None, None, index, filtered_indices
|
| 80 |
+
|
| 81 |
+
global_variables.update_votes(username, current_image, voted_concepts)
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
gr.Info("Submit success")
|
| 84 |
return get_next_image(
|
| 85 |
split,
|
|
|
|
| 88 |
profile
|
| 89 |
)
|
| 90 |
|
| 91 |
+
def save_current_work(
|
| 92 |
+
profile: gr.OAuthProfile,
|
| 93 |
+
):
|
| 94 |
+
username = profile.username
|
| 95 |
+
global_variables.save_current_work(username)
|
| 96 |
+
gr.Info("Save success")
|
| 97 |
|
| 98 |
with gr.Blocks() as interface:
|
| 99 |
with gr.Row():
|
|
|
|
| 135 |
)
|
| 136 |
gr.LoginButton()
|
| 137 |
submit_button = gr.Button(
|
| 138 |
+
value="Local Submit",
|
| 139 |
+
)
|
| 140 |
+
with gr.Row():
|
| 141 |
+
save_button = gr.Button(
|
| 142 |
+
value="Save",
|
| 143 |
)
|
| 144 |
with gr.Group():
|
| 145 |
gr.Markdown(
|
|
|
|
| 192 |
inputs=common_input,
|
| 193 |
outputs=common_output,
|
| 194 |
)
|
| 195 |
+
|
| 196 |
+
save_button.click(
|
| 197 |
+
save_current_work,
|
| 198 |
+
outputs=[image]
|
| 199 |
+
)
|