Exemplo n.º 1
0
 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 botocore.exceptions.ClientError:
         raise AWSTokenError()
     except Exception as e:
         raise e
Exemplo n.º 2
0
 def validate_model_name(self, value):
     try:
         RequestModelFactory.find(value)
     except NameError:
         raise serializers.ValidationError(
             f'The specified model name {value} does not exist.')
     return value
Exemplo n.º 3
0
 def clean_fields(self, exclude=None):
     super().clean_fields(exclude=exclude)
     try:
         RequestModelFactory.find(self.model_name)
     except NameError:
         raise ValidationError(f'The specified model name {self.model_name} does not exist.')
     except Exception:
         raise ValidationError('The attributes does not match the model.')
Exemplo n.º 4
0
 def validate(self, data):
     try:
         RequestModelFactory.create(data['model_name'], data['model_attrs'])
     except Exception:
         model = RequestModelFactory.find(data['model_name'])
         schema = model.schema()
         required_fields = ', '.join(schema['required']) if 'required' in schema else ''
         raise serializers.ValidationError(
             'The attributes does not match the model.'
             'You need to correctly specify the required fields: {}'.format(required_fields)
         )
     return data
Exemplo n.º 5
0
 def validate(self, data):
     try:
         RequestModelFactory.create(data["model_name"], data["model_attrs"])
     except Exception:
         model = RequestModelFactory.find(data["model_name"])
         schema = model.schema()
         required_fields = ", ".join(
             schema["required"]) if "required" in schema else ""
         raise serializers.ValidationError(
             "The attributes does not match the model."
             "You need to correctly specify the required fields: {}".format(
                 required_fields))
     return data
Exemplo n.º 6
0
 def create_model(self):
     model_name = self.request.data['model_name']
     model_attrs = self.request.data['model_attrs']
     try:
         model = RequestModelFactory.create(model_name, model_attrs)
         return model
     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))
Exemplo n.º 7
0
 def create_model(self):
     model_name = self.request.data["model_name"]
     model_attrs = self.request.data["model_attrs"]
     try:
         model = RequestModelFactory.create(model_name, model_attrs)
         return model
     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))
Exemplo n.º 8
0
def test_request_model_factory_creates_model_correctly():
    model = GCPEntitiesRequestModel(key='lorem',
                                    type='PLAIN_TEXT',
                                    language='en')
    model_name = model.Config.title
    attributes = model.dict()
    restored_model = RequestModelFactory.create(model_name, attributes)
    assert restored_model == model
Exemplo n.º 9
0
def execute_pipeline(text: str, project_type: str, model_name: str,
                     model_attrs: dict, template: str, label_mapping: dict):
    task = TaskFactory.create(project_type)
    model = RequestModelFactory.create(model_name=model_name,
                                       attributes=model_attrs)
    template = MappingTemplate(label_collection=task.label_collection,
                               template=template)
    post_processor = PostProcessor(label_mapping)
    labels = pipeline(text=text,
                      request_model=model,
                      mapping_template=template,
                      post_processing=post_processor)
    return labels.dict()
Exemplo n.º 10
0
def execute_pipeline(data: str, config: AutoLabelingConfig):
    label_collection = get_label_collection(config.task_type)
    model = RequestModelFactory.create(model_name=config.model_name,
                                       attributes=config.model_attrs)
    template = MappingTemplate(label_collection=label_collection,
                               template=config.template)
    post_processor = PostProcessor(config.label_mapping)
    labels = pipeline(text=data,
                      request_model=model,
                      mapping_template=template,
                      post_processing=post_processor)
    labels = create_labels(config.task_type, labels)
    return labels
Exemplo n.º 11
0
def test_request_model_factory_raises_exception_if_model_does_not_exist():
    with pytest.raises(NameError):
        RequestModelFactory.create('NotExistModel', {})
Exemplo n.º 12
0
 def test_called_with_proper_model(self, mock):
     self.assert_create(self.project.admin, status.HTTP_200_OK)
     _, kwargs = mock.call_args
     expected = RequestModelFactory.create(self.data["model_name"],
                                           self.data["model_attrs"])
     self.assertEqual(kwargs["model"], expected)
Exemplo n.º 13
0
def test_find_model():
    model = RequestModelFactory.find('Amazon Comprehend Sentiment Analysis')
    assert model == AmazonComprehendSentimentRequestModel