def run_with_conda_env(bundle_path, command): config = load_saved_bundle_config(bundle_path) metadata = config['metadata'] env_name = metadata['service_name'] + '_' + metadata['service_version'] yaml = YAML() yaml.default_flow_style = False tmpf = tempfile.NamedTemporaryFile(delete=False) env_path = tmpf.name + '.yaml' yaml.dump(config['env']['conda_env'], Path(env_path)) pip_req = os.path.join(bundle_path, 'requirements.txt') subprocess.call( 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda ' 'parameter requires conda but it\'s not installed."; exit 1; }} && ' 'conda env update -n {env_name} -f {env_file} && ' 'conda init bash && ' 'eval "$(conda shell.bash hook)" && ' 'conda activate {env_name} && ' '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip ' 'dependencies."; }} && {cmd}'.format( env_name=env_name, env_file=env_path, pip_req=pip_req, cmd=command, ), shell=True, ) return
def serve(port, bundle_path=bundle_path, with_conda=False): if with_conda: config = load_saved_bundle_config(bundle_path) metadata = config['metadata'] env_name = metadata['service_name'] + '_' + metadata[ 'service_version'] pip_req = os.path.join(bundle_path, 'requirements.txt') subprocess.call( 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda ' 'parameter requires conda but it\'s not installed."; exit 1; }} && ' 'conda env update -n {env_name} -f {env_file} && ' 'conda init bash && ' 'eval "$(conda shell.bash hook)" && ' 'conda activate {env_name} && ' '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip ' 'dependencies."; }} &&' 'bentoml serve {bundle_path} --port {port}'.format( env_name=env_name, env_file=os.path.join(bundle_path, 'environment.yml'), bundle_path=bundle_path, port=port, pip_req=pip_req, ), shell=True, ) return track_cli('serve') bento_service = load(bundle_path) server = BentoAPIServer(bento_service, port=port) server.start()
def serve_gunicorn(port, workers, timeout, bundle_path=bundle_path, with_conda=False): if with_conda: config = load_saved_bundle_config(bundle_path) metadata = config['metadata'] env_name = metadata['service_name'] + '_' + metadata[ 'service_version'] pip_req = os.path.join(bundle_path, 'requirements.txt') subprocess.call( 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda ' 'parameter requires conda but it\'s not installed."; exit 1; }} && ' 'conda env update -n {env_name} -f {env_file} && ' 'conda init bash && ' 'eval "$(conda shell.bash hook)" && ' 'conda activate {env_name} && ' '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip ' 'dependencies."; }} &&' 'bentoml serve_gunicorn {bundle_path} -p {port} -w {workers} ' '--timeout {timeout}'.format( env_name=env_name, env_file=os.path.join(bundle_path, 'environment.yml'), bundle_path=bundle_path, port=port, workers=workers, timeout=timeout, pip_req=pip_req, ), shell=True, ) return track_cli('serve_gunicorn') from bentoml.server.gunicorn_server import GunicornBentoServer gunicorn_app = GunicornBentoServer(bundle_path, port, workers, timeout) gunicorn_app.run()
def run(ctx, api_name, bundle_path=bundle_path, with_conda=False): if with_conda: config = load_saved_bundle_config(bundle_path) metadata = config['metadata'] env_name = metadata['service_name'] + '_' + metadata[ 'service_version'] yaml = YAML() yaml.default_flow_style = False tmpf = tempfile.NamedTemporaryFile(delete=False) env_path = tmpf.name yaml.dump(config['env']['conda_env'], Path(env_path)) pip_req = os.path.join(bundle_path, 'requirements.txt') subprocess.call( 'command -v conda >/dev/null 2>&1 || {{ echo >&2 "--with-conda ' 'parameter requires conda but it\'s not installed."; exit 1; }} && ' 'conda env update -n {env_name} -f {env_file} && ' 'conda init bash && ' 'eval "$(conda shell.bash hook)" && ' 'conda activate {env_name} && ' '{{ [ -f {pip_req} ] && pip install -r {pip_req} || echo "no pip ' 'dependencies."; }} &&' 'bentoml {api_name} {bundle_path} {args}'.format( env_name=env_name, env_file=env_path, bundle_path=bundle_path, api_name=api_name, args=' '.join(map(escape_shell_params, ctx.args)), pip_req=pip_req, ), shell=True, ) return track_cli('run') api = load_bento_service_api(bundle_path, api_name) api.handle_cli(ctx.args)