Beispiel #1
0
 def get_service_api(self, api_name=None):
     if api_name:
         try:
             return next((api for api in self._service_apis if api.name == api_name))
         except StopIteration:
             raise NotFound(
                 "Can't find API '{}' in service '{}'".format(api_name, self.name)
             )
     elif len(self._service_apis):
         return self._service_apis[0]
     else:
         raise NotFound("Can't find default API for service '{}'".format(self.name))
Beispiel #2
0
 def _load_from_string(self, model_name):
     try:
         transformers_model = getattr(
             import_module("transformers"),
             self._model_type).from_pretrained(model_name)
         tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
         self._model = {"model": transformers_model, "tokenizer": tokenizer}
     except EnvironmentError:
         raise NotFound("model with the name {} is not present "
                        "in the transformers library".format(model_name))
     except AttributeError:
         raise NotFound("transformers has no model type called {}".format(
             self._model_type))
Beispiel #3
0
 def _load_from_directory(self, path):
     if self._model_type is None:
         raise NotFound("Type of transformers model not found. "
                        "This should be present in a file called "
                        "'_model_type.txt' in the artifacts of the bundle.")
     if self._tokenizer_type is None:
         raise NotFound(
             "Type of transformers tokenizer not found. "
             "This should be present in a file called 'tokenizer_type.txt' "
             "in the artifacts of the bundle.")
     transformers_model = getattr(import_module("transformers"),
                                  self._model_type).from_pretrained(path)
     tokenizer = getattr(import_module("transformers"),
                         self._tokenizer_type).from_pretrained(path)
     self._model = {"model": transformers_model, "tokenizer": tokenizer}
Beispiel #4
0
    def _load_from_string(self, model_name):
        try:
            transformers_model = \
                getattr(import_module('transformers'), self._model_type) \
                .from_pretrained(model_name)

            tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)

            self._model = {'model': transformers_model, 'tokenizer': tokenizer}

        except EnvironmentError:
            raise NotFound("model with name {} not present in "
                           "the transformers library".format(model_name))
        except AttributeError:
            raise NotFound("transformers has no model type "
                           "called {}".format(self._model_type))
Beispiel #5
0
    def get_inference_api(self, api_name):
        """Find the inference API in this BentoService with a specific name.

        When the api_name is None, this returns the first Inference API found in the
        `self.inference_apis` list.

        :param api_name: the target Inference API's name
        :return:
        """
        if api_name:
            try:
                return next((api for api in self.inference_apis
                             if api.name == api_name))
            except StopIteration:
                raise NotFound("Can't find API '{}' in service '{}'".format(
                    api_name, self.name))
        elif len(self.inference_apis) > 0:
            return self.inference_apis[0]
        else:
            raise NotFound(
                f"Can't find any inference API in service '{self.name}'")