From 55707bfd90744d9906ad62aa46e458d4fa1ec665 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 18 Feb 2021 16:37:31 +0900 Subject: [PATCH] Add APIs for testing config --- app/api/urls.py | 16 ++++++++++++++ app/api/views.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/app/api/urls.py b/app/api/urls.py index f5808ca9..b55fa9f7 100644 --- a/app/api/urls.py +++ b/app/api/urls.py @@ -13,6 +13,7 @@ from .views import StatisticsAPI from .views import RoleMappingList, RoleMappingDetail, Roles from .views import AutoLabelingTemplateListAPI, AutoLabelingTemplateDetailAPI from .views import AutoLabelingConfigList, AutoLabelingConfigDetail, AutoLabelingConfigTest, AutoLabelingAnnotation +from .views import AutoLabelingConfigParameterTest, AutoLabelingTemplateTest, AutoLabelingMappingTest urlpatterns = [ path('health', Health.as_view(), name='health'), @@ -84,6 +85,21 @@ urlpatterns = [ view=AutoLabelingAnnotation.as_view(), name='auto_labeling_annotation' ), + path( + route='auto-labeling-parameter-testing', + view=AutoLabelingConfigParameterTest.as_view(), + name='auto_labeling_parameter_testing' + ), + path( + route='projects//auto-labeling-template-testing', + view=AutoLabelingTemplateTest.as_view(), + name='auto_labeling_template_test' + ), + path( + route='projects//auto-labeling-mapping-testing', + view=AutoLabelingMappingTest.as_view(), + name='auto_labeling_mapping_test' + ) ] # urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'xml']) diff --git a/app/api/views.py b/app/api/views.py index 62fbd50a..14ddba00 100644 --- a/app/api/views.py +++ b/app/api/views.py @@ -573,6 +573,63 @@ class AutoLabelingConfigTest(APIView): ) +class AutoLabelingConfigParameterTest(APIView): + permission_classes = [IsAuthenticated & IsProjectAdmin] + + def post(self, *args, **kwargs): + model_name = self.request.data['model_name'] + model_attrs = self.request.data['model_attrs'] + sample_text = self.request.data['text'] + try: + model = RequestModelFactory.create(model_name, model_attrs) + except Exception: + model = RequestModelFactory.find(model_name) + schema = model.schema() + required_fields = ', '.join(schema['required']) if 'required' in schema else '' + raise ValidationError( + 'The attributes does not match the model.' + 'You need to correctly specify the required fields: {}'.format(required_fields) + ) + try: + request = model.build() + response = request.send(text=sample_text) + return Response(response, status=status.HTTP_200_OK) + except requests.exceptions.ConnectionError: + raise URLConnectionError + except Exception as e: + raise e + + +class AutoLabelingTemplateTest(APIView): + permission_classes = [IsAuthenticated & IsProjectAdmin] + + def post(self, *args, **kwargs): + response = self.request.data['response'] + template = self.request.data['template'] + project = get_object_or_404(Project, pk=self.kwargs['project_id']) + task = TaskFactory.create(project.project_type) + template = MappingTemplate( + label_collection=task.label_collection, + template=template + ) + labels = template.render(response) + return Response(labels.dict(), status=status.HTTP_200_OK) + + +class AutoLabelingMappingTest(APIView): + permission_classes = [IsAuthenticated & IsProjectAdmin] + + def post(self, *args, **kwargs): + response = self.request.data['response'] + label_mapping = self.request.data['label_mapping'] + project = get_object_or_404(Project, pk=self.kwargs['project_id']) + task = TaskFactory.create(project.project_type) + labels = task.label_collection(response) + post_processor = PostProcessor(label_mapping) + labels = post_processor.transform(labels) + return Response(labels.dict(), status=status.HTTP_200_OK) + + class AutoLabelingAnnotation(generics.CreateAPIView): pagination_class = None permission_classes = [IsAuthenticated & IsInProjectOrAdmin]