Esempio n. 1
0
def _upload_character():
    auth = request.form.get('Auth')
    if not auth or not check_auth(auth):
        return authenticate()
    try:
        user = request.form.get('user')
        zipfile = request.files.get('zipfile')
        lang = request.files.get('lang')

        saved_dir = os.path.join(INCOMING_DIR, user)
        if not os.path.isdir(saved_dir):
            os.makedirs(saved_dir)

        # Clean the incoming directory
        for f in os.listdir(saved_dir):
            f = os.path.join(saved_dir, f)
            if os.path.isfile(f):
                os.unlink(f)
            else:
                shutil.rmtree(f, True)

        saved_zipfile = os.path.join(saved_dir, zipfile.filename)
        zipfile.save(saved_zipfile)
        logger.info("Get zip file {}".format(zipfile.filename))

        from loader import AIMLCharacterZipLoader
        characters = AIMLCharacterZipLoader.load(
            saved_zipfile, saved_dir, 'upload')
        ret, response = True, "Done"
        for character in characters:
            character.local = False
            character.id = '{}/{}'.format(user, character.id)
            _ret, _response = add_character(character)
            if not _ret:
                ret = _ret
                response = _response
        os.remove(saved_zipfile)

        return Response(json_encode({
            'ret': ret,
            'response': response
        }),
            mimetype="application/json")

    except Exception as ex:
        return Response(json_encode({
            'ret': False,
            'response': str(ex)
        }),
            mimetype="application/json")
Esempio n. 2
0
def _upload_character():
    auth = request.form.get('Auth')
    if not auth or not check_auth(auth):
        return authenticate()
    try:
        user = request.form.get('user')
        zipfile = request.files.get('zipfile')
        lang = request.files.get('lang')

        saved_dir = os.path.join(INCOMING_DIR, user)
        if not os.path.isdir(saved_dir):
            os.makedirs(saved_dir)

        # Clean the incoming directory
        for f in os.listdir(saved_dir):
            f = os.path.join(saved_dir, f)
            if os.path.isfile(f):
                os.unlink(f)
            else:
                shutil.rmtree(f, True)

        saved_zipfile = os.path.join(saved_dir, zipfile.filename)
        zipfile.save(saved_zipfile)
        logger.info("Get zip file {}".format(zipfile.filename))

        from loader import AIMLCharacterZipLoader
        characters = AIMLCharacterZipLoader.load(saved_zipfile, saved_dir,
                                                 'upload')
        ret, response = True, "Done"
        for character in characters:
            character.local = False
            character.id = '{}/{}'.format(user, character.id)
            _ret, _response = add_character(character)
            if not _ret:
                ret = _ret
                response = _response
        os.remove(saved_zipfile)

        return Response(json_encode({
            'ret': ret,
            'response': response
        }),
                        mimetype="application/json")

    except Exception as ex:
        return Response(json_encode({
            'ret': False,
            'response': str(ex)
        }),
                        mimetype="application/json")
Esempio n. 3
0
def _batch_chat():
    auth = request.form.get('Auth')
    if not auth or not check_auth(auth):
        return authenticate()

    questions = request.form.get('questions')
    questions = json.loads(questions)
    session = request.form.get('session')
    lang = request.form.get('lang', 'en-US')
    responses = []
    for idx, question in questions:
        response, ret = ask(str(question), lang, session)
        responses.append((idx, response, ret))
    return Response(json_encode({'ret': 0, 'response': responses}),
                    mimetype="application/json")
Esempio n. 4
0
def _batch_chat():
    auth = request.form.get('Auth')
    if not auth or not check_auth(auth):
        return authenticate()

    questions = request.form.get('questions')
    questions = json.loads(questions)
    session = request.form.get('session')
    lang = request.form.get('lang', 'en')
    responses = []
    for idx, question in questions:
        response, ret = ask(str(question), lang, session)
        responses.append((idx, response, ret))
    return Response(json_encode({'ret': 0, 'response': responses}),
                    mimetype="application/json")