Browse Source

Add APIs for testing config

pull/1206/head
Hironsan 3 years ago
parent
commit
55707bfd90
2 changed files with 73 additions and 0 deletions
  1. 16
      app/api/urls.py
  2. 57
      app/api/views.py

16
app/api/urls.py

@ -13,6 +13,7 @@ from .views import StatisticsAPI
from .views import RoleMappingList, RoleMappingDetail, Roles from .views import RoleMappingList, RoleMappingDetail, Roles
from .views import AutoLabelingTemplateListAPI, AutoLabelingTemplateDetailAPI from .views import AutoLabelingTemplateListAPI, AutoLabelingTemplateDetailAPI
from .views import AutoLabelingConfigList, AutoLabelingConfigDetail, AutoLabelingConfigTest, AutoLabelingAnnotation from .views import AutoLabelingConfigList, AutoLabelingConfigDetail, AutoLabelingConfigTest, AutoLabelingAnnotation
from .views import AutoLabelingConfigParameterTest, AutoLabelingTemplateTest, AutoLabelingMappingTest
urlpatterns = [ urlpatterns = [
path('health', Health.as_view(), name='health'), path('health', Health.as_view(), name='health'),
@ -84,6 +85,21 @@ urlpatterns = [
view=AutoLabelingAnnotation.as_view(), view=AutoLabelingAnnotation.as_view(),
name='auto_labeling_annotation' name='auto_labeling_annotation'
), ),
path(
route='auto-labeling-parameter-testing',
view=AutoLabelingConfigParameterTest.as_view(),
name='auto_labeling_parameter_testing'
),
path(
route='projects/<int:project_id>/auto-labeling-template-testing',
view=AutoLabelingTemplateTest.as_view(),
name='auto_labeling_template_test'
),
path(
route='projects/<int:project_id>/auto-labeling-mapping-testing',
view=AutoLabelingMappingTest.as_view(),
name='auto_labeling_mapping_test'
)
] ]
# urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'xml']) # urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'xml'])

57
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): class AutoLabelingAnnotation(generics.CreateAPIView):
pagination_class = None pagination_class = None
permission_classes = [IsAuthenticated & IsInProjectOrAdmin] permission_classes = [IsAuthenticated & IsInProjectOrAdmin]

Loading…
Cancel
Save