From 3e841f12a5b54184bf1dc2237500a4a8a272ba2c Mon Sep 17 00:00:00 2001 From: Deepankar Mahapatro Date: Mon, 17 Jul 2023 15:50:46 +0530 Subject: [PATCH] feat: support directories --- examples/directory/README.md | 69 ++++++++++++++++++++++++++++++++++ examples/directory/__init__.py | 6 +++ examples/directory/app1.py | 6 +++ examples/directory/app2.py | 6 +++ lcserve/flow.py | 13 +++++++ 5 files changed, 100 insertions(+) create mode 100644 examples/directory/README.md create mode 100644 examples/directory/__init__.py create mode 100644 examples/directory/app1.py create mode 100644 examples/directory/app2.py diff --git a/examples/directory/README.md b/examples/directory/README.md new file mode 100644 index 00000000..884e6de9 --- /dev/null +++ b/examples/directory/README.md @@ -0,0 +1,69 @@ +## Deploy endpoints from different files at a time + +lc-serve also allows you to deploy endpoints from different files at a time. This is useful when you want to deploy endpoints from different files at a time. + +```bash +. +├── app1.py +├── app2.py +├── __init__.py +└── README.md +``` + +```python +# app1.py +from lcserve import serving + +@serving +def one() -> int: + return 1 +``` + + +```python +# app2.py +from lcserve import serving + +@serving +def two() -> int: + return 2 + +``` + +```python +# __init__.py +try: + from .app1 import one + from .app2 import two +except ImportError: + from app1 import one + from app2 import two +``` + +There are 2 ways to deploy this. + +1. Inside the `dirname` where the files are located. + +```bash +lc-serve deploy jcloud . +``` + +2. Outside the `dirname` where the files are located. + +```bash +lc-serve deploy jcloud dirname +``` + + +##### Gotcha + +To support both the ways on JCloud, you'd need to allow both ways of importing from the app files. e.g.- + +```python +# __init__.py +try: + from .app1 import one +except ImportError: + from app1 import one +``` + diff --git a/examples/directory/__init__.py b/examples/directory/__init__.py new file mode 100644 index 00000000..2fd9918f --- /dev/null +++ b/examples/directory/__init__.py @@ -0,0 +1,6 @@ +try: + from .app1 import one + from .app2 import two +except ImportError: + from app1 import one + from app2 import two diff --git a/examples/directory/app1.py b/examples/directory/app1.py new file mode 100644 index 00000000..87806ea5 --- /dev/null +++ b/examples/directory/app1.py @@ -0,0 +1,6 @@ +from lcserve import serving + + +@serving +def one() -> int: + return 1 diff --git a/examples/directory/app2.py b/examples/directory/app2.py new file mode 100644 index 00000000..1a6afddb --- /dev/null +++ b/examples/directory/app2.py @@ -0,0 +1,6 @@ +from lcserve import serving + + +@serving +def two() -> int: + return 2 diff --git a/lcserve/flow.py b/lcserve/flow.py index 76ba0181..0b2a5f8f 100644 --- a/lcserve/flow.py +++ b/lcserve/flow.py @@ -38,6 +38,7 @@ ServingGatewayConfigFile = 'servinggateway_config.yml' APP_LOGS_URL = "[https://cloud.jina.ai/](https://cloud.jina.ai/user/flows?action=detail&id={app_id}&tab=logs)" PRICING_URL = "****{cph}**** ([Read about pricing here](https://github.com/jina-ai/langchain-serve#-pricing))" +INIT_MODULE = '__init__' def syncify(f): @@ -150,6 +151,11 @@ def get_module_dir( _add_to_path(lcserve_app=lcserve_app) if module_str is not None: + # if module_str is ., then it is the current directory. So, we can use __init__ as the module + if module_str == '.': + module_str = INIT_MODULE + # if module_str is a directory, then importing `module_str` will import the __init__.py file in that directory + _module = _load_module_from_str(module_str) _is_websocket = _any_websocket_router_in_module(_module) _module_dir = _get_parent_dir(modname=module_str, filename=_module.__file__) @@ -462,6 +468,13 @@ def get_flow_dict( env: str = None, lcserve_app: bool = False, ) -> Dict: + # if module_str is ., then it is the current directory. So, we can use __init__ as the module + if module_str == '.': + module_str = INIT_MODULE + # if module_str is a directory, we need to change the module_str to __init__, as during upload we only upload the directory. Only for jcloud + elif module_str and os.path.isdir(module_str) and jcloud: + module_str = INIT_MODULE + if jcloud: jcloud_config = get_jcloud_config( config_path=jcloud_config_path, timeout=timeout, is_websocket=is_websocket