| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- """
- Pydantic schemas for Project API.
- Defines request and response models for project operations.
- """
- from datetime import datetime
- from typing import Optional, List
- from enum import Enum
- from pydantic import BaseModel, Field
- # ============== 枚举定义 ==============
- class ProjectStatus(str, Enum):
- """项目状态"""
- DRAFT = "draft"
- CONFIGURING = "configuring"
- READY = "ready"
- IN_PROGRESS = "in_progress"
- COMPLETED = "completed"
- class ProjectSource(str, Enum):
- """项目来源"""
- INTERNAL = "internal"
- EXTERNAL = "external"
- # ============== 基础Schema ==============
- class ProjectCreate(BaseModel):
- """Schema for creating a new project."""
-
- name: str = Field(..., min_length=1, description="Project name")
- description: str = Field(default="", description="Project description")
- config: str = Field(..., min_length=1, description="Label Studio configuration")
- task_type: Optional[str] = Field(None, description="Task type (text_classification, image_classification, object_detection, ner)")
-
- class Config:
- json_schema_extra = {
- "example": {
- "name": "Image Classification Project",
- "description": "Classify images into categories",
- "config": "<View><Image name='image' value='$image'/><Choices name='choice' toName='image'><Choice value='Cat'/><Choice value='Dog'/></Choices></View>",
- "task_type": "image_classification"
- }
- }
- class ProjectUpdate(BaseModel):
- """Schema for updating an existing project."""
-
- name: Optional[str] = Field(None, min_length=1, description="Project name")
- description: Optional[str] = Field(None, description="Project description")
- config: Optional[str] = Field(None, min_length=1, description="Label Studio configuration")
-
- class Config:
- json_schema_extra = {
- "example": {
- "name": "Updated Project Name",
- "description": "Updated description"
- }
- }
- class ProjectResponse(BaseModel):
- """Schema for project response."""
-
- id: str = Field(..., description="Project unique identifier")
- name: str = Field(..., description="Project name")
- description: str = Field(..., description="Project description")
- config: str = Field(..., description="Label Studio configuration")
- created_at: datetime = Field(..., description="Project creation timestamp")
- task_count: int = Field(default=0, description="Number of tasks in project")
-
- class Config:
- json_schema_extra = {
- "example": {
- "id": "proj_123abc",
- "name": "Image Classification Project",
- "description": "Classify images into categories",
- "config": "<View><Image name='image' value='$image'/></View>",
- "created_at": "2024-01-12T10:30:00",
- "task_count": 5
- }
- }
- # ============== 扩展Schema(对外API支持) ==============
- class LabelConfig(BaseModel):
- """标签配置"""
- name: str = Field(..., description="标签名称")
- color: Optional[str] = Field(None, description="标签颜色(十六进制)")
- hotkey: Optional[str] = Field(None, description="快捷键")
-
- class Config:
- json_schema_extra = {
- "example": {
- "name": "正面",
- "color": "#52c41a",
- "hotkey": "1"
- }
- }
- class ProjectStatusUpdate(BaseModel):
- """项目状态更新请求"""
- status: ProjectStatus = Field(..., description="目标状态")
-
- class Config:
- json_schema_extra = {
- "example": {
- "status": "ready"
- }
- }
- class ProjectConfigUpdate(BaseModel):
- """项目配置更新请求"""
- config: str = Field(..., description="XML配置")
- labels: Optional[List[LabelConfig]] = Field(None, description="标签列表")
- task_type: Optional[str] = Field(None, description="任务类型")
-
- class Config:
- json_schema_extra = {
- "example": {
- "config": "<View>...</View>",
- "labels": [
- {"name": "正面", "color": "#52c41a"},
- {"name": "负面", "color": "#f5222d"}
- ],
- "task_type": "text_classification"
- }
- }
- class ProjectResponseExtended(BaseModel):
- """扩展的项目响应(包含状态和来源)"""
- id: str = Field(..., description="项目ID")
- name: str = Field(..., description="项目名称")
- description: str = Field(..., description="项目描述")
- config: str = Field(..., description="XML配置")
- task_type: Optional[str] = Field(None, description="任务类型")
- status: ProjectStatus = Field(default=ProjectStatus.DRAFT, description="项目状态")
- source: ProjectSource = Field(default=ProjectSource.INTERNAL, description="项目来源")
- external_id: Optional[str] = Field(None, description="外部系统项目ID")
- created_at: datetime = Field(..., description="创建时间")
- updated_at: Optional[datetime] = Field(None, description="更新时间")
- task_count: int = Field(default=0, description="总任务数")
- completed_task_count: int = Field(default=0, description="已完成任务数")
- assigned_task_count: int = Field(default=0, description="已分配任务数")
-
- class Config:
- json_schema_extra = {
- "example": {
- "id": "proj_123abc",
- "name": "图像分类项目",
- "description": "对商品图片进行分类",
- "config": "<View>...</View>",
- "task_type": "image_classification",
- "status": "in_progress",
- "source": "external",
- "external_id": "sample_center_001",
- "created_at": "2024-01-12T10:30:00",
- "updated_at": "2024-01-12T15:30:00",
- "task_count": 100,
- "completed_task_count": 45,
- "assigned_task_count": 100
- }
- }
|