cortexa-v2-sdk 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cortexa_sdk/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # Cortexa V2 SDK
2
+
3
+ `cortexa_v2_sdk` 让训练平台通过稳定的 Python API 查询数基已发布归档,并在 Ray Worker 开训时按不可变 `archive_id` 下载训练数据。发行包使用新名称,Python 导入路径仍为 `cortexa_sdk`,CLI 命令仍为 `cortexa-sdk`。列表发现不依赖共享数据库、对象存储扫描或 `manifest.json`。
4
+
5
+ ## 运行要求
6
+
7
+ - Python 3.10 或更高版本
8
+ - 可访问 Cortexa API
9
+ - 有效的 Cortexa API Key
10
+
11
+ ## 安装
12
+
13
+ 安装正式版本:
14
+
15
+ ```bash
16
+ python3 -m pip install "cortexa_v2_sdk==0.1.0"
17
+ ```
18
+
19
+ 仓库内开发安装:
20
+
21
+ ```bash
22
+ git clone https://github.com/verteklab/cortexa.git
23
+ cd cortexa
24
+ python3 -m pip install -e .
25
+ ```
26
+
27
+ 验证:
28
+
29
+ ```bash
30
+ python3 -c "from importlib.metadata import version; print(version('cortexa_v2_sdk'))"
31
+ # 期望输出:0.1.0
32
+ ```
33
+
34
+ ## 配置
35
+
36
+ 配置优先级固定为:函数参数 > JSON 配置文件 > 环境变量。
37
+
38
+ ### 环境变量
39
+
40
+ ```bash
41
+ export CORTEXA_API_KEY="<API Key>"
42
+ export CORTEXA_BASE_URL="https://cortexa.example.com/api/v1"
43
+ export CORTEXA_DATASET_DIR="/data/ray_dataset_zip_cache"
44
+ ```
45
+
46
+ | 变量 | 必填 | 说明 |
47
+ |---|---|---|
48
+ | `CORTEXA_API_KEY` | 是 | 请求头 `X-API-KEY` 的值 |
49
+ | `CORTEXA_BASE_URL` | 是 | Cortexa v1 API 基址;SDK 自动从 `/api/v1` 派生归档 `/api/v2` 地址 |
50
+ | `CORTEXA_DATASET_DIR` | 否 | 默认下载目录,缺省为 `~/.cortexa/datasets` |
51
+ | `CORTEXA_CONFIG` | 否 | JSON 配置路径,缺省为 `~/.cortexa/config.json` |
52
+
53
+ ### 配置文件
54
+
55
+ 生成模板:
56
+
57
+ ```bash
58
+ cortexa-sdk --init-config
59
+ ```
60
+
61
+ 编辑 `~/.cortexa/config.json`:
62
+
63
+ ```json
64
+ {
65
+ "api_key": "<API Key>",
66
+ "base_url": "https://cortexa.example.com/api/v1",
67
+ "dataset_dir": "/data/ray_dataset_zip_cache"
68
+ }
69
+ ```
70
+
71
+ 不要把 API Key 提交到 Git、镜像或训练任务配置中;通过部署环境或密钥管理服务注入。
72
+
73
+ ## 查询训练可用归档
74
+
75
+ 数基归档详情选择“同步到鼎算”时只发布归档引用,不预生成 ZIP。鼎算后端通过 SDK 查询这个列表:
76
+
77
+ ```python
78
+ from cortexa_sdk import CortexaClient
79
+
80
+ client = CortexaClient()
81
+ result = client.list_training_archives(
82
+ keyword="桥梁",
83
+ page=1,
84
+ page_size=20,
85
+ )
86
+
87
+ for archive in result["list"]:
88
+ print(
89
+ archive["_id"],
90
+ archive["name"],
91
+ archive.get("dataset_id"),
92
+ archive.get("display", {}).get("format"),
93
+ )
94
+ ```
95
+
96
+ 返回分页结构:
97
+
98
+ ```json
99
+ {
100
+ "list": [
101
+ {
102
+ "_id": "归档ID",
103
+ "dataset_id": "来源数据集ID",
104
+ "name": "桥梁病害检测",
105
+ "status": "approved",
106
+ "display": {
107
+ "registered": true,
108
+ "training_name": "桥梁病害训练集",
109
+ "format": "YOLO",
110
+ "samples": 8600,
111
+ "version": "v1.0"
112
+ }
113
+ }
114
+ ],
115
+ "total": 1,
116
+ "page": 1,
117
+ "page_size": 20
118
+ }
119
+ ```
120
+
121
+ 训练平台必须保存 `_id` 作为 `archive_id`。`dataset_id` 只用于来源追溯,不能替代不可变归档 ID。
122
+
123
+ ## 读取归档详情
124
+
125
+ ```python
126
+ archive = client.archive_detail("<archive_id>")
127
+ print(archive["name"])
128
+ print(archive["asset_snapshot"])
129
+ ```
130
+
131
+ 训练平台应先从 `list_training_archives()` 取得可用 ID,再读取详情。直接知道归档 ID 不等于该归档已发布给训练平台。
132
+
133
+ ## Ray Worker 按需下载
134
+
135
+ Worker 开训时按 `archive_id` 请求训练格式。该调用会在数基创建或复用普通归档导出任务,等待任务完成并把 ZIP 下载到本地;它不会创建跨平台 manifest 或共享存储副本。
136
+
137
+ 使用客户端:
138
+
139
+ ```python
140
+ from cortexa_sdk import CortexaClient, ExportType
141
+
142
+ client = CortexaClient()
143
+ zip_path = client.download_archive(
144
+ archive_id="<archive_id>",
145
+ export_type=ExportType.YOLO,
146
+ split_ratio="8:2",
147
+ download_dir="/data/ray_dataset_zip_cache",
148
+ assets_included=True,
149
+ )
150
+ print(zip_path)
151
+ ```
152
+
153
+ Ray Worker 也可以调用模块级函数:
154
+
155
+ ```python
156
+ from cortexa_sdk import ExportType, download_archive
157
+
158
+ zip_path = download_archive(
159
+ archive_id="<archive_id>",
160
+ export_type=ExportType.COCO,
161
+ split_ratio="7:3",
162
+ api_key="<API Key>",
163
+ base_url="https://cortexa.example.com/api/v1",
164
+ download_dir="/data/ray_dataset_zip_cache",
165
+ )
166
+ ```
167
+
168
+ 归档训练下载只支持:
169
+
170
+ - 格式:`ExportType.YOLO`、`ExportType.COCO`
171
+ - 划分:`8:2`、`7:3`、`9:1`
172
+
173
+ 同一 Worker 节点应按 `archive_id` 复用本地缓存,避免多个训练进程重复下载。
174
+
175
+ ## 下载旧数据集
176
+
177
+ 旧流程仍可按 `dataset_id` 下载:
178
+
179
+ ```python
180
+ from cortexa_sdk import AnnotationType, ExportType, download_dataset
181
+
182
+ zip_path = download_dataset(
183
+ dataset_id="<dataset_id>",
184
+ export_type=ExportType.JSON,
185
+ annotation_type=AnnotationType.RECT,
186
+ assets_included=True,
187
+ )
188
+ ```
189
+
190
+ CLI 当前只支持旧 `dataset_id` 下载,不支持归档列表和 `archive_id` 下载:
191
+
192
+ ```bash
193
+ cortexa-sdk \
194
+ --dataset-id "<dataset_id>" \
195
+ --export-type YOLO \
196
+ --annotation-type rect \
197
+ --download-dir ./tmp/datasets
198
+ ```
199
+
200
+ 归档发现和归档下载必须使用 Python API。
201
+
202
+ ## 解析下载包
203
+
204
+ ```python
205
+ from cortexa_sdk.parser import extract_zip, load_dataset
206
+
207
+ dataset_dir = extract_zip("./tmp/datasets/<dataset_id>.zip")
208
+ samples = load_dataset(dataset_dir)
209
+ print(f"samples={len(samples)}")
210
+ ```
211
+
212
+ `load_dataset()` 返回结构化 `EvalSample` 列表,可读取样本 split、媒体路径和问答标注。
213
+
214
+ ## 异常处理
215
+
216
+ ```python
217
+ from cortexa_sdk import CortexaClient, ExportType
218
+
219
+ try:
220
+ client = CortexaClient()
221
+ path = client.download_archive(
222
+ "<archive_id>",
223
+ export_type=ExportType.YOLO,
224
+ )
225
+ except ValueError as exc:
226
+ # 配置、格式或划分参数错误
227
+ print(exc)
228
+ except RuntimeError as exc:
229
+ # Cortexa 返回业务错误或导出任务失败
230
+ print(exc)
231
+ ```
232
+
233
+ HTTP 连接、鉴权和超时错误由 `requests` 异常体系抛出。生产 Worker 应让任务框架记录异常并按任务策略重试,不要把失败当作空数据集继续训练。
234
+
235
+ ## 发布到 PyPI
236
+
237
+ PyPI 上的版本不可覆盖。`cortexa_v2_sdk` 是独立于 `cortexa-sdk` 的新发行包,版本从 `0.1.0` 开始;后续发布前必须把 `pyproject.toml` 中的版本改为一个从未上传过的新版本。
238
+
239
+ ### 1. 准备发布提交
240
+
241
+ ```bash
242
+ git checkout <release-branch>
243
+ python3 -m pytest -q cortexa_sdk/test_sdk.py
244
+ black .
245
+ python3 .docsmith/scripts/gen.example.py
246
+ git diff --check
247
+ ```
248
+
249
+ 确认 `pyproject.toml`、SDK 源码、测试和文档属于同一提交,然后提交:
250
+
251
+ ```bash
252
+ git add -p
253
+ git diff --cached --name-only
254
+ git diff --cached --check
255
+ git commit -m "chore(sdk): release cortexa_v2_sdk 0.1.0"
256
+ ```
257
+
258
+ 逐项确认暂存内容只包含本次 SDK 版本、源码、测试和受影响文档。仓库存在其他未提交改动时,不要使用 `git add .` 或暂存整个目录。
259
+
260
+ ### 2. 构建并校验产物
261
+
262
+ ```bash
263
+ RELEASE_DIR="$(mktemp -d)"
264
+ python3 -m build --outdir "$RELEASE_DIR"
265
+ python3 -m twine check "$RELEASE_DIR"/*
266
+ ls -lh "$RELEASE_DIR"
267
+ ```
268
+
269
+ 期望生成:
270
+
271
+ - `cortexa_v2_sdk-0.1.0-py3-none-any.whl`
272
+ - `cortexa_v2_sdk-0.1.0.tar.gz`
273
+ - Twine 输出 `PASSED`
274
+
275
+ 使用隔离环境安装 wheel:
276
+
277
+ ```bash
278
+ python3 -m venv /tmp/cortexa-v2-sdk-release-venv
279
+ /tmp/cortexa-v2-sdk-release-venv/bin/python -m pip install \
280
+ "$RELEASE_DIR/cortexa_v2_sdk-0.1.0-py3-none-any.whl"
281
+ /tmp/cortexa-v2-sdk-release-venv/bin/python -c \
282
+ "from cortexa_sdk import CortexaClient, download_archive; from importlib.metadata import version; print(version('cortexa_v2_sdk'))"
283
+ ```
284
+
285
+ 期望输出 `0.1.0`,且导入不报错。
286
+
287
+ ### 3. TestPyPI 验证
288
+
289
+ 在 TestPyPI 创建 API Token,并只放在当前终端环境中:
290
+
291
+ ```bash
292
+ export TWINE_USERNAME="__token__"
293
+ read -s TWINE_PASSWORD
294
+ export TWINE_PASSWORD
295
+ python3 -m twine upload \
296
+ --repository-url https://test.pypi.org/legacy/ \
297
+ "$RELEASE_DIR"/*
298
+ ```
299
+
300
+ 从 TestPyPI 安装时,运行依赖仍从正式 PyPI 获取:
301
+
302
+ ```bash
303
+ python3 -m venv /tmp/cortexa-v2-sdk-testpypi-venv
304
+ /tmp/cortexa-v2-sdk-testpypi-venv/bin/python -m pip install \
305
+ --index-url https://test.pypi.org/simple/ \
306
+ --extra-index-url https://pypi.org/simple/ \
307
+ "cortexa_v2_sdk==0.1.0"
308
+ ```
309
+
310
+ ### 4. 正式发布并打标签
311
+
312
+ 确认 TestPyPI 安装和 SDK 冒烟调用正常后,上传同一目录中的原始产物,不要重新构建:
313
+
314
+ ```bash
315
+ git push origin HEAD
316
+ python3 -m twine upload "$RELEASE_DIR"/*
317
+ unset TWINE_PASSWORD TWINE_USERNAME
318
+
319
+ git tag -a cortexa-v2-sdk-v0.1.0 -m "cortexa_v2_sdk 0.1.0"
320
+ git push origin cortexa-v2-sdk-v0.1.0
321
+ ```
322
+
323
+ 验证正式版本:
324
+
325
+ ```bash
326
+ python3 -m pip index versions cortexa_v2_sdk
327
+ python3 -m pip install --no-cache-dir "cortexa_v2_sdk==0.1.0"
328
+ ```
329
+
330
+ ## 发版检查表
331
+
332
+ - [ ] `pyproject.toml` 版本未在 PyPI 使用
333
+ - [ ] SDK 测试全部通过
334
+ - [ ] `black .`、docsmith 和 `git diff --check` 通过
335
+ - [ ] wheel 与 sdist 均通过 `twine check`
336
+ - [ ] 隔离环境可导入 SDK,版本正确
337
+ - [ ] TestPyPI 安装及关键 API 冒烟通过
338
+ - [ ] 正式 PyPI 上传的是已经验证过的同一批产物
339
+ - [ ] 已推送 `sdk-v<version>` 标签
340
+ - [ ] API Token 未写入文件、命令参数、提交或日志