Skip to content

Commit

Permalink
refactor(cli): add hello subparser (#1985)
Browse files Browse the repository at this point in the history
* refactor(cli): add hello subparser
  • Loading branch information
hanxiao authored Feb 19, 2021
1 parent b2c2a97 commit 6bf1d20
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ Just starting out? Try Jina's "Hello, World" - a simple image neural search demo


```bash
jina hello-world # more options in --help
jina hello mnist # more options in --help
```

...or even easier for Docker users, **no install required**:

```bash
docker run -v "$(pwd)/j:/j" jinaai/jina hello-world --workdir /j && open j/hello-world.html
docker run -v "$(pwd)/j:/j" jinaai/jina hello mnist --workdir /j && open j/hello-world.html
# replace "open" with "xdg-open" on Linux
```

Expand All @@ -88,7 +88,7 @@ This downloads the Fashion-MNIST training and test dataset and tells Jina to ind
For NLP engineers, we provide a simple chatbot demo for answering Covid-19 questions. You will need PyTorch and Transformers, which can be installed along with Jina:
```bash
pip install "jina[torch,transformers]"
jina hello-world-chatbot
jina hello chatbot
```

This downloads [CovidQA dataset](https://www.kaggle.com/xhlulu/covidqa) and tells Jina to index 418 question-answer pairs with DistilBERT. The index process takes about 1 minute on CPU. Then it opens a webpage where you can input questions and ask Jina.
Expand Down Expand Up @@ -774,7 +774,7 @@ with f:
```


That is the essence behind `jina hello-world`. It is merely a taste of what Jina can do. We’re really excited to see what you do with Jina! You can easily create a Jina project from templates with one terminal command:
That is the essence behind `jina hello mnist`. It is merely a taste of what Jina can do. We’re really excited to see what you do with Jina! You can easily create a Jina project from templates with one terminal command:

```bash
pip install jina[hub] && jina hub new --type app
Expand Down
10 changes: 8 additions & 2 deletions cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ def hello_world(args: 'Namespace'):
hello_world(args)


def hello_world_chatbot(args: 'Namespace'):
from jina.helloworld.chatbot import hello_world
def hello(args: 'Namespace'):
if args.hello == 'mnist':
from jina.helloworld import hello_world
elif args.hello == 'chatbot':
from jina.helloworld.chatbot import hello_world
else:
raise ValueError(f'{args.hello} must be one of [`mnist`, `chatbot`]')

hello_world(args)


Expand Down
26 changes: 15 additions & 11 deletions cli/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ def _gaa(key, parser):
_update_autocomplete()

ac_table = {
'commands': ['--help', '--version', '--version-full', 'hello-world', 'pod', 'flow', 'optimizer', 'gateway', 'ping',
'check', 'hub', 'pea', 'log', 'client', 'export-api', 'hello-world-chatbot'], 'completions': {
'hello-world': ['--help', '--workdir', '--download-proxy', '--shards', '--parallel', '--uses-index',
'commands': ['--help', '--version', '--version-full', 'hello', 'pod', 'flow', 'optimizer', 'gateway', 'ping',
'check', 'hub', 'pea', 'log', 'client', 'export-api', 'hello-world'], 'completions': {
'hello mnist': ['--help', '--workdir', '--download-proxy', '--shards', '--parallel', '--uses-index',
'--index-data-url', '--index-labels-url', '--index-request-size', '--uses-query',
'--query-data-url', '--query-labels-url', '--query-request-size', '--num-query', '--top-k'],
'hello chatbot': ['--help', '--workdir', '--download-proxy', '--uses', '--index-data-url', '--demo-url',
'--port-expose', '--parallel', '--unblock-query-flow'],
'hello': ['--help', 'mnist', 'chatbot'],
'pod': ['--help', '--name', '--log-config', '--identity', '--hide-exc-info', '--port-ctrl', '--ctrl-with-ipc',
'--timeout-ctrl', '--ssh-server', '--ssh-keyfile', '--ssh-password', '--uses', '--py-modules',
'--port-in', '--port-out', '--host-in', '--host-out', '--socket-in', '--socket-out', '--dump-interval',
'--read-only', '--memory-hwm', '--on-error-strategy', '--num-part', '--uses-internal', '--entrypoint',
'--docker-kwargs', '--pull-latest', '--volumes', '--host', '--port-expose', '--silent-remote-logs',
'--upload-files', '--workspace-id', '--daemon', '--runtime-backend', '--runtime', '--runtime-cls',
'--timeout-ready', '--env', '--expose-public', '--pea-id', '--pea-role', '--uses-before',
'--uses-after', '--parallel', '--shards', '--polling', '--scheduling', '--pod-role'],
'flow': ['--help', '--name', '--log-config', '--identity', '--hide-exc-info', '--uses', '--inspect',
'--optimize-level'],
'--timeout-ready', '--env', '--expose-public', '--pea-id', '--pea-role', '--noblock-on-start',
'--uses-before', '--uses-after', '--parallel', '--shards', '--polling', '--scheduling', '--pod-role'],
'flow': ['--help', '--name', '--log-config', '--identity', '--hide-exc-info', '--uses', '--inspect'],
'optimizer': ['--help', '--name', '--log-config', '--identity', '--hide-exc-info', '--uses', '--output-dir'],
'gateway': ['--help', '--name', '--log-config', '--identity', '--hide-exc-info', '--port-ctrl',
'--ctrl-with-ipc', '--timeout-ctrl', '--ssh-server', '--ssh-keyfile', '--ssh-password', '--uses',
Expand All @@ -55,7 +57,8 @@ def _gaa(key, parser):
'--max-message-size', '--proxy', '--prefetch', '--prefetch-on-recv', '--restful', '--rest-api',
'--compress', '--compress-min-bytes', '--compress-min-ratio', '--host', '--port-expose', '--daemon',
'--runtime-backend', '--runtime', '--runtime-cls', '--timeout-ready', '--env', '--expose-public',
'--pea-id', '--pea-role'], 'ping': ['--help', '--timeout', '--retries', '--print-response'],
'--pea-id', '--pea-role', '--noblock-on-start'],
'ping': ['--help', '--timeout', '--retries', '--print-response'],
'check': ['--help', '--summary-exec', '--summary-driver'], 'hub login': ['--help'],
'hub new': ['--help', '--output-dir', '--template', '--type', '--overwrite'],
'hub init': ['--help', '--output-dir', '--template', '--type', '--overwrite'],
Expand All @@ -73,11 +76,12 @@ def _gaa(key, parser):
'--read-only', '--memory-hwm', '--on-error-strategy', '--num-part', '--uses-internal', '--entrypoint',
'--docker-kwargs', '--pull-latest', '--volumes', '--host', '--port-expose', '--silent-remote-logs',
'--upload-files', '--workspace-id', '--daemon', '--runtime-backend', '--runtime', '--runtime-cls',
'--timeout-ready', '--env', '--expose-public', '--pea-id', '--pea-role'],
'--timeout-ready', '--env', '--expose-public', '--pea-id', '--pea-role', '--noblock-on-start'],
'log': ['--help', '--groupby-regex', '--refresh-time'],
'client': ['--help', '--request-size', '--mode', '--top-k', '--mime-type', '--continue-on-error',
'--return-results', '--max-message-size', '--proxy', '--prefetch', '--prefetch-on-recv', '--restful',
'--rest-api', '--compress', '--compress-min-bytes', '--compress-min-ratio', '--host',
'--port-expose'], 'export-api': ['--help', '--yaml-path', '--json-path'],
'hello-world-chatbot': ['--help', '--workdir', '--download-proxy', '--uses', '--index-data-url', '--demo-url',
'--port-expose', '--parallel', '--unblock-query-flow']}}
'hello-world': ['--help', '--workdir', '--download-proxy', '--shards', '--parallel', '--uses-index',
'--index-data-url', '--index-labels-url', '--index-request-size', '--uses-query',
'--query-data-url', '--query-labels-url', '--query-request-size', '--num-query', '--top-k']}}
2 changes: 1 addition & 1 deletion jina/helloworld/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def write_html(html_path):

colored_url = colored('https://opensource.jina.ai', color='cyan', attrs='underline')
default_logger.success(
f'🤩 Intrigued? Play with "jina hello-world --help" and learn more about Jina at {colored_url}')
f'🤩 Intrigued? Play with "jina hello mnist --help" and learn more about Jina at {colored_url}')


def download_data(targets, download_proxy=None, task_name='download fashion-mnist'):
Expand Down
25 changes: 12 additions & 13 deletions jina/parsers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
__copyright__ = "Copyright (c) 2020 Jina AI Limited. All rights reserved."
__license__ = "Apache-2.0"

from jina.parsers.peapods.runtimes.distributed import mixin_distributed_feature_parser


def set_pea_parser(parser=None):
"""Set the parser for the Pea
Expand All @@ -20,6 +18,7 @@ def set_pea_parser(parser=None):
from .peapods.runtimes.container import mixin_container_runtime_parser
from .peapods.runtimes.remote import mixin_remote_parser
from .peapods.pea import mixin_pea_parser
from .peapods.runtimes.distributed import mixin_distributed_feature_parser

mixin_base_ppr_parser(parser)
mixin_zmq_runtime_parser(parser)
Expand Down Expand Up @@ -114,7 +113,7 @@ def get_main_parser():
:return: the parser
"""
from .base import set_base_parser
from .helloworld import set_hw_parser, set_hw_chatbot_parser
from .helloworld import set_hw_parser, set_hello_parser
from .helper import _chf, _SHOW_ALL_ARGS
from .check import set_check_parser
from .export_api import set_export_api_parser
Expand All @@ -131,11 +130,10 @@ def get_main_parser():
description='use `%(prog)-8s [sub-command] --help` '
'to get detailed information about each sub-command', required=True)

set_hw_parser(sp.add_parser('hello-world',
help='👋 Hello World! Hello Jina!',
description='Start the hello-world demo, a simple end2end image index and search demo '
'without any extra dependencies.',
formatter_class=_chf))
set_hello_parser(sp.add_parser('hello',
help='👋 Hello World! Hello Jina!',
description='Start hello-world demos',
formatter_class=_chf))

set_pod_parser(sp.add_parser('pod',
help='Start a Pod',
Expand Down Expand Up @@ -194,9 +192,10 @@ def get_main_parser():
formatter_class=_chf,
**(dict(help='export Jina API to file')) if _SHOW_ALL_ARGS else {}))

set_hw_chatbot_parser(sp.add_parser('hello-world-chatbot',
**(dict(help='Covid-19 chatbot based on DistilBERT')) if _SHOW_ALL_ARGS else {},
description='Start a hello-world demo: a simple Covid-19 chatbot. '
'Pytorch and transformers are required to run this demo',
formatter_class=_chf))
set_hw_parser(sp.add_parser('hello-world',
description='Start the hello-world demo, a simple end2end image index and search demo '
'without any extra dependencies.',
formatter_class=_chf,
**(dict(help='👋 Hello World! Hello Jina!')) if _SHOW_ALL_ARGS else {}))

return parser
26 changes: 25 additions & 1 deletion jina/parsers/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pkg_resources import resource_filename

from .base import set_base_parser
from .helper import add_arg_group, _SHOW_ALL_ARGS
from .helper import add_arg_group, _SHOW_ALL_ARGS, _chf
from ..helper import random_identity


Expand All @@ -21,6 +21,30 @@ def mixin_hw_base_parser(parser):
help='The proxy when downloading sample data')


def set_hello_parser(parser=None):
"""Set the hello parser"""

if not parser:
parser = set_base_parser()

spp = parser.add_subparsers(dest='hello',
description='use `%(prog)-8s [sub-command] --help` '
'to get detailed information about each sub-command', required=True)

set_hw_parser(
spp.add_parser('mnist',
help='Start a simple end2end fashion images index & search demo without any extra dependencies.',
description='Run a fashion search demo',
formatter_class=_chf))

set_hw_chatbot_parser(
spp.add_parser('chatbot',
help='Start a simple Covid-19 chatbot. Pytorch and transformers are '
'required to run this demo',
description='Run a chatbot QA demo',
formatter_class=_chf))


def set_hw_parser(parser=None):
"""Set the hello world parser
Expand Down

0 comments on commit 6bf1d20

Please sign in to comment.