Disable notification discussion fallback
Browse files- src/notifications.py +15 -19
src/notifications.py
CHANGED
|
@@ -6,9 +6,8 @@ leaderboard Space, which triggers Hugging Face's native notification.
|
|
| 6 |
Configuration (see :mod:`src.envs`):
|
| 7 |
* ``HF_TOKEN`` β token used to post the comment / discussion.
|
| 8 |
* ``REPO_ID`` β the Space repo id (default notification target).
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
notification.
|
| 12 |
"""
|
| 13 |
|
| 14 |
import logging
|
|
@@ -46,28 +45,25 @@ def send_hf_notification(
|
|
| 46 |
repo_id = repo_id or REPO_ID
|
| 47 |
if discussion_num is None:
|
| 48 |
discussion_num = NOTIFICATION_DISCUSSION_NUM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 51 |
comment = f"@{username}\n\n{message}\n\nTime: {now}"
|
| 52 |
|
| 53 |
api = HfApi(token=HF_TOKEN)
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
url = f"https://huggingface.co/spaces/{repo_id}/discussions/{discussion_num}"
|
| 63 |
-
else:
|
| 64 |
-
discussion = api.create_discussion(
|
| 65 |
-
repo_id=repo_id,
|
| 66 |
-
repo_type="space",
|
| 67 |
-
title=f"Task notification for @{username}",
|
| 68 |
-
description=comment,
|
| 69 |
-
)
|
| 70 |
-
url = getattr(discussion, "url", f"https://huggingface.co/spaces/{repo_id}/discussions")
|
| 71 |
except Exception:
|
| 72 |
logger.error("[notify] failed to notify @%s", username, exc_info=True)
|
| 73 |
return None
|
|
|
|
| 6 |
Configuration (see :mod:`src.envs`):
|
| 7 |
* ``HF_TOKEN`` β token used to post the comment / discussion.
|
| 8 |
* ``REPO_ID`` β the Space repo id (default notification target).
|
| 9 |
+
* ``NOTIFICATION_DISCUSSION_NUM`` β comments are posted on this existing
|
| 10 |
+
discussion. If unset, notification sending is skipped.
|
|
|
|
| 11 |
"""
|
| 12 |
|
| 13 |
import logging
|
|
|
|
| 45 |
repo_id = repo_id or REPO_ID
|
| 46 |
if discussion_num is None:
|
| 47 |
discussion_num = NOTIFICATION_DISCUSSION_NUM
|
| 48 |
+
if not discussion_num:
|
| 49 |
+
logger.warning(
|
| 50 |
+
"[notify] NOTIFICATION_DISCUSSION_NUM not set β skipping notification for %r",
|
| 51 |
+
username,
|
| 52 |
+
)
|
| 53 |
+
return None
|
| 54 |
|
| 55 |
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 56 |
comment = f"@{username}\n\n{message}\n\nTime: {now}"
|
| 57 |
|
| 58 |
api = HfApi(token=HF_TOKEN)
|
| 59 |
try:
|
| 60 |
+
api.comment_discussion(
|
| 61 |
+
repo_id=repo_id,
|
| 62 |
+
repo_type="space",
|
| 63 |
+
discussion_num=int(discussion_num),
|
| 64 |
+
comment=comment,
|
| 65 |
+
)
|
| 66 |
+
url = f"https://huggingface.co/spaces/{repo_id}/discussions/{discussion_num}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception:
|
| 68 |
logger.error("[notify] failed to notify @%s", username, exc_info=True)
|
| 69 |
return None
|