def parse(self, request): request.setHeader('Content-Type', 'application/json') if request.method.decode('utf-8', 'strict') == 'GET': request_params = decode_parameters(request) else: request_params = simplejson.loads(request.content.read().decode( 'utf-8', 'strict')) if 'query' in request_params: request_params['q'] = request_params.pop('query') if 'q' not in request_params: request.setResponseCode(404) dumped = json_to_string( {"error": "Invalid parse parameter specified"}) returnValue(dumped) else: data = self.data_router.extract(request_params) try: request.setResponseCode(200) response = yield (self.data_router.parse(data) if self._testing else threads.deferToThread( self.data_router.parse, data)) returnValue(json_to_string(response)) except InvalidProjectError as e: request.setResponseCode(404) returnValue(json_to_string({"error": "{}".format(e)})) except Exception as e: request.setResponseCode(500) logger.exception(e) returnValue(json_to_string({"error": "{}".format(e)}))
def evaluate(self, request): data_string = request.content.read().decode("utf-8", "strict") params = { key.decode("utf-8", "strict"): value[0].decode("utf-8", "strict") for key, value in request.args.items() } request.setHeader("Content-Type", "application/json") try: request.setResponseCode(200) response = yield self.data_router.evaluate(data_string, params.get("project"), params.get("model")) returnValue(json_to_string(response)) except MaxWorkerProcessError as e: request.setResponseCode(403) returnValue(json_to_string({"error": "{}".format(e)})) except InvalidProjectError as e: request.setResponseCode(500) returnValue(json_to_string({"error": "{}".format(e)})) except UnsupportedModelError as e: request.setResponseCode(500) returnValue(json_to_string({"error": "{}".format(e)})) except Exception as e: request.setResponseCode(500) returnValue(json_to_string({"error": "{}".format(e)}))
def dumps(self, training_data, **kwargs): """Writes Training Data to a string in json format.""" js_entity_synonyms = defaultdict(list) for k, v in training_data.entity_synonyms.items(): if k != v: js_entity_synonyms[v].append(k) formatted_synonyms = [{ "value": value, "synonyms": syns } for value, syns in js_entity_synonyms.items()] formatted_examples = [ example.as_dict() for example in training_data.training_examples ] return json_to_string( { "rasa_nlu_data": { "common_examples": formatted_examples, "regex_features": training_data.regex_features, "lookup_tables": training_data.lookup_tables, "entity_synonyms": formatted_synonyms, } }, **kwargs)
def dumps(self, training_data, **kwargs): js_entity_synonyms = defaultdict(list) for k, v in training_data.entity_synonyms.items(): if k != v: js_entity_synonyms[v].append(k) formatted_synonyms = [{ 'value': value, 'synonyms': syns } for value, syns in js_entity_synonyms.items()] formatted_examples = [ example.as_dict() for example in training_data.training_examples ] formatted_label_examples = [ example.as_dict() for example in training_data.label_training_examples or [] ] return json_to_string( { 'rasa_nlu_data': { 'common_examples': formatted_examples, 'label_examples': formatted_label_examples, 'regex_features': training_data.regex_features, 'entity_synonyms': formatted_synonyms, } }, **kwargs)
def dump_to_data_file(data): if isinstance(data, str): data_string = data else: data_string = utils.json_to_string(data) return utils.create_temporary_file(data_string, "_training_data")
def version(self, request): """Returns the Rasa server's version""" request.setHeader('Content-Type', 'application/json') return json_to_string( {'version': rasa.__version__, 'minimum_compatible_version': MINIMUM_COMPATIBLE_VERSION} )
def evaluate(self, request): data_string = request.content.read().decode('utf-8', 'strict') params = { key.decode('utf-8', 'strict'): value[0].decode('utf-8', 'strict') for key, value in request.args.items() } request.setHeader('Content-Type', 'application/json') try: request.setResponseCode(200) response = yield self.data_router.evaluate(data_string, params.get('project'), params.get('model')) returnValue(json_to_string(response)) except Exception as e: request.setResponseCode(500) returnValue(json_to_string({"error": "{}".format(e)}))
def version(self, request): """Returns the Rasa server's version""" request.setHeader("Content-Type", "application/json") return json_to_string( { "version": rasa.__version__, "minimum_compatible_version": MINIMUM_COMPATIBLE_VERSION, } )
def call2graph(self, request): request.setHeader('Content-Type', 'application/json') if request.method.decode('utf-8', 'strict') == 'GET': request_params = decode_parameters(request) else: request_params = simplejson.loads(request.content.read().decode( 'utf-8', 'strict')) if 'data' in request_params: request_params['d'] = request_params.pop('data') if 'filename' in request_params: request_params['f'] = request_params.pop('filename') if 'd' not in request_params and 'f' not in request_params: request.setResponseCode(404) dumped = json_to_string( {"error": "Invalid parse parameter specified"}) returnValue(dumped) else: # TODO 提取文件名中的手机号 # 封装解析参数 data = self.data_router.extract(request_params) try: request.setResponseCode(200) # 解析Array内容 response = yield (self.data_router.parse(data) if self._testing else threads.deferToThread( self.data_router.parse, data)) print("-->", response) # TODO 校验文档有效性 returnValue(json_to_string(response)) except InvalidProjectError as e: request.setResponseCode(404) returnValue(json_to_string({"error": "{}".format(e)})) except Exception as e: request.setResponseCode(500) logger.exception(e) returnValue(json_to_string({"error": "{}".format(e)}))
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)}))
def test_is_nlu_file_with_json(): test = { "rasa_nlu_data": { "lookup_tables": [ {"name": "plates", "elements": ["beans", "rice", "tacos", "cheese"]} ] } } directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") with open(file, "w", encoding="utf-8") as f: f.write(json_to_string(test)) assert data.is_nlu_file(file)
def test_is_nlu_file_with_json(): test = { "rasa_nlu_data": { "lookup_tables": [ {"name": "plates", "elements": ["beans", "rice", "tacos", "cheese"]} ] } } directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") io.write_text_file(json_to_string(test), file) assert data.is_nlu_file(file)
def run_cmdline(model_path, component_builder=None): interpreter = Interpreter.load(model_path, component_builder) regex_interpreter = RegexInterpreter() print_success( "NLU model loaded. Type a message and press enter to parse it.") while True: print_success("Next message:") message = input().strip() if message.startswith(INTENT_MESSAGE_PREFIX): loop = asyncio.get_event_loop() result = loop.run_until_complete(regex_interpreter.parse(message)) else: result = interpreter.parse(message) print(json_to_string(result))
def view(self): return json_to_string(self.__dict__, indent=4)
def status(self, request): request.setHeader('Content-Type', 'application/json') return json_to_string(self.data_router.get_status())