certifai.model.sdk.composed_wrapper module

class certifai.model.sdk.composed_wrapper.ComposedModelWrapper(port: int = 8551, host: str = '127.0.0.1')

ComposedModelWrapper provides a Flask app that dispatches to multiple simple model wrappers.

Parameters
  • model (IBaseModel) – any predictor object that has a predict method which takes a sequence of data vectors as a numpy array and returns a sequence of corresponding predicted values. To override default predict behaviour see SimpleModelWrapper.predict().

  • endpoint_url (Optional[str]) – valid url route string to create POST endpoint for model invoke e.g. /api/model/predict. defaults to /predict.

  • port (Optional[int]) – the port of the webserver. Defaults to 8551

  • host (Optional[str]) – the hostname to listen on. Set this to ‘0.0.0.0’ to have the server available externally as well. Defaults to ‘127.0.0.1’.

  • encoder (Optional[Callable[[Sequence],Sequence]]) – optional function used to transform the model’s input (e.g. - to perform one-hot encoding and so on).

  • decoder (Optional[Callable[[Sequence],Sequence]]) – optional function used to transform the model’s output (e.g. - to binarize with some threshold).

  • supports_soft_scores (Optional[bool]) – True, if model supports soft scores. default is False

  • score_labels (Optional[list]) – ordered list of class labels corresponding to each predicted score array in-case of soft scoring model

  • threshold (Optional[float]) – value at which prediction to be considered positive; only used in binary-classification when model returns simple list of scores for the positive class

  • model_type (Optional[ModelTypesEnum]) – type of third-party model to import. currently supported ‘h2o_mojo’

  • model_path (Optional[str]) – disk path of third-party model to import. currently supported ‘h2o_mojo’

add_wrapped_model(mount_prefix: str, wrapped_model: certifai.model.sdk.simple_wrapper.SimpleModelWrapper) None

adds a wrapped simple model to create ComposedModelWrapper with multiple dispatch route endpoints e.g mount_prefix of /models/svm and endpoint_url (from SimpleModelWrapper.endpoint_url) of /predict will create a route POST endpoint /models/svm/predict

Parameters
  • wrapped_model (SimpleModelWrapper) – wrapped simple model to add

  • mount_prefix (str) – prefix to be appended to wrapped simple model’s route (POST endpoint)

Returns

None

run(production: Optional[bool] = False, worker_class: Optional[certifai.model.utils.gunicorn_conf.WorkerTypeEnum] = WorkerTypeEnum.gevent, log_level: Optional[certifai.model.utils.gunicorn_conf.LogLevelEnum] = LogLevelEnum.info, num_workers: Optional[int] = 3, timeout_secs: Optional[int] = 20)

start model server

Parameters
  • production (Optional[bool]) – start gunicorn server if True else run native Flask app. default is False

  • worker_class (Optional[str]) – type of gunicorn worker. default is gevent. supported type (gthread,gevent,sync)

  • log_level (Optional[str]) – logging level. default is info.

  • num_workers (Optional[int]) – number of gunicorn worker processes to start. default is 3

  • timeout_secs (Optional[int]) – gunicorn worker timeout in secs. default is 20

Returns

None

predict(npinstances: numpy.ndarray) numpy.ndarray

Override this method to change the way the model is called. The default implementation calls model.predict(npinstances)

Parameters

npinstances (np.ndarray) – numpy array of shape (n_samples, n_features) to predict on

Returns

numpy array of model predictions of shape (n_samples,)

Return type

np.ndarray