async def train(request): # if not set will use the default project name, e.g. "default" project = request.args.get("project", None) # if set will not generate a model name but use the passed one model_name = request.args.get("model", None) try: model_config, data = extract_data_and_config(request) except Exception as e: raise ErrorResponse( 500, "ServerError", "An unexpected error occurred.", details={"error": str(e)}, ) data_file = dump_to_data_file(data) try: path_to_model = await data_router.start_train_process( data_file, project, RasaNLUModelConfig(model_config), model_name ) zipped_path = utils.zip_folder(path_to_model) return await response.file(zipped_path) except MaxWorkerProcessError as e: raise ErrorResponse( 403, "NoFreeProcess", "No process available for training.", details={"error": str(e)}, ) except InvalidProjectError as e: raise ErrorResponse( 404, "ProjectNotFound", "Project '{}' not found.".format(project), details={"error": str(e)}, ) except TrainingException as e: raise ErrorResponse( 500, "ServerError", "An unexpected error occurred.", details={"error": str(e)}, )
async def test_project_with_model_server(trained_nlu_model): fingerprint = "somehash" model_endpoint = EndpointConfig("http://server.com/models/nlu/tags/latest") zip_path = zip_folder(trained_nlu_model) # mock a response that returns a zipped model with io.open(zip_path, "rb") as f: responses.add( responses.GET, model_endpoint.url, headers={ "ETag": fingerprint, "filename": "my_model_xyz.zip" }, body=f.read(), content_type="application/zip", stream=True, ) project = await load_from_server(model_server=model_endpoint) assert project.fingerprint == fingerprint
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
def train(self, request): # if not set will use the default project name, e.g. "default" project = parameter_or_default(request, "project", default=None) # if set will not generate a model name but use the passed one model_name = parameter_or_default(request, "model", default=None) try: model_config, data = self.extract_data_and_config(request) except Exception as e: request.setResponseCode(400) returnValue(json_to_string({"error": "{}".format(e)})) data_file = dump_to_data_file(data) request.setHeader('Content-Type', 'application/zip') try: request.setResponseCode(200) request.setHeader("Content-Disposition", "attachment") path_to_model = yield self.data_router.start_train_process( data_file, project, RasaNLUModelConfig(model_config), model_name) zipped_path = utils.zip_folder(path_to_model) zip_content = io.open(zipped_path, 'r+b').read() return returnValue(zip_content) except MaxTrainingError as e: request.setResponseCode(403) returnValue(json_to_string({"error": "{}".format(e)})) except InvalidProjectError as e: request.setResponseCode(404) returnValue(json_to_string({"error": "{}".format(e)})) except TrainingException as e: request.setResponseCode(500) returnValue(json_to_string({"error": "{}".format(e)}))