def get_all_training_data():
    form = GetterForm(request.args)
    result = {'limit': form.limit.data, 'page': form.page.data, 'status': 'success'}
    if form.validate():
        training_data = TrainingData.get_by(limit=form.limit.data, offset=form.get_offset())
        data = []
        for item in training_data:  # type: TrainingData
            data.append(item.serialize(exclude=['created_at', 'modified_at']))
        result['data'] = data
        result['data_count'] = len(training_data)
        result['total_page'] = TrainingData.get_page(limit=form.limit.data)
    else:
        result['error'] = form.get_error()
        result['status'] = 'error'
    return jsonify(result)
def add_training_data():
    form = DataForm()
    result = {'status': 'success'}
    if form.validate():
        data = TrainingData()
        form.populate_obj(data)
        rst, message = data.save()
        if rst:
            detect_language.delay(data.serialize())
            result['data'] = data.serialize(exclude=['created_at', 'modified_at'])
        else:
            result['status'] = 'error'
            result['error'] = message
    else:
        result['status'] = 'error'
        result['error'] = form.get_error()
    return jsonify(result)
def get_training_data(id):
    result = {'status': 'success'}
    data = TrainingData.get_one_by({'id': id})  # type: TrainingData
    if data is None:
        result['status'] = 'error'
        result['message'] = "Data can't be found"
    else:
        result['data'] = data.serialize(exclude=['created_at', 'modified_at'])
    return jsonify(result)
def delete_training_data(id):
    result = {'status': 'success'}
    data = TrainingData.get_one_by({'id': id})  # type: TrainingData
    if data.__class__ == TrainingData:
        rst, message = data.delete()
        if not rst:
            result['status'] = 'error'
            result['message'] = message
    return jsonify(result)
Example #5
0
def _parse_training_data(body: Dict[str, Any]) -> TrainingData:
    """Parses request body into training data if the data is correct.

    :param body: Raw request body as dict.
    :return: TrainingData.
    """
    return TrainingData(
        text=body['text'],
        label=_parse_spam_label(body['label']).value)
Example #6
0
def detect_language(model):
    try:
        lang = LanguageDetection()
        data = None
        if model['class'] == 'TrainingData':
            data = TrainingData.get_one_by({'id': model['id']})  # type: TrainingData
            data.lang = lang.detect_language(data.get_message())
            data.save()
        else:
            data = TestData.get_one_by({'id': model['id']})  # type: TestData
            data.lang = lang.detect_language(data.get_message())
            data.save()
        app.logger.info("Language detected : {}".format(data.lang))
        return True
    except Exception as e:
        app.logger.error("{}".format(e))
        return False
def update_training_data(id):
    form = DataForm()
    result = {'status': 'success'}
    if form.validate():
        data = TrainingData.get_one_by({'id': id})  # type: TrainingData
        if data.__class__ is TrainingData:
            form.populate_obj(data)
            detect_language.delay(data.serialize())
            rst, message = data.save()
            if rst:
                result['data'] = data.serialize(exclude=['created_at', 'modified_at'])
            else:
                result['status'] = 'error'
                result['error'] = message
        else:
            result['status'] = 'error'
            result['error'] = "Data can't be found"
            result['id'] = id
    else:
        result['status'] = 'error'
        result['error'] = form.get_error()
    return jsonify(result)