-
Notifications
You must be signed in to change notification settings - Fork 18
Description
Describe the bug
When defining a custom element to use in a model, the the API for serving up the model breaks. For example, using this simple no-op transformer results in the API breaking with a error message like AttributeError: Can't get attribute 'MyTransformer' on <module '__main__' from '...'> even when loaded into an API like https://github.com/brooklynbagel/vetiver-reprex-custom-elements/blob/3ec27d180be0c6ec115af1554fbd2b8f830fa73b/attempt-2/api/app.py.
class MyTransformer(TransformerMixin, BaseEstimator):
def fit(self, X, y=None):
return self
def transform(self, X):
return XTo Reproduce
Steps to reproduce the behavior:
- Define a custom element to use a model
- Deploy said model
- Either deploy API for model or run locally with
uvicorn app:api - See error
AttributeError: Can't get attribute 'MyTransformer' on <module '__main__' from '...'>
Expected behavior
API should start up normally with no error
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
Smartphone (please complete the following information):
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
Additional context
See reprex
It does work when forcing the transformer into module __main__, see
class MyTransformer(TransformerMixin, BaseEstimator):
def fit(self, X, y=None):
return self
def transform(self, X):
return X
# this fixes the `AttributeError`
setattr(sys.modules["__main__"], "MyTransformer", MyTransformer)API deployed on dogfood: https://connect.posit.it/content/aaac8d80-fb22-48d0-98df-bf1683f91170