Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve Slack & nutripatrol notifiers #1450

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 12 additions & 43 deletions robotoff/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,17 @@
):
reason = "human"
elif prediction_subtype == "text" and prediction_label == "beauty":
reason = "beauty"
# Don't send beauty text detection to moderation service for
# now
continue

Check warning on line 198 in robotoff/notifier.py

View check run for this annotation

Codecov / codecov/patch

robotoff/notifier.py#L198

Added line #L198 was not covered by tests

if "label" in prediction.data:
if prediction_subtype == "text":
comment = f"Robotoff detection: '{prediction.data['text']}' ({prediction.data['label']})"
else:
comment = f"Robotoff detection: {prediction.data['label']}"
else:
comment = "Robotoff detection"

Check warning on line 206 in robotoff/notifier.py

View check run for this annotation

Codecov / codecov/patch

robotoff/notifier.py#L206

Added line #L206 was not covered by tests

data = {
"barcode": product_id.barcode,
Expand All @@ -205,9 +215,7 @@
"image_id": image_id,
"flavor": product_id.server_type.value,
"reason": reason,
"comment": json.dumps(
{k: v for k, v in prediction.data.items() if k != "likelihood"}
),
"comment": comment,
}
try:
logger.info("Notifying image %s to moderation service", image_url)
Expand All @@ -228,7 +236,6 @@

# Slack channel IDs.
ROBOTOFF_ALERT_CHANNEL = "CGKPALRCG" # robotoff-alerts-annotations
ROBOTOFF_PRIVATE_IMAGE_ALERT_CHANNEL = "GGMRWLEF2" # moderation-off-alerts-private

BASE_URL = "https://slack.com/api"
POST_MESSAGE_URL = BASE_URL + "/chat.postMessage"
Expand All @@ -241,44 +248,6 @@
"""Should not be called directly, use the NotifierFactory instead."""
self.slack_token = slack_token

def notify_image_flag(
self,
predictions: list[Prediction],
source_image: str,
product_id: ProductIdentifier,
):
"""Sends alerts to Slack channels for flagged images."""
if not predictions:
return

text = ""
slack_channel = self.ROBOTOFF_PRIVATE_IMAGE_ALERT_CHANNEL

for flagged in predictions:
flag_type = flagged.data["type"]
label = flagged.data["label"]

if not _sensitive_image(flag_type, label):
continue

if flag_type in ("safe_search_annotation", "label_annotation"):
likelihood = flagged.data["likelihood"]
text += f"type: {flag_type}\nlabel: *{label}*, score: {likelihood}\n"
else:
match_text = flagged.data["text"]
text += f"type: {flag_type}\nlabel: *{label}*, match: {match_text}\n"

if text:
edit_url = f"{settings.BaseURLProvider.world(product_id.server_type)}/cgi/product.pl?type=edit&code={product_id.barcode}"
image_url = settings.BaseURLProvider.image_url(
product_id.server_type, source_image
)

full_text = f"{text}\n <{image_url}|Image> -- <{edit_url}|*Edit*>"
message = _slack_message_block(full_text, with_image=image_url)

self._post_message(message, slack_channel, **self.COLLAPSE_LINKS_PARAMS)

def notify_automatic_processing(self, insight: ProductInsight):
server_type = ServerType[insight.server_type]
product_url = (
Expand Down
18 changes: 4 additions & 14 deletions tests/unit/test_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_notify_image_flag_public(mocker, monkeypatch):
[
Prediction(
type=PredictionType.image_flag,
data={"text": "bad_word", "type": "SENSITIVE", "label": "flagged"},
data={"text": "bad_word", "type": "text", "label": "flagged"},
)
],
"/source_image/2.jpg",
Expand All @@ -116,7 +116,7 @@ def test_notify_image_flag_public(mocker, monkeypatch):
"image_id": "2",
"flavor": "off",
"reason": "other",
"comment": '{"text": "bad_word", "type": "SENSITIVE", "label": "flagged"}',
"comment": "Robotoff detection: 'bad_word' (flagged)",
},
)

Expand All @@ -143,17 +143,7 @@ def test_notify_image_flag_private(mocker, monkeypatch):
DEFAULT_PRODUCT_ID,
)

assert len(mock_http.mock_calls) == 2
mock_http.assert_any_call(
slack_notifier.POST_MESSAGE_URL,
data=PartialRequestMatcher(
f"type: label_annotation\nlabel: *face*, score: 0.8\n\n <{settings.BaseURLProvider.image_url(DEFAULT_SERVER_TYPE, '/source_image/2.jpg')}|Image> -- <{settings.BaseURLProvider.world(DEFAULT_SERVER_TYPE)}/cgi/product.pl?type=edit&code=123|*Edit*>",
slack_notifier.ROBOTOFF_PRIVATE_IMAGE_ALERT_CHANNEL,
settings.BaseURLProvider.image_url(
DEFAULT_SERVER_TYPE, "/source_image/2.jpg"
),
),
)
assert len(mock_http.mock_calls) == 1
mock_http.assert_any_call(
"https://images.org",
json={
Expand All @@ -165,7 +155,7 @@ def test_notify_image_flag_private(mocker, monkeypatch):
"image_id": "2",
"flavor": "off",
"reason": "human",
"comment": '{"type": "label_annotation", "label": "face"}',
"comment": "Robotoff detection: face",
"confidence": 0.8,
},
)
Expand Down
Loading