Beispiel #1
0
def get_model_response():
    params = request.get_json()
    _logger.info('request params: %s' % params)

    try:
        dialog_context = parse_dataset_param(params, param_name='context')
    except KeyError as e:
        return get_api_error_response(
            'Malformed request, no "%s" param was found' % e.message, 400,
            _logger)
    except ValueError as e:
        return get_api_error_response('Malformed request: %s' % e.message, 400,
                                      _logger)

    emotion = params.get('emotion', DEFAULT_CONDITION)
    if emotion not in EMOTIONS_TYPES:
        return get_api_error_response(
            'Malformed request, emotion param "%s" is not in emotion list %s' %
            (emotion, list(EMOTIONS_TYPES)), 400, _logger)

    response = get_response(dialog_context, emotion)

    if not response:
        _logger.error('No response for context: %s; emotion "%s"' %
                      (dialog_context, emotion))
        return jsonify({}), 200

    _logger.info('Given response: "%s" for context: %s; emotion "%s"' %
                 (response, dialog_context, emotion))

    return jsonify({'response': response}), 200
Beispiel #2
0
def get_model_response():
    params = request.get_json()
    _logger.info('request params: %s' % params)

    try:
        dialog_context = parse_dataset_param(params, param_name='context')
    except KeyError as e:
        return get_api_error_response('Malformed request, no "%s" param was found' % e.message, 400, _logger)
    except ValueError as e:
        return get_api_error_response('Malformed request: %s' % e.message, 400, _logger)

    emotion = params.get('emotion', DEFAULT_CONDITION)
    if emotion not in EMOTIONS_TYPES:
        return get_api_error_response('Malformed request, emotion param "%s" is not in emotion list %s' %
                                      (emotion, list(EMOTIONS_TYPES)), 400, _logger)

    response = get_response(dialog_context, emotion)

    if not response:
        _logger.error('No response for context: %s; emotion "%s"' % (dialog_context, emotion))
        return jsonify({}), 200

    _logger.info('Given response: "%s" for context: %s; emotion "%s"' % (response, dialog_context, emotion))

    return jsonify({'response': response}), 200
Beispiel #3
0
def get_model_response():
    params = request.get_json()
    _logger.info('request params: {}'.format(params))

    try:
        dialog_context = parse_dataset_param(params, param_name='context')
    except KeyError as e:
        return get_api_error_response(
            'Malformed request, no "{}" param was found'.format(e), 400,
            _logger)
    except ValueError as e:
        return get_api_error_response('Malformed request: {}'.format(e), 400,
                                      _logger)

    emotion = params.get('emotion', DEFAULT_CONDITION)
    if emotion not in EMOTIONS_TYPES:
        return get_api_error_response(
            'Malformed request, emotion param "{}" is not in emotion list {}'.
            format(emotion, list(EMOTIONS_TYPES)), 400, _logger)

    response = get_response(dialog_context, emotion)
    _logger.info('Given response: "{}" for context: {}; emotion "{}"'.format(
        response, dialog_context, emotion))

    return jsonify({'response': response}), 200
Beispiel #4
0
async def on_message(message):
    if (message.author.bot == True):
        return
    if message.content.startswith('-'):
        context.append(str(message.content)[1:].strip())
        response = get_response(context, DEFAULT_CONDITION)
        context.append(response)
        await client.send_typing(message.channel)
        await asyncio.sleep(1)
        await client.send_message(message.channel, response)
Beispiel #5
0
async def on_message(message):
    if not (message.author == client.user
            ):  #Check to ensure the bot does not respond to its own messages
        if (client.user.mentioned_in(message)
                or isinstance(message.channel, discord.abc.PrivateChannel)
            ):  #Check if the bot is mentioned or if the message is in DMs
            async with message.channel.typing():  #Show that the bot is typing
                txtinput = message.content.replace(
                    "<@" + str(client.user.id) + ">", "").replace(
                        "<@!" + str(client.user.id) + ">", ""
                    )  #Filter out the mention so the bot does not get confused
                if (len(txtinput) > 220):  #Spam protection
                    txt = "I am sorry, that is too long for me."
                dicestr = re.search("Roll (\d{1,2})d(\d{1,3})",
                                    message.content)
                if (dicestr != None):
                    dice = [dicestr.group(1), dicestr.group(2)]
                    output = "I rolled "
                    for i in range(int(dice[0])):
                        output += str(random.randrange(1, int(dice[1]))) + ", "
                    txt = output
                else:
                    blob = TextBlob(txtinput)
                    #lang = blob.detect_language()
                    lang = "en"
                    if (lang != "en"):
                        txtinput = str(blob.translate(from_lang=lang, to="en"))
                    _context.append(txtinput)
                    COND = await es.new_sentiment_analysis(
                        txtinput
                    )  #Determine how to respond to the sentence emotionally
                    await client.change_presence(
                        activity=discord.Game(name='feeling ' + COND)
                    )  #Display the bot's emotion as a status
                    txt = get_response(_context, COND)  #Get a response!
                    response_blob = TextBlob(txt)
                    if (lang != "en"):
                        txt = str(
                            response_blob.translate(from_lang="en", to=lang))
                bot_message = await message.channel.send(txt)  #Fire away!
Beispiel #6
0
def get_model_response():
    params = request.get_json()
    _logger.info('request params: %s' % params)

    try:
        dialog_context = parse_dataset_param(params, param_name='context')
    except KeyError as e:
        return get_api_error_response(
            'Malformed request, no "%s" param was found' % str(e), 400,
            _logger)
    except ValueError as e:
        return get_api_error_response('Malformed request: %s' % str(e), 400,
                                      _logger)

    emotion = params.get('emotion', DEFAULT_CONDITION)
    custom_chatbot = chatbots.get(emotion)

    if emotion not in EMOTIONS_TYPES and not custom_chatbot:
        return get_api_error_response(
            'Malformed request, emotion param "%s" is not in emotion list %s' %
            (emotion, list(EMOTIONS_TYPES)), 400, _logger)
    if custom_chatbot:
        try:
            response = custom_chatbot.make_prediction(dialog_context[-1])[0]
        except IndexError:
            response = "I'm not sure."
    else:
        response = get_response(dialog_context, emotion)

    if not response:
        _logger.error('No response for context: %s; emotion "%s"' %
                      (dialog_context, emotion))
        return jsonify({}), 200

    _logger.info('Given response: "%s" for context: %s; emotion "%s"' %
                 (response, dialog_context, emotion))

    return jsonify({'response': response}), 200
 def handle_text_message(self, msg_text, msg):
     self._context.append(msg_text.strip())
     response = get_response(self._context, DEFAULT_CONDITION)
     self._context.append(response)
     self._send_text(response)
Beispiel #8
0
 def handle_text_message(self, msg_text, msg):
     self._context.append(msg_text.strip())
     response = get_response(self._context, DEFAULT_CONDITION)
     self._context.append(response)
     self._send_text(response)