Skip to content

Commit

Permalink
Include handler's dependencies to the archived env yml (#218)
Browse files Browse the repository at this point in the history
* Include handler's dependencies to the archived env yml

* Add tests for handler dependencies
  • Loading branch information
yubozhao authored and parano committed Jul 19, 2019
1 parent 03817cb commit 1984378
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bentoml/handlers/base_handlers.py
Expand Up @@ -61,6 +61,10 @@ def handle_aws_lambda_event(self, event, func):
def request_schema(self):
return {"application/json": {"schema": {"type": "object"}}}

@property
def pip_dependencies(self):
return []

def handle_clipper_strings(self, inputs, func):
"""Handle incoming input data for clipper cluster.
Expand Down
4 changes: 4 additions & 0 deletions bentoml/handlers/fastai_image_handler.py
Expand Up @@ -85,6 +85,10 @@ def request_schema(self):
},
}

@property
def pip_dependencies(self):
return ['imageio', 'fastai']

def handle_request(self, request, func):
try:
from fastai.vision import Image, pil2tensor
Expand Down
4 changes: 4 additions & 0 deletions bentoml/handlers/image_handler.py
Expand Up @@ -90,6 +90,10 @@ def request_schema(self):
},
}

@property
def pip_dependencies(self):
return ['imageio']

def handle_request(self, request, func):
"""Handle http request that has image file/s. It will convert image into a
ndarray for the function to consume.
Expand Down
5 changes: 4 additions & 1 deletion bentoml/service.py
Expand Up @@ -347,8 +347,8 @@ class BentoService(BentoServiceBase):

def __init__(self, artifacts=None, env=None):
self._init_artifacts(artifacts)
self._init_env(env)
self._config_service_apis()
self._init_env(env)
self.name = self.__class__.name()

def _init_artifacts(self, artifacts):
Expand Down Expand Up @@ -380,6 +380,9 @@ def _init_env(self, env=None):
else:
self._env = env

for api in self._service_apis:
self._env.add_handler_dependencies(api.handler.pip_dependencies)

@property
def artifacts(self):
return self._artifacts
Expand Down
5 changes: 5 additions & 0 deletions bentoml/service_env.py
Expand Up @@ -137,6 +137,11 @@ def add_conda_pip_dependencies(self, pip_dependencies):
pip_dependencies = [pip_dependencies]
self._conda_env.add_pip_dependencies(pip_dependencies)

def add_handler_dependencies(self, handler_dependencies):
if not isinstance(handler_dependencies, list):
handler_dependencies = [handler_dependencies]
self._conda_env.add_pip_dependencies(handler_dependencies)

def set_setup_sh(self, setup_sh_path_or_content):
setup_sh_file = Path(setup_sh_path_or_content)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_service.py
Expand Up @@ -29,3 +29,21 @@ def test_custom_api_name():
lambda x: x
)
assert str(e.value).startswith("Invalid API name")


def test_handler_pip_dependencies():

@bentoml.artifacts([bentoml.artifact.PickleArtifact('artifact')])
class TestModel(bentoml.BentoService):
@bentoml.api(bentoml.handlers.FastaiImageHandler)
def test(self, image):
return image

empy_artifact = []
service = TestModel.pack(artifact=empy_artifact)

# ordereddict
env = service._env._conda_env._conda_env
pip_requirements = env.get('dependencies')[2]['pip']
assert pip_requirements[1] == 'imageio'
assert pip_requirements[2] == 'fastai'

0 comments on commit 1984378

Please sign in to comment.