Exemplo n.º 1
0
    def test_evolving(self, model, conf_file, model_dir, mode):
        if 'E' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode and 'TI' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                deep_download(['-test', '-c', config_path])
            shutil.rmtree(str(model_path), ignore_errors=True)

            logfile = io.BytesIO(b'')
            _, exitstatus = pexpect.run(sys.executable +
                                        " -m deeppavlov.evolve " + str(c) +
                                        " --iterations 1 --p_size 1",
                                        timeout=None,
                                        withexitstatus=True,
                                        logfile=logfile)
            if exitstatus != 0:
                logfile.seek(0)
                raise RuntimeError(
                    'Training process of {} returned non-zero exit code: \n{}'.
                    format(
                        model_dir, ''.join(
                            (line.decode() for line in logfile.readlines()))))

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 2
0
    def test_param_search(self, model, conf_file, model_dir, mode):
        if 'PS' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode and 'TI' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                deep_download(['-c', config_path])
            shutil.rmtree(str(model_path), ignore_errors=True)

            logfile = io.BytesIO(b'')
            p = pexpect.popen_spawn.PopenSpawn(
                sys.executable + f" -m deeppavlov.paramsearch {c} --folds 2",
                timeout=None,
                logfile=logfile)
            p.readlines()
            if p.wait() != 0:
                raise RuntimeError(
                    'Training process of {} returned non-zero exit code: \n{}'.
                    format(model_dir,
                           logfile.getvalue().decode()))

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 3
0
def test_param_search():
    model_dir = 'faq'
    conf_file = 'paramsearch/tfidf_logreg_autofaq_psearch.json'

    download_config(conf_file)

    c = test_configs_path / conf_file
    model_path = download_path / model_dir

    install_config(c)
    deep_download(c)

    shutil.rmtree(str(model_path), ignore_errors=True)

    logfile = io.BytesIO(b'')
    p = pexpect.popen_spawn.PopenSpawn(
        sys.executable + f" -m deeppavlov.paramsearch {c} --folds 2",
        timeout=None,
        logfile=logfile)
    p.readlines()
    if p.wait() != 0:
        raise RuntimeError(
            'Training process of {} returned non-zero exit code: \n{}'.format(
                model_dir,
                logfile.getvalue().decode()))

    shutil.rmtree(str(download_path), ignore_errors=True)
Exemplo n.º 4
0
    def test_consecutive_training_and_interacting(self, model, conf_file,
                                                  model_dir, mode):
        if 'TI' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                install_config(config_path)
                deep_download(config_path)
            shutil.rmtree(str(model_path), ignore_errors=True)

            logfile = io.BytesIO(b'')
            p = pexpect.popen_spawn.PopenSpawn(
                sys.executable + " -m deeppavlov train " + str(c),
                timeout=None,
                logfile=logfile)
            p.readlines()
            if p.wait() != 0:
                raise RuntimeError(
                    'Training process of {} returned non-zero exit code: \n{}'.
                    format(model_dir,
                           logfile.getvalue().decode()))
            self.interact(c, model_dir)

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 5
0
def main():
    args = parser.parse_args()
    pipeline_config_path = find_config(args.config_path)
    if args.download or args.mode == 'download':
        deep_download(['-c', pipeline_config_path])
    token = args.token or os.getenv('TELEGRAM_TOKEN')

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path,
                                         to_train=False,
                                         to_validate=False)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        if not token:
            log.error(
                'Token required: initiate -t param or TELEGRAM_BOT env var with Telegram bot token'
            )
        else:
            interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'riseapi':
        start_model_server(pipeline_config_path)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size,
                          args.file_path)
Exemplo n.º 6
0
def main():
    args = parser.parse_args()

    pipeline_config_path = find_config(args.config_path)
    https = args.https
    ssl_key = args.key
    ssl_cert = args.cert

    if args.download or args.mode == 'download':
        deep_download(pipeline_config_path)

    multi_instance = args.multi_instance
    stateful = args.stateful

    start_epoch_num = args.start_epoch_num

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path, recursive=args.recursive, 
                                         start_epoch_num=start_epoch_num)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path, to_train=False, to_validate=False,
                                         start_epoch_num=start_epoch_num)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        token = args.token
        interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'interactmsbot':
        ms_id = args.ms_id
        ms_secret = args.ms_secret
        run_ms_bf_default_agent(model_config=pipeline_config_path,
                                app_id=ms_id,
                                app_secret=ms_secret,
                                multi_instance=multi_instance,
                                stateful=stateful,
                                port=args.port)
    elif args.mode == 'alexa':
        run_alexa_default_agent(model_config=pipeline_config_path,
                                multi_instance=multi_instance,
                                stateful=stateful,
                                port=args.port,
                                https=https,
                                ssl_key=ssl_key,
                                ssl_cert=ssl_cert)
    elif args.mode == 'riseapi':
        alice = args.api_mode == 'alice'
        if alice:
            start_alice_server(pipeline_config_path, https, ssl_key, ssl_cert, port=args.port)
        else:
            start_model_server(pipeline_config_path, https, ssl_key, ssl_cert, port=args.port)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size, args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
    elif args.mode == 'crossval':
        if args.folds < 2:
            log.error('Minimum number of Folds is 2')
        else:
            n_folds = args.folds
            calc_cv_score(pipeline_config_path, n_folds=n_folds, is_loo=False)
Exemplo n.º 7
0
    def test_consecutive_training_and_interacting(self, model, conf_file,
                                                  model_dir, mode):
        if 'TI' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                deep_download(['-test', '-c', config_path])
            shutil.rmtree(str(model_path), ignore_errors=True)

            logfile = io.BytesIO(b'')
            _, exitstatus = pexpect.run("python3 -m deeppavlov.deep train " +
                                        str(c),
                                        timeout=None,
                                        withexitstatus=True,
                                        logfile=logfile)
            if exitstatus != 0:
                logfile.seek(0)
                raise RuntimeError(
                    'Training process of {} returned non-zero exit code: \n{}'.
                    format(
                        model_dir, ''.join(
                            (line.decode() for line in logfile.readlines()))))
            self.interact(c, model_dir)

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 8
0
def main():
    args = parser.parse_args()
    pipeline_config_path = args.config_path
    if not Path(pipeline_config_path).is_file():
        configs = [c for c in Path(__file__).parent.glob(f'configs/**/{pipeline_config_path}.json')
                   if str(c.with_suffix('')).endswith(pipeline_config_path)]  # a simple way to not allow * and ?
        if configs:
            log.info(f"Interpriting '{pipeline_config_path}' as '{configs[0]}'")
            pipeline_config_path = str(configs[0])

    token = args.token or os.getenv('TELEGRAM_TOKEN')

    if args.download or args.mode == 'download':
        deep_download(['-c', pipeline_config_path])

    if args.mode == 'train':
        train_model_from_config(pipeline_config_path)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        if not token:
            log.error('Token required: initiate -t param or TELEGRAM_BOT env var with Telegram bot token')
        else:
            interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'riseapi':
        start_model_server(pipeline_config_path)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size, args.file_path)
Exemplo n.º 9
0
def test_evolving():
    model_dir = 'evolution'
    conf_file = 'evolution/evolve_intents_snips.json'
    download_config(conf_file)

    c = test_configs_path / conf_file
    model_path = download_path / model_dir

    install_config(c)
    deep_download(c)

    shutil.rmtree(str(model_path), ignore_errors=True)

    logfile = io.BytesIO(b'')
    p = pexpect.popen_spawn.PopenSpawn(
        sys.executable +
        f" -m deeppavlov.evolve {c} --iterations 1 --p_size 1",
        timeout=None,
        logfile=logfile)
    p.readlines()
    if p.wait() != 0:
        raise RuntimeError(
            'Training process of {} returned non-zero exit code: \n{}'.format(
                model_dir,
                logfile.getvalue().decode()))

    shutil.rmtree(str(download_path), ignore_errors=True)
Exemplo n.º 10
0
    def test_interacting_pretrained_model(self, model, conf_file, model_dir, mode):
        if 'IP' in mode:
            config_file_path = str(test_configs_path.joinpath(conf_file))
            deep_download(['-test', '-c', config_file_path])

            self.interact(test_configs_path / conf_file, model_dir, PARAMS[model][(conf_file, model_dir, mode)])
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 11
0
def main():
    args = parser.parse_args()
    pipeline_config_path = find_config(args.config_path)

    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    log.info("use gpu id:" + args.gpu_id)

    if args.download or args.mode == 'download':
        deep_download(pipeline_config_path)

    multi_instance = args.multi_instance
    stateful = args.stateful

    start_epoch_num = args.start_epoch_num

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path,
                                         recursive=args.recursive,
                                         start_epoch_num=start_epoch_num)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path,
                                         to_train=False,
                                         to_validate=False,
                                         start_epoch_num=start_epoch_num)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        token = args.token
        interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'interactmsbot':
        ms_id = args.ms_id
        ms_secret = args.ms_secret
        run_ms_bf_default_agent(model_config=pipeline_config_path,
                                app_id=ms_id,
                                app_secret=ms_secret,
                                multi_instance=multi_instance,
                                stateful=stateful)
    elif args.mode == 'riseapi':
        alice = args.api_mode == 'alice'
        https = args.https
        ssl_key = args.key
        ssl_cert = args.cert
        if alice:
            start_alice_server(pipeline_config_path, https, ssl_key, ssl_cert)
        else:
            start_model_server(pipeline_config_path, https, ssl_key, ssl_cert)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size,
                          args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
    elif args.mode == 'crossval':
        if args.folds < 2:
            log.error('Minimum number of Folds is 2')
        else:
            n_folds = args.folds
            calc_cv_score(pipeline_config_path, n_folds=n_folds, is_loo=False)
Exemplo n.º 12
0
    def test_interacting_pretrained_model(self, model, conf_file, model_dir, mode):
        if 'IP' in mode:
            config_file_path = str(test_configs_path.joinpath(conf_file))
            install_config(config_file_path)
            deep_download(config_file_path)

            self.interact(test_configs_path / conf_file, model_dir, PARAMS[model][(conf_file, model_dir, mode)])
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 13
0
    def test_inferring_pretrained_model(self, model, conf_file, model_dir,
                                        mode):
        if 'IP' in mode:
            config_file_path = str(test_configs_path.joinpath(conf_file))
            install_config(config_file_path)
            deep_download(config_file_path)

            self.infer(test_configs_path / conf_file,
                       PARAMS[model][(conf_file, model_dir, mode)])
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 14
0
def build_model(config: Union[str, Path, dict],
                mode: str = 'infer',
                load_trained: bool = False,
                download: bool = False,
                serialized: Optional[bytes] = None) -> Chainer:
    """Build and return the model described in corresponding configuration file."""
    config = parse_config(config)

    if serialized:
        serialized: list = pickle.loads(serialized)

    if download:
        deep_download(config)

    import_packages(config.get('metadata', {}).get('imports', []))

    model_config = config['chainer']

    model = Chainer(model_config['in'], model_config['out'],
                    model_config.get('in_y'))

    for component_config in model_config['pipe']:
        if load_trained and ('fit_on' in component_config
                             or 'in_y' in component_config):
            try:
                component_config['load_path'] = component_config['save_path']
            except KeyError:
                log.warning(
                    'No "save_path" parameter for the {} component, so "load_path" will not be renewed'
                    .format(
                        component_config.get(
                            'class_name',
                            component_config.get('ref', 'UNKNOWN'))))

        if serialized and 'in' in component_config:
            component_serialized = serialized.pop(0)
        else:
            component_serialized = None

        component = from_params(component_config,
                                mode=mode,
                                serialized=component_serialized)

        if 'id' in component_config:
            model._components_dict[component_config['id']] = component

        if 'in' in component_config:
            c_in = component_config['in']
            c_out = component_config['out']
            in_y = component_config.get('in_y', None)
            main = component_config.get('main', False)
            model.append(component, c_in, c_out, in_y, main)

    return model
Exemplo n.º 15
0
def build_model(config: Union[str, Path, dict], mode: str = 'infer',
                load_trained: bool = False, download: bool = False,
                serialized: Optional[bytes] = None) -> Chainer:
    """Build and return the model described in corresponding configuration file."""
    config = parse_config(config)

    if serialized:
        serialized: list = pickle.loads(serialized)

    if download:
        deep_download(config)

    import_packages(config.get('metadata', {}).get('imports', []))

    model_config = config['chainer']

    model = Chainer(model_config['in'], model_config['out'], model_config.get('in_y'))

    for component_config in model_config['pipe']:
        if load_trained and ('fit_on' in component_config or 'in_y' in component_config):
            try:
                component_config['load_path'] = component_config['save_path']
            except KeyError:
                log.warning('No "save_path" parameter for the {} component, so "load_path" will not be renewed'
                            .format(component_config.get('class_name', component_config.get('ref', 'UNKNOWN'))))

        if serialized and 'in' in component_config:
            component_serialized = serialized.pop(0)
        else:
            component_serialized = None

        component = from_params(component_config, mode=mode, serialized=component_serialized)

        if 'in' in component_config:
            c_in = component_config['in']
            c_out = component_config['out']
            in_y = component_config.get('in_y', None)
            main = component_config.get('main', False)
            model.append(component, c_in, c_out, in_y, main)

    return model
Exemplo n.º 16
0
def test_crossvalidation():
    model_dir = 'faq'
    conf_file = 'cv/cv_tfidf_autofaq.json'

    download_config(conf_file)

    c = test_configs_path / conf_file
    model_path = download_path / model_dir

    install_config(c)
    deep_download(c)
    shutil.rmtree(str(model_path),  ignore_errors=True)

    logfile = io.BytesIO(b'')
    p = pexpect.popen_spawn.PopenSpawn(sys.executable + f" -m deeppavlov crossval {c} --folds 2",
                                       timeout=None, logfile=logfile)
    p.readlines()
    if p.wait() != 0:
        raise RuntimeError('Training process of {} returned non-zero exit code: \n{}'
                           .format(model_dir, logfile.getvalue().decode()))

    shutil.rmtree(str(download_path), ignore_errors=True)
Exemplo n.º 17
0
def test_evolving():
    model_dir = 'evolution'
    conf_file = 'evolution/evolve_intents_snips.json'
    download_config(conf_file)

    c = test_configs_path / conf_file
    model_path = download_path / model_dir

    install_config(c)
    deep_download(c)

    shutil.rmtree(str(model_path), ignore_errors=True)

    logfile = io.BytesIO(b'')
    p = pexpect.popen_spawn.PopenSpawn(sys.executable + f" -m deeppavlov.evolve {c} --iterations 1 --p_size 1",
                                       timeout=None, logfile=logfile)
    p.readlines()
    if p.wait() != 0:
        raise RuntimeError('Training process of {} returned non-zero exit code: \n{}'
                           .format(model_dir, logfile.getvalue().decode()))

    shutil.rmtree(str(download_path), ignore_errors=True)
Exemplo n.º 18
0
    def test_evolving(self, model, conf_file, model_dir, mode):
        if 'E' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode and 'TI' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                deep_download(['-c', config_path])
            shutil.rmtree(str(model_path),  ignore_errors=True)

            logfile = io.BytesIO(b'')
            _, exitstatus = pexpect.run(sys.executable + f" -m deeppavlov.evolve {c} --iterations 1 --p_size 1",
                                        timeout=None, withexitstatus=True,
                                        logfile=logfile)
            if exitstatus != 0:
                logfile.seek(0)
                raise RuntimeError('Training process of {} returned non-zero exit code: \n{}'
                                   .format(model_dir, ''.join((line.decode() for line in logfile.readlines()))))

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 19
0
    def test_consecutive_training_and_interacting(self, model, conf_file, model_dir, mode):
        if 'TI' in mode:
            c = test_configs_path / conf_file
            model_path = download_path / model_dir

            if 'IP' not in mode:
                config_path = str(test_configs_path.joinpath(conf_file))
                install_config(config_path)
                deep_download(config_path)
            shutil.rmtree(str(model_path),  ignore_errors=True)

            logfile = io.BytesIO(b'')
            p = pexpect.popen_spawn.PopenSpawn(sys.executable + " -m deeppavlov train " + str(c), timeout=None,
                                               logfile=logfile)
            p.readlines()
            if p.wait() != 0:
                raise RuntimeError('Training process of {} returned non-zero exit code: \n{}'
                                   .format(model_dir, logfile.getvalue().decode()))
            self.interact(c, model_dir)

            shutil.rmtree(str(download_path), ignore_errors=True)
        else:
            pytest.skip("Unsupported mode: {}".format(mode))
Exemplo n.º 20
0
def main():
    args = parser.parse_args()
    pipeline_config_path = find_config(args.config_path)
    if args.download or args.mode == 'download':
        deep_download(['-c', pipeline_config_path])
    token = args.token or os.getenv('TELEGRAM_TOKEN')

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path, to_train=False, to_validate=False)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        if not token:
            log.error('Token required: initiate -t param or TELEGRAM_BOT env var with Telegram bot token')
        else:
            interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'riseapi':
        start_model_server(pipeline_config_path)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size, args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
Exemplo n.º 21
0
    'name': 'default_vocab',
    'save_path': 'vocabs/token.dict',
    'load_path': 'vocabs/token.dict',
    'fit_on': ['utterance'],
    'level': 'token',
    'tokenizer': {
        'name': 'split_tokenizer'
    },
    'main': True
}
# chainer.pipe: a list of consequently run components
vocab_config['chainer']['pipe'] = [vocab_comp_config]

json.dump(vocab_config, open("gobot/vocab_config.json", 'wt'))
""" Download "dstc2_v2" dataset, need to do only once """
deep_download(['--config', 'gobot/vocab_config.json'])
dstc2_path = deeppavlov.__path__[
    0] + '/../download/dstc2'  # Data was downloaded to dstc2_path
"""
Step 3: Vocabulary Building
"""

train_evaluate_model_from_config("gobot/vocab_config.json")

vocabs_path = deeppavlov.__path__[0] + '/../download/vocabs'
vocab_comp_config['in'] = ['utterance']
vocab_comp_config['out'] = ['utterance_token_indices']

vocab_config['chainer']['pipe'] = [vocab_comp_config]
vocab_config['chainer']['out'] = ['utterance_token_indices']
"""
Exemplo n.º 22
0
	def __init__(self, config_path, download=False):
		if download == True:
			deep_download(config_path)
		self.model = build_model_from_config(config_path)
Exemplo n.º 23
0
def main():
    args = parser.parse_args()
    pipeline_config_path = find_config(args.config_path)

    if args.download or args.mode == 'download':
        deep_download(pipeline_config_path)

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path,
                                         recursive=args.recursive,
                                         start_epoch_num=args.start_epoch_num)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path,
                                         to_train=False,
                                         start_epoch_num=args.start_epoch_num)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'telegram':
        interact_model_by_telegram(model_config=pipeline_config_path,
                                   token=args.token)
    elif args.mode == 'msbot':
        start_ms_bf_server(model_config=pipeline_config_path,
                           app_id=args.ms_id,
                           app_secret=args.ms_secret,
                           port=args.port,
                           https=args.https,
                           ssl_key=args.key,
                           ssl_cert=args.cert)
    elif args.mode == 'alexa':
        start_alexa_server(model_config=pipeline_config_path,
                           port=args.port,
                           https=args.https,
                           ssl_key=args.key,
                           ssl_cert=args.cert)
    elif args.mode == 'alice':
        start_alice_server(model_config=pipeline_config_path,
                           port=args.port,
                           https=args.https,
                           ssl_key=args.key,
                           ssl_cert=args.cert)
    elif args.mode == 'riseapi':
        start_model_server(pipeline_config_path,
                           args.https,
                           args.key,
                           args.cert,
                           port=args.port)
    elif args.mode == 'risesocket':
        start_socket_server(pipeline_config_path,
                            args.socket_type,
                            port=args.port,
                            socket_file=args.socket_file)
    elif args.mode == 'agent-rabbit':
        start_rabbit_service(model_config=pipeline_config_path,
                             service_name=args.service_name,
                             agent_namespace=args.agent_namespace,
                             batch_size=args.batch_size,
                             utterance_lifetime_sec=args.utterance_lifetime,
                             rabbit_host=args.rabbit_host,
                             rabbit_port=args.rabbit_port,
                             rabbit_login=args.rabbit_login,
                             rabbit_password=args.rabbit_password,
                             rabbit_virtualhost=args.rabbit_virtualhost)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size,
                          args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
    elif args.mode == 'crossval':
        if args.folds < 2:
            log.error('Minimum number of Folds is 2')
        else:
            calc_cv_score(pipeline_config_path,
                          n_folds=args.folds,
                          is_loo=False)
Exemplo n.º 24
0
import argparse

from deeppavlov.models.morpho_tagger.common import predict_with_model
from deeppavlov.deep import find_config
from deeppavlov.download import deep_download

parser = argparse.ArgumentParser()
parser.add_argument("config_path", help="path to file with prediction configuration")
parser.add_argument("-d", "--download", action="store_true", help="download model components")

if __name__ == "__main__":
    args = parser.parse_args()
    config_path = find_config(args.config_path)
    if args.download:
        deep_download(['-c', config_path])
    predict_with_model(config_path)
Exemplo n.º 25
0
def train_evaluate_model_from_config(config: [str, Path, dict], iterator=None, *,
                                     to_train=True, to_validate=True, download=False,
                                     start_epoch_num=0, recursive=False) -> Dict[str, Dict[str, float]]:
    """Make training and evaluation of the model described in corresponding configuration file."""
    config = parse_config(config)

    if download:
        deep_download(config)

    if to_train and recursive:
        for subconfig in get_all_elems_from_json(config['chainer'], 'config_path'):
            log.info(f'Training "{subconfig}"')
            train_evaluate_model_from_config(subconfig, download=False, recursive=True)

    import_packages(config.get('metadata', {}).get('imports', []))

    if iterator is None:
        try:
            data = read_data_by_config(config)
        except ConfigError as e:
            to_train = False
            log.warning(f'Skipping training. {e.message}')
        else:
            iterator = get_iterator_from_config(config, data)

    train_config = {
        'metrics': ['accuracy'],
        'validate_best': to_validate,
        'test_best': True,
        'show_examples': False
    }

    try:
        train_config.update(config['train'])
    except KeyError:
        log.warning('Train config is missing. Populating with default values')

    in_y = config['chainer'].get('in_y', ['y'])
    if isinstance(in_y, str):
        in_y = [in_y]
    if isinstance(config['chainer']['out'], str):
        config['chainer']['out'] = [config['chainer']['out']]
    metrics_functions = _parse_metrics(train_config['metrics'], in_y, config['chainer']['out'])

    if to_train:
        model = fit_chainer(config, iterator)

        if callable(getattr(model, 'train_on_batch', None)):
            _train_batches(model, iterator, train_config, metrics_functions, start_epoch_num=start_epoch_num)

        model.destroy()

    res = {}

    if iterator is not None and (train_config['validate_best'] or train_config['test_best']):
        model = build_model(config, load_trained=to_train)
        log.info('Testing the best saved model')

        if train_config['validate_best']:
            report = {
                'valid': _test_model(model, metrics_functions, iterator,
                                     train_config.get('batch_size', -1), 'valid',
                                     show_examples=train_config['show_examples'])
            }

            res['valid'] = report['valid']['metrics']

            print(json.dumps(report, ensure_ascii=False))

        if train_config['test_best']:
            report = {
                'test': _test_model(model, metrics_functions, iterator,
                                    train_config.get('batch_size', -1), 'test',
                                    show_examples=train_config['show_examples'])
            }

            res['test'] = report['test']['metrics']

            print(json.dumps(report, ensure_ascii=False))

        model.destroy()

    return res
Exemplo n.º 26
0
def main():
    args = parser.parse_args()

    pipeline_config_path = find_config(args.config_path)
    https = args.https
    ssl_key = args.key
    ssl_cert = args.cert

    if args.download or args.mode == 'download':
        deep_download(pipeline_config_path)

    multi_instance = args.multi_instance
    stateful = args.stateful

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path,
                                         recursive=args.recursive,
                                         start_epoch_num=args.start_epoch_num)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path,
                                         to_train=False,
                                         start_epoch_num=args.start_epoch_num)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        token = args.token
        interact_model_by_telegram(
            model_config=pipeline_config_path,
            token=token,
            default_skill_wrap=not args.no_default_skill)
    elif args.mode == 'interactmsbot':
        ms_id = args.ms_id
        ms_secret = args.ms_secret
        run_ms_bf_default_agent(model_config=pipeline_config_path,
                                app_id=ms_id,
                                app_secret=ms_secret,
                                multi_instance=multi_instance,
                                stateful=stateful,
                                port=args.port,
                                https=https,
                                ssl_key=ssl_key,
                                ssl_cert=ssl_cert,
                                default_skill_wrap=not args.no_default_skill)
    elif args.mode == 'alexa':
        run_alexa_default_agent(model_config=pipeline_config_path,
                                multi_instance=multi_instance,
                                stateful=stateful,
                                port=args.port,
                                https=https,
                                ssl_key=ssl_key,
                                ssl_cert=ssl_cert,
                                default_skill_wrap=not args.no_default_skill)
    elif args.mode == 'riseapi':
        alice = args.api_mode == 'alice'
        if alice:
            start_alice_server(pipeline_config_path,
                               https,
                               ssl_key,
                               ssl_cert,
                               port=args.port)
        else:
            start_model_server(pipeline_config_path,
                               https,
                               ssl_key,
                               ssl_cert,
                               port=args.port)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size,
                          args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
    elif args.mode == 'crossval':
        if args.folds < 2:
            log.error('Minimum number of Folds is 2')
        else:
            n_folds = args.folds
            calc_cv_score(pipeline_config_path, n_folds=n_folds, is_loo=False)
Exemplo n.º 27
0
def train_evaluate_model_from_config(
        config: Union[str, Path, dict],
        iterator: Union[DataLearningIterator, DataFittingIterator] = None,
        *,
        to_train: bool = True,
        evaluation_targets: Optional[Iterable[str]] = None,
        to_validate: Optional[bool] = None,
        download: bool = False,
        start_epoch_num: Optional[int] = None,
        recursive: bool = False) -> Dict[str, Dict[str, float]]:
    """Make training and evaluation of the model described in corresponding configuration file."""
    config = parse_config(config)

    if download:
        deep_download(config)

    if to_train and recursive:
        for subconfig in get_all_elems_from_json(config['chainer'],
                                                 'config_path'):
            log.info(f'Training "{subconfig}"')
            train_evaluate_model_from_config(subconfig,
                                             download=False,
                                             recursive=True)

    import_packages(config.get('metadata', {}).get('imports', []))

    if iterator is None:
        try:
            data = read_data_by_config(config)
        except ConfigError as e:
            to_train = False
            log.warning(f'Skipping training. {e.message}')
        else:
            iterator = get_iterator_from_config(config, data)

    if 'train' not in config:
        log.warning('Train config is missing. Populating with default values')
    train_config = config.get('train')

    if start_epoch_num is not None:
        train_config['start_epoch_num'] = start_epoch_num

    if 'evaluation_targets' not in train_config and (
            'validate_best' in train_config or 'test_best' in train_config):
        log.warning(
            '"validate_best" and "test_best" parameters are deprecated.'
            ' Please, use "evaluation_targets" list instead')

        train_config['evaluation_targets'] = []
        if train_config.pop('validate_best', True):
            train_config['evaluation_targets'].append('valid')
        if train_config.pop('test_best', True):
            train_config['evaluation_targets'].append('test')

    trainer_class = get_model(train_config.pop('class_name', 'nn_trainer'))
    trainer = trainer_class(config['chainer'], **train_config)

    if to_train:
        trainer.train(iterator)

    res = {}

    if iterator is not None:
        if to_validate is not None:
            if evaluation_targets is None:
                log.warning(
                    '"to_validate" parameter is deprecated and will be removed in future versions.'
                    ' Please, use "evaluation_targets" list instead')
                evaluation_targets = ['test']
                if to_validate:
                    evaluation_targets.append('valid')
            else:
                log.warn(
                    'Both "evaluation_targets" and "to_validate" parameters are specified.'
                    ' "to_validate" is deprecated and will be ignored')

        res = trainer.evaluate(iterator,
                               evaluation_targets,
                               print_reports=True)
        trainer.get_chainer().destroy()

    res = {k: v['metrics'] for k, v in res.items()}

    return res
Exemplo n.º 28
0
def main():
    args = parser.parse_args()
    pipeline_config_path = find_config(args.config_path)

    if args.download or args.mode == 'download':
        deep_download(['-c', pipeline_config_path])
    token = args.token or os.getenv('TELEGRAM_TOKEN')

    ms_id = args.ms_id or os.getenv('MS_APP_ID')
    ms_secret = args.ms_secret or os.getenv('MS_APP_SECRET')

    multi_instance = args.multi_instance
    stateful = args.stateful

    if args.mode == 'train':
        train_evaluate_model_from_config(pipeline_config_path)
    elif args.mode == 'evaluate':
        train_evaluate_model_from_config(pipeline_config_path,
                                         to_train=False,
                                         to_validate=False)
    elif args.mode == 'interact':
        interact_model(pipeline_config_path)
    elif args.mode == 'interactbot':
        if not token:
            log.error(
                'Token required: initiate -t param or TELEGRAM_BOT env var with Telegram bot token'
            )
        else:
            interact_model_by_telegram(pipeline_config_path, token)
    elif args.mode == 'interactmsbot':
        if not ms_id:
            log.error(
                'Microsoft Bot Framework app id required: initiate -i param '
                'or MS_APP_ID env var with Microsoft app id')
        elif not ms_secret:
            log.error(
                'Microsoft Bot Framework app secret required: initiate -s param '
                'or MS_APP_SECRET env var with Microsoft app secret')
        else:
            run_ms_bf_default_agent(model_config_path=pipeline_config_path,
                                    app_id=ms_id,
                                    app_secret=ms_secret,
                                    multi_instance=multi_instance,
                                    stateful=stateful)
    elif args.mode == 'riseapi':
        alice = args.api_mode == 'alice'
        https = args.https
        ssl_key = args.key
        ssl_cert = args.cert
        start_model_server(pipeline_config_path, alice, https, ssl_key,
                           ssl_cert)
    elif args.mode == 'predict':
        predict_on_stream(pipeline_config_path, args.batch_size,
                          args.file_path)
    elif args.mode == 'install':
        install_from_config(pipeline_config_path)
    elif args.mode == 'crossval':
        if args.folds < 2:
            log.error('Minimum number of Folds is 2')
        else:
            n_folds = args.folds
            calc_cv_score(pipeline_config_path=pipeline_config_path,
                          n_folds=n_folds,
                          is_loo=False)
Exemplo n.º 29
0
import argparse

from deeppavlov.core.common.file import find_config
from deeppavlov.download import deep_download
from deeppavlov.models.morpho_tagger.common import predict_with_model

parser = argparse.ArgumentParser()
parser.add_argument("config_path",
                    help="path to file with prediction configuration")
parser.add_argument("-d",
                    "--download",
                    action="store_true",
                    help="download model components")

if __name__ == "__main__":
    args = parser.parse_args()
    config_path = find_config(args.config_path)
    if args.download:
        deep_download(config_path)
    predict_with_model(config_path)