示例#1
0
文件: bots.py 项目: rajma/Nacer-NLP
def bots():
    if request.method == 'GET':
        code, user_id = checkAuth(request)
        if code == 200:
            bots = Bot.query.filter_by(user_id=user_id)
            bots_obj = []
            for bot in bots:
                bot_obj = {}
                bot_obj['id'] = bot.id
                bot_obj['bot_guid'] = bot.bot_guid
                bot_obj['user_id'] = bot.user_id
                bot_obj['name'] = bot.name
                bot_obj['persona'] = bot.persona
                bot_obj['last_trained'] = bot.last_trained
                bot_obj['used'] = (
                    bot.used - datetime.datetime(1970, 1, 1)).total_seconds()

                bots_obj.append(bot_obj)
            app.logger.info('GET /api/bots list of bots returned successfully')
            return jsonify({"bots": bots_obj})
        elif code == 400:
            app.logger.warning('GET /api/bots invalid authorization token')
            return jsonify({"error": "Invalid Authorization Token"}), 400
        elif code == 401:
            app.logger.warning('GET /api/bots no authorization token sent')
            return jsonify({"error": "No Authorization Token Sent"}), 401
    elif request.method == 'POST':
        code, user_id = checkAuth(request)
        post_data = request.get_json()
        if not post_data:
            response_object = {'status': 'fail', 'message': 'Invalid payload.'}
            app.logger.warning('POST /api/bots post object invalid')
            return jsonify(response_object), 400
        name = post_data.get('name')

        if code == 200:
            bot = Bot(user_id=user_id, name=name.lower(), words=json.dumps({}))
            db.session.add(bot)
            try:
                db.session.commit()
            except Exception as e:
                app.logger.error('POST /api/bots ' + str(e))
                return jsonify({"success": False, "error": str(e)})
            app.logger.info('POST /api/bots bot added successfully')
            return jsonify({'success': True}), 200
        elif code == 400:
            app.logger.warning('POST /api/bots invalid authorization token')
            return jsonify({"error": "Invalid Authorization Token"}), 400
        elif code == 401:
            app.logger.warning('POST /api/bots no authorization token sent')
            return jsonify({"error": "No Authorization Token Sent"}), 401
示例#2
0
def upload(bot_guid):
    code, user_id = checkAuth(request)

    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                #Get the file information
                file = request.files['file']
                filename = secure_filename(file.filename)

                data = json.load(file)
                load_from_json(data, bot, bot_guid)
                app.logger.info('POST /api/upload/' + bot_guid +
                                ' successfully uploaded ozz data')
                return jsonify({
                    "filename": filename,
                    "type": file.content_type
                })
            else:
                app.logger.warning('POST /api/upload/' + bot_guid +
                                   ' not authorized')
                return jsonify({"error": "Not Authorized"}), 401
        else:
            app.logger.warning('POST /api/upload/' + bot_guid +
                               ' bot does not exist')
            return jsonify({"error": "Bot doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('POST /api/upload/' + bot_guid +
                           ' invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('POST /api/upload/' + bot_guid +
                           ' no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#3
0
文件: bots.py 项目: rajma/Nacer-NLP
def persona(bot_guid):
    code, user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if not bot:
            return jsonify({"error": "Bot Not Found"}), 404
        if bot.user_id == user_id:
            if request.method == 'GET':
                return jsonify({"persona": bot.persona})
            elif request.method == 'PUT':
                put_data = request.get_json()
                try:
                    bot.persona = put_data['persona']
                    db.session.commit()
                except Exception as e:
                    app.logger.error('GET /api/bots/' + bot_guid +
                                     '/persona ' + str(e))
                    return jsonify({"success": False, "error": str(e)})
                app.logger.info('GET /api/bots/' + bot_guid +
                                '/persona returned persona')
                return jsonify({"persona": bot.persona})
        else:
            app.logger.warning('/api/bots/' + bot_guid +
                               '/persona Unauthorized')
            return jsonify({"error": "Unauthorized"}), 401
    elif code == 400:
        app.logger.warning('/api/bots/' + bot_guid +
                           '/persona Invalid Authorization Token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('/api/bots/' + bot_guid +
                           '/persona No Authorization Token Sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#4
0
def entity(bot_guid,entity_name):
    code,user_id = checkAuth(request)
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                if request.method == 'GET':
                    entity= Entity.query.filter_by(bot_guid=bot_guid).filter_by(name=entity_name).first()
                    if entity:
                        if type(entity.examples) == str:
                            entity.examples = json.loads(entity.examples)
                        app.logger.info('GET /api/entities/'+bot_guid+'/'+entity_name+' successfully returned entity examples')
                        return jsonify({"examples":entity.examples})
                elif request.method == 'POST':
                    post_data = request.get_json()
                    if not post_data:
                        response_object = {
                            'status': 'fail',
                            'message': 'Invalid payload.'
                        }
                        app.logger.warning('POST /api/entities/'+bot_guid+'/'+entity_name+' invalid post object')
                        return jsonify(response_object), 400
                    else:
                        entity= Entity.query.filter_by(bot_guid=bot_guid).filter_by(name=entity_name).first()
                        if entity:
                            if type(entity.examples) == str:
                                entity.examples = json.loads(entity.examples)
                            new_example = post_data['new_example']
                            entity.examples[new_example] = []
                            flag_modified(entity, "examples")
                            try:
                                db.session.commit()
                            except Exception as e:
                                app.logger.error('POST /api/entities/'+bot_guid+'/'+entity_name+' '+str(e))
                                return jsonify({"success":False,"error":str(e)})
                            app.logger.info('POST /api/entities/'+bot_guid+'/'+entity_name+' successfully added entity')
                            return jsonify({"success":True})
                elif request.method == 'DELETE':
                    entity= Entity.query.filter_by(bot_guid=bot_guid).filter_by(name=entity_name).first()
                    if entity:
                        old_example = request.args['old_example']
                        entity.examples.pop(old_example,0)
                        flag_modified(entity, "examples")
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('DELETE /api/entities/'+bot_guid+'/'+entity_name+' '+str(e))
                            return jsonify({"success":False,"error":str(e)})
                        app.logger.info('POST /api/entities/'+bot_guid+'/'+entity_name+' successfully deleted entity')
                        return jsonify({"success":True})
    elif code == 400:
        app.logger.warning('/api/entities/'+bot_guid+'/'+entity_name+' invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/api/entities/'+bot_guid+'/'+entity_name+' no authorization token sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#5
0
def syn(bot_guid, entity_name, example_name):
    code, user_id = checkAuth(request)
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                if request.method == 'POST':
                    post_data = request.get_json()
                    if not post_data:
                        response_object = {
                            'status': 'fail',
                            'message': 'Invalid payload.'
                        }
                        return jsonify(response_object), 400
                    else:
                        entity = Entity.query.filter_by(
                            bot_guid=bot_guid).filter_by(
                                name=entity_name).first()
                        if entity:
                            synonym = post_data['synonym']
                            if type(entity.examples) == str:
                                entity.examples = json.loads(entity.examples)
                            if example_name in entity.examples:
                                entity.examples[example_name].append(synonym)
                                flag_modified(entity, "examples")
                            try:
                                db.session.commit()
                            except Exception as e:
                                return jsonify({"success": "false"})
                            return jsonify({"success": "true"})
                elif request.method == 'DELETE':
                    entity = Entity.query.filter_by(
                        bot_guid=bot_guid).filter_by(name=entity_name).first()
                    if entity:
                        synonym = request.args['synonym']
                        if type(entity.examples) == str:
                            entity.examples = json.loads(entity.examples)
                        if example_name in entity.examples:
                            entity.examples[example_name].remove(synonym)
                            flag_modified(entity, "examples")
                        try:
                            db.session.commit()
                        except Exception as e:
                            return jsonify({"success": "false"})
                        return jsonify({"success": "true"})
    elif code == 400:
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#6
0
def download(bot_guid):
    code, user_id = checkAuth(request)
    # code, user_id = 200,16
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                ozz_data = {"ozz_data": {"intents": [], "entities": []}}
                intents = Intent.query.filter_by(bot_guid=bot_guid).all()
                entities = Entity.query.filter_by(bot_guid=bot_guid).all()

                for intent in intents:
                    intent_obj = {}
                    intent_obj['name'] = intent.name
                    intent_obj['utterances'] = intent.utterances
                    intent_obj['responses'] = intent.responses
                    intent_obj['patterns'] = intent.patterns
                    ozz_data['ozz_data']['intents'].append(intent_obj)

                for entity in entities:
                    ent_obj = {}
                    ent_obj['name'] = entity.name
                    ent_obj['values'] = []
                    for value in entity.examples:
                        value_obj = {}
                        value_obj['name'] = value
                        value_obj['synonyms'] = entity.examples[value]
                        ent_obj['values'].append(value_obj)

                    ozz_data['ozz_data']['entities'].append(ent_obj)
                app.logger.info('GET /api/download/' + bot_guid +
                                ' successfully downloaded ozz data')
                return jsonify(ozz_data)
            else:
                app.logger.warning('GET /api/download/' + bot_guid +
                                   ' not authorized')
                return jsonify({"error": "Not Authorized"}), 401
        else:
            app.logger.warning('GET /api/download/' + bot_guid +
                               ' bot does not exist')
            return jsonify({"error": "Bot doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('GET /api/download/' + bot_guid +
                           ' invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('GET /api/download/' + bot_guid +
                           ' no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#7
0
def analytics(bot_guid):
    code,user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if request.method == 'GET':
                if bot.user_id == user_id:
                    analytics_logs = Analytics.query.filter_by(bot_guid=bot_guid).all()
                    intents = Intent.query.filter_by(bot_guid=bot_guid).all()

                    intent_count = {}
                    for intent in intents:
                        intent_count[intent.name] = 0
                    
                    calls = 0
                    success_count = 0.0
                    resp_time = 0.0
                    times = []
                    for log in analytics_logs:
                        if log.confident:
                            success_count += 1.0
                        if log.intent in intent_count:
                            intent_count[log.intent] += 1
                        resp_time += float(log.response_time)
                        calls += 1
                        times.append(log.created)
                    avg_resp_time = float("{:.3f}".format(resp_time/calls)) 
                    success_percentage = float("{:.3f}".format((success_count/calls) * 100))
                    app.logger.info('GET /analytics - analytics response success')
                    return jsonify({"calls":calls,"avg_resp_time":avg_resp_time,"success_percentage":success_percentage,"intent_count":intent_count,"times":times})
                else:
                    app.logger.warning('GET /analytics not authorized')
                    return jsonify({"error":"Not Authorized"}),401
            else:
                app.logger.warning('/analytics not authorized')
                return jsonify({"error":"Not Authorized"}),401
        else:
            app.logger.warning('/analytics bot does not exist')
            return jsonify({"error":"Bot Doesn't exist"}),404
    elif code == 400:
        app.logger.warning('/analytics invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/analytics no authorization token sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#8
0
def intent(bot_guid,intent_name):
    code,user_id = checkAuth(request)
    if code == 200:
        global interpreters
        nlus = interpreters
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(name=intent_name).first()
                if intent:
                    model = bot.active_model
                    if model:
                        nlu = nlus[model]
                    else:
                        nlu = None
                    if request.method == 'GET':
                        intent_obj = {}
                        intent_obj['is_folder'] = False
                        intent_obj['responses'] = intent.responses
                        intent_obj['utterances'] = []
                        for utterance in intent.utterances:
                            intent_obj['utterances'].append({"utterance":utterance,"entities":[]})
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('GET /api/intents/'+bot_guid+ '/'+ intent_name+ ' ' + str(e))
                            return jsonify({"success":False,'errors':str(e)}),400
                        app.logger.info('GET /api/intents/'+bot_guid+ '/'+ intent_name+ ' successfully returned intent information')
                        return jsonify(intent_obj)
                else:
                   app.logger.warning('/api/intents/'+bot_guid+ '/'+ intent_name+ ' intent does not exist')
                   return jsonify({"error":"Intent Doesn't exist"}),404 
            else:
                app.logger.warning('/api/intents/'+bot_guid+ '/'+ intent_name+ ' not authorized')
                return jsonify({"error":"Not Authorized"}),401
        else:
            app.logger.warning('/api/intents/'+bot_guid+ '/'+ intent_name+ ' bot does not exist')
            return jsonify({"error":"Bot Doesn't exist"}),404
    elif code == 400:
        app.logger.warning('/api/intents/'+bot_guid+ '/'+ intent_name+ ' invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/api/intents/'+bot_guid+ '/'+ intent_name+ ' authorization token not sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#9
0
def upload_dialogflow(bot_guid):
    code, user_id = checkAuth(request)

    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                #Get the file information
                file = request.files['file']
                filename = secure_filename(file.filename)

                print(filename)
                path = os.path.join(os.getcwd(), 'data/dialogflow')
                file.save(path + '/dialogflow.zip')
                try:
                    zip_ref = zipfile.ZipFile(
                        os.path.join(path, 'dialogflow.zip'), 'r')
                    zip_ref.extractall(path + '/contents')
                    zip_ref.close()
                except Exception as e:
                    print(str(e))
                app.logger.info('POST /api/upload/' + bot_guid +
                                ' successfully uploaded ozz data')
                return jsonify({
                    "filename": filename,
                    "type": file.content_type
                })
            else:
                app.logger.warning('POST /api/upload/' + bot_guid +
                                   ' not authorized')
                return jsonify({"error": "Not Authorized"}), 401
        else:
            app.logger.warning('POST /api/upload/' + bot_guid +
                               ' bot does not exist')
            return jsonify({"error": "Bot doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('POST /api/upload/' + bot_guid +
                           ' invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('POST /api/upload/' + bot_guid +
                           ' no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#10
0
def folders(bot_guid):
    code,user_id = checkAuth(request)
    print(code)
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                if request.method == 'DELETE':
                    folders = request.args.getlist('folders[]')
                    base = request.args['base']
                    print(base)
                    try:
                        for folder in folders:
                            pres = '.'.join(base.split('/')[1:])
                            if base != '/':
                                folder = pres + '.' + folder
                        
                            intent_name = folder + '%'
                            query = Intent.query.filter_by(bot_guid=bot_guid).filter(Intent.name.like(intent_name))
                            query.delete(synchronize_session=False)
                        db.session.commit()
                    except Exception as e:
                        app.logger.error('DELETE /api/folders/'+bot_guid+ ' ' + str(e))
                        return jsonify({"success":False,"errors":str(e)})
                    app.logger.info('DELETE /api/folders/'+bot_guid+ ' successfully deleted folder')
                    return jsonify({"success":True})
                else:
                    app.logger.warning('/api/folders/'+bot_guid+ ' method not allowed')
                    return jsonify({"error":"Method Not Allowed"}),405
            else:
                app.logger.warning('/api/folders/'+bot_guid+ ' not authorized')
                return jsonify({"error":"Not Authorized"}),401
        else:
            app.logger.warning('/api/folders/'+bot_guid+ ' bot not found')
            return jsonify({"error":"Bot Not Found"}),404
    elif code == 400:
        app.logger.warning('/api/folders/'+bot_guid+ ' invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/api/folders/'+bot_guid+ ' no authorization token sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#11
0
def uploadexcel(bot_guid):
    if request.method == 'POST':
        code, user_id = checkAuth(request)
        if code == 200:
            bot = Bot.query.filter_by(bot_guid=bot_guid).first()
            if bot:
                if bot.user_id == user_id:
                    tsv_file = request.files['file']
                    filename = secure_filename(tsv_file.filename)
                    tsv_file_read = tsv_file.readlines()
                    for row in tsv_file_read:
                        line = row.decode('utf-8')

                        intent_name, questions, answers = line.split('\t')
                        questions = questions.split('<->')
                        answers = answers.split('<->')

                        questions = [q for q in questions if len(q) > 0]
                        answers = [a for a in answers if len(a) > 0]

                        if intent_name == 'Intent' or intent_name == 'intent':
                            continue
                        else:
                            intent = Intent.query.filter_by(
                                bot_guid=bot_guid).filter_by(
                                    name=intent_name).first()
                            patterns = []
                            for utt_copy in questions:
                                if len(utt_copy) == 0:
                                    continue
                                parameters, regex = get_regex(utt_copy)
                                new_obj = {}
                                new_obj['string'] = utt_copy
                                new_obj['regex'] = regex
                                new_obj['entities'] = parameters
                                new_obj['len'] = len(utt_copy)
                                patterns.append(json.dumps(new_obj))

                                for word in stopWords:
                                    utt_copy = utt_copy.replace(word, " ")

                                utt_words = utt_copy.split(" ")

                                words_json = json.loads(bot.words)
                                if type(words_json) == str:
                                    words_json = {}
                                for word in utt_words:
                                    if word in words_json:
                                        if intent_name in words_json[word]:
                                            words_json[word][intent_name] += 1
                                        else:
                                            words_json[word][intent_name] = 1
                                    else:
                                        words_json[word] = {intent_name: 1}
                            try:
                                if intent:
                                    intent.utterances = questions
                                    intent.responses += answers
                                    intent.patterns = patterns
                                    flag_modified(intent, "utterances")
                                    flag_modified(intent, "responses")
                                    flag_modified(intent, "patterns")
                                    db.session.commit()
                                else:
                                    name = intent_name
                                    utterances = questions
                                    responses = answers
                                    has_entities = False
                                    intent = Intent(name=name,
                                                    bot_guid=bot_guid,
                                                    utterances=utterances,
                                                    has_entities=has_entities,
                                                    responses=responses,
                                                    patterns=patterns)
                                    db.session.add(intent)
                                    db.session.commit()
                            except Exception as e:
                                app.logger.error('POST /api/upload_csv/' +
                                                 bot_guid + ' ' + str(e))
                                return jsonify({
                                    "success": False,
                                    "errors": str(e)
                                })
                    app.logger.info('POST /api/upload_csv/' + bot_guid +
                                    ' uploaded bot data from csv')
                    return jsonify({"success": True})
                else:
                    app.logger.warning('POST /api/upload_csv/' + bot_guid +
                                       ' not authorized')
                    return jsonify({"error": "Not Authorized"}), 401
            else:
                app.logger.warning('POST /api/upload_csv/' + bot_guid +
                                   ' bot does not exist')
                return jsonify({"error": "Bot doesn't exist"}), 404
        elif code == 400:
            app.logger.warning('POST /api/upload_csv/' + bot_guid +
                               ' invalid authorization token')
            return jsonify({"error": "Invalid Authorization Token"}), 400
        elif code == 401:
            app.logger.warning('POST /api/upload_csv/' + bot_guid +
                               ' no authorization token sent')
            return jsonify({"error": "No Authorization Token Sent"}), 401
    return render_template('file.html')
示例#12
0
def train(bot_guid):
    code, user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                for key in redis_db.scan_iter(match=bot_guid + '_*'):
                    redis_db.delete(key)
                print('training')
                rasa_data = {
                    "rasa_nlu_data": {
                        "common_examples": [{
                            "text": "Nomnffl",
                            "intent": "None",
                            "entities": []
                        }, {
                            "text": "Nekdg",
                            "intent": "None",
                            "entities": []
                        }, {
                            "text": "Fesf",
                            "intent": "None",
                            "entities": []
                        }, {
                            "text": "this is the this",
                            "intent": "None",
                            "entities": []
                        }, {
                            "text": "likesdike mike",
                            "intent": "None",
                            "entities": []
                        }],
                        "entity_synonyms": [],
                        "regex_features": []
                    }
                }
                intents = Intent.query.filter_by(bot_guid=bot_guid).filter(
                    ~Intent.name.like('eliza.%')).all()
                entities = Entity.query.filter_by(bot_guid=bot_guid)
                ent_data = {}
                words_json = {}
                for entity in entities:
                    ent_data[entity.name] = []
                    if type(entity.examples) == str:
                        entity.examples = json.loads(entity.examples)
                    for example_key in entity.examples:
                        ent_data[entity.name].extend(
                            entity.examples[example_key])
                        rasa_data['rasa_nlu_data']['entity_synonyms'].append({
                            "value":
                            example_key,
                            "synonyms":
                            entity.examples[example_key]
                        })
                for intent in intents:
                    for utterance in intent.utterances:
                        utt_copy = utterance
                        intent_name = intent.name
                        for word in stopWords:
                            utt_copy = utt_copy.replace(word, " ")

                        utt_words = utt_copy.split(" ")

                        for word in utt_words:
                            if word in words_json:
                                if intent_name in words_json[word]:
                                    words_json[word][intent_name] += 1
                                else:
                                    words_json[word][intent_name] = 1
                            else:
                                words_json[word] = {intent_name: 1}

                        terminals = []
                        common_example = {}
                        common_example['text'] = utterance.lower()
                        common_example['intent'] = intent.name
                        common_example['entities'] = []
                        new_examples = []
                        new_values = []
                        for entity in entities:
                            if type(entity.examples) == str:
                                entity.examples = json.loads(entity.examples)
                            for example_key in entity.examples:
                                utterance = utterance.lower()
                                ent_examples = entity.examples[example_key]
                                # Used to consider key as well - but key is automatically added (not verified) when retreiving so these steps are not needed
                                # if example_key.lower() not in ent_examples:
                                #     if example_key in ent_examples:
                                #         ent_examples.remove(example_key)
                                #     ent_examples.append(example_key.lower())
                                for example in ent_examples:
                                    example = example.lower()
                                    test_utterance = ' ' + utterance + ' '
                                    test_example = ' ' + example + ' '
                                    if test_example in test_utterance:
                                        #print("here")
                                        start = utterance.find(example)
                                        end = start + len(example)
                                        is_valid = True
                                        if len(terminals) > 0:
                                            mod_terminals = [
                                                x for x in terminals
                                            ]
                                            for term_start, term_end, name, value in terminals:
                                                if term_start <= start and term_end >= end:
                                                    is_valid = False
                                                    continue

                                                if start <= term_start and end >= term_end:
                                                    mod_terminals.remove(
                                                        (term_start, term_end,
                                                         name, value))
                                                    continue
                                            terminals = [x for x in terminals]
                                        if is_valid:
                                            terminals.append(
                                                (start, end, entity.name,
                                                 example))
                        for terminal in terminals:
                            common_example['entities'].append({
                                "start":
                                terminal[0],
                                "end":
                                terminal[1],
                                "entity":
                                terminal[2],
                                "value":
                                terminal[3]
                            })
                            remaining_examples = ent_data[terminal[2]]
                            if terminal[3] in remaining_examples:
                                remaining_examples.remove(terminal[3])
                            values = generate(utterance, terminal[3],
                                              remaining_examples, intent.name,
                                              entities)
                            new_values += values
                        rasa_data['rasa_nlu_data']['common_examples'].append(
                            common_example)
                        # rasa_data['rasa_nlu_data']['common_examples'] += new_values
                        # print(type(new_values))
                        # print(len(new_values))
                        # child_count = 0
                        # for val in new_values:
                        #     if type(val) == dict:
                        #         child_count += 1
                        # print(child_count)
                #print(rasa_data['rasa_nlu_data']['common_examples'])
                job = q.enqueue(train_bot,
                                bot,
                                rasa_data,
                                words_json,
                                timeout=600)
                job_id = job.get_id()
                print(job_id)
                app.logger.info('GET /api/train/' + bot_guid +
                                ' bot training started')
                return jsonify({"success": True, "job_id": job_id})
                #train_bot(bot,rasa_data, words_json)
            else:
                app.logger.warning('GET /api/train/' + bot_guid +
                                   ' not authorized')
                return jsonify({"error": "Not Authorized"}), 401
        else:
            app.logger.warning('GET /api/train/' + bot_guid +
                               ' bot does not exist')
            return jsonify({"error": "Bot doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('GET /api/train/' + bot_guid +
                           ' invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('GET /api/train/' + bot_guid +
                           ' no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#13
0
def entities(bot_guid):
    code,user_id = checkAuth(request)
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                if request.method == 'GET':
                    entities = Entity.query.filter_by(bot_guid=bot_guid)
                    entities_obj = []
                    for entity in entities:
                        if type(entity.examples) == str:
                            entity.examples = json.loads(entity.examples)
                        entity_obj = {}
                        entity_obj['name'] = entity.name
                        entity_obj['examples'] = entity.examples
                        entity_obj['num_examples'] = len(entity.examples)
                        entities_obj.append(entity_obj)
                    app.logger.warning('GET /api/entities/'+bot_guid+ ' successfully returned entities')
                    return jsonify({"entities":entities_obj})
                elif request.method == 'POST':
                    post_data = request.get_json()
                    if not post_data:
                        response_object = {
                            'status': 'fail',
                            'message': 'Invalid payload.'
                        }
                        app.logger.warning('POST /api/entities/'+bot_guid+ ' invalid post object')
                        return jsonify(response_object), 400
                    else:
                        name = post_data.get('name')
                        examples = post_data.get('examples')
                        
                        entity = Entity.query.filter_by(name=name).first()
                        if entity:
                            entity.examples = examples
                        else:
                            entity = Entity(
                                name = name.lower(),
                                bot_guid=bot_guid,
                                examples=examples
                            )
                            db.session.add(entity)
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('POST /api/entities/'+bot_guid+ ' ' + str(e))
                            return jsonify({"success":False,"error":str(e)})
                        app.logger.info('POST /api/entities/'+bot_guid+ ' successfully returned entities')
                        return jsonify({"success":True})
                elif request.method == 'DELETE':
                    entity_name = request.args['entity']
                    entity = Entity.query.filter_by(bot_guid=bot_guid).filter_by(name=entity_name).first()
                    if entity:
                        db.session.delete(entity)
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('DELETE /api/entities/'+bot_guid+ ' ' + str(e))
                            return jsonify({"success":False,"error":str(e)})
                        app.logger.info('DELETE /api/entities/'+bot_guid+ ' successfully deleted entity')
                        return jsonify({"success":True})
                    else:
                        app.logger.warning('DELETE /api/entities/'+bot_guid+ ' bot does not exist')
                        return jsonify({"success":False,"error":"Entity doesn't exsit"}),404
    elif code == 400:
        app.logger.warning('/api/entities/'+bot_guid+ ' invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/api/entities/'+bot_guid+ ' no authorization token sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#14
0
文件: bots.py 项目: rajma/Nacer-NLP
def update_bots(bot_guid):
    code, user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if not bot:
            app.logger.warning('/api/bots/' + bot_guid + ' Bot Not Found')
            return jsonify({"error": "Bot Not Found"}), 404
        if bot.user_id == user_id:
            if request.method == 'PUT':
                # print('here')
                put_data = request.get_json()

                if 'last_trained' in put_data and bot.last_trained != put_data[
                        'last_trained']:
                    if 'active_model' in put_data:
                        model_directory = put_data['active_model']
                        config = './project/config.json'
                        current_model = bot.active_model

                        if current_model and current_model != "":
                            global interpreters
                            interpreters.pop(current_model, 0)
                            if os.path.exists(current_model):
                                shutil.rmtree(current_model)

                        interpreters[model_directory] = NLUParser(
                            model_directory, config)

                        training_obj = Training(
                            bot_guid=bot_guid,
                            user_id=user_id,
                            trained_at=put_data['last_trained'],
                            train_time=put_data['train_time'])

                        db.session.add(training_obj)

                        bot.active_model = put_data['active_model']
                        bot.words = put_data['words']
                        bot.last_trained = put_data['last_trained']
                        bot.name = put_data['name']

                bot.persona = put_data['persona']
                bot.used = datetime.datetime.utcnow()

                try:
                    db.session.commit()
                except Exception as e:
                    app.logger.error('PUT /api/bots/' + bot_guid + ' ' +
                                     str(e))
                    return jsonify({'errors': str(e)}), 400
                app.logger.info('PUT /api/bots/' + bot_guid +
                                ' bot updated successfully')
                return jsonify({"success": True})
            elif request.method == 'DELETE':
                try:
                    db.session.delete(bot)
                    db.session.commit()
                except Exception as e:
                    app.logger.error('DELETE /api/bots/' + bot_guid + ' ' +
                                     str(e))
                    return jsonify({'errors': str(e)}), 400
                bots = Bot.query.filter_by(user_id=user_id)
                bots_obj = []
                for bot in bots:
                    bot_obj = {}
                    bot_obj['id'] = bot.id
                    bot_obj['bot_guid'] = bot.bot_guid
                    bot_obj['user_id'] = bot.user_id
                    bot_obj['name'] = bot.name
                    bot_obj['used'] = (
                        bot.used -
                        datetime.datetime(1970, 1, 1)).total_seconds()

                    bots_obj.append(bot_obj)
                app.logger.info('DELETE /api/bots/' + bot_guid +
                                ' bot deleted successfully')
                return jsonify({"bots": bots_obj})
        else:
            app.logger.warning('/api/bots/' + bot_guid + ' unauthorized')
            return jsonify({"error": "Unauthorized"}), 401
    elif code == 400:
        app.logger.warning('/api/bots/' + bot_guid +
                           ' invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('/api/bots/' + bot_guid +
                           ' no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#15
0
def intents(bot_guid):
    code,user_id = checkAuth(request)
    if code == 200:
        global interpreters
        nlus = interpreters
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        base = request.args.get('base')
        if not base:
            base = '/'
        if bot:
            if bot.user_id == user_id:
                if request.method == 'GET':
                    if base == '/':
                        intents = Intent.query.filter_by(bot_guid=bot_guid)
                        base_pattern = '/'
                    else:
                        base_pattern = '.'.join(base[1:].split('/')) + '.%'
                        intents = Intent.query.filter_by(bot_guid=bot_guid).filter(Intent.name.like(base_pattern)).all()
                    intents_obj = []
                    empty_folders = []
                    folder_list = {}
                    for intent in intents:
                        intent_obj = {}
                        folders = intent.name.split('.')
                        if len(folders) > 1:
                            if not base_pattern == '/':
                                folder_name = folders[len(base_pattern[:-2].split('.'))]
                            else:
                                folder_name = folders[0]
                            if folders[-1] == folder_name:
                                is_folder = intent.is_folder
                            else:
                                is_folder = True
                            if not folder_name in folder_list:
                                if base_pattern == '/':
                                    pattern = folder_name +'.%'
                                else:
                                    pattern = base_pattern[:-1] + folder_name + '.%'
                                contents = Intent.query.filter_by(bot_guid=bot_guid).filter_by(is_folder=False).filter(Intent.name.like(pattern)).all()
                                folder_list[folder_name] = intent.modified
                                intent_obj['name'] = folder_name
                                intent_obj['is_folder'] = is_folder
                                intent_obj['count'] = len(contents)
                                intent_obj['utterances'] = len(intent.utterances)
                                intent_obj['responses'] = len(intent.responses)
                                intent_obj['patterns'] = len(intent.patterns)
                                intent_obj['calls'] = intent.calls
                                intent_obj['modified'] = intent.modified
                                intents_obj.append(intent_obj)
                        else:
                            intent_obj['is_folder'] = intent.is_folder
                            intent_obj['name'] = intent.name
                            intent_obj['count'] = 0
                            intent_obj['utterances'] = len(intent.utterances)
                            intent_obj['responses'] = len(intent.responses)
                            intent_obj['patterns'] = len(intent.patterns)
                            intent_obj['calls'] = intent.calls
                            intent_obj['modified'] = intent.modified
                            if not intent.is_folder:
                                intents_obj.append(intent_obj)
                            else:
                                empty_folders.append(intent_obj)
                    for folder in empty_folders:
                        if not (folder['name'] in folder_list):
                            intents_obj.append(folder)
                    app.logger.info('GET /api/intents/'+bot_guid+ ' successfully returned list of intents')
                    return jsonify({"intents":intents_obj})
                elif request.method == 'POST':
                    post_data = request.get_json()
                    if not post_data:
                        response_object = {
                            'status': 'fail',
                            'message': 'Invalid payload.'
                        }
                        return jsonify(response_object), 400
                    else:
                        if base == '/':
                            prefix = ''
                        else: 
                            prefix = '.'.join(base[1:].split('/'))+'.'
                        name = prefix + post_data.get('name')
                        utterances = post_data.get('utterances')
                        responses = post_data.get('responses')
                        has_entities = post_data.get('has_entities')
                        is_folder = post_data.get('is_folder')

                        
                        intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(name=name).filter_by(is_folder=is_folder).first()
                        if intent:
                            return jsonify({"success":False,"error":"An intent with that name already exists"})
                        else:
                            intent = Intent(
                                name = name,
                                bot_guid=bot_guid,
                                utterances=utterances,
                                has_entities=has_entities,
                                responses = responses,
                                is_folder=is_folder,
                                patterns=[]
                            )
                            db.session.add(intent)
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('POST /api/intents/'+bot_guid+ ' ' + str(e))
                            return jsonify({"success":False,"errors":str(e)})
                        app.logger.info('POST /api/intents/'+bot_guid+ ' successfully added intent')
                        return jsonify({"success":True})
                elif request.method == 'PUT':
                    put_data = request.get_json()
                    if not put_data:
                        response_object = {
                            'status': 'fail',
                            'message': 'Invalid payload.'
                        }
                        app.logger.warning('PUT /api/intents/'+bot_guid+ ' invalid put object')
                        return jsonify(response_object), 400
                    else:
                        old_name = put_data['old_name']
                        new_name = put_data['new_name']

                        if old_name[-1] == "%":
                            intents = Intent.query.filter_by(bot_guid=bot_guid).filter(Intent.name.like(old_name)).all()
                            for intent in intents:
                                name = intent.name
                                intent.name = name.replace(old_name[:-1],new_name)
                                intent.modified = datetime.datetime.utcnow()
                                try:
                                    db.session.commit()
                                except Exception as e:
                                    app.logger.error('PUT /api/intents/'+bot_guid+ ' ' + str(e))
                                    return jsonify({"success":False})
                            app.logger.info('PUT /api/intents/'+bot_guid+ ' successfully updated intent')
                            return jsonify({"success":True})
                        else:
                            intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(name=old_name).first()
                            if intent:
                                intent.name = new_name
                                intent.modified = datetime.datetime.utcnow()
                                try:
                                    db.session.commit()
                                except Exception as e:
                                    app.logger.error('PUT /api/intents/'+bot_guid+ ' ' + str(e))
                                    return jsonify({"success":False})
                                app.logger.info('GET /api/intents/'+bot_guid+ ' successfully updated intent')
                                return jsonify({"success":True})
                            else:
                                app.logger.warning('PUT /api/intents/'+bot_guid+ ' intent does not exist')
                                return jsonify({"success":False,"errors":"Intent doesn't exist"})
                elif request.method == 'DELETE':
                    intent_name = request.args['intent']
                    if intent_name[0] == '.':
                        intent_name = intent_name[1:]
                    if intent_name[-1] == '%':
                        try:
                            query = Intent.query.filter_by(bot_guid=bot_guid).filter(Intent.name.like(intent_name))
                            query.delete(synchronize_session=False)
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('DELETE /api/intents/'+bot_guid+ ' ' + str(e))
                            return jsonify({"success":False,"errors":str(e)})
                        app.logger.info('DELETE /api/intents/'+bot_guid+ ' successfully deleted intent')
                        return jsonify({"success":True})
                    else:
                        intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(name=intent_name).first()
                        if intent:
                            db.session.delete(intent)
                            try:
                                db.session.commit()
                            except Exception as e:
                                app.logger.error('DELETE /api/intents/'+bot_guid+ ' ' + str(e))
                                return jsonify({"success":False,"errors":str(e)})
                            app.logger.info('DELETE /api/intents/'+bot_guid+ ' successfully deleted intent')
                            return jsonify({"success":True})
                        else:
                            app.logger.warning('DELETE /api/intents/'+bot_guid+ ' intent does not exist')
                            return jsonify({"success":False,"errors":"intent doesn't exist"})
    elif code == 400:
        app.logger.warning('/api/intents/'+bot_guid+ ' invalid authorization token')
        return jsonify({"error":"Invalid Authorization Token"}),400
    elif code == 401:
        app.logger.warning('/api/intents/'+bot_guid+ ' no authorization token sent')
        return jsonify({"error":"No Authorization Token Sent"}),401
示例#16
0
def pattern(bot_guid, intent_name):
    code, user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        global interpreters
        nlus = interpreters
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(
                    name=intent_name).first()
                if intent:
                    model = bot.active_model
                    if model:
                        nlu = nlus[model]
                    else:
                        nlu = None
                    if request.method == 'GET':
                        patterns_obj = []
                        patterns = intent.patterns
                        for pattern in patterns:
                            pattern = json.loads(pattern)
                            pat_obj = {}
                            pat_obj["string"] = pattern['string']
                            pat_obj["regex"] = pattern['regex']
                            pat_obj["len"] = pattern['len']
                            pat_obj["entities"] = pattern['entities']
                            patterns_obj.append(pat_obj)
                        patterns_obj = sorted(patterns_obj,
                                              key=lambda k: k['len'],
                                              reverse=True)
                        app.logger.info(
                            'GET /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/patterns successfully returned patterns')
                        return jsonify({"patterns": patterns_obj})
                    if request.method == 'POST':
                        post_data = request.get_json()
                        new_pattern = post_data['value']

                        for pattern in intent.patterns:
                            pattern = json.loads(pattern)
                            if new_pattern == pattern["string"]:
                                return jsonify({"success": False})
                        parameters, regex = get_regex(new_pattern)
                        new_obj = {}
                        new_obj['string'] = new_pattern
                        new_obj['regex'] = regex
                        new_obj['entities'] = parameters
                        new_obj['len'] = len(new_pattern)
                        pats = [json.dumps(new_obj)] + intent.patterns
                        intent.patterns = [p for p in pats]
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('POST /api/intents/' + bot_guid +
                                             '/' + intent_name + '/patterns ' +
                                             str(e))
                            return jsonify({
                                "success": False,
                                "errors": str(e)
                            })
                        app.logger.info(
                            'POST /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/patterns successfully added patterns')
                        return jsonify({"pattern": new_obj})
                    elif request.method == 'PUT':
                        put_data = request.get_json()
                        old_pattern = put_data['old_pattern']
                        new_pattern = put_data['new_pattern']
                        parameters, regex = get_regex(new_pattern)

                        new_patterns = []

                        for pattern in intent.patterns:
                            pattern = json.loads(pattern)
                            if pattern["string"] == old_pattern:
                                pattern["string"] = new_pattern
                                pattern["regex"] = regex
                                pattern["entities"] = parameters
                                pattern["len"] = len(new_pattern)
                            new_patterns.append(json.dumps(pattern))
                        intent.patterns = new_patterns
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('PUT /api/intents/' + bot_guid +
                                             '/' + intent_name + '/patterns ' +
                                             str(e))
                            return jsonify({
                                "success": False,
                                "errors": str(e)
                            })
                        app.logger.info(
                            'GET /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/patterns successfully updated patterns')
                        return jsonify({"success": True})
                    elif request.method == 'DELETE':
                        old_pattern = request.args['pattern']
                        new_patterns = []
                        for pattern in intent.patterns:
                            pattern = json.loads(pattern)
                            if (pattern['string'] != old_pattern):
                                new_patterns.append(json.dumps(pattern))
                        intent.patterns = new_patterns
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('DELETE /api/intents/' +
                                             bot_guid + '/' + intent_name +
                                             '/patterns ' + str(e))
                            return jsonify({
                                "success": False,
                                "errors": str(e)
                            })
                        app.logger.info(
                            'DELETE /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/patterns successfully deleted patterns')
                        return jsonify({"success": True})
                else:
                    app.logger.warning('/api/intents/' + bot_guid + '/' +
                                       intent_name +
                                       '/patterns intent does not exist')
                    return jsonify({"error": "Intent Doesn't exist"}), 404
            else:
                app.logger.warning('/api/intents/' + bot_guid + '/' +
                                   intent_name + '/patterns not authorized')
                return jsonify({"error": "Not Authorized"}), 401
        else:
            app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                               '/patterns bot does not exist')
            return jsonify({"error": "Bot Doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                           '/patterns invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                           '/patterns no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401
示例#17
0
def intent(bot_guid, intent_name):
    try:
        code, user_id = checkAuth(request)
        # code = 200
        # user_id = 16
        if code == 200:
            global interpreters
            nlus = interpreters
            bot = Bot.query.filter_by(bot_guid=bot_guid).first()
            if bot:
                if bot.user_id == user_id:
                    intent = Intent.query.filter_by(
                        bot_guid=bot_guid).filter_by(name=intent_name).first()
                    if intent:
                        model = bot.active_model
                        if model:
                            nlu = nlus[model]
                        else:
                            nlu = None
                        intent.modified = datetime.datetime.utcnow()
                        if request.method == 'POST':
                            post_data = request.get_json()
                            new_utterance = post_data['value']

                            stop_words = [" a ", " an ", " the ", " is "]

                            utt_copy = new_utterance
                            for word in stop_words:
                                utt_copy = utt_copy.replace(word, " ")

                            utt_words = utt_copy.split(" ")

                            words_json = json.loads(bot.words)
                            if type(words_json) == str:
                                words_json = {}
                            for word in utt_words:
                                if word in words_json:
                                    if intent_name in words_json[word]:
                                        words_json[word][intent_name] += 1
                                    else:
                                        words_json[word][intent_name] = 1
                                else:
                                    words_json[word] = {intent_name: 1}

                            bot.words = json.dumps(words_json)
                            if new_utterance in intent.utterances:
                                app.logger.warning(
                                    'POST /api/intents/' + bot_guid + '/' +
                                    intent_name +
                                    '/utterances utterance already exists')
                                return jsonify({})
                            else:
                                utts = [new_utterance] + intent.utterances
                                intent.utterances = [u for u in utts]
                                if nlu:
                                    int, entities, confidence = nlu.parse(
                                        new_utterance)
                                else:
                                    entities = []
                                try:
                                    db.session.commit()
                                except Exception as e:
                                    app.logger.error('POST /api/intents/' +
                                                     bot_guid + '/' +
                                                     intent_name +
                                                     '/utterances ' + str(e))
                                    return jsonify({
                                        "success": False,
                                        "errors": str(e)
                                    })
                                app.logger.warning(
                                    'POST /api/intents/' + bot_guid + '/' +
                                    intent_name +
                                    '/utterances utterance successfully added')
                                return jsonify({
                                    "utterance": new_utterance,
                                    "entities": entities
                                })
                        elif request.method == 'PUT':
                            put_data = request.get_json()
                            old_utterance = put_data['old_utterance']
                            new_utterance = put_data['value']
                            intent.utterances = [
                                new_utterance if u == old_utterance else u
                                for u in intent.utterances
                            ]
                            intent.modified = datetime.datetime.utcnow()
                            int, entities, confidence = nlu.parse(
                                new_utterance)
                            try:
                                db.session.commit()
                            except Exception as e:
                                app.logger.error('PUT /api/intents/' +
                                                 bot_guid + '/' + intent_name +
                                                 '/utterances ' + str(e))
                                return jsonify({
                                    "success": False,
                                    "errors": str(e)
                                })
                            app.logger.info(
                                'PUT /api/intents/' + bot_guid + '/' +
                                intent_name +
                                '/utterances utterance successfully updated')
                            return jsonify({
                                "utterance": new_utterance,
                                "entities": entities
                            })
                        elif request.method == 'DELETE':
                            old_utterance = request.args['utterance']
                            new_utterances = []
                            for utterance in intent.utterances:
                                if (utterance != old_utterance):
                                    new_utterances.append(utterance)
                            intent.utterances = new_utterances
                            intent.modified = datetime.datetime.utcnow()
                            try:
                                db.session.commit()
                            except Exception as e:
                                app.logger.error('DELETE /api/intents/' +
                                                 bot_guid + '/' + intent_name +
                                                 '/utterances ' + str(e))
                                return jsonify({
                                    "success": False,
                                    "errors": str(e)
                                })
                            app.logger.info(
                                'DELETE /api/intents/' + bot_guid + '/' +
                                intent_name +
                                '/utterances utterance successfully deleted')
                            return jsonify({"success": True})
                    else:
                        app.logger.warning('/api/intents/' + bot_guid + '/' +
                                           intent_name +
                                           '/utterances intent does not exist')
                        return jsonify({"error": "Intent Doesn't exist"}), 404
        elif code == 400:
            app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                               '/utterances invalid authorization token')
            return jsonify({"error": "Invalid Authorization Token"}), 400
        elif code == 401:
            app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                               '/utterances no authorization token sent')
            return jsonify({"error": "No Authorization Token Sent"}), 401
    except Exception as e:
        app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                           '/utterances ' + str(e))
        return jsonify({'success': False, 'errors': str(e)}), 400
示例#18
0
def response(bot_guid, intent_name):
    code, user_id = checkAuth(request)
    # code = 200
    # user_id = 16
    if code == 200:
        global interpreters
        nlus = interpreters
        bot = Bot.query.filter_by(bot_guid=bot_guid).first()
        if bot:
            if bot.user_id == user_id:
                intent = Intent.query.filter_by(bot_guid=bot_guid).filter_by(
                    name=intent_name).first()
                if intent:
                    intent.modified = datetime.datetime.utcnow()
                    if request.method == 'POST':
                        post_data = request.get_json()
                        new_response = post_data['value']

                        if new_response in intent.responses:
                            return jsonify({})
                        else:
                            resps = [new_response] + intent.responses
                            intent.responses = [r for r in resps]
                            try:
                                db.session.commit()
                            except Exception as e:
                                app.logger.error('POST /api/intents/' +
                                                 bot_guid + '/' + intent_name +
                                                 '/responses ' + str(e))
                                return jsonify({
                                    "success": False,
                                    "errors": str(e)
                                })
                            app.logger.info(
                                'POST /api/intents/' + bot_guid + '/' +
                                intent_name +
                                '/responses successfully added response')
                            return jsonify({"success": True})
                    elif request.method == 'PUT':
                        put_data = request.get_json()
                        old_response = put_data['old_response']
                        new_response = put_data['value']
                        intent.responses = [
                            new_response if u == old_response else u
                            for u in intent.responses
                        ]
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('PUT /api/intents/' + bot_guid +
                                             '/' + intent_name +
                                             '/responses ' + str(e))
                            return jsonify({
                                "success": False,
                                "errors": str(e)
                            })
                        app.logger.info(
                            'PUT /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/responses successfully updated response')
                        return jsonify({"success": True})
                    elif request.method == 'DELETE':
                        old_response = request.args['response']
                        new_responses = []
                        for response in intent.responses:
                            if (response != old_response):
                                new_responses.append(response)
                        intent.responses = new_responses
                        try:
                            db.session.commit()
                        except Exception as e:
                            app.logger.error('DELETE /api/intents/' +
                                             bot_guid + '/' + intent_name +
                                             '/responses ' + str(e))
                            return jsonify({
                                "success": False,
                                "errors": str(e)
                            })
                        app.logger.info(
                            'DELETE /api/intents/' + bot_guid + '/' +
                            intent_name +
                            '/responses successfully deleted response')
                        return jsonify({"success": True})
                else:
                    app.logger.warning('/api/intents/' + bot_guid + '/' +
                                       intent_name +
                                       '/responses intent does not exist')
                    return jsonify({"error": "Intent Doesn't exist"}), 404
    elif code == 400:
        app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                           '/responses invalid authorization token')
        return jsonify({"error": "Invalid Authorization Token"}), 400
    elif code == 401:
        app.logger.warning('/api/intents/' + bot_guid + '/' + intent_name +
                           '/responses no authorization token sent')
        return jsonify({"error": "No Authorization Token Sent"}), 401