|
1 | 1 | # Custom Handlers |
2 | 2 |
|
3 | | -There are two different ways that vetiver supports flexible handling for models that do not work automatically with the vetiver framework. The first way is with [new model types,](#new-model-type) where there is no current implementation for the type of model you would like to deploy. The second way is when you would like to implement a current handler, but [in a different way](#different-model-implementation). In either case, you *must* make a custom handler from the base `VetiverHandler`. A minimal custom handler could look like the following: |
| 3 | +There are two different ways that vetiver supports flexible handling for models that do not work automatically with the vetiver framework. The first way is with new model types where there is no current implementation for the type of model you would like to deploy. The second way is when you would like to implement a current handler, but in a different way. In either case, you should create a custom handler from vetiver's `BaseHandler()`. At a minimum, you must give the type of your model via `model_type` how predictions should be made, via the method `handler_predict()`. Then, initialize your handler with your model, and pass the object into `VetiverModel`. |
| 4 | + |
| 5 | +This example shows a custom handler of `newmodeltype` type. |
4 | 6 |
|
5 | 7 | ```python |
6 | | -from vetiver.handlers.base import VetiverHandler |
| 8 | +from vetiver.handlers.base import BaseHandler |
7 | 9 |
|
8 | | -class SampleCustomHandler(VetiverHandler): |
| 10 | +class CustomHandler(BaseHandler): |
9 | 11 | def __init__(model, ptype_data): |
10 | 12 | super().__init__(model, ptype_data) |
11 | 13 |
|
12 | | - def handler_predict(self, input_data, check_ptype): |
| 14 | + model_type = staticmethod(lambda: newmodeltype) |
| 15 | + |
| 16 | + def handler_predict(self, input_data, check_ptype: bool): |
13 | 17 | """ |
14 | | - handler_predict defines how to make predictions from your model |
| 18 | + Generates method for /predict endpoint in VetiverAPI |
| 19 | +
|
| 20 | + The `handler_predict` function executes at each API call. Use this |
| 21 | + function for calling `predict()` and any other tasks that must be executed at each API call. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + input_data: |
| 26 | + Test data |
| 27 | + check_ptype: bool |
| 28 | + Whether the ptype should be enforced |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + prediction |
| 33 | + Prediction from model |
15 | 34 | """ |
16 | 35 | # your code here |
17 | | -``` |
| 36 | + prediction = model.fancy_new_predict(input_data) |
18 | 37 |
|
19 | | -## New model type |
20 | | -If your model type is not supported by vetiver, you should create and then register the handler using [single dispatch](https://docs.python.org/3/library/functools.html#functools.singledispatch). Once the new type is registered, you are able to use `VetiverModel()` as normal. Here is a template for such a function: |
21 | | - |
22 | | -```python |
23 | | -from vetiver.handlers._interface import create_handler |
| 38 | + return prediction |
24 | 39 |
|
25 | | -@create_handler.register |
26 | | -def _(model: {_model_type}, ptype_data): |
27 | | - return SampleCustomHandler(model, ptype_data) |
| 40 | +new_model = CustomHandler(model, ptype_data) |
28 | 41 |
|
29 | | -VetiverModel(your_model, "your_model") |
| 42 | +VetiverModel(new_model, "custom_model") |
30 | 43 | ``` |
31 | 44 |
|
32 | 45 | If your model is a common type, please consider [submitting a pull request](https://github.com/rstudio/vetiver-python/pulls). |
33 | | - |
34 | | -## Different model implementation |
35 | | -If your model's prediction function is different than vetiver's, you should create a custom handler with a `handler_predict` method to make predictions. Then, initialize your handler with your model, and pass the object into `VetiverModel`. |
36 | | - |
37 | | -```python |
38 | | -new_model = SampleCustomHandler(your_model, your_ptype_data) |
39 | | - |
40 | | -VetiverModel(new_model, "your_model") |
41 | | -``` |
|
0 commit comments