From a2de1763033a8b983145de1a8987b30e31d438a2 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 9 Dec 2021 12:51:28 +0900 Subject: [PATCH] Add TextLabel API --- backend/api/tests/api/test_annotation.py | 47 ++++++++++++++++++++++-- backend/api/urls.py | 12 +++++- backend/api/views/tasks/text.py | 13 +++++++ 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 backend/api/views/tasks/text.py diff --git a/backend/api/tests/api/test_annotation.py b/backend/api/tests/api/test_annotation.py index fe87ecb9..eb81e6a7 100644 --- a/backend/api/tests/api/test_annotation.py +++ b/backend/api/tests/api/test_annotation.py @@ -1,8 +1,8 @@ from rest_framework import status from rest_framework.reverse import reverse -from ...models import (DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, Category, - Span) +from ...models import (DOCUMENT_CLASSIFICATION, SEQ2SEQ, SEQUENCE_LABELING, + Category, Span, TextLabel) from .utils import (CRUDMixin, make_annotation, make_doc, make_label, make_user, prepare_project) @@ -58,6 +58,12 @@ class TestSpanList(TestAnnotationList): make_annotation(cls.task, doc=doc, user=member, start_offset=0, end_offset=1) +class TestTextList(TestAnnotationList): + model = TextLabel + task = SEQ2SEQ + view_name = 'text_list' + + class TestSharedAnnotationList(CRUDMixin): model = Category task = DOCUMENT_CLASSIFICATION @@ -92,7 +98,7 @@ class TestSharedCategoryList(TestSharedAnnotationList): view_name = 'category_list' -class TestSharedSpanList(TestSharedCategoryList): +class TestSharedSpanList(TestSharedAnnotationList): model = Span task = SEQUENCE_LABELING view_name = 'span_list' @@ -110,6 +116,12 @@ class TestSharedSpanList(TestSharedCategoryList): cls.start_offset += 1 +class TestSharedTextList(TestSharedAnnotationList): + model = TextLabel + task = SEQ2SEQ + view_name = 'text_list' + + class TestAnnotationCreation(CRUDMixin): task = DOCUMENT_CLASSIFICATION view_name = 'annotation_list' @@ -149,6 +161,14 @@ class TestSpanCreation(TestAnnotationCreation): return {'label': label.id, 'start_offset': 0, 'end_offset': 1} +class TestTextLabelCreation(TestAnnotationCreation): + task = SEQ2SEQ + view_name = 'text_list' + + def create_data(self): + return {'text': 'example'} + + class TestAnnotationDetail(CRUDMixin): task = SEQUENCE_LABELING view_name = 'annotation_detail' @@ -218,6 +238,18 @@ class TestSpanDetail(TestAnnotationDetail): view_name = 'span_detail' +class TestTextDetail(TestAnnotationDetail): + task = SEQ2SEQ + view_name = 'text_detail' + + def setUp(self): + super().setUp() + self.data = {'text': 'changed'} + + def create_annotation_data(self, doc): + return make_annotation(task=self.task, doc=doc, user=self.project.users[0]) + + class TestSharedAnnotationDetail(CRUDMixin): task = DOCUMENT_CLASSIFICATION view_name = 'annotation_detail' @@ -255,3 +287,12 @@ class TestSharedSpanDetail(TestSharedAnnotationDetail): def make_annotation(self, doc, member): return make_annotation(self.task, doc=doc, user=member, start_offset=0, end_offset=1) + + +class TestSharedTextDetail(TestSharedAnnotationDetail): + task = SEQ2SEQ + view_name = 'text_detail' + + def setUp(self): + super().setUp() + self.data = {'text': 'changed'} diff --git a/backend/api/urls.py b/backend/api/urls.py index b03b24cc..04e25f26 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -4,7 +4,7 @@ from .views import (annotation, annotation_relations, auto_labeling, comment, example, example_state, export_dataset, health, import_dataset, import_export, label, project, relation_types, role, statistics, tag, task, user) -from .views.tasks import category, span +from .views.tasks import category, span, text urlpatterns_project = [ path( @@ -133,6 +133,16 @@ urlpatterns_project = [ view=span.SpanDetailAPI.as_view(), name='span_detail' ), + path( + route='examples//texts', + view=text.TextLabelListAPI.as_view(), + name='text_list' + ), + path( + route='examples//texts/', + view=text.TextLabelDetailAPI.as_view(), + name='text_detail' + ), path( route='tags', view=tag.TagList.as_view(), diff --git a/backend/api/views/tasks/text.py b/backend/api/views/tasks/text.py new file mode 100644 index 00000000..c6c0d23c --- /dev/null +++ b/backend/api/views/tasks/text.py @@ -0,0 +1,13 @@ +from ...models import TextLabel +from ...serializers import TextLabelSerializer +from .base import BaseDetailAPI, BaseListAPI + + +class TextLabelListAPI(BaseListAPI): + annotation_class = TextLabel + serializer_class = TextLabelSerializer + + +class TextLabelDetailAPI(BaseDetailAPI): + queryset = TextLabel.objects.all() + serializer_class = TextLabelSerializer