Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
feat: support directories
Browse files Browse the repository at this point in the history
  • Loading branch information
deepankarm committed Jul 17, 2023
1 parent 84f855e commit 3e841f1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
69 changes: 69 additions & 0 deletions examples/directory/README.md
Original file line number Diff line number Diff line change
@@ -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
```

6 changes: 6 additions & 0 deletions examples/directory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try:
from .app1 import one
from .app2 import two
except ImportError:
from app1 import one
from app2 import two
6 changes: 6 additions & 0 deletions examples/directory/app1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lcserve import serving


@serving
def one() -> int:
return 1
6 changes: 6 additions & 0 deletions examples/directory/app2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lcserve import serving


@serving
def two() -> int:
return 2
13 changes: 13 additions & 0 deletions lcserve/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e841f1

Please sign in to comment.