File size: 14,912 Bytes
e43edbb e054d0c e43edbb e054d0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
"""
适配器抽象基类模块
定义任务队列、存储、数据库等适配器的抽象接口
"""
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Dict, List, Optional, AsyncGenerator, Any
if TYPE_CHECKING:
from ..models.domain import Task
class TaskQueueAdapter(ABC):
"""
任务队列适配器抽象基类
定义任务队列的通用接口,支持本地(asyncio.subprocess)和
服务器(Celery)两种实现方式。
Example:
>>> adapter = AsyncTrainingManager(db_path="./data/tasks.db")
>>> job_id = await adapter.enqueue("task-123", {"exp_name": "test"})
>>> status = await adapter.get_status(job_id)
>>> async for progress in adapter.subscribe_progress("task-123"):
... print(progress)
"""
@abstractmethod
async def enqueue(self, task_id: str, config: Dict, priority: str = "normal") -> str:
"""
将任务加入队列
Args:
task_id: 任务唯一标识
config: 任务配置字典,包含训练所需的所有参数
priority: 任务优先级 ("low", "normal", "high")
Returns:
job_id: 队列中的作业ID
Raises:
ValueError: 配置无效时抛出
"""
pass
@abstractmethod
async def get_status(self, job_id: str) -> Dict:
"""
获取任务状态
Args:
job_id: 作业ID
Returns:
状态字典,包含:
- status: 任务状态 (queued, running, completed, failed, cancelled)
- progress: 进度 (0.0-1.0)
- current_stage: 当前阶段名称
- message: 状态消息
- error_message: 错误信息(如果失败)
"""
pass
@abstractmethod
async def cancel(self, job_id: str) -> bool:
"""
取消任务
Args:
job_id: 作业ID
Returns:
是否成功取消
"""
pass
@abstractmethod
async def subscribe_progress(self, task_id: str) -> AsyncGenerator[Dict, None]:
"""
订阅任务进度(用于 SSE 流)
Args:
task_id: 任务ID
Yields:
进度信息字典,包含:
- type: 消息类型 ("progress", "log", "heartbeat")
- stage: 当前阶段
- progress: 进度值
- message: 进度消息
- status: 状态 (running, completed, failed, cancelled)
Note:
当 status 为终态时,生成器会自动结束
"""
pass
class ProgressAdapter(ABC):
"""
进度管理适配器抽象基类
用于更新和订阅任务进度,支持本地(内存队列)和
服务器(Redis Pub/Sub)两种实现。
"""
@abstractmethod
async def update_progress(self, task_id: str, progress: Dict) -> None:
"""
更新进度
Args:
task_id: 任务ID
progress: 进度信息字典
"""
pass
@abstractmethod
async def get_progress(self, task_id: str) -> Optional[Dict]:
"""
获取当前进度
Args:
task_id: 任务ID
Returns:
最新进度信息,不存在则返回 None
"""
pass
@abstractmethod
async def subscribe(self, task_id: str) -> AsyncGenerator[Dict, None]:
"""
订阅进度更新
Args:
task_id: 任务ID
Yields:
进度信息字典
"""
pass
class StorageAdapter(ABC):
"""
存储适配器抽象基类
定义文件存储的通用接口,支持本地文件系统和
对象存储(S3/MinIO)两种实现方式。
Example:
>>> adapter = LocalStorageAdapter(base_path="./data/files")
>>> file_id = await adapter.upload_file(data, "audio.wav", {"purpose": "training"})
>>> content = await adapter.download_file(file_id)
>>> await adapter.delete_file(file_id)
"""
@abstractmethod
async def upload_file(
self,
file_data: bytes,
filename: str,
metadata: Dict[str, Any]
) -> str:
"""
上传文件
Args:
file_data: 文件二进制数据
filename: 原始文件名
metadata: 文件元数据,可包含:
- content_type: MIME类型
- purpose: 文件用途 (training, reference, output)
- 其他自定义字段
Returns:
file_id: 文件唯一标识
Raises:
IOError: 存储失败时抛出
"""
pass
@abstractmethod
async def download_file(self, file_id: str) -> bytes:
"""
下载文件
Args:
file_id: 文件唯一标识
Returns:
文件二进制数据
Raises:
FileNotFoundError: 文件不存在时抛出
"""
pass
@abstractmethod
async def delete_file(self, file_id: str) -> bool:
"""
删除文件
Args:
file_id: 文件唯一标识
Returns:
是否成功删除
"""
pass
@abstractmethod
async def get_file_metadata(self, file_id: str) -> Optional[Dict[str, Any]]:
"""
获取文件元数据
Args:
file_id: 文件唯一标识
Returns:
文件元数据字典,包含:
- id: 文件ID
- filename: 原始文件名
- content_type: MIME类型
- size_bytes: 文件大小
- purpose: 文件用途
- uploaded_at: 上传时间
- 音频文件额外包含: duration_seconds, sample_rate
文件不存在时返回 None
"""
pass
@abstractmethod
async def list_files(
self,
purpose: Optional[str] = None,
limit: int = 50,
offset: int = 0
) -> List[Dict[str, Any]]:
"""
列出文件
Args:
purpose: 按用途筛选 (training, reference, output)
limit: 返回数量限制
offset: 偏移量
Returns:
文件元数据列表
"""
pass
@abstractmethod
async def file_exists(self, file_id: str) -> bool:
"""
检查文件是否存在
Args:
file_id: 文件唯一标识
Returns:
文件是否存在
"""
pass
class DatabaseAdapter(ABC):
"""
数据库适配器抽象基类
定义数据持久化的通用接口,支持 SQLite 和
PostgreSQL 两种实现方式。
管理以下实体:
- Task: Quick Mode 一键训练任务
- Experiment: Advanced Mode 实验
- Stage: 实验中的各个阶段
- File: 上传的文件记录(可选,与StorageAdapter配合)
Example:
>>> adapter = SQLiteAdapter(db_path="./data/app.db")
>>> task = await adapter.create_task(task_data)
>>> task = await adapter.get_task(task_id)
>>> await adapter.update_task(task_id, {"status": "completed"})
"""
# ============================================================
# Task CRUD (Quick Mode)
# ============================================================
@abstractmethod
async def create_task(self, task: "Task") -> "Task":
"""
创建任务
Args:
task: Task 领域模型实例
Returns:
创建后的 Task 实例(包含生成的字段如 created_at)
"""
pass
@abstractmethod
async def get_task(self, task_id: str) -> Optional["Task"]:
"""
获取任务
Args:
task_id: 任务唯一标识
Returns:
Task 实例,不存在则返回 None
"""
pass
@abstractmethod
async def update_task(self, task_id: str, updates: Dict[str, Any]) -> Optional["Task"]:
"""
更新任务
Args:
task_id: 任务唯一标识
updates: 要更新的字段字典
Returns:
更新后的 Task 实例,不存在则返回 None
"""
pass
@abstractmethod
async def list_tasks(
self,
status: Optional[str] = None,
limit: int = 50,
offset: int = 0
) -> List["Task"]:
"""
查询任务列表
Args:
status: 按状态筛选
limit: 返回数量限制
offset: 偏移量
Returns:
Task 实例列表
"""
pass
@abstractmethod
async def delete_task(self, task_id: str) -> bool:
"""
删除任务
Args:
task_id: 任务唯一标识
Returns:
是否成功删除
"""
pass
@abstractmethod
async def count_tasks(self, status: Optional[str] = None) -> int:
"""
统计任务数量
Args:
status: 按状态筛选
Returns:
任务数量
"""
pass
@abstractmethod
async def get_task_by_exp_name(self, exp_name: str) -> Optional["Task"]:
"""
根据实验名称获取任务
用于检查 exp_name 是否已存在。
Args:
exp_name: 实验名称
Returns:
Task 实例,不存在则返回 None
"""
pass
# ============================================================
# Experiment CRUD (Advanced Mode)
# ============================================================
@abstractmethod
async def create_experiment(self, experiment: Dict[str, Any]) -> Dict[str, Any]:
"""
创建实验
Args:
experiment: 实验数据字典
Returns:
创建后的实验数据
"""
pass
@abstractmethod
async def get_experiment(self, exp_id: str) -> Optional[Dict[str, Any]]:
"""
获取实验
Args:
exp_id: 实验唯一标识
Returns:
实验数据字典,不存在则返回 None
"""
pass
@abstractmethod
async def update_experiment(
self,
exp_id: str,
updates: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
更新实验
Args:
exp_id: 实验唯一标识
updates: 要更新的字段字典
Returns:
更新后的实验数据,不存在则返回 None
"""
pass
@abstractmethod
async def list_experiments(
self,
status: Optional[str] = None,
limit: int = 50,
offset: int = 0
) -> List[Dict[str, Any]]:
"""
查询实验列表
Args:
status: 按状态筛选
limit: 返回数量限制
offset: 偏移量
Returns:
实验数据列表
"""
pass
@abstractmethod
async def delete_experiment(self, exp_id: str) -> bool:
"""
删除实验
Args:
exp_id: 实验唯一标识
Returns:
是否成功删除
"""
pass
# ============================================================
# Stage 操作 (Advanced Mode)
# ============================================================
@abstractmethod
async def update_stage(
self,
exp_id: str,
stage_type: str,
updates: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
更新阶段状态
Args:
exp_id: 实验唯一标识
stage_type: 阶段类型
updates: 要更新的字段字典
Returns:
更新后的阶段数据,不存在则返回 None
"""
pass
@abstractmethod
async def get_stage(
self,
exp_id: str,
stage_type: str
) -> Optional[Dict[str, Any]]:
"""
获取阶段状态
Args:
exp_id: 实验唯一标识
stage_type: 阶段类型
Returns:
阶段数据字典,不存在则返回 None
"""
pass
@abstractmethod
async def get_all_stages(self, exp_id: str) -> List[Dict[str, Any]]:
"""
获取实验的所有阶段状态
Args:
exp_id: 实验唯一标识
Returns:
阶段数据列表
"""
pass
# ============================================================
# File 记录 (可选,与 StorageAdapter 配合)
# ============================================================
@abstractmethod
async def create_file_record(self, file_data: Dict[str, Any]) -> Dict[str, Any]:
"""
创建文件记录
Args:
file_data: 文件元数据
Returns:
创建后的文件记录
"""
pass
@abstractmethod
async def get_file_record(self, file_id: str) -> Optional[Dict[str, Any]]:
"""
获取文件记录
Args:
file_id: 文件唯一标识
Returns:
文件记录,不存在则返回 None
"""
pass
@abstractmethod
async def delete_file_record(self, file_id: str) -> bool:
"""
删除文件记录
Args:
file_id: 文件唯一标识
Returns:
是否成功删除
"""
pass
@abstractmethod
async def list_file_records(
self,
purpose: Optional[str] = None,
limit: int = 50,
offset: int = 0
) -> List[Dict[str, Any]]:
"""
查询文件记录列表
Args:
purpose: 按用途筛选
limit: 返回数量限制
offset: 偏移量
Returns:
文件记录列表
"""
pass
|