You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To create a model handler, you should create a subclass of vetiver's `BaseHandler` class. This handler should include the following:
19
19
20
-
-`model_type`: A static method that declares the type of your model.
21
-
-`handler_predict()`: A method that defines how predictions should be made for your model. This method is used at the /predict endpoint in the VetiverAPI.
20
+
-`model_type`: A static method that declares the type of your model.
21
+
-`handler_predict()`: A method that defines how predictions should be made for your model. This method is used at the /predict endpoint in the VetiverAPI.
22
22
23
23
Here's an example of a handler for a model of `newmodeltype` type. Once you have defined your handler, you can initialize it with your model and pass it to the `VetiverModel` class.
24
24
25
-
```python
25
+
```python
26
26
from vetiver.handlers.base import BaseHandler
27
27
28
28
classCustomHandler(BaseHandler):
@@ -60,7 +60,7 @@ To deploy custom code, you need to include the necessary source code in your dep
60
60
61
61
If your `VetiverModel` includes custom source code, you need to include that code in your deployment files to build an API in another location. The example below shows a user-created `FeatureSelector`, which is part of a scikit-learn pipeline.
62
62
63
-
```{.python filename="model.py"}
63
+
```{.python filename="model.py"}
64
64
from sklearn.base import BaseEstimator, TransformerMixin
When you run this line, 3 files are generated: a Dockerfile, an `app.py` file, and a `vetiver_requirements.txt`. In the `app.py` file, you'll need to add an import statement that is formatted `from {name of file, excluding .py, that has custom element} import {name of custom element}`.
107
107
108
-
```{.python filename="app.py"}
108
+
```{.python filename="app.py"}
109
109
from vetiver import VetiverModel
110
110
import vetiver
111
111
import pins
@@ -121,7 +121,7 @@ api = vetiver_api.app
121
121
122
122
Add a line to your Dockerfile to copy your source file(s) into your Docker container. The format will be `COPY path/to/your/filename.py /vetiver/app/filename.py`, where the destination is always in the `/vetiver/app/` directory.
123
123
124
-
```{.bash filename="Dockerfile"}
124
+
```{.bash filename="Dockerfile"}
125
125
# # Generated by the vetiver package; edit with care
This will generate an `app.py` file, where you'll need to add an import statement that is formatted `from {name of file, excluding .py, that has custom element} import {name of custom element}`.
160
160
161
-
```{.python filename=="app.py"}
161
+
```{.python filename="=\"app.py\""}
162
162
from vetiver import VetiverModel
163
163
import vetiver
164
164
import pins
@@ -174,7 +174,7 @@ api = vetiver_api.app
174
174
175
175
After editing the `app.py` file, you can deploy it to Posit Connect using the `rsconnect` package. Use the `rsconnect.api.actions.deploy_python_fastapi()` function to deploy the API, specifying the Connect server URL, API key, directory containing the `app.py` and `model.py` files, and the entry point of the API.
176
176
177
-
```{.python}
177
+
```python
178
178
from rsconnect.api.actions import deploy_python_fastapi
directory="./", # path to the directory containing the app.py and model.py files
192
192
entry_point="app:api"# the API is the app.py file, in a variable named api
193
193
)
194
-
195
194
```
196
-
197
195
:::
198
196
197
+
## Common Pitfalls
198
+
199
+
When deploying custom code, the most common error is something similar to `AttributeError: Can't get attribute 'ExampleModel' on <module '__main__' (built-in)>`. There are a few possible causes for this error:
200
+
201
+
1. The original `ExampleModel` may have been pinned from inside a Jupyter Notebook. Because pickling only saves a reference for how to read a class, not the source code, a custom model transformer pinned from a Jupyter Notebook cannot be imported and resolved later. To fix this, repin your model/transformer from inside a Python script.
202
+
203
+
2. You may not be uploading the custom code to be used later. When deploying, you'll want to add the files containing your custom code to the `extra_files` argument so that it can be imported, eg, `vetiver.deploy_rsconnect(connect_server, board, model_name, extra_files=['custom_model.py', 'requirements.txt'])` .
204
+
199
205
Please note that the above steps are a general guide, and you may need to adapt them to your specific use case and deployment environment. If you have any questions, please consider [opening an issue](https://github.com/rstudio/vetiver-python/issues/new).
0 commit comments