cortexa-v2-sdk 0.1.0__tar.gz

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.
@@ -0,0 +1,351 @@
1
+ Metadata-Version: 2.4
2
+ Name: cortexa_v2_sdk
3
+ Version: 0.1.0
4
+ Summary: Python SDK for querying published Cortexa archives and downloading immutable archive or dataset exports.
5
+ Author: Cortexa
6
+ Project-URL: Homepage, https://github.com/verteklab/cortexa
7
+ Project-URL: Repository, https://github.com/verteklab/cortexa
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: requests
11
+
12
+ # Cortexa V2 SDK
13
+
14
+ `cortexa_v2_sdk` 让训练平台通过稳定的 Python API 查询数基已发布归档,并在 Ray Worker 开训时按不可变 `archive_id` 下载训练数据。发行包使用新名称,Python 导入路径仍为 `cortexa_sdk`,CLI 命令仍为 `cortexa-sdk`。列表发现不依赖共享数据库、对象存储扫描或 `manifest.json`。
15
+
16
+ ## 运行要求
17
+
18
+ - Python 3.10 或更高版本
19
+ - 可访问 Cortexa API
20
+ - 有效的 Cortexa API Key
21
+
22
+ ## 安装
23
+
24
+ 安装正式版本:
25
+
26
+ ```bash
27
+ python3 -m pip install "cortexa_v2_sdk==0.1.0"
28
+ ```
29
+
30
+ 仓库内开发安装:
31
+
32
+ ```bash
33
+ git clone https://github.com/verteklab/cortexa.git
34
+ cd cortexa
35
+ python3 -m pip install -e .
36
+ ```
37
+
38
+ 验证:
39
+
40
+ ```bash
41
+ python3 -c "from importlib.metadata import version; print(version('cortexa_v2_sdk'))"
42
+ # 期望输出:0.1.0
43
+ ```
44
+
45
+ ## 配置
46
+
47
+ 配置优先级固定为:函数参数 > JSON 配置文件 > 环境变量。
48
+
49
+ ### 环境变量
50
+
51
+ ```bash
52
+ export CORTEXA_API_KEY="<API Key>"
53
+ export CORTEXA_BASE_URL="https://cortexa.example.com/api/v1"
54
+ export CORTEXA_DATASET_DIR="/data/ray_dataset_zip_cache"
55
+ ```
56
+
57
+ | 变量 | 必填 | 说明 |
58
+ |---|---|---|
59
+ | `CORTEXA_API_KEY` | 是 | 请求头 `X-API-KEY` 的值 |
60
+ | `CORTEXA_BASE_URL` | 是 | Cortexa v1 API 基址;SDK 自动从 `/api/v1` 派生归档 `/api/v2` 地址 |
61
+ | `CORTEXA_DATASET_DIR` | 否 | 默认下载目录,缺省为 `~/.cortexa/datasets` |
62
+ | `CORTEXA_CONFIG` | 否 | JSON 配置路径,缺省为 `~/.cortexa/config.json` |
63
+
64
+ ### 配置文件
65
+
66
+ 生成模板:
67
+
68
+ ```bash
69
+ cortexa-sdk --init-config
70
+ ```
71
+
72
+ 编辑 `~/.cortexa/config.json`:
73
+
74
+ ```json
75
+ {
76
+ "api_key": "<API Key>",
77
+ "base_url": "https://cortexa.example.com/api/v1",
78
+ "dataset_dir": "/data/ray_dataset_zip_cache"
79
+ }
80
+ ```
81
+
82
+ 不要把 API Key 提交到 Git、镜像或训练任务配置中;通过部署环境或密钥管理服务注入。
83
+
84
+ ## 查询训练可用归档
85
+
86
+ 数基归档详情选择“同步到鼎算”时只发布归档引用,不预生成 ZIP。鼎算后端通过 SDK 查询这个列表:
87
+
88
+ ```python
89
+ from cortexa_sdk import CortexaClient
90
+
91
+ client = CortexaClient()
92
+ result = client.list_training_archives(
93
+ keyword="桥梁",
94
+ page=1,
95
+ page_size=20,
96
+ )
97
+
98
+ for archive in result["list"]:
99
+ print(
100
+ archive["_id"],
101
+ archive["name"],
102
+ archive.get("dataset_id"),
103
+ archive.get("display", {}).get("format"),
104
+ )
105
+ ```
106
+
107
+ 返回分页结构:
108
+
109
+ ```json
110
+ {
111
+ "list": [
112
+ {
113
+ "_id": "归档ID",
114
+ "dataset_id": "来源数据集ID",
115
+ "name": "桥梁病害检测",
116
+ "status": "approved",
117
+ "display": {
118
+ "registered": true,
119
+ "training_name": "桥梁病害训练集",
120
+ "format": "YOLO",
121
+ "samples": 8600,
122
+ "version": "v1.0"
123
+ }
124
+ }
125
+ ],
126
+ "total": 1,
127
+ "page": 1,
128
+ "page_size": 20
129
+ }
130
+ ```
131
+
132
+ 训练平台必须保存 `_id` 作为 `archive_id`。`dataset_id` 只用于来源追溯,不能替代不可变归档 ID。
133
+
134
+ ## 读取归档详情
135
+
136
+ ```python
137
+ archive = client.archive_detail("<archive_id>")
138
+ print(archive["name"])
139
+ print(archive["asset_snapshot"])
140
+ ```
141
+
142
+ 训练平台应先从 `list_training_archives()` 取得可用 ID,再读取详情。直接知道归档 ID 不等于该归档已发布给训练平台。
143
+
144
+ ## Ray Worker 按需下载
145
+
146
+ Worker 开训时按 `archive_id` 请求训练格式。该调用会在数基创建或复用普通归档导出任务,等待任务完成并把 ZIP 下载到本地;它不会创建跨平台 manifest 或共享存储副本。
147
+
148
+ 使用客户端:
149
+
150
+ ```python
151
+ from cortexa_sdk import CortexaClient, ExportType
152
+
153
+ client = CortexaClient()
154
+ zip_path = client.download_archive(
155
+ archive_id="<archive_id>",
156
+ export_type=ExportType.YOLO,
157
+ split_ratio="8:2",
158
+ download_dir="/data/ray_dataset_zip_cache",
159
+ assets_included=True,
160
+ )
161
+ print(zip_path)
162
+ ```
163
+
164
+ Ray Worker 也可以调用模块级函数:
165
+
166
+ ```python
167
+ from cortexa_sdk import ExportType, download_archive
168
+
169
+ zip_path = download_archive(
170
+ archive_id="<archive_id>",
171
+ export_type=ExportType.COCO,
172
+ split_ratio="7:3",
173
+ api_key="<API Key>",
174
+ base_url="https://cortexa.example.com/api/v1",
175
+ download_dir="/data/ray_dataset_zip_cache",
176
+ )
177
+ ```
178
+
179
+ 归档训练下载只支持:
180
+
181
+ - 格式:`ExportType.YOLO`、`ExportType.COCO`
182
+ - 划分:`8:2`、`7:3`、`9:1`
183
+
184
+ 同一 Worker 节点应按 `archive_id` 复用本地缓存,避免多个训练进程重复下载。
185
+
186
+ ## 下载旧数据集
187
+
188
+ 旧流程仍可按 `dataset_id` 下载:
189
+
190
+ ```python
191
+ from cortexa_sdk import AnnotationType, ExportType, download_dataset
192
+
193
+ zip_path = download_dataset(
194
+ dataset_id="<dataset_id>",
195
+ export_type=ExportType.JSON,
196
+ annotation_type=AnnotationType.RECT,
197
+ assets_included=True,
198
+ )
199
+ ```
200
+
201
+ CLI 当前只支持旧 `dataset_id` 下载,不支持归档列表和 `archive_id` 下载:
202
+
203
+ ```bash
204
+ cortexa-sdk \
205
+ --dataset-id "<dataset_id>" \
206
+ --export-type YOLO \
207
+ --annotation-type rect \
208
+ --download-dir ./tmp/datasets
209
+ ```
210
+
211
+ 归档发现和归档下载必须使用 Python API。
212
+
213
+ ## 解析下载包
214
+
215
+ ```python
216
+ from cortexa_sdk.parser import extract_zip, load_dataset
217
+
218
+ dataset_dir = extract_zip("./tmp/datasets/<dataset_id>.zip")
219
+ samples = load_dataset(dataset_dir)
220
+ print(f"samples={len(samples)}")
221
+ ```
222
+
223
+ `load_dataset()` 返回结构化 `EvalSample` 列表,可读取样本 split、媒体路径和问答标注。
224
+
225
+ ## 异常处理
226
+
227
+ ```python
228
+ from cortexa_sdk import CortexaClient, ExportType
229
+
230
+ try:
231
+ client = CortexaClient()
232
+ path = client.download_archive(
233
+ "<archive_id>",
234
+ export_type=ExportType.YOLO,
235
+ )
236
+ except ValueError as exc:
237
+ # 配置、格式或划分参数错误
238
+ print(exc)
239
+ except RuntimeError as exc:
240
+ # Cortexa 返回业务错误或导出任务失败
241
+ print(exc)
242
+ ```
243
+
244
+ HTTP 连接、鉴权和超时错误由 `requests` 异常体系抛出。生产 Worker 应让任务框架记录异常并按任务策略重试,不要把失败当作空数据集继续训练。
245
+
246
+ ## 发布到 PyPI
247
+
248
+ PyPI 上的版本不可覆盖。`cortexa_v2_sdk` 是独立于 `cortexa-sdk` 的新发行包,版本从 `0.1.0` 开始;后续发布前必须把 `pyproject.toml` 中的版本改为一个从未上传过的新版本。
249
+
250
+ ### 1. 准备发布提交
251
+
252
+ ```bash
253
+ git checkout <release-branch>
254
+ python3 -m pytest -q cortexa_sdk/test_sdk.py
255
+ black .
256
+ python3 .docsmith/scripts/gen.example.py
257
+ git diff --check
258
+ ```
259
+
260
+ 确认 `pyproject.toml`、SDK 源码、测试和文档属于同一提交,然后提交:
261
+
262
+ ```bash
263
+ git add -p
264
+ git diff --cached --name-only
265
+ git diff --cached --check
266
+ git commit -m "chore(sdk): release cortexa_v2_sdk 0.1.0"
267
+ ```
268
+
269
+ 逐项确认暂存内容只包含本次 SDK 版本、源码、测试和受影响文档。仓库存在其他未提交改动时,不要使用 `git add .` 或暂存整个目录。
270
+
271
+ ### 2. 构建并校验产物
272
+
273
+ ```bash
274
+ RELEASE_DIR="$(mktemp -d)"
275
+ python3 -m build --outdir "$RELEASE_DIR"
276
+ python3 -m twine check "$RELEASE_DIR"/*
277
+ ls -lh "$RELEASE_DIR"
278
+ ```
279
+
280
+ 期望生成:
281
+
282
+ - `cortexa_v2_sdk-0.1.0-py3-none-any.whl`
283
+ - `cortexa_v2_sdk-0.1.0.tar.gz`
284
+ - Twine 输出 `PASSED`
285
+
286
+ 使用隔离环境安装 wheel:
287
+
288
+ ```bash
289
+ python3 -m venv /tmp/cortexa-v2-sdk-release-venv
290
+ /tmp/cortexa-v2-sdk-release-venv/bin/python -m pip install \
291
+ "$RELEASE_DIR/cortexa_v2_sdk-0.1.0-py3-none-any.whl"
292
+ /tmp/cortexa-v2-sdk-release-venv/bin/python -c \
293
+ "from cortexa_sdk import CortexaClient, download_archive; from importlib.metadata import version; print(version('cortexa_v2_sdk'))"
294
+ ```
295
+
296
+ 期望输出 `0.1.0`,且导入不报错。
297
+
298
+ ### 3. TestPyPI 验证
299
+
300
+ 在 TestPyPI 创建 API Token,并只放在当前终端环境中:
301
+
302
+ ```bash
303
+ export TWINE_USERNAME="__token__"
304
+ read -s TWINE_PASSWORD
305
+ export TWINE_PASSWORD
306
+ python3 -m twine upload \
307
+ --repository-url https://test.pypi.org/legacy/ \
308
+ "$RELEASE_DIR"/*
309
+ ```
310
+
311
+ 从 TestPyPI 安装时,运行依赖仍从正式 PyPI 获取:
312
+
313
+ ```bash
314
+ python3 -m venv /tmp/cortexa-v2-sdk-testpypi-venv
315
+ /tmp/cortexa-v2-sdk-testpypi-venv/bin/python -m pip install \
316
+ --index-url https://test.pypi.org/simple/ \
317
+ --extra-index-url https://pypi.org/simple/ \
318
+ "cortexa_v2_sdk==0.1.0"
319
+ ```
320
+
321
+ ### 4. 正式发布并打标签
322
+
323
+ 确认 TestPyPI 安装和 SDK 冒烟调用正常后,上传同一目录中的原始产物,不要重新构建:
324
+
325
+ ```bash
326
+ git push origin HEAD
327
+ python3 -m twine upload "$RELEASE_DIR"/*
328
+ unset TWINE_PASSWORD TWINE_USERNAME
329
+
330
+ git tag -a cortexa-v2-sdk-v0.1.0 -m "cortexa_v2_sdk 0.1.0"
331
+ git push origin cortexa-v2-sdk-v0.1.0
332
+ ```
333
+
334
+ 验证正式版本:
335
+
336
+ ```bash
337
+ python3 -m pip index versions cortexa_v2_sdk
338
+ python3 -m pip install --no-cache-dir "cortexa_v2_sdk==0.1.0"
339
+ ```
340
+
341
+ ## 发版检查表
342
+
343
+ - [ ] `pyproject.toml` 版本未在 PyPI 使用
344
+ - [ ] SDK 测试全部通过
345
+ - [ ] `black .`、docsmith 和 `git diff --check` 通过
346
+ - [ ] wheel 与 sdist 均通过 `twine check`
347
+ - [ ] 隔离环境可导入 SDK,版本正确
348
+ - [ ] TestPyPI 安装及关键 API 冒烟通过
349
+ - [ ] 正式 PyPI 上传的是已经验证过的同一批产物
350
+ - [ ] 已推送 `sdk-v<version>` 标签
351
+ - [ ] API Token 未写入文件、命令参数、提交或日志
@@ -0,0 +1,157 @@
1
+ ---
2
+ audience: [engineering, business]
3
+ edit_policy: factual_direct
4
+ source_of_truth:
5
+ - backend/server/settings.py
6
+ - backend/server/urls.py
7
+ - backend/requirements.txt
8
+ - frontend/package.json
9
+ - frontend_v2/package.json
10
+ - frontend_v2/.umirc.ts
11
+ - {dir: frontend_v2/src/}
12
+ - {dir: cortexa_sdk/}
13
+ - {dir: eval/}
14
+ - pyproject.toml
15
+ - .docsmith/features.yml
16
+ - {fact: "仓库结构与开发者入口"}
17
+ ---
18
+
19
+ <!--
20
+ Copyright (c) 2025 Vortek Inc. and Tuanliu (Hainan Special Economic Zone) Technology Co., Ltd.
21
+ All rights reserved.
22
+ -->
23
+
24
+ # Cortexa
25
+
26
+ **把原始图片和视频变成可训练数据集**:上传素材、标注(人工或 AI 预标注)、审查、打包导出,一条线走完。
27
+
28
+ - **多种标注类型**:矩形框、多边形、3D 立方体、折线、自定义答题模板,覆盖检测/分割/3D/质检场景
29
+ - **AI 预标注加速**:接入 PaddleX 小目标检测、SAM2/SAM3 语义分割、3D-MOOD 立方体检测模型,并支持模板 AI 按组生成答案建议,标注员确认后保存
30
+ - **一行代码拉数据集**:Python SDK 支持 JSON/YOLO/COCO/LabelMe 格式导出,也能查询已发布归档,并在 Ray Worker 开训时按 `archive_id` 临时生成和下载训练数据
31
+
32
+ ## 架构
33
+
34
+ ```mermaid
35
+ graph TD
36
+ Browser["UmiJS + Ant Design 旧前端<br/>:8001"]
37
+ BrowserV2["React + Umi + Arco v2 工作台<br/>:8002"]
38
+ API["Django REST Framework<br/>:8000"]
39
+ Worker["Celery Worker"]
40
+ Beat["Celery Beat"]
41
+ Mongo[(MongoDB)]
42
+ MinIO[(MinIO 对象存储)]
43
+ RabbitMQ[(RabbitMQ)]
44
+ AI["AI 模型服务<br/>(PaddleX / SAM2 / SAM3 / 3D-MOOD / 模板 AI)"]
45
+ SDK["cortexa_v2_sdk<br/>Python CLI + API"]
46
+
47
+ Browser -->|HTTP /api/v1/| API
48
+ BrowserV2 -->|/api/v2/* + 复用 /api/v1/*| API
49
+ SDK -->|HTTP /api/v1/| API
50
+ API --> Mongo
51
+ API --> MinIO
52
+ API --> RabbitMQ
53
+ RabbitMQ --> Worker
54
+ Beat -->|定时任务| RabbitMQ
55
+ Worker --> Mongo
56
+ Worker --> MinIO
57
+ Worker --> AI
58
+ ```
59
+
60
+ ## 前置条件
61
+
62
+ | 依赖 | 版本 | 用途 |
63
+ |---|---|---|
64
+ | Python | >= 3.10 | 后端运行时 |
65
+ | Node.js | >= 20 | 前端构建(含 v2) |
66
+ | pnpm | >= 10.33.0 | 前端包管理 |
67
+ | MongoDB | >= 6 | 主数据库 |
68
+ | MinIO | 最新 | 对象存储(图片/视频/zip) |
69
+ | RabbitMQ | >= 3.12 | Celery 消息队列 |
70
+
71
+ ## 快速启动
72
+
73
+ ```bash
74
+ # 1. 克隆仓库
75
+ git clone <repo-url> && cd cortexa
76
+
77
+ # 2. 启动后端
78
+ cd backend
79
+ cp .env.example .env # 填写 MongoDB / MinIO / RabbitMQ 地址
80
+ python3 -m venv venv && source venv/bin/activate
81
+ pip install -r requirements.txt
82
+ python manage.py runserver 0.0.0.0:8000 # API 服务
83
+
84
+ # 3. 启动 Celery(另开两个终端)
85
+ celery -A server worker -l info # 异步任务处理
86
+ celery -A server beat -l info # 定时任务调度
87
+
88
+ # 4. 启动当前生产前端
89
+ cd ../frontend
90
+ pnpm install && pnpm dev # 开发模式,访问 http://localhost:8001
91
+
92
+ # 或启动 v2 工作台(质检验收按角色显示,其余入口按下列顺序排列)
93
+ # 首页、数据管理、数据标注、数据归档、质检验收、标注类型管理、AI 模型适配、AI 标签映射、人员绩效
94
+ cd ../frontend_v2
95
+ pnpm install
96
+ API_URL=http://127.0.0.1:8000 pnpm dev
97
+ # 开发模式,先访问 http://localhost:8002/login
98
+ # API_URL 由 Umi define 注入,v2 请求会访问 http://127.0.0.1:8000/api
99
+ # 使用管理员账号登录后进入 http://localhost:8002/home
100
+ # V2 登录沿用旧前端的会话键:cortexa_token、cortexa_userInfo、可选 PhilVault token 与 roleList
101
+
102
+ # 首次启用 v2:先将数据记录集合改名为 asset_packages(可重复执行)
103
+ cd ../backend
104
+ python scripts/migrate_data_records_to_asset_packages.py
105
+
106
+ # 再为历史根目录创建 raw 数据包记录(可重复执行)
107
+ cd ../backend
108
+ python scripts/migrate_root_folders_to_data_records.py
109
+
110
+ # 最后为历史数据包回填列表统计投影(可重复执行)
111
+ # V2 列表的筛选、分页和聚合只读统计侧表;切换流量前必须确认 skipped=0
112
+ python scripts/rebuild_asset_package_statistics.py
113
+
114
+ # 5. 安装 SDK(可选,用于脚本化下载数据集)
115
+ pip install cortexa_v2_sdk
116
+ cortexa-sdk --init-config # 生成 ~/.cortexa/config.json
117
+ ```
118
+
119
+ ## 仓库结构
120
+
121
+ | 目录 | 说明 |
122
+ |---|---|
123
+ | `backend/` | Django REST Framework API 服务(Python 3.10 + MongoDB) |
124
+ | `frontend/` | UmiJS + Ant Design 标注 Web 界面(TypeScript + React) |
125
+ | `frontend_v2/` | React + TypeScript + Umi Core + Arco Design;依次提供首页、数据管理、数据标注、数据归档、质检验收、标注类型管理、AI 模型适配、AI 标签映射和人员绩效,其中质检验收按角色显示;覆盖数据包创建、深度校验、增量升版、移动/导出、标注导入回写、全量绩效聚合及图片、视频、多模态抽检复核;外部已标注导入复用 `/api/v1/dataset`,V2 关联项目维护既有 `datasets` 模型 |
126
+ | `cortexa_sdk/` | 用户侧 Python SDK(`pip install cortexa_v2_sdk`):查询已发布归档、按 `archive_id` 下载训练数据,以及旧数据集下载和解析 |
127
+ | `eval/` | 数据集准备 CLI(`python -m eval`):列出、下载并解析为 `eval_samples.json` |
128
+
129
+ `frontend_v2` 通过 Umi qiankun `slave` 模式嵌入统一门户,直接消费门户发布在共享文档根节点上的语义主题 token,并即时跟随浅色/深色预设;独立运行在 `localhost:8002` 且没有兼容主题键时默认使用内置浅色回退值。
130
+
131
+ ## 文档导航
132
+
133
+ | 文档 | 说明 |
134
+ |---|---|
135
+ | [`PRODUCT.md`](PRODUCT.md) | 产品能力总览 |
136
+ | [`cortexa_sdk/README.md`](cortexa_sdk/README.md) | SDK 安装、配置、归档 API、Ray Worker 接入与 PyPI 发版流程 |
137
+ | [`docs/product/feature-map.md`](docs/product/feature-map.md) | 完整能力矩阵 |
138
+ | [`docs/reference/api.md`](docs/reference/api.md) | HTTP 接口清单 |
139
+ | [`docs/reference/config.md`](docs/reference/config.md) | 环境变量配置参考 |
140
+ | [`docs/reference/data-model.md`](docs/reference/data-model.md) | 数据模型 |
141
+ | [`docs/reference/glossary.md`](docs/reference/glossary.md) | 术语表 |
142
+ | [`docs/reference/ui-design.md`](docs/reference/ui-design.md) | v2 UI 设计与 Arco 组件规范 |
143
+ | [`docs/how-to/local-development.md`](docs/how-to/local-development.md) | 本地开发 |
144
+ | [`docs/how-to/v2-platform-operation-manual.md`](docs/how-to/v2-platform-operation-manual.md) | 数据管理平台 V2 用户操作手册 |
145
+ | [`docs/how-to/deploy.md`](docs/how-to/deploy.md) | 部署 |
146
+ | [`docs/how-to/troubleshooting.md`](docs/how-to/troubleshooting.md) | 排障 |
147
+ | [`docs/explanation/architecture.md`](docs/explanation/architecture.md) | 架构设计 |
148
+
149
+ ## Contributing
150
+
151
+ 1. 从 `main` 拉分支,命名 `feat/xxx` 或 `fix/xxx`
152
+ 2. 后端改完跑 `pytest`,前端改完跑 `pnpm build`
153
+ 3. 提 PR 并填写 `.github/pull_request_template.md` 模板
154
+
155
+ ## License
156
+
157
+ Copyright (c) 2025 Vortek Inc. and Tuanliu (Hainan Special Economic Zone) Technology Co., Ltd. All rights reserved.