Files
DataMate/runtime/datamate-python/app/module/annotation/interface/editor.py
Jerry Yan cda22a720c feat(annotation): 优化文本标注分段功能实现
- 新增 getEditorTaskSegmentsUsingGet 接口用于获取任务分段信息
- 移除 SegmentInfo 中的 text、start、end 字段,精简数据结构
- 添加 EditorTaskSegmentsResponse 类型定义用于分段摘要响应
- 实现服务端 get_task_segments 方法,支持分段信息查询
- 重构前端组件缓存机制,使用 segmentSummaryFileRef 管理分段状态
- 优化分段构建逻辑,提取 _build_segment_contexts 公共方法
- 调整后端 _build_text_task 方法中的分段处理流程
- 更新 API 类型定义,统一 RequestParams 和 RequestPayload 类型
2026-02-04 16:59:04 +08:00

119 lines
4.1 KiB
Python

"""
Label Studio Editor(前端嵌入式)接口
说明:
- 不依赖 Label Studio Server;仅复用其“编辑器”前端库
- DataMate 负责提供 tasks/annotations 数据与保存能力
- 当前支持 dataset_type=TEXT/IMAGE 的项目
"""
from __future__ import annotations
from typing import Optional
from fastapi import APIRouter, Depends, Query, Path
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.logging import get_logger
from app.db.session import get_db
from app.module.annotation.schema.editor import (
EditorProjectInfo,
EditorTaskListResponse,
EditorTaskResponse,
EditorTaskSegmentsResponse,
UpsertAnnotationRequest,
UpsertAnnotationResponse,
)
from app.module.annotation.service.editor import AnnotationEditorService
from app.module.shared.schema import StandardResponse
logger = get_logger(__name__)
router = APIRouter(
prefix="/editor",
tags=["annotation/editor"],
)
@router.get(
"/projects/{project_id}",
response_model=StandardResponse[EditorProjectInfo],
)
async def get_editor_project_info(
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
db: AsyncSession = Depends(get_db),
):
service = AnnotationEditorService(db)
info = await service.get_project_info(project_id)
return StandardResponse(code=200, message="success", data=info)
@router.get(
"/projects/{project_id}/tasks",
response_model=StandardResponse[EditorTaskListResponse],
)
async def list_editor_tasks(
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
page: int = Query(0, ge=0, description="页码(从0开始)"),
size: int = Query(50, ge=1, le=200, description="每页大小"),
exclude_source_documents: Optional[bool] = Query(
None,
alias="excludeSourceDocuments",
description="是否排除已被转换为TXT的源文档文件(PDF/DOC/DOCX,仅文本数据集生效)",
),
db: AsyncSession = Depends(get_db),
):
service = AnnotationEditorService(db)
result = await service.list_tasks(
project_id,
page=page,
size=size,
exclude_source_documents=exclude_source_documents,
)
return StandardResponse(code=200, message="success", data=result)
@router.get(
"/projects/{project_id}/tasks/{file_id}",
response_model=StandardResponse[EditorTaskResponse],
)
async def get_editor_task(
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
file_id: str = Path(..., description="文件ID(t_dm_dataset_files.id)"),
segment_index: Optional[int] = Query(None, alias="segmentIndex", description="段落索引(分段模式下使用)"),
db: AsyncSession = Depends(get_db),
):
service = AnnotationEditorService(db)
task = await service.get_task(project_id, file_id, segment_index=segment_index)
return StandardResponse(code=200, message="success", data=task)
@router.get(
"/projects/{project_id}/tasks/{file_id}/segments",
response_model=StandardResponse[EditorTaskSegmentsResponse],
)
async def list_editor_task_segments(
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
file_id: str = Path(..., description="文件ID(t_dm_dataset_files.id)"),
db: AsyncSession = Depends(get_db),
):
service = AnnotationEditorService(db)
result = await service.get_task_segments(project_id, file_id)
return StandardResponse(code=200, message="success", data=result)
@router.put(
"/projects/{project_id}/tasks/{file_id}/annotation",
response_model=StandardResponse[UpsertAnnotationResponse],
)
async def upsert_editor_annotation(
request: UpsertAnnotationRequest,
project_id: str = Path(..., description="标注项目ID(t_dm_labeling_projects.id)"),
file_id: str = Path(..., description="文件ID(t_dm_dataset_files.id)"),
db: AsyncSession = Depends(get_db),
):
service = AnnotationEditorService(db)
result = await service.upsert_annotation(project_id, file_id, request)
return StandardResponse(code=200, message="success", data=result)