def plot(self, output: Optional[str] = None, inline_display: bool = False) -> None: """ Visualize the Document recursively. :param output: a filename specifying the name of the image to be created, the suffix svg/jpg determines the file type of the output image :param inline_display: show image directly inside the Jupyter Notebook """ image_type = 'svg' if output and output.endswith('jpg'): image_type = 'jpg' url = self._mermaid_to_url(image_type) showed = False if inline_display: try: from IPython.display import display, Image display(Image(url=url)) showed = True except: # no need to panic users pass if output: download_mermaid_url(url, output) elif not showed: from jina.logging.predefined import default_logger default_logger.info(f'Document visualization: {url}')
def hello_world(args): """ Execute the chatbot example. :param args: arguments passed from CLI """ Path(args.workdir).mkdir(parents=True, exist_ok=True) with ImportExtensions( required=True, help_text='this demo requires Pytorch and Transformers to be installed, ' 'if you haven\'t, please do `pip install jina[torch,transformers]`', ): import transformers, torch assert [torch, transformers] #: prevent pycharm auto remove the above line targets = { 'covid-csv': { 'url': args.index_data_url, 'filename': os.path.join(args.workdir, 'dataset.csv'), } } # download the data download_data(targets, args.download_proxy, task_name='download csv data') # now comes the real work # load index flow from a YAML file f = _get_flow(args) # index it! with f: f.index( DocumentArray.from_csv( targets['covid-csv']['filename'], field_resolver={'question': 'text'} ), show_progress=True, ) url_html_path = 'file://' + os.path.abspath( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'static/index.html' ) ) try: webbrowser.open(url_html_path, new=2) except: pass # intentional pass, browser support isn't cross-platform finally: default_logger.info( f'You should see a demo page opened in your browser, ' f'if not, you may open {url_html_path} manually' ) if not args.unblock_query_flow: f.block()
def arg_wrapper(*args, **kwargs): start_t = time.perf_counter() start_mem = used_memory(unit=1) r = func(*args, **kwargs) elapsed = time.perf_counter() - start_t end_mem = used_memory(unit=1) # level_prefix = ''.join('-' for v in inspect.stack() if v and v.index is not None and v.index >= 0) level_prefix = '' mem_status = f'memory Δ {get_readable_size(end_mem - start_mem)} {get_readable_size(start_mem)} -> {get_readable_size(end_mem)}' default_logger.info( f'{level_prefix} {func.__qualname__} time: {elapsed}s {mem_status}' ) return r
def fork_hello(args) -> None: """Fork the hello world demos into a new directory :param args: the arg from cli """ from_path = os.path.join(os.path.dirname(__file__), args.project) shutil.copytree(from_path, args.destination) full_path = os.path.abspath(args.destination) default_logger.info(f'{args.project} project is forked to {full_path}') default_logger.info(f''' To run the project: ~$ cd {full_path} ~$ python app.py ''')
def __init__(self, args: 'argparse.Namespace'): """ Create a new :class:`NetworkChecker`. :param args: args provided by the CLI. """ import time from jina.logging.profile import TimeContext from jina.serve.runtimes.worker import WorkerRuntime ctrl_addr = f'{args.host}:{args.port}' try: total_time = 0 total_success = 0 for j in range(args.retries): with TimeContext(f'ping {ctrl_addr} at {j} round', default_logger) as tc: r = WorkerRuntime.is_ready(ctrl_addr) if not r: default_logger.warning( 'not responding, retry (%d/%d) in 1s' % (j + 1, args.retries)) else: total_success += 1 total_time += tc.duration time.sleep(1) if total_success < args.retries: default_logger.warning('message lost %.0f%% (%d/%d) ' % ( (1 - total_success / args.retries) * 100, args.retries - total_success, args.retries, )) if total_success > 0: default_logger.info('avg. latency: %.0f ms' % (total_time / total_success * 1000)) exit(0) except KeyboardInterrupt: pass # returns 1 (anomaly) when it comes to here exit(1)
def write_html(html_path): """ Method to present results in browser. :param html_path: path of the written html """ with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'demo.html')) as fp, open(html_path, 'w') as fw: t = fp.read() t = t.replace('{% RESULT %}', '\n'.join(result_html)) t = t.replace( '{% PRECISION_EVALUATION %}', '{:.2f}%'.format(evaluation_value['Precision'] * 100.0), ) t = t.replace( '{% RECALL_EVALUATION %}', '{:.2f}%'.format(evaluation_value['Recall'] * 100.0), ) t = t.replace('{% TOP_K %}', str(top_k)) fw.write(t) url_html_path = 'file://' + os.path.abspath(html_path) try: webbrowser.open(url_html_path, new=2) except: pass # intentional pass, browser support isn't cross-platform finally: default_logger.info( f'You should see a "demo.html" opened in your browser, ' f'if not you may open {url_html_path} manually') colored_url = colored('https://github.com/jina-ai/jina', color='cyan', attrs='underline') default_logger.info( f'🤩 Intrigued? Play with `jina hello fashion --help` and learn more about Jina at {colored_url}' )
def export_schema(args): """Export to JSON Schemas :param args: args from CLI """ if args.yaml_path: dump_api = api_to_dict() for yp in args.yaml_path: f_name = (yp % __version__) if '%s' in yp else yp with open(f_name, 'w', encoding='utf8') as fp: JAML.dump(dump_api, fp) default_logger.info(f'API is exported to {f_name}') if args.json_path: dump_api = api_to_dict() for jp in args.json_path: f_name = (jp % __version__) if '%s' in jp else jp with open(f_name, 'w', encoding='utf8') as fp: json.dump(dump_api, fp, sort_keys=True) default_logger.info(f'API is exported to {f_name}') if args.schema_path: dump_api = get_full_schema() for jp in args.schema_path: f_name = (jp % __version__) if '%s' in jp else jp with open(f_name, 'w', encoding='utf8') as fp: json.dump(dump_api, fp, sort_keys=True) default_logger.info(f'API is exported to {f_name}')
def _get_run_args(print_args: bool = True): from jina.logging.predefined import default_logger from jina.parsers import get_main_parser from jina.helper import colored from jina import __resources_path__ parser = get_main_parser() if len(sys.argv) > 1: from argparse import _StoreAction, _StoreTrueAction args = parser.parse_args() if print_args: p = parser._actions[-1].choices[sys.argv[1]] default_args = { a.dest: a.default for a in p._actions if isinstance(a, (_StoreAction, _StoreTrueAction)) } with open(os.path.join(__resources_path__, 'jina.logo')) as fp: logo_str = fp.read() param_str = [] for k, v in sorted(vars(args).items()): j = f'{k.replace("_", "-"): >30.30} = {str(v):30.30}' if default_args.get(k, None) == v: param_str.append(' ' + j) else: param_str.append('🔧️ ' + colored(j, 'blue', 'on_yellow')) param_str = '\n'.join(param_str) default_logger.info( f'\n{logo_str}\n▶️ {" ".join(sys.argv)}\n{param_str}\n') return args else: parser.print_help() exit()
def export_api(args: 'Namespace'): """ Export the API :param args: arguments coming from the CLI. """ import json from cli.export import api_to_dict from jina import __version__ from jina.jaml import JAML from jina.logging.predefined import default_logger from jina.schemas import get_full_schema if args.yaml_path: dump_api = api_to_dict() for yp in args.yaml_path: f_name = (yp % __version__) if '%s' in yp else yp with open(f_name, 'w', encoding='utf8') as fp: JAML.dump(dump_api, fp) default_logger.info(f'API is exported to {f_name}') if args.json_path: dump_api = api_to_dict() for jp in args.json_path: f_name = (jp % __version__) if '%s' in jp else jp with open(f_name, 'w', encoding='utf8') as fp: json.dump(dump_api, fp, sort_keys=True) default_logger.info(f'API is exported to {f_name}') if args.schema_path: dump_api = get_full_schema() for jp in args.schema_path: f_name = (jp % __version__) if '%s' in jp else jp with open(f_name, 'w', encoding='utf8') as fp: json.dump(dump_api, fp, sort_keys=True) default_logger.info(f'API is exported to {f_name}')
def hello_world(args): """ Execute the multimodal example. :param args: arguments passed from CLI """ Path(args.workdir).mkdir(parents=True, exist_ok=True) with ImportExtensions( required=True, help_text= 'this demo requires Pytorch and Transformers to be installed, ' 'if you haven\'t, please do `pip install jina[torch,transformers]`', ): import transformers, torch, torchvision assert [ torch, transformers, torchvision, ] #: prevent pycharm auto remove the above line # args.workdir = '0bae16ce-5bb2-43be-bcd4-6f1969e8068f' targets = { 'people-img': { 'url': args.index_data_url, 'filename': os.path.join(args.workdir, 'dataset.zip'), } } # download the data if not os.path.exists(targets['people-img']['filename']): download_data(targets, args.download_proxy, task_name='download zip data') with zipfile.ZipFile(targets['people-img']['filename'], 'r') as fp: fp.extractall(args.workdir) # this envs are referred in index and query flow YAMLs os.environ['HW_WORKDIR'] = args.workdir os.environ['PY_MODULE'] = os.path.abspath( os.path.join(cur_dir, 'my_executors.py')) # now comes the real work # load index flow from a YAML file # index it! f = Flow.load_config('flow-index.yml') with f, open(f'{args.workdir}/people-img/meta.csv', newline='') as fp: f.index(inputs=DocumentArray.from_csv(fp), request_size=10, show_progress=True) # search it! f = Flow.load_config('flow-search.yml') # switch to HTTP gateway f.protocol = 'http' f.port_expose = args.port_expose url_html_path = 'file://' + os.path.abspath( os.path.join(cur_dir, 'static/index.html')) with f: try: webbrowser.open(url_html_path, new=2) except: pass # intentional pass, browser support isn't cross-platform finally: default_logger.info( f'You should see a demo page opened in your browser, ' f'if not, you may open {url_html_path} manually') if not args.unblock_query_flow: f.block()