|
|
@ -6,9 +6,10 @@ from rest_framework import serializers |
|
|
|
from rest_framework.exceptions import ValidationError |
|
|
|
from rest_polymorphic.serializers import PolymorphicSerializer |
|
|
|
|
|
|
|
from .models import (AutoLabelingConfig, Comment, Document, DocumentAnnotation, |
|
|
|
Label, Project, Role, RoleMapping, Seq2seqAnnotation, |
|
|
|
Seq2seqProject, SequenceAnnotation, |
|
|
|
from .models import (DOCUMENT_CLASSIFICATION, SEQ2SEQ, SEQUENCE_LABELING, |
|
|
|
SPEECH2TEXT, AutoLabelingConfig, Comment, Document, |
|
|
|
DocumentAnnotation, Label, Project, Role, RoleMapping, |
|
|
|
Seq2seqAnnotation, Seq2seqProject, SequenceAnnotation, |
|
|
|
SequenceLabelingProject, Speech2textAnnotation, |
|
|
|
Speech2textProject, Tag, TextClassificationProject) |
|
|
|
|
|
|
@ -85,7 +86,7 @@ class DocumentSerializer(serializers.ModelSerializer): |
|
|
|
request = self.context.get('request') |
|
|
|
project = instance.project |
|
|
|
model = project.get_annotation_class() |
|
|
|
serializer = project.get_annotation_serializer() |
|
|
|
serializer = get_annotation_serializer(task=project.project_type) |
|
|
|
annotations = model.objects.filter(document=instance.id) |
|
|
|
if request and not project.collaborative_annotation: |
|
|
|
annotations = annotations.filter(user=request.user) |
|
|
@ -286,3 +287,16 @@ class AutoLabelingConfigSerializer(serializers.ModelSerializer): |
|
|
|
'You need to correctly specify the required fields: {}'.format(required_fields) |
|
|
|
) |
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
def get_annotation_serializer(task: str): |
|
|
|
mapping = { |
|
|
|
DOCUMENT_CLASSIFICATION: DocumentAnnotationSerializer, |
|
|
|
SEQUENCE_LABELING: SequenceAnnotationSerializer, |
|
|
|
SEQ2SEQ: Seq2seqAnnotationSerializer, |
|
|
|
SPEECH2TEXT: Speech2textAnnotationSerializer |
|
|
|
} |
|
|
|
try: |
|
|
|
return mapping[task] |
|
|
|
except KeyError: |
|
|
|
raise ValueError(f'{task} is not implemented.') |