""" Template API router. Provides endpoints for template management and XML configuration validation. """ from typing import List, Optional from fastapi import APIRouter, HTTPException, status, Query, Request from schemas.template import ( TemplateResponse, TemplateListResponse, TemplateCategory, TemplateCategoryListResponse, ConfigValidationRequest, ConfigValidationResponse, ValidationError ) import xml.etree.ElementTree as ET import re router = APIRouter( prefix="/api/templates", tags=["templates"] ) # 预设模板类别 TEMPLATE_CATEGORIES = [ TemplateCategory( id="image_classification", name="图像分类", description="对图像进行分类标注", icon="image" ), TemplateCategory( id="object_detection", name="目标检测", description="在图像中标注目标边界框", icon="box" ), TemplateCategory( id="image_segmentation", name="图像分割", description="对图像进行像素级分割标注", icon="layers" ), TemplateCategory( id="text_classification", name="文本分类", description="对文本进行分类标注", icon="file-text" ), TemplateCategory( id="ner", name="命名实体识别", description="标注文本中的命名实体", icon="tag" ), TemplateCategory( id="text_labeling", name="文本标注", description="对文本进行通用标注", icon="edit" ), TemplateCategory( id="audio_transcription", name="音频转写", description="将音频转写为文本", icon="mic" ), TemplateCategory( id="video_annotation", name="视频标注", description="对视频进行标注", icon="video" ), ] # 预设模板 PREDEFINED_TEMPLATES = [ # 图像分类模板 TemplateResponse( id="image_classification_basic", name="图像分类 - 基础", category="image_classification", description="简单的图像分类任务,支持单选分类", config=""" """, tags=["图像", "分类", "单选"] ), TemplateResponse( id="image_classification_multi", name="图像分类 - 多标签", category="image_classification", description="支持多标签的图像分类任务", config=""" """, tags=["图像", "分类", "多选"] ), # 目标检测模板 TemplateResponse( id="object_detection_bbox", name="目标检测 - 边界框", category="object_detection", description="使用矩形框标注图像中的目标", config=""" """, tags=["图像", "检测", "边界框"] ), TemplateResponse( id="object_detection_keypoint", name="目标检测 - 关键点", category="object_detection", description="使用关键点标注图像中的目标", config=""" """, tags=["图像", "检测", "关键点"] ), # 图像分割模板 TemplateResponse( id="image_segmentation_polygon", name="图像分割 - 多边形", category="image_segmentation", description="使用多边形进行图像分割标注", config=""" """, tags=["图像", "分割", "多边形"] ), TemplateResponse( id="image_segmentation_brush", name="图像分割 - 画笔", category="image_segmentation", description="使用画笔工具进行图像分割标注", config=""" """, tags=["图像", "分割", "画笔"] ), # 文本分类模板 TemplateResponse( id="text_classification_sentiment", name="文本分类 - 情感分析", category="text_classification", description="对文本进行情感分类", config=""" """, tags=["文本", "分类", "情感"] ), TemplateResponse( id="text_classification_topic", name="文本分类 - 主题分类", category="text_classification", description="对文本进行主题分类", config=""" """, tags=["文本", "分类", "主题"] ), # 命名实体识别模板 TemplateResponse( id="ner_basic", name="命名实体识别 - 基础", category="ner", description="标注文本中的命名实体", config=""" """, tags=["文本", "NER", "实体"] ), TemplateResponse( id="ner_relation", name="命名实体识别 - 关系抽取", category="ner", description="标注实体及其关系", config=""" """, tags=["文本", "NER", "关系"] ), # 文本标注模板 TemplateResponse( id="text_labeling_qa", name="文本标注 - 问答对", category="text_labeling", description="标注问答对数据", config="""