Skip to content

Commit

Permalink
feat: improve Slack & nutripatrol notifiers
Browse files Browse the repository at this point in the history
- don't send beauty events to nutripatrol
- send real text comment to Nutripatrol
- stop sending moderation notifications to Slack
  • Loading branch information
raphael0202 committed Oct 28, 2024
1 parent 96401ef commit ef0f861
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 57 deletions.
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 @@ def notify_image_flag(
):
reason = "human"
elif prediction_subtype == "text" and prediction_label == "beauty":
reason = "beauty"
# Don't send beauty text detection to moderation service for
# now
continue

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"

data = {
"barcode": product_id.barcode,
Expand All @@ -205,9 +215,7 @@ def notify_image_flag(
"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 @@ class SlackNotifier(NotifierInterface):

# 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 @@ def __init__(self, slack_token: str):
"""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

0 comments on commit ef0f861

Please sign in to comment.