ChatterBot icon indicating copy to clipboard operation
ChatterBot copied to clipboard

Unable to get a response from the chatbot - Help Appreciated

Open rahulnad opened this issue 6 years ago • 4 comments

I am unable to get a response back from my chatbot. The chatbot takes in inputs but unable to give a response back. Appreciate any help or insights as to how to make it work.

from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
from chatterbot.logic import LogicAdapter
app = Flask(__name__)

# Creating an instance of the chatbot 

chatbot = ChatBot("Amigo")
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.english")

bot = ChatBot(
    'Exact Response Example Bot',
    #storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch'
        },
        {
            'import_path': 'chatterbot.logic.SpecificResponseAdapter',
            'input_text': 'Help me!',
            'output_text': 'Ok, here is a link'
        }
    ]
)  

@app.route("/")
def home():    
    return render_template("home.html") 

@app.route("/get")
def get_bot_response():
    while True:
     try:
        user_input = input()
        bot_response = bot.get_default_response(user_input)
    # Press ctrl-c or ctrl-d on the keyboard to exit
     except (KeyboardInterrupt, EOFError, SystemExit):
        break
    return str(bot_response)

if __name__ == "__main__":    
    app.run()

rahulnad avatar Sep 11 '19 03:09 rahulnad

I think the while loop didn't end because you are trying to get method browser. could you try like this?

@app.route("/get")
def get_bot_response():
    user_input = input()
    bot_response = bot.get_default_response(user_input)
    return str(bot_response)

vkosuri avatar Sep 11 '19 09:09 vkosuri

@vkosuri - Thank you for your message. I have been trying to include custom input statements and wrote something like this. But my bot does not generate a response.

First py file app.py

#import files
from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
from chatterbot.logic import LogicAdapter
from chatterbot.adapters import Adapter
import logging

app = Flask(__name__)

logging.basicConfig(level=logging.INFO)

# Creating an instance of the chatbot 

chatbot = ChatBot("Amigo")
#english_bot = ChatBot("Chatterbot", storage_adapter="chatterbot.storage.SQLStorageAdapter")
#trainer = ChatterBotCorpusTrainer(english_bot)
#trainer.train("chatterbot.corpus.english")
logical_bot = ChatBot(
    'Chatterbot',
    logic_adapters=[
        {
            'import_path': 'response.MyLogicAdapter'
        }
    ]
)

@app.route("/")
def home():    
    return render_template("home.html") 

@app.route("/get")

def get_bot_response():
    userText = request.args.get('msg')
    return str(logical_bot.get_response(userText))

if __name__ == "__main__":    
    app.run()`

Second py file response.py containing my custom logical adapters

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
from chatterbot.logic import LogicAdapter
from chatterbot.adapters import Adapter

class MyLogicAdapter(LogicAdapter):

 def __init__(self, chatbot, **kwargs):
     super().__init__(chatbot, **kwargs)

     def can_process(self, statement):
      if statement.text.startswith('Hey Mike'):
        return str('Hi')
      else:
        return False

     def process(self, input_statement, additional_response_selection_parameters):
        import random

        # Randomly select a confidence between 0 and 1
        confidence = random.uniform(0, 1)

        # For this example, we will just return the input as output
        selected_statement = input_statement
        selected_statement.confidence = confidence

        return selected_statement

How do i tweak this to get a response from my bot. Thank you for any inputs.

rahulnad avatar Sep 12 '19 04:09 rahulnad

Have you tried flask chatterbot https://github.com/chamkank/flask-chatterbot

vkosuri avatar Sep 12 '19 11:09 vkosuri

Yes i have tried that. The bot sends random response

rahulnad avatar Sep 13 '19 03:09 rahulnad