Skip to content

Commit cdab851

Browse files
authored
fix api error (#549)
* fix api error * fix 400
1 parent 0a6028d commit cdab851

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

swanlab/api/upload/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from ..http import get_http, sync_error_handler
1111
from .model import ColumnModel
1212
from typing import List, Tuple, Dict
13-
from swanlab.error import FileError
13+
from swanlab.error import FileError, ApiError
14+
from swanlab.log import swanlog
1415
import json
1516
import yaml
1617
import os
@@ -140,7 +141,11 @@ def upload_column(columns: List[ColumnModel]):
140141
http = get_http()
141142
url = f'/experiment/{http.exp_id}/column'
142143
# WARNING 这里不能使用并发请求,可见 https://github.com/SwanHubX/SwanLab-Server/issues/113
143-
_ = [http.post(url, data=column.to_dict()) for column in columns]
144+
for column in columns:
145+
try:
146+
http.post(url, column.to_dict())
147+
except ApiError as e:
148+
swanlog.error(f"Upload column {column.key} failed: {e.resp.status_code}")
144149

145150

146151
__all__ = [

swanlab/api/upload/model.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class ColumnModel:
1717
def __init__(self, key, column_type: str, error: dict = None):
1818
"""
1919
:param key: 列名称
20-
:param column_type: 列类型,'FLOAT', 'IMAGE', 'AUDIO', 'TEXT', 'ANY',必须为大写,如果传入 'DEFAULT',则会转为 'FLOAT'
21-
:param error: 错误信息,如果错误信息不为None,column_type必须为'ANY'
20+
:param column_type: 列类型,'FLOAT', 'IMAGE', 'AUDIO', 'TEXT',必须为大写,如果传入 'DEFAULT',则会转为 'FLOAT'
21+
:param error: 错误信息,如果错误信息不为None
2222
"""
2323
self.key = key
2424
if column_type == "DEFAULT":
@@ -27,14 +27,11 @@ def __init__(self, key, column_type: str, error: dict = None):
2727
self.error = error
2828

2929
def to_dict(self):
30-
if self.error is not None:
31-
return {
32-
"key": self.key,
33-
"type": 'ANY',
34-
"error": self.error
35-
}
36-
else:
37-
return {
38-
"key": self.key,
39-
"type": self.column_type,
40-
}
30+
return {
31+
"key": self.key,
32+
"type": self.column_type,
33+
} if self.error is None else {
34+
"key": self.key,
35+
"type": self.column_type,
36+
"error": self.error
37+
}

swanlab/cloud/_log_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def upload(self):
6868
if msg[0] in UploadType:
6969
upload_tasks_dict[msg[0]].extend(msg[1])
7070
tasks_key_list = [key for key in upload_tasks_dict if len(upload_tasks_dict[key]) > 0]
71+
7172
# 同步执行所有的上传任务
72-
7373
results = [x.value['upload'](upload_tasks_dict[x]) for x in tasks_key_list]
7474
for index, result in enumerate(results):
7575
# 如果出现已知问题

test/create_error_chart.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
r"""
4+
@DATE: 2024/5/16 20:42
5+
@File: create_error_chart.py
6+
@IDE: pycharm
7+
@Description:
8+
创建错误图表
9+
"""
10+
from tutils import open_dev_mode
11+
import swanlab
12+
13+
swanlab.login(api_key=open_dev_mode())
14+
15+
swanlab.init(
16+
description="此实验的图表将出现问题",
17+
log_level="debug",
18+
cloud=True,
19+
)
20+
21+
for i in range(10):
22+
swanlab.log({"success": i, "error": "abc"})

0 commit comments

Comments
 (0)