Exemple #1
0
def write_html(html_path: str):
    global num_docs_evaluated
    global evaluation_value

    with open(resource_filename('jina', '/'.join(('resources', 'helloworld.html'))), 'r') 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['PrecisionEvaluator'] * 100.0))
        t = t.replace(
            '{% RECALL_EVALUATION %}',
            '{:.2f}%'.format(evaluation_value['RecallEvaluator'] * 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
    finally:
        default_logger.success(
            f'You should see a "hello-world.html" opened in your browser, '
            f'if not you may open {url_html_path} manually')

    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}'
    )
Exemple #2
0
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 = (Flow().add(uses=MyTransformer,
                    parallel=args.parallel).add(uses=MyIndexer,
                                                workspace=args.workdir))

    # index it!
    with f, open(targets['covid-csv']['filename']) as fp:
        f.index(DocumentArray.from_csv(fp, field_resolver={'question':
                                                           'text'}))

        # switch to REST gateway at runtime
        f.use_rest_gateway(args.port_expose)

        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.success(
                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()
Exemple #3
0
def fork_hello(args):
    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.success(f'{args.project} project is forked to {full_path}')
    default_logger.info(
        f'''
    To run the project:
    ~$ cd {full_path}
    ~$ python app.py
    '''
    )
Exemple #4
0
def write_html(html_path):
    with open(resource_filename('jina', '/'.join(('resources', 'helloworld.html'))), 'r') as fp, \
            open(html_path, 'w') as fw:
        t = fp.read()
        t = t.replace('{% RESULT %}', '\n'.join(result_html))
        fw.write(t)

    url_html_path = 'file://' + os.path.abspath(html_path)

    try:
        webbrowser.open(url_html_path, new=2)
    except:
        pass
    finally:
        default_logger.success(f'You should see a "hello-world.html" opened in your browser, '
                               f'if not you may open {url_html_path} manually')

    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}')
Exemple #5
0
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.success(
            f'You should see a "hello-world.html" opened in your browser, '
            f'if not you may open {url_html_path} manually')

    colored_url = colored('https://opensource.jina.ai',
                          color='cyan',
                          attrs='underline')
    default_logger.success(
        f'🤩 Intrigued? Play with "jina hello fashion --help" and learn more about Jina at {colored_url}'
    )
Exemple #6
0
def hello_world(args):
    # this envs are referred in index and query flow YAMLs
    os.environ['RESOURCE_DIR'] = resource_filename('jina', 'resources')
    os.environ['SHARDS'] = str(args.shards)
    os.environ['REPLICAS'] = str(args.replicas)
    os.environ['HW_WORKDIR'] = args.workdir
    os.environ['WITH_LOGSERVER'] = str(args.logserver)

    # reduce the network load by using `fp16`, or even `uint8`
    os.environ['JINA_ARRAY_QUANT'] = 'fp16'

    # now comes the real work
    # load index flow from a YAML file

    f = Flow.load_config(args.index_yaml_path)
    # run it!
    with f:
        default_logger.success(
            f'hello-world server is started at {f.host}:{f.port_expose}, '
            f'you can now use "python client.py --port-expose {f.port_expose} --host {f.host}" to send request!'
        )
        f.block()
Exemple #7
0
 def test_expand_env(self):
     print(expand_env_var('${PATH}-${AA}'))
     default_logger.info('aa')
     default_logger.success('aa')
Exemple #8
0
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
    # 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)

    # search it!
    f = Flow.load_config('flow-search.yml')
    # switch to REST gateway
    f.use_rest_gateway(args.port_expose)

    url_html_path = 'file://' + os.path.abspath(
        os.path.join(os.path.dirname(os.path.realpath(__file__)),
                     '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.success(
                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()