def test_handles_pipeline_with_non_existing_component(component_builder): _config = utilities.base_test_conf("pretrained_embeddings_spacy") _config.pipeline.append({"name": "my_made_up_component"}) with pytest.raises(Exception) as execinfo: train(_config, data=DEFAULT_DATA_PATH, component_builder=component_builder) assert "Failed to find component" in str(execinfo.value)
def test_train_model_empty_pipeline(component_builder): # Should return an empty pipeline _config = utilities.base_test_conf(pipeline_template=None) with pytest.raises(ValueError): train(_config, data=DEFAULT_DATA_PATH, component_builder=component_builder)
def save_nlu_data(data, path, append=True): write_mode = 'w' if append: write_mode = 'a' nlu_text = "" with open(path, write_mode) as nlu_data: for intent, texts in data['intent'].items(): nlu_text += '\n## intent:' + intent + '\n' for text in texts: nlu_text += '- ' + text + '\n' for intent, texts in data['lookup'].items(): nlu_text += '\n## lookup:' + intent + '\n' for text in texts: nlu_text += '- ' + text + '\n' for intent, texts in data['synonym'].items(): nlu_text += '\n## synonym:' + intent + '\n' for text in texts: nlu_text += '- ' + text + '\n' nlu_data.write(nlu_text) train(RASA_CONFIG_PATH, RASA_NLU_DATA_PATH, RASA_MODEL_SAVE_PATH, 'current', 'model') global interpreter interpreter = Interpreter.load(NLU_MODEL_PATH)
def test_train_model_empty_pipeline(component_builder): # Should return an empty pipeline _config = utilities.base_test_conf(pipeline_template=None) with pytest.raises(ValueError): train( _config, data=DEFAULT_DATA_PATH, component_builder=component_builder)
def train_nlu(config: Text, nlu_data: Text, output: Text, train_path: Optional[Text]) -> Optional["Interpreter"]: """Trains a NLU model. Args: config: Path to the config file for NLU. nlu_data: Path to the NLU training data. output: Output path. train_path: If `None` the model will be trained in a temporary directory, otherwise in the provided directory. Returns: If `train_path` is given it returns the path to the model archive, otherwise the path to the directory with the trained model files. """ import rasa_nlu _train_path = train_path or tempfile.mkdtemp() _, nlu_model, _ = rasa_nlu.train(config, nlu_data, _train_path, project="", fixed_model_name="nlu") if not train_path: nlu_data = data.get_nlu_directory(nlu_data) output_path = create_output_path(output, prefix="nlu-") new_fingerprint = model.model_fingerprint(config, nlu_data=nlu_data) model.create_package_rasa(_train_path, output_path, new_fingerprint) print_success("Your Rasa NLU model is trained and saved at '{}'." "".format(output_path)) return nlu_model
def test_train_model(pipeline_template, component_builder, tmpdir): _config = utilities.base_test_conf(pipeline_template) (trained, _, persisted_path) = train(_config, path=tmpdir.strpath, data=DEFAULT_DATA_PATH, component_builder=component_builder) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_train_named_model(component_builder, tmpdir): _config = utilities.base_test_conf("keyword") (trained, _, persisted_path) = train(_config, path=tmpdir.strpath, project="my_keyword_model", data=DEFAULT_DATA_PATH, component_builder=component_builder) assert trained.pipeline normalized_path = os.path.dirname(os.path.normpath(persisted_path)) # should be saved in a dir named after a project assert os.path.basename(normalized_path) == "my_keyword_model"
def test_train_model_noents(language, pipeline, component_builder, tmpdir): _config = RasaNLUModelConfig({"pipeline": pipeline, "language": language}) (trained, _, persisted_path) = train(_config, path=tmpdir.strpath, data="./data/test/demo-rasa-noents.json", component_builder=component_builder) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_train_model_on_test_pipelines(language, pipeline, component_builder, tmpdir): _config = RasaNLUModelConfig({"pipeline": pipeline, "language": language}) (trained, _, persisted_path) = train(_config, path=tmpdir.strpath, data=DEFAULT_DATA_PATH, component_builder=component_builder) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_train_model(pipeline_template, component_builder, tmpdir): _config = utilities.base_test_conf(pipeline_template) (trained, _, persisted_path) = train( _config, path=tmpdir.strpath, data=DEFAULT_DATA_PATH, component_builder=component_builder) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_train_named_model(component_builder, tmpdir): _config = utilities.base_test_conf("keyword") (trained, _, persisted_path) = train( _config, path=tmpdir.strpath, project="my_keyword_model", data=DEFAULT_DATA_PATH, component_builder=component_builder) assert trained.pipeline normalized_path = os.path.dirname(os.path.normpath(persisted_path)) # should be saved in a dir named after a project assert os.path.basename(normalized_path) == "my_keyword_model"
def test_train_model_noents(language, pipeline, component_builder, tmpdir): _config = RasaNLUModelConfig({"pipeline": pipeline, "language": language}) (trained, _, persisted_path) = train( _config, path=tmpdir.strpath, data="./data/test/demo-rasa-noents.json", component_builder=component_builder) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_train_model_multithread(language, pipeline, component_builder, tmpdir): _config = RasaNLUModelConfig({"pipeline": pipeline, "language": language}) (trained, _, persisted_path) = train( _config, path=tmpdir.strpath, data=DEFAULT_DATA_PATH, component_builder=component_builder, num_threads=2) assert trained.pipeline loaded = Interpreter.load(persisted_path, component_builder) assert loaded.pipeline assert loaded.parse("hello") is not None assert loaded.parse("Hello today is Monday, again!") is not None
def test_random_seed(component_builder, tmpdir): """test if train result is the same for two runs of tf embedding""" _config = utilities.base_test_conf("supervised_embeddings") # set fixed random seed to 1 _config.set_component_attr(5, random_seed=1) # first run (trained_a, _, persisted_path_a) = train(_config, path=tmpdir.strpath + "_a", data=DEFAULT_DATA_PATH, component_builder=component_builder) # second run (trained_b, _, persisted_path_b) = train(_config, path=tmpdir.strpath + "_b", data=DEFAULT_DATA_PATH, component_builder=component_builder) loaded_a = Interpreter.load(persisted_path_a, component_builder) loaded_b = Interpreter.load(persisted_path_b, component_builder) result_a = loaded_a.parse("hello")["intent"]["confidence"] result_b = loaded_b.parse("hello")["intent"]["confidence"] assert result_a == result_b
def test_random_seed(component_builder, tmpdir): """test if train result is the same for two runs of tf embedding""" _config = utilities.base_test_conf("supervised_embeddings") # set fixed random seed to 1 _config.set_component_attr(5, random_seed=1) # first run (trained_a, _, persisted_path_a) = train( _config, path=tmpdir.strpath + "_a", data=DEFAULT_DATA_PATH, component_builder=component_builder) # second run (trained_b, _, persisted_path_b) = train( _config, path=tmpdir.strpath + "_b", data=DEFAULT_DATA_PATH, component_builder=component_builder) loaded_a = Interpreter.load(persisted_path_a, component_builder) loaded_b = Interpreter.load(persisted_path_b, component_builder) result_a = loaded_a.parse("hello")["intent"]["confidence"] result_b = loaded_b.parse("hello")["intent"]["confidence"] assert result_a == result_b