Skip to content

[Feature] Add Swanlab Slack notification #4887

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/source/Instruction/命令行参数.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,10 @@ soft overlong 奖励参数
- swanlab_project: swanlab的project,需要在页面中预先创建好:[https://swanlab.cn/space/~](https://swanlab.cn/space/~)。
- swanlab_workspace: 默认为None,会使用api-key对应的username。
- swanlab_exp_name: 实验名,可以为空,为空时默认传入--output_dir的值。
- swanlab_notification_methods: 推送方式。可以为空,可以为多个值,可选值为`['slack', 'lark']`。
- swanlab_lark_webhook_url: 默认为None。swanlab的lark webhook url,用于推送实验结果到飞书。
- swanlab_lark_secret: 默认为None。swanlab的lark secret,用于推送实验结果到飞书。
- swanlab_slack_webhook_url: 默认为None。swanlab的slack webhook url,用于推送实验结果到slack。
- swanlab_mode: 可选cloud和local,云模式或者本地模式。

### 推理参数
Expand Down
2 changes: 2 additions & 0 deletions docs/source_en/Instruction/Command-line-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,10 @@ Soft overlong reward parameters:
- **swanlab_project**: SwanLab's project, which needs to be created in advance on the page: [https://swanlab.cn/space/~](https://swanlab.cn/space/~)
- **swanlab_workspace**: Defaults to `None`, will use the username associated with the API key
- **swanlab_exp_name**: Experiment name, can be left empty. If empty, the value of `--output_dir` will be used by default
- swanlab_notification_methods: Ways to send notifications by SwanLab, can be left empty or multiple values from `slack` or `lark`.
- swanlab_lark_webhook_url: Defaults to None. SwanLab's Lark webhook URL, used for pushing experiment results to Lark.
- swanlab_lark_secret: Defaults to None. SwanLab's Lark secret, used for pushing experiment results to Lark.
- swanlab_slack_webhook_url: Defaults to None. SwanLab's Slack webhook URL, used for pushing experiment results to Slack.
- **swanlab_mode**: Optional values are `cloud` and `local`, representing cloud mode or local mode


Expand Down
18 changes: 15 additions & 3 deletions swift/llm/argument/train_args.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
import os
from dataclasses import dataclass, field
from typing import Literal, Optional
from typing import List, Literal, Optional

from transformers import Seq2SeqTrainingArguments
from transformers.utils.versions import require_version
Expand Down Expand Up @@ -68,9 +68,15 @@ class SwanlabArguments:
swanlab_project: Optional[str] = None
swanlab_workspace: Optional[str] = None
swanlab_exp_name: Optional[str] = None
swanlab_mode: Literal['cloud', 'local'] = 'cloud'

swanlab_notification_methods: Optional[List[str]] = None
# lark https://docs.swanlab.cn/plugin/notification-lark.html
swanlab_lark_webhook_url: Optional[str] = None
swanlab_lark_secret: Optional[str] = None
swanlab_mode: Literal['cloud', 'local'] = 'cloud'

# slack https://docs.swanlab.cn/plugin/notification-slack.html
swanlab_slack_webhook_url: Optional[str] = None

def _init_swanlab(self):
if not is_swanlab_available():
Expand All @@ -83,14 +89,20 @@ def _init_swanlab(self):
if self.swanlab_token:
swanlab.login(self.swanlab_token)

if self.swanlab_lark_webhook_url is not None:
if 'lark' in self.swanlab_notification_methods and self.swanlab_lark_webhook_url is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error will occur if self.swanlab_notification_methods is None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if self.swanlab_notification_methods and 'lark' in self.swanlab_notification_methods and self.swanlab_lark_webhook_url is not None:

Would this argument be too long? Do you think if self.swanlab_notification_methods variable is needed?

from swanlab.plugin.notification import LarkCallback
lark_callback = LarkCallback(
webhook_url=self.swanlab_lark_webhook_url,
secret=self.swanlab_lark_secret,
)
swanlab.register_callbacks([lark_callback])

if 'slack' in self.swanlab_notification_methods and self.swanlab_slack_webhook_url is not None:
from swanlab.plugin.notification import SlackCallback

slack_callback = SlackCallback(webhook_url=self.swanlab_slack_webhook_url, language='zh')
swanlab.register_callbacks([slack_callback])

INTEGRATION_TO_CALLBACK['swanlab'] = SwanLabCallback(
project=self.swanlab_project,
workspace=self.swanlab_workspace,
Expand Down
23 changes: 23 additions & 0 deletions swift/ui/llm_train/report_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class ReportTo(BaseUI):
'en': 'Experiment of SwanLab'
},
},
'swanlab_notification_methods': {
'label': {
'zh': 'SwanLab通知方式',
'en': 'Notification methods of SwanLab'
},
},
'swanlab_lark_webhook_url': {
'label': {
'zh': 'SwanLab飞书Webhook地址',
Expand All @@ -59,6 +65,12 @@ class ReportTo(BaseUI):
'en': 'Secret of SwanLab Lark Callback'
},
},
'swanlab_slack_webhook_url': {
'label': {
'zh': 'SwanLab Slack Webhook地址',
'en': 'Webhook URL of SwanLab Slack Callback'
},
},
'swanlab_mode': {
'label': {
'zh': 'SwanLab工作模式',
Expand All @@ -81,9 +93,20 @@ def do_build_ui(cls, base_tab: Type['BaseUI']):
scale=20)
gr.Textbox(elem_id='swanlab_token', lines=1, scale=20)
gr.Textbox(elem_id='swanlab_project', lines=1, scale=20)

with gr.Row():
gr.Dropdown(
elem_id='swanlab_notification_methods',
multiselect=True,
is_list=True,
choices=['lark', 'slack'],
allow_custom_value=True,
scale=20)
with gr.Row():
gr.Textbox(elem_id='swanlab_lark_webhook_url', lines=1, scale=20)
gr.Textbox(elem_id='swanlab_lark_secret', lines=1, scale=20)
with gr.Row():
gr.Textbox(elem_id='swanlab_slack_webhook_url', lines=1, scale=20)
with gr.Row():
gr.Textbox(elem_id='swanlab_workspace', lines=1, scale=20)
gr.Textbox(elem_id='swanlab_exp_name', lines=1, scale=20)
Expand Down