Spaces:
Runtime error
Runtime error
Commit ·
f4ef5b4
1
Parent(s): 1709520
Update flagging functionality in app.py and flagging.py to use metadata for user information and change dataset name to versioned format
Browse files- app.py +14 -5
- flagging.py +13 -20
app.py
CHANGED
|
@@ -69,21 +69,26 @@ def inference(image):
|
|
| 69 |
|
| 70 |
|
| 71 |
def flag_img_input(
|
| 72 |
-
image: gr.Image,
|
| 73 |
):
|
| 74 |
"""Wrapper for flagging"""
|
| 75 |
-
logger.info("Flagging image -
|
| 76 |
|
| 77 |
# Decode blob data if necessary
|
| 78 |
if is_blob_data(image):
|
| 79 |
image = decode_blob_data(image)
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
logger.info("Image flagged successfully")
|
| 83 |
|
| 84 |
|
| 85 |
# Flagging
|
| 86 |
-
dataset_name = "SEA-AI/crowdsourced-sea-images"
|
| 87 |
hf_writer = HuggingFaceDatasetSaver(get_token(), dataset_name)
|
| 88 |
flagged_counter = FlaggedCounter(dataset_name)
|
| 89 |
|
|
@@ -148,6 +153,10 @@ with gr.Blocks(theme=theme, css=css, title="SEA.AI Vision Demo") as demo:
|
|
| 148 |
flag: gr.Button(FLAG_TXT, interactive=True, visible=visible),
|
| 149 |
notice: gr.Markdown(value=NOTICE, visible=visible),
|
| 150 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
# This needs to be called prior to the first call to callback.flag()
|
| 153 |
hf_writer.setup([img_input], "flagged")
|
|
@@ -160,7 +169,7 @@ with gr.Blocks(theme=theme, css=css, title="SEA.AI Vision Demo") as demo:
|
|
| 160 |
show_api=False,
|
| 161 |
).then(
|
| 162 |
flag_img_input,
|
| 163 |
-
[img_input],
|
| 164 |
[],
|
| 165 |
preprocess=False,
|
| 166 |
show_api=True,
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
def flag_img_input(
|
| 72 |
+
image: gr.Image, name: str = "misdetection", email: str = "anonymous@example.com"
|
| 73 |
):
|
| 74 |
"""Wrapper for flagging"""
|
| 75 |
+
logger.info("Flagging image - name: %s, email: %s", name, email)
|
| 76 |
|
| 77 |
# Decode blob data if necessary
|
| 78 |
if is_blob_data(image):
|
| 79 |
image = decode_blob_data(image)
|
| 80 |
|
| 81 |
+
metadata = {
|
| 82 |
+
"name": name,
|
| 83 |
+
"email": email,
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
hf_writer.flag([image], metadata=metadata)
|
| 87 |
logger.info("Image flagged successfully")
|
| 88 |
|
| 89 |
|
| 90 |
# Flagging
|
| 91 |
+
dataset_name = "SEA-AI/crowdsourced-sea-images-v2"
|
| 92 |
hf_writer = HuggingFaceDatasetSaver(get_token(), dataset_name)
|
| 93 |
flagged_counter = FlaggedCounter(dataset_name)
|
| 94 |
|
|
|
|
| 153 |
flag: gr.Button(FLAG_TXT, interactive=True, visible=visible),
|
| 154 |
notice: gr.Markdown(value=NOTICE, visible=visible),
|
| 155 |
}
|
| 156 |
+
|
| 157 |
+
# add hidden textbox for name and email (hacky but well...)
|
| 158 |
+
name = gr.Textbox(label="name", visible=False, value="anonymous")
|
| 159 |
+
email = gr.Textbox(label="email", visible=False, value="anonymous@example.com")
|
| 160 |
|
| 161 |
# This needs to be called prior to the first call to callback.flag()
|
| 162 |
hf_writer.setup([img_input], "flagged")
|
|
|
|
| 169 |
show_api=False,
|
| 170 |
).then(
|
| 171 |
flag_img_input,
|
| 172 |
+
[img_input, name, email],
|
| 173 |
[],
|
| 174 |
preprocess=False,
|
| 175 |
show_api=True,
|
flagging.py
CHANGED
|
@@ -108,11 +108,10 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
|
|
| 108 |
except huggingface_hub.utils.EntryNotFoundError:
|
| 109 |
pass
|
| 110 |
|
| 111 |
-
def flag(
|
| 112 |
self,
|
| 113 |
flag_data: list[Any],
|
| 114 |
-
|
| 115 |
-
username: str | None = None,
|
| 116 |
) -> int:
|
| 117 |
if self.separate_dirs:
|
| 118 |
# JSONL files to support dataset preview on the Hub
|
|
@@ -131,8 +130,7 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
|
|
| 131 |
components_dir=components_dir,
|
| 132 |
path_in_repo=path_in_repo,
|
| 133 |
flag_data=flag_data,
|
| 134 |
-
|
| 135 |
-
username=username or "",
|
| 136 |
)
|
| 137 |
|
| 138 |
def _flag_in_dir(
|
|
@@ -141,12 +139,11 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
|
|
| 141 |
components_dir: Path,
|
| 142 |
path_in_repo: str | None,
|
| 143 |
flag_data: list[Any],
|
| 144 |
-
|
| 145 |
-
username: str = "",
|
| 146 |
) -> int:
|
| 147 |
# Deserialize components (write images/audio to files)
|
| 148 |
features, row = self._deserialize_components(
|
| 149 |
-
components_dir, flag_data,
|
| 150 |
)
|
| 151 |
|
| 152 |
# Write generic info to dataset_infos.json + upload
|
|
@@ -226,8 +223,7 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
|
|
| 226 |
self,
|
| 227 |
data_dir: Path,
|
| 228 |
flag_data: list[Any],
|
| 229 |
-
|
| 230 |
-
username: str = "",
|
| 231 |
) -> tuple[dict[Any, Any], list[Any]]:
|
| 232 |
"""Deserialize components and return the corresponding row for the flagged sample.
|
| 233 |
|
|
@@ -279,10 +275,9 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
|
|
| 279 |
)
|
| 280 |
else:
|
| 281 |
row.append("")
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
row.append(username)
|
| 286 |
return features, row
|
| 287 |
|
| 288 |
|
|
@@ -299,8 +294,7 @@ class myHuggingFaceDatasetSaver(HuggingFaceDatasetSaver):
|
|
| 299 |
self,
|
| 300 |
data_dir: Path,
|
| 301 |
flag_data: list[Any],
|
| 302 |
-
|
| 303 |
-
username: str = "",
|
| 304 |
) -> tuple[dict[Any, Any], list[Any]]:
|
| 305 |
"""Deserialize components and return the corresponding row for the flagged sample.
|
| 306 |
|
|
@@ -353,8 +347,7 @@ class myHuggingFaceDatasetSaver(HuggingFaceDatasetSaver):
|
|
| 353 |
)
|
| 354 |
else:
|
| 355 |
row.append("")
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
row.append(username)
|
| 360 |
return features, row
|
|
|
|
| 108 |
except huggingface_hub.utils.EntryNotFoundError:
|
| 109 |
pass
|
| 110 |
|
| 111 |
+
def flag( # pylint: disable=arguments-differ
|
| 112 |
self,
|
| 113 |
flag_data: list[Any],
|
| 114 |
+
metadata: dict[str, str] | None = None,
|
|
|
|
| 115 |
) -> int:
|
| 116 |
if self.separate_dirs:
|
| 117 |
# JSONL files to support dataset preview on the Hub
|
|
|
|
| 130 |
components_dir=components_dir,
|
| 131 |
path_in_repo=path_in_repo,
|
| 132 |
flag_data=flag_data,
|
| 133 |
+
metadata=metadata if metadata is not None else {},
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
def _flag_in_dir(
|
|
|
|
| 139 |
components_dir: Path,
|
| 140 |
path_in_repo: str | None,
|
| 141 |
flag_data: list[Any],
|
| 142 |
+
metadata: dict[str, str],
|
|
|
|
| 143 |
) -> int:
|
| 144 |
# Deserialize components (write images/audio to files)
|
| 145 |
features, row = self._deserialize_components(
|
| 146 |
+
components_dir, flag_data, metadata
|
| 147 |
)
|
| 148 |
|
| 149 |
# Write generic info to dataset_infos.json + upload
|
|
|
|
| 223 |
self,
|
| 224 |
data_dir: Path,
|
| 225 |
flag_data: list[Any],
|
| 226 |
+
metadata: dict[str, str],
|
|
|
|
| 227 |
) -> tuple[dict[Any, Any], list[Any]]:
|
| 228 |
"""Deserialize components and return the corresponding row for the flagged sample.
|
| 229 |
|
|
|
|
| 275 |
)
|
| 276 |
else:
|
| 277 |
row.append("")
|
| 278 |
+
for key, value in metadata.items():
|
| 279 |
+
features[key] = {"dtype": "string", "_type": "Value"}
|
| 280 |
+
row.append(value)
|
|
|
|
| 281 |
return features, row
|
| 282 |
|
| 283 |
|
|
|
|
| 294 |
self,
|
| 295 |
data_dir: Path,
|
| 296 |
flag_data: list[Any],
|
| 297 |
+
metadata: dict[str, str],
|
|
|
|
| 298 |
) -> tuple[dict[Any, Any], list[Any]]:
|
| 299 |
"""Deserialize components and return the corresponding row for the flagged sample.
|
| 300 |
|
|
|
|
| 347 |
)
|
| 348 |
else:
|
| 349 |
row.append("")
|
| 350 |
+
for key, value in metadata.items():
|
| 351 |
+
features[key] = {"dtype": "string", "_type": "Value"}
|
| 352 |
+
row.append(value)
|
|
|
|
| 353 |
return features, row
|