Skip to content

Commit 88c23a4

Browse files
committed
adding advanced usage docs for custom handlers
1 parent b2ab951 commit 88c23a4

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Custom Handlers
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:
4+
5+
```python
6+
from vetiver.handlers.base import VetiverHandler
7+
8+
class SampleCustomHandler(VetiverHandler):
9+
def __init__(model, ptype_data):
10+
super().__init__(model, ptype_data)
11+
12+
def handler_predict(self, input_data, check_ptype):
13+
"""
14+
handler_predict defines how to make predictions from your model
15+
"""
16+
# your code here
17+
```
18+
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
24+
25+
@create_handler.register
26+
def _(model: {_model_type}, ptype_data):
27+
return SampleCustomHandler(model, ptype_data)
28+
29+
VetiverModel(your_model, "your_model")
30+
```
31+
32+
If your datatype is a common type, please consider submitting a pull request.
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+
```

docs/source/conf.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
# add these directories to sys.path here. If the directory is relative to the
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
13-
# import os
14-
# import sys
15-
# sys.path.insert(0, os.path.abspath('.'))
13+
import os
14+
import sys
15+
sys.path.insert(0, os.path.abspath('.'))
16+
from vetiver import __version__
1617

1718

1819
# -- Project information -----------------------------------------------------
@@ -22,7 +23,7 @@
2223
author = "Isabel Zimmerman"
2324

2425
# The full version, including alpha/beta/rc tags
25-
release = "0.1.3"
26+
release = __version__
2627

2728

2829
# -- General configuration ---------------------------------------------------
@@ -64,6 +65,13 @@
6465

6566
}
6667

68+
source_suffix = {
69+
'.rst': 'restructuredtext',
70+
'.txt': 'markdown',
71+
'.md': 'markdown',
72+
}
73+
myst_heading_anchors = 2
74+
6775
html_logo = "../figures/logo.png"
6876
html_favicon = "../figures/logo.png"
6977

docs/source/index.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ Version
4242
Deploy
4343
==================
4444

45-
.. currentmodule:: vetiver
46-
4745
.. autosummary::
4846
:toctree: reference/
4947
:caption: Deploy
@@ -56,3 +54,10 @@ Deploy
5654
~load_pkgs
5755
~vetiver_write_app
5856
~vetiver_write_docker
57+
58+
Advanced Usage
59+
==================
60+
.. toctree::
61+
advancedusage/custom_handler.md
62+
:caption: Advanced Usage
63+

vetiver/vetiver_model.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def __init__(
1818
class VetiverModel:
1919
"""Create VetiverModel class for serving.
2020
21-
Attributes
21+
Parameters
2222
----------
2323
model :
24-
A trained model, such as an sklearn or spacy model
24+
A trained model, such as an sklearn or torch model
2525
name : string
2626
Model name or ID
2727
ptype_data : pd.DataFrame, np.array
@@ -32,6 +32,19 @@ class VetiverModel:
3232
A detailed description of the model. if omitted, a brief description will be generated
3333
metadata : dict
3434
Other details to be saved and accessed for serving
35+
36+
Attributes
37+
----------
38+
ptype : pydantic.main.BaseModel
39+
Data prototype
40+
handler_predict:
41+
Method to make predictions from a trained model
42+
43+
Notes
44+
-----
45+
VetiverModel can also take an initialized custom VetiverHandler
46+
as a model, for advanced use cases or non-supported model types.
47+
3548
"""
3649

3750
def __init__(

0 commit comments

Comments
 (0)