class PythonPredictor(BaseLanguagePredictor):
    def __init__(self):
        super(PythonPredictor, self).__init__()
        self._model_adapter = None

    def configure(self, params):
        super(PythonPredictor, self).configure(params)

        self._model_adapter = PythonModelAdapter(
            model_dir=self._custom_model_path)

        sys.path.append(self._custom_model_path)
        self._model_adapter.load_custom_hooks()
        self._model = self._model_adapter.load_model_from_artifact()
        if self._model is None:
            raise Exception("Failed to load model")

    def predict(self, df):
        kwargs = {}
        if self._positive_class_label and self._negative_class_label:
            kwargs[
                POSITIVE_CLASS_LABEL_ARG_KEYWORD] = self._positive_class_label
            kwargs[
                NEGATIVE_CLASS_LABEL_ARG_KEYWORD] = self._negative_class_label

        predictions = self._model_adapter.predict(data=df,
                                                  model=self._model,
                                                  **kwargs)
        return predictions
Exemplo n.º 2
0
class PythonPredictor(BaseLanguagePredictor):
    def __init__(self):
        super(PythonPredictor, self).__init__()
        self._model_adapter = None
        self._mlops = None

    def configure(self, params):
        super(PythonPredictor, self).configure(params)

        self._model_adapter = PythonModelAdapter(model_dir=self._custom_model_path)

        sys.path.append(self._custom_model_path)
        self._model_adapter.load_custom_hooks()
        self._model = self._model_adapter.load_model_from_artifact()
        if self._model is None:
            raise Exception("Failed to load model")

    def predict(self, input_filename):

        kwargs = {}
        if self._positive_class_label and self._negative_class_label:
            kwargs[POSITIVE_CLASS_LABEL_ARG_KEYWORD] = self._positive_class_label
            kwargs[NEGATIVE_CLASS_LABEL_ARG_KEYWORD] = self._negative_class_label

        start_predict = time.time()
        predictions = self._model_adapter.predict(
            input_filename, model=self._model, unstructured_mode=self._unstructured_mode, **kwargs
        )
        end_predict = time.time()
        execution_time_ms = (end_predict - start_predict) * 1000

        # TODO: call monitor only if we are in structured mode
        self.monitor(input_filename, predictions, execution_time_ms)

        return predictions
class PythonPredictor(BaseLanguagePredictor):
    def __init__(self):
        super(PythonPredictor, self).__init__()
        self._model_adapter = None
        self._mlops = None

    def configure(self, params):
        super(PythonPredictor, self).configure(params)

        self._model_adapter = PythonModelAdapter(
            model_dir=self._custom_model_path, target_type=self._target_type)

        sys.path.append(self._custom_model_path)
        self._model_adapter.load_custom_hooks()
        self._model = self._model_adapter.load_model_from_artifact()
        if self._model is None:
            raise Exception("Failed to load model")

    @property
    def supported_payload_formats(self):
        return self._model_adapter.supported_payload_formats

    def predict(self, input_filename):
        kwargs = {}
        kwargs[TARGET_TYPE_ARG_KEYWORD] = self._target_type
        if self._positive_class_label and self._negative_class_label:
            kwargs[
                POSITIVE_CLASS_LABEL_ARG_KEYWORD] = self._positive_class_label
            kwargs[
                NEGATIVE_CLASS_LABEL_ARG_KEYWORD] = self._negative_class_label
        if self._class_labels:
            kwargs[CLASS_LABELS_ARG_KEYWORD] = self._class_labels

        start_predict = time.time()
        predictions = self._model_adapter.predict(input_filename,
                                                  model=self._model,
                                                  **kwargs)
        end_predict = time.time()
        execution_time_ms = (end_predict - start_predict) * 1000

        self.monitor(input_filename, predictions, execution_time_ms)

        return predictions

    def transform(self, input_filename):
        return self._model_adapter.transform(input_filename, model=self._model)

    def predict_unstructured(self, data, **kwargs):
        str_or_tuple = self._model_adapter.predict_unstructured(
            model=self._model, data=data, **kwargs)
        if isinstance(str_or_tuple, (str, bytes, type(None))):
            ret = str_or_tuple, None
        elif isinstance(str_or_tuple, tuple):
            ret = str_or_tuple
        else:
            raise DrumCommonException(
                "Wrong type returned in unstructured mode: {}".format(
                    type(str_or_tuple)))
        return ret
class PythonPredictor(BaseLanguagePredictor):
    def __init__(self):
        super(PythonPredictor, self).__init__()
        self._model_adapter = None
        self._mlops = None

    def configure(self, params):
        super(PythonPredictor, self).configure(params)

        self._model_adapter = PythonModelAdapter(model_dir=self._code_dir,
                                                 target_type=self._target_type)

        sys.path.append(self._code_dir)
        self._model_adapter.load_custom_hooks()
        self._model = self._model_adapter.load_model_from_artifact()
        if self._model is None:
            raise Exception("Failed to load model")

    @property
    def supported_payload_formats(self):
        return self._model_adapter.supported_payload_formats

    def model_info(self):
        model_info = super(PythonPredictor, self).model_info()
        model_info.update(self._model_adapter.model_info())
        return model_info

    def has_read_input_data_hook(self):
        return self._model_adapter.has_read_input_data_hook()

    def _predict(self, **kwargs):
        kwargs[TARGET_TYPE_ARG_KEYWORD] = self._target_type
        if self._positive_class_label is not None and self._negative_class_label is not None:
            kwargs[
                POSITIVE_CLASS_LABEL_ARG_KEYWORD] = self._positive_class_label
            kwargs[
                NEGATIVE_CLASS_LABEL_ARG_KEYWORD] = self._negative_class_label
        if self._class_labels:
            kwargs[CLASS_LABELS_ARG_KEYWORD] = self._class_labels

        predictions = self._model_adapter.predict(model=self._model, **kwargs)
        return predictions

    def transform(self, **kwargs):
        return self._model_adapter.transform(model=self._model, **kwargs)

    def predict_unstructured(self, data, **kwargs):
        str_or_tuple = self._model_adapter.predict_unstructured(
            model=self._model, data=data, **kwargs)
        if isinstance(str_or_tuple, (str, bytes, type(None))):
            ret = str_or_tuple, None
        elif isinstance(str_or_tuple, tuple):
            ret = str_or_tuple
        else:
            raise DrumCommonException(
                "Wrong type returned in unstructured mode: {}".format(
                    type(str_or_tuple)))
        return ret
Exemplo n.º 5
0
class PythonPredictor(ConnectableComponent):
    def __init__(self, engine):
        super(PythonPredictor, self).__init__(engine)
        self.model = None
        self.positive_class_label = None
        self.negative_class_label = None
        self.custom_model_path = None
        self._model_adapter = None

    def configure(self, params):
        super(PythonPredictor, self).configure(params)
        self.custom_model_path = self._params["__custom_model_path__"]
        self.positive_class_label = self._params.get("positiveClassLabel")
        self.negative_class_label = self._params.get("negativeClassLabel")
        self._model_adapter = PythonModelAdapter(
            model_dir=self.custom_model_path)

        sys.path.append(self.custom_model_path)
        self._model_adapter.load_custom_hooks()
        self.model = self._model_adapter.load_model_from_artifact()
        if self.model is None:
            raise Exception("Failed to load model")

    def _materialize(self, parent_data_objs, user_data):
        df = parent_data_objs[0]

        kwargs = {}
        if self.positive_class_label and self.negative_class_label:
            kwargs[
                POSITIVE_CLASS_LABEL_ARG_KEYWORD] = self.positive_class_label
            kwargs[
                NEGATIVE_CLASS_LABEL_ARG_KEYWORD] = self.negative_class_label

        predictions = self._model_adapter.predict(data=df,
                                                  model=self.model,
                                                  **kwargs)
        return [predictions]