Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

fix: Copy models in bento before build #214

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions bentoctl/deployment_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
import os
import typing as t
from contextlib import contextmanager
from pathlib import Path
import shutil

import bentoml
import cerberus
Expand All @@ -12,7 +12,6 @@
import yaml
from bentoml import Bento
from bentoml.exceptions import NotFound
from bentoml.models import get as get_model

from bentoctl.exceptions import (
BentoNotFound,
Expand Down Expand Up @@ -244,17 +243,26 @@ def generate(self, destination_dir=os.curdir, values_only=False):

return generated_files

@contextmanager
def _prepare_bento_dir(self) -> t.Generator[str, None, None]:
def _prepare_bento_dir(self) -> None:
"""
Copy models in the bento before deployment.
"""
assert self.bento is not None
with fs.open_fs("temp://") as temp_fs, fs.open_fs(self.bento.path) as bento_fs:
fs.mirror.mirror(bento_fs, temp_fs)
models_fs = temp_fs.makedirs("models", recreate=True)
for model_info in self.bento.info.models:
model = get_model(model_info.tag)
model_fs = models_fs.makedirs(model_info.tag.path())
fs.mirror.mirror(model.path, model_fs)
yield temp_fs.getsyspath("/")
bento_models_directory_path = os.path.join(self.bento.path, "models")

for model_info in self.bento.info.models:
new_model_directory_path = os.path.join(
bento_models_directory_path, model_info.tag.path()
)
os.makedirs(new_model_directory_path, exist_ok=True)

path_to_model_in_store = os.path.expanduser(
os.path.join("~/bentoml/models/", model_info.tag.path())
)
# Update model if it already exists
if os.path.exists(new_model_directory_path):
shutil.rmtree(new_model_directory_path)
shutil.copytree(path_to_model_in_store, new_model_directory_path)

def create_deployable(self, destination_dir=os.curdir) -> str:
"""
Expand All @@ -264,13 +272,14 @@ def create_deployable(self, destination_dir=os.curdir) -> str:
# NOTE: In the case of debug mode, we want to keep the deployable
# for debugging purpose. So by setting overwrite_deployable to false,
# we don't delete the deployable after the build.
with self._prepare_bento_dir() as bento_path:
return self.operator.create_deployable(
bento_path=bento_path,
destination_dir=destination_dir,
bento_metadata=get_bento_metadata(bento_path),
overwrite_deployable=not is_debug_mode(),
)
self._prepare_bento_dir()

return self.operator.create_deployable(
bento_path=self.bento.path,
destination_dir=destination_dir,
bento_metadata=get_bento_metadata(self.bento.path),
overwrite_deployable=not is_debug_mode(),
)

def create_repository(self):
(
Expand Down
Loading