예제 #1
0
    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/json')

        try:
            request.setResponseCode(200)

            response = yield self.data_router.start_train_process(
                    data_file, project,
                    RasaNLUModelConfig(model_config), model_name)

            returnValue(json_to_string({'info': 'new model trained: {}'
                                                ''.format(response)}))
        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)}))
예제 #2
0
    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:
            try:
                request.setResponseCode(200)
                response = yield self.data_router.inference(
                    request_params['q'])
                returnValue(json_to_string(response))
            except Exception as e:
                request.setResponseCode(500)
                logger.exception(e)
                returnValue(json_to_string({"error": "{}".format(e)}))
예제 #3
0
    def delete(self, request):

        request.setHeader('Content-Type', 'application/json')

        botId = parameter_or_default(request, "BotRecordId", default=None)

        tenanetId = parameter_or_default(request, "TenanetID", default=None)

        request.setHeader('Content-Type', 'application/json')

        try:
            request.setResponseCode(200)

            self.data_router.unload_model(tenanetId=tenanetId, botId=botId)

            response = yield self.data_router.delete(path="projects",
                                                     tenanetId=tenanetId,
                                                     botId=botId)
            returnValue(json_to_string(response))

        except Exception as e:
            request.setResponseCode(500)
            returnValue(
                json_to_string({
                    "Code": "0",
                    "Message": "{}".format(e),
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))
예제 #4
0
    def train(self, request):
        try:
            model_config, data = self.extract_data_and_config(request)
            botId = model_config["BotRecordId"]
            tenanetId = model_config["TenanetID"]
        except Exception as e:
            request.setResponseCode(400)
            returnValue(
                json_to_string({
                    "Code": "0",
                    "Message": "{}".format(e),
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))

        data_file = dump_to_data_file(data)

        request.setHeader('Content-Type', 'application/json')

        try:
            request.setResponseCode(200)

            response = yield self.data_router.start_train_process(
                data_file, tenanetId, RasaNLUModelConfig(model_config), botId)

            returnValue(
                json_to_string({
                    'Code': '1',
                    "Message": "",
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))
        except MaxTrainingError as e:
            request.setResponseCode(403)
            returnValue(
                json_to_string({
                    "Code": "0",
                    "Message": "{}".format(e),
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))
        except InvalidProjectError as e:
            request.setResponseCode(404)
            returnValue(
                json_to_string({
                    "Code": "0",
                    "Message": "{}".format(e),
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))
        except TrainingException as e:
            request.setResponseCode(500)
            returnValue(
                json_to_string({
                    "Code": "0",
                    "Message": "{}".format(e),
                    "TenanetID": tenanetId,
                    "BotRecordId": botId
                }))
예제 #5
0
    def parse(self, request):
        start = time.time()
        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'))

        botId = parameter_or_default(request, "BotRecordId", default="default")

        tenanetId = parameter_or_default(request,
                                         "TenanetID",
                                         default="default")

        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": {
                    "statusCode": 404,
                    "message": "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))
                end = time.time()
                logger.info("parse time cost %.2f s" % (end - start))

                returnValue(json_to_string(response))
            except InvalidProjectError as e:
                request.setResponseCode(404)
                returnValue(
                    json_to_string({
                        "error": {
                            "statusCode": 404,
                            "message": "{}".format(e)
                        }
                    }))
            except Exception as e:
                request.setResponseCode(500)
                logger.exception("error in parse function :" + e)
                returnValue(
                    json_to_string({
                        "error": {
                            "statusCode": 500,
                            "message": "{}".format(e)
                        }
                    }))
예제 #6
0
def dump_to_data_file(data):
    if isinstance(data, six.string_types):
        data_string = data
    else:
        data_string = utils.json_to_string(data)

    return utils.create_temporary_file(data_string, "_training_data")
예제 #7
0
    def version(self, request):
        """Returns the Rasa server's version"""

        request.setHeader('Content-Type', 'application/json')
        return json_to_string(
            {'version': __version__,
             'minimum_compatible_version': MINIMUM_COMPATIBLE_VERSION}
        )
예제 #8
0
    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)}))
예제 #9
0
    def rasaconfig(self, request):
        """Returns the in-memory configuration of the Rasa server"""

        # DEPRECATED: I don't think there is a use case for this endpoint
        # anymore - when training a new model, the user should always post
        # the configuration as part of the request instead of relying on
        # the servers config.
        request.setHeader('Content-Type', 'application/json')
        return json_to_string(self.default_model_config)
예제 #10
0
def dump_to_data_file(data):
    if isinstance(data, six.string_types):
        data_string = data
    else:

        if "rasa_nlu_data" in data:
            if "common_examples" in data["rasa_nlu_data"]:
                common_examples = data["rasa_nlu_data"]["common_examples"]
                # indices = np.random.permutation(len(common_examples))
                # common_examples =common_examples[indices]
                np.random.shuffle(common_examples)
                data["rasa_nlu_data"]["common_examples"] = common_examples

        data_string = utils.json_to_string(data)

    return utils.create_temporary_file(data_string, "_training_data")
예제 #11
0
파일: rasa.py 프로젝트: Houlong66/rasa-demo
    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,
                "entity_synonyms": formatted_synonyms
            }
        }, **kwargs)
예제 #12
0
 def view(self):
     return json_to_string(self.__dict__, indent=4)
예제 #13
0
 def status(self, request):
     request.setHeader('Content-Type', 'application/json')
     return json_to_string(self.data_router.get_status())