Skip to content

Commit

Permalink
Fix bundle load with requirements_txt_file in @env parameter (#1566)
Browse files Browse the repository at this point in the history
* Fix bundle load with requirements_txt_file env parameter

* linter & unit test fix
  • Loading branch information
parano committed Apr 5, 2021
1 parent 7d40998 commit 03c7533
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
10 changes: 10 additions & 0 deletions bentoml/saved_bundle/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ def load_bento_service_class(bundle_path):
# Set cls._version, service instance can access it via svc.version
model_service_class._bento_service_bundle_version = metadata["service_version"]

if (
model_service_class._env
and model_service_class._env._requirements_txt_file is not None
):
# Load `requirement.txt` from bundle directory instead of the user-provided
# file path, which may only available during the bundle save process
model_service_class._env._requirements_txt_file = os.path.join(
bundle_path, "requirements.txt"
)

return model_service_class


Expand Down
35 changes: 24 additions & 11 deletions bentoml/service/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def __init__(
self._pip_trusted_host = pip_trusted_host
self._pip_extra_index_url = pip_extra_index_url
self._requirements_txt_file = requirements_txt_file
self._requirements_txt_content = None

self._conda_env = CondaEnv(
channels=conda_channels,
Expand Down Expand Up @@ -294,12 +295,23 @@ def set_setup_sh(self, setup_sh_path_or_content):
self._setup_sh = setup_sh_path_or_content.encode("utf-8")

def requirements_txt_content(self):
requirements_txt_file = Path(self._requirements_txt_file)
if not requirements_txt_file.is_file():
raise BentoMLException(
f"requirement txt file not found at '{requirements_txt_file}'"
)
return requirements_txt_file
if self._requirements_txt_content:
# This branch handles the case when the _requirements_txt_content is
# previously set by the BentoBundle loader
return self._requirements_txt_content

if not self._requirements_txt_file:
raise BentoMLException("requirement txt file not specified")

if not self._requirements_txt_content:
req_txt_file = Path(self._requirements_txt_file)
if not req_txt_file.is_file():
raise BentoMLException(
f"requirement txt file not found at '{self._requirements_txt_file}'"
)
self._requirements_txt_content = req_txt_file.read_text()

return self._requirements_txt_content

def infer_pip_packages(self, bento_service):
if self._infer_pip_packages:
Expand Down Expand Up @@ -327,7 +339,7 @@ def save(self, path):
requirements_txt_file = os.path.join(path, "requirements.txt")
with open(requirements_txt_file, "wb") as f:
if self._requirements_txt_file:
f.write(self.requirements_txt_content().read_bytes())
f.write(self.requirements_txt_content().encode("utf-8"))
else:
if self._pip_index_url:
f.write(f"--index-url={self._pip_index_url}\n".encode("utf-8"))
Expand Down Expand Up @@ -361,14 +373,15 @@ def to_dict(self):
if self._setup_sh:
env_dict["setup_sh"] = self._setup_sh

if self._pip_packages:
if self._requirements_txt_file:
env_dict["requirements_txt"] = self.requirements_txt_content()
elif self._pip_packages:
# pip_packages are ignored when the requirements_txt_file parameter is
# specified by the user
env_dict["pip_packages"] = [
str(pkg_req) for pkg_req in self._pip_packages.values()
]

if self._requirements_txt_file:
env_dict["requirements_txt"] = self.requirements_txt_content().read_text()

env_dict["conda_env"] = self._conda_env._conda_env
env_dict["python_version"] = self._python_version
env_dict["docker_base_image"] = self._docker_base_image
Expand Down

0 comments on commit 03c7533

Please sign in to comment.