Python rasa_nlu.config.load() Examples

The following are 30 code examples of rasa_nlu.config.load(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rasa_nlu.config , or try the search function .
Example #1
Source File: bot.py    From rasa_core with Apache License 2.0 7 votes vote down vote up
def train_nlu():
    from rasa_nlu.training_data import load_data
    from rasa_nlu import config
    from rasa_nlu.model import Trainer

    training_data = load_data('data/nlu.md')
    trainer = Trainer(config.load("config.yml"))
    trainer.train(training_data)
    model_directory = trainer.persist('models/nlu/',
                                      fixed_model_name="current")

    return model_directory 
Example #2
Source File: time_train_test.py    From rasa_lookup_demo with Apache License 2.0 6 votes vote down vote up
def print_stats(
    count,
    num_words_list,
    num_words,
    lookup_construct_time,
    td_load_time,
    train_time,
    persist_time,
    eval_time,
    total_time,
):

    print("{0:.2f} % done".format((count + 1) / len(num_words_list) * 100))
    print("with {} words in lookup table:".format(num_words))
    print("    took {} sec. to construct lookup table".format(lookup_construct_time))
    print("    took {} sec. to load training data".format(td_load_time))
    print("    took {} sec. to train model".format(train_time))
    print("    took {} sec. to perist model".format(persist_time))
    print("    took {} sec. to evaluate on test set".format(eval_time))
    print("    took {} sec. total".format(total_time)) 
Example #3
Source File: conftest.py    From rasa_nlu with Apache License 2.0 6 votes vote down vote up
def zipped_nlu_model():
    spacy_config_path = "sample_configs/config_pretrained_embeddings_spacy.yml"

    cfg = config.load(spacy_config_path)
    trainer = Trainer(cfg)
    td = training_data.load_data(DEFAULT_DATA_PATH)

    trainer.train(td)
    trainer.persist("test_models",
                    project_name="test_model_pretrained_embeddings")

    model_dir_list = os.listdir(TEST_MODEL_PATH)

    # directory name of latest model
    model_dir = sorted(model_dir_list)[-1]

    # path of that directory
    model_path = os.path.join(TEST_MODEL_PATH, model_dir)

    zip_path = zip_folder(model_path)

    return zip_path 
Example #4
Source File: time_train_test.py    From rasa_lookup_demo with Apache License 2.0 6 votes vote down vote up
def train_model():
    # trains a model and times it
    t = time()
    # training_data = load_data('demo_train.md')
    training_data = load_data("data/company_train_lookup.json")
    td_load_time = time() - t
    trainer = Trainer(config.load("config.yaml"))
    t = time()
    trainer.train(training_data)
    train_time = time() - t
    clear_model_dir()
    t = time()
    model_directory = trainer.persist(
        "./tmp/models"
    )  # Returns the directory the model is stored in
    persist_time = time() - t
    return td_load_time, train_time, persist_time 
Example #5
Source File: test_multitenancy.py    From rasa_nlu with Apache License 2.0 6 votes vote down vote up
def train_models(component_builder, data):
    # Retrain different multitenancy models
    def train(cfg_name, project_name):
        from rasa_nlu import training_data

        cfg = config.load(cfg_name)
        trainer = Trainer(cfg, component_builder)
        training_data = training_data.load_data(data)

        trainer.train(training_data)
        trainer.persist("test_projects", project_name=project_name)

    train("sample_configs/config_pretrained_embeddings_spacy.yml",
          "test_project_spacy")
    train("sample_configs/config_pretrained_embeddings_mitie.yml",
          "test_project_mitie")
    train("sample_configs/config_pretrained_embeddings_mitie_2.yml",
          "test_project_mitie_2") 
Example #6
Source File: test_multitenancy.py    From Rasa_NLU_Chi with Apache License 2.0 6 votes vote down vote up
def train_models(component_builder, data):
    # Retrain different multitenancy models
    def train(cfg_name, project_name):
        from rasa_nlu.train import create_persistor
        from rasa_nlu import training_data

        cfg = config.load(cfg_name)
        trainer = Trainer(cfg, component_builder)
        training_data = training_data.load_data(data)

        trainer.train(training_data)
        trainer.persist("test_projects", project_name=project_name)

    train("sample_configs/config_spacy.yml", "test_project_spacy_sklearn")
    train("sample_configs/config_mitie.yml", "test_project_mitie")
    train("sample_configs/config_mitie_sklearn.yml", "test_project_mitie_sklearn") 
Example #7
Source File: test_evaluation.py    From Rasa_NLU_Chi with Apache License 2.0 6 votes vote down vote up
def test_run_cv_evaluation():
    td = training_data.load_data('data/examples/rasa/demo-rasa.json')
    nlu_config = config.load("sample_configs/config_spacy.yml")

    n_folds = 2
    results, entity_results = run_cv_evaluation(td, n_folds, nlu_config)

    assert len(results.train["Accuracy"]) == n_folds
    assert len(results.train["Precision"]) == n_folds
    assert len(results.train["F1-score"]) == n_folds
    assert len(results.test["Accuracy"]) == n_folds
    assert len(results.test["Precision"]) == n_folds
    assert len(results.test["F1-score"]) == n_folds
    assert len(entity_results.train['ner_crf']["Accuracy"]) == n_folds
    assert len(entity_results.train['ner_crf']["Precision"]) == n_folds
    assert len(entity_results.train['ner_crf']["F1-score"]) == n_folds
    assert len(entity_results.test['ner_crf']["Accuracy"]) == n_folds
    assert len(entity_results.test['ner_crf']["Precision"]) == n_folds
    assert len(entity_results.test['ner_crf']["F1-score"]) == n_folds 
Example #8
Source File: bot.py    From rasa_bot with Apache License 2.0 5 votes vote down vote up
def run(serve_forever=True):
    agent = Agent.load("models/dialogue",
                       interpreter=RasaNLUInterpreter("models/ivr/demo"))

    if serve_forever:
        agent.handle_channel(ConsoleInputChannel())
    return agent 
Example #9
Source File: conftest.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def default_config():
    return config.load(CONFIG_DEFAULTS_PATH) 
Example #10
Source File: classification.py    From azure-iot-starter-kits with MIT License 5 votes vote down vote up
def __init__(self, training_data_file = "training_data.json",
                 config_file = "training_config.json"):
        training_data = load_data(training_data_file)        
        trainer = Trainer(config.load(config_file))
        self.interpreter = trainer.train(training_data)
        self.confidence_threshold = 0.7

        # Create supported intents
        context = { 'confidence_threshold': self.confidence_threshold }
        self.intents = {
                "greet"     : intent.HelloIntent(self, context),
                "get_time"  : intent.GetTimeIntent(self, context),
                "ask_joke"  : intent.JokeIntent(self, context),
                "unknown"   : intent.UnKnownIntent(self, context)
            } 
Example #11
Source File: server.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def _load_default_config(path):
        if path:
            return config.load(path).as_dict()
        else:
            return {} 
Example #12
Source File: evaluate.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def run_evaluation(data_path, model_path,
                   component_builder=None):  # pragma: no cover
    """Evaluate intent classification and entity extraction."""

    # get the metadata config from the package data
    interpreter = Interpreter.load(model_path, component_builder)
    test_data = training_data.load_data(data_path,
                                        interpreter.model_metadata.language)
    extractors = get_entity_extractors(interpreter)
    entity_predictions, tokens = get_entity_predictions(interpreter,
                                                        test_data)
    if duckling_extractors.intersection(extractors):
        entity_predictions = remove_duckling_entities(entity_predictions)
        extractors = remove_duckling_extractors(extractors)

    if is_intent_classifier_present(interpreter):
        intent_targets = get_intent_targets(test_data)
        intent_predictions = get_intent_predictions(interpreter, test_data)
        logger.info("Intent evaluation results:")
        evaluate_intents(intent_targets, intent_predictions)

    if extractors:
        entity_targets = get_entity_targets(test_data)

        logger.info("Entity evaluation results:")
        evaluate_entities(entity_targets, entity_predictions, tokens,
                          extractors) 
Example #13
Source File: test_featurizers.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def test_mitie_featurizer(mitie_feature_extractor, default_config):
    from rasa_nlu.featurizers.mitie_featurizer import MitieFeaturizer

    ftr = MitieFeaturizer.create(config.load("sample_configs/config_mitie.yml"))
    sentence = "Hey how are you today"
    tokens = MitieTokenizer().tokenize(sentence)
    vecs = ftr.features_for_tokens(tokens, mitie_feature_extractor)
    expected = np.array([0., -4.4551446, 0.26073121, -1.46632245, -1.84205751])
    assert np.allclose(vecs[:5], expected, atol=1e-5) 
Example #14
Source File: test_config.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def test_invalid_config_json():
    file_config = """pipeline: [spacy_sklearn"""  # invalid yaml
    with tempfile.NamedTemporaryFile("w+", suffix="_tmp_config_file.json") as f:
        f.write(file_config)
        f.flush()
        with pytest.raises(rasa_nlu.config.InvalidConfigError):
            config.load(f.name) 
Example #15
Source File: test_config.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def test_invalid_pipeline_template():
    args = {"pipeline": "my_made_up_name"}
    f = write_file_config(args)
    with pytest.raises(InvalidConfigError) as execinfo:
        config.load(f.name)
    assert "unknown pipeline template" in str(execinfo.value) 
Example #16
Source File: test_config.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def test_pipeline_looksup_registry():
    pipeline_template = list(registered_pipeline_templates)[0]
    args = {"pipeline": pipeline_template}
    f = write_file_config(args)
    final_config = config.load(f.name)
    components = [c.get("name") for c in final_config.pipeline]
    assert components == registered_pipeline_templates[pipeline_template] 
Example #17
Source File: test_config.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def test_set_attr_on_component(default_config):
    cfg = config.load("sample_configs/config_spacy.yml")
    cfg.set_component_attr("intent_classifier_sklearn", C=324)

    expected = {"C": 324, "name": "intent_classifier_sklearn"}

    assert cfg.for_component("intent_classifier_sklearn") == expected
    assert cfg.for_component("tokenizer_spacy") == {"name": "tokenizer_spacy"} 
Example #18
Source File: conftest.py    From Rasa_NLU_Chi with Apache License 2.0 5 votes vote down vote up
def default_config():
    return config.load(CONFIG_DEFAULTS_PATH) 
Example #19
Source File: trainer.py    From weather-bot with MIT License 5 votes vote down vote up
def train_nlu():
    training_data = load_data('data/nlu-data.md')
    trainer = Trainer(config.load("nlu-config.yml"))
    trainer.train(training_data)
    model_directory = trainer.persist('models/nlu/', fixed_model_name="current")
    return model_directory 
Example #20
Source File: bot.py    From rasa_bot with Apache License 2.0 5 votes vote down vote up
def train_nlu():
    from rasa_nlu.training_data import load_data
    from rasa_nlu.config import RasaNLUModelConfig
    from rasa_nlu.model import Trainer
    from rasa_nlu import config

    training_data = load_data("data/nlu.json")
    trainer = Trainer(config.load("data/nlu_model_config.json"))
    trainer.train(training_data)
    model_directory = trainer.persist("models/", project_name="ivr", fixed_model_name="demo")

    return model_directory 
Example #21
Source File: train.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def train(nlu_config: Union[Text, RasaNLUModelConfig],
          data: Text,
          path: Optional[Text] = None,
          project: Optional[Text] = None,
          fixed_model_name: Optional[Text] = None,
          storage: Optional[Text] = None,
          component_builder: Optional[ComponentBuilder] = None,
          training_data_endpoint: Optional[EndpointConfig] = None,
          **kwargs: Any
          ) -> Tuple[Trainer, Interpreter, Text]:
    """Loads the trainer and the data and runs the training of the model."""

    if isinstance(nlu_config, str):
        nlu_config = config.load(nlu_config)

    # Ensure we are training a model that we can save in the end
    # WARN: there is still a race condition if a model with the same name is
    # trained in another subprocess
    trainer = Trainer(nlu_config, component_builder)
    persistor = create_persistor(storage)
    if training_data_endpoint is not None:
        training_data = load_data_from_endpoint(training_data_endpoint,
                                                nlu_config.language)
    else:
        training_data = load_data(data, nlu_config.language)
    interpreter = trainer.train(training_data, **kwargs)

    if path:
        persisted_path = trainer.persist(path,
                                         persistor,
                                         project,
                                         fixed_model_name)
    else:
        persisted_path = None

    return trainer, interpreter, persisted_path 
Example #22
Source File: test_config.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_override_defaults_supervised_embeddings_pipeline():
    cfg = config.load("data/test/config_embedding_test.yml")
    builder = ComponentBuilder()

    component1_cfg = cfg.for_component(0)

    component1 = builder.create_component(component1_cfg, cfg)
    assert component1.max_ngram == 3

    component2_cfg = cfg.for_component(1)
    component2 = builder.create_component(component2_cfg, cfg)
    assert component2.epochs == 10 
Example #23
Source File: test_config.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_set_attr_on_component(default_config):
    cfg = config.load("sample_configs/config_pretrained_embeddings_spacy.yml")
    cfg.set_component_attr(6, C=324)

    assert cfg.for_component(1) == {"name": "SpacyTokenizer"}
    assert cfg.for_component(6) == {"name": "SklearnIntentClassifier",
                                    "C": 324} 
Example #24
Source File: test_config.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_invalid_pipeline_template():
    args = {"pipeline": "my_made_up_name"}
    f = write_file_config(args)
    with pytest.raises(config.InvalidConfigError) as execinfo:
        config.load(f.name)
    assert "unknown pipeline template" in str(execinfo.value) 
Example #25
Source File: test_config.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_invalid_config_json():
    file_config = """pipeline: [spacy_sklearn"""  # invalid yaml
    with tempfile.NamedTemporaryFile("w+",
                                     suffix="_tmp_config_file.json") as f:
        f.write(file_config)
        f.flush()
        with pytest.raises(config.InvalidConfigError):
            config.load(f.name) 
Example #26
Source File: test_config.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_blank_config():
    file_config = {}
    f = write_file_config(file_config)
    final_config = config.load(f.name)
    assert final_config.as_dict() == defaults 
Example #27
Source File: test_evaluation.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def test_run_cv_evaluation():
    td = training_data.load_data('data/examples/rasa/demo-rasa.json')
    nlu_config = config.load(
        "sample_configs/config_pretrained_embeddings_spacy.yml")

    n_folds = 2
    results, entity_results = cross_validate(td, n_folds, nlu_config)

    assert len(results.train["Accuracy"]) == n_folds
    assert len(results.train["Precision"]) == n_folds
    assert len(results.train["F1-score"]) == n_folds
    assert len(results.test["Accuracy"]) == n_folds
    assert len(results.test["Precision"]) == n_folds
    assert len(results.test["F1-score"]) == n_folds
    assert len(entity_results.train[
        'CRFEntityExtractor']["Accuracy"]) == n_folds
    assert len(entity_results.train[
        'CRFEntityExtractor']["Precision"]) == n_folds
    assert len(entity_results.train[
        'CRFEntityExtractor']["F1-score"]) == n_folds
    assert len(entity_results.test[
        'CRFEntityExtractor']["Accuracy"]) == n_folds
    assert len(entity_results.test[
        'CRFEntityExtractor']["Precision"]) == n_folds
    assert len(entity_results.test[
        'CRFEntityExtractor']["F1-score"]) == n_folds 
Example #28
Source File: server.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def _load_default_config(path):
        if path:
            return config.load(path).as_dict()
        else:
            return {} 
Example #29
Source File: server.py    From rasa_nlu with Apache License 2.0 5 votes vote down vote up
def _load_default_config(path):
        if path:
            return config.load(path).as_dict()
        else:
            return {} 
Example #30
Source File: test.py    From rasa_nlu with Apache License 2.0 4 votes vote down vote up
def main():
    parser = create_argument_parser()
    cmdline_args = parser.parse_args()
    utils.configure_colored_logging(cmdline_args.loglevel)

    if cmdline_args.mode == "crossvalidation":

        # TODO: move parsing into sub parser
        # manual check argument dependency
        if cmdline_args.model is not None:
            parser.error("Crossvalidation will train a new model "
                         "- do not specify external model.")

        if cmdline_args.config is None:
            parser.error("Crossvalidation will train a new model "
                         "you need to specify a model configuration.")

        nlu_config = config.load(cmdline_args.config)
        data = training_data.load_data(cmdline_args.data)
        data = drop_intents_below_freq(data, cutoff=5)
        results, entity_results = cross_validate(
            data, int(cmdline_args.folds), nlu_config)
        logger.info("CV evaluation (n={})".format(cmdline_args.folds))

        if any(results):
            logger.info("Intent evaluation results")
            return_results(results.train, "train")
            return_results(results.test, "test")
        if any(entity_results):
            logger.info("Entity evaluation results")
            return_entity_results(entity_results.train, "train")
            return_entity_results(entity_results.test, "test")

    elif cmdline_args.mode == "evaluation":
        run_evaluation(cmdline_args.data,
                       cmdline_args.model,
                       cmdline_args.report,
                       cmdline_args.successes,
                       cmdline_args.errors,
                       cmdline_args.confmat,
                       cmdline_args.histogram)

    logger.info("Finished evaluation")