0

Im building a chatbot. How to stop it from being trained everytime i run the program?

My program :

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Adithyan AK')

trainer = ChatterBotCorpusTrainer(chatbot)

trainer.train("chatterbot.corpus.english")

trainer.train("chatterbot.corpus.english.greetings")

trainer.train("chatterbot.corpus.english.conversations")

while(True):

    query = input("You : ")
    response = chatbot.get_response(query)

    print(response)

Im getting the following Log message everytime I run the above program.

[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /root/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-
[nltk_data]       date!
/usr/local/lib/python3.7/dist-packages/chatterbot/corpus.py:38: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  return yaml.load(data_file)
Training ai.yml: [####################] 100%
Training botprofile.yml: [####################] 100%
Training computers.yml: [####################] 100%
Training conversations.yml: [####################] 100%
Training emotion.yml: [####################] 100%
Training food.yml: [####################] 100%
Training gossip.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training health.yml: [####################] 100%
Training history.yml: [####################] 100%
Training humor.yml: [####################] 100%
Training literature.yml: [####################] 100%
Training money.yml: [####################] 100%
Training movies.yml: [####################] 100%
Training politics.yml: [####################] 100%
Training psychology.yml: [####################] 100%
Training science.yml: [####################] 100%
Training sports.yml: [####################] 100%
Training trivia.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training conversations.yml: [####################] 100%
  1. I can see that the nltk_data & conversations are being downloaded everytime I run the program. It takes 5-10 seconds to download and ask for user input. I understand the importance of keeping it up to data. But I dont want it to update everytime unless I wish to do manually. Is there anyway I can stop downloading the nltk_data and conversations.yml file everytime? (because my main program can't wait for 5-10 seconds for the chatbot to load)

  2. Is there anyway I can stop printing these logs on console?

Adithyan AK
  • 1
  • 1
  • 1
  • This may help you, https://stackoverflow.com/questions/60213258/how-to-save-chatbot-model-using-pickle/. Also you may try running first time with code if used, `nltk.download('punkt')` to download necessary files and comment it out for all other subsequent runs. – B200011011 Feb 13 '20 at 21:40

3 Answers3

0

easy, train and pickle the bots first. In your main program unpickle the trained bots, suppress logging, and away you go.

Nick
  • 97
  • 10
0

I had similar issues when i try to add a chatbot to my test website. My solution may not what you are seeking, but i put here.

  • I first created a bot.py file that include do the bot training, then import it to view.py which deals with page redirects. This causes retraining each time.

  • Then i realize what i did wrong is that i import this file into my view.py and that makes this module retraining. Instead, i create a new bot and specify to same SQL and it worked.

here are some codes.

train_bot/bot.py (literally taken from chatterbot documentation.)

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

    
# Create a new instance of a ChatBot
bot = ChatBot(
    'train',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': 'I am sorry, but I do not understand.',
            'maximum_similarity_threshold': 0.90
        }
    ],
    database_uri='sqlite:///database-chatbot.db'
)

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(bot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")

print('Type something to begin...')


if __name__ =='__main__':
    # The following loop will execute each time the user enters input
    while True:
        try:
            user_input = input()

            bot_response = bot.get_response(user_input)

            print(bot_response)

        # Press ctrl-c or ctrl-d on the keyboard to exit
        except (KeyboardInterrupt, EOFError, SystemExit):
            break

view.py

alice = ChatBot(
        'Alice',
        storage_adapter='chatterbot.storage.SQLStorageAdapter',
        logic_adapters=[
            {
                'import_path': 'chatterbot.logic.BestMatch',
                'default_response': 'I am sorry, but I do not understand.',
                'maximum_similarity_threshold': 0.90
            }
        ],
        database_uri='sqlite:///train_bot/database-chatbot.db' #this is the same URI for the previously trained bot
    )

now it worked as expected.

Argos.LEE
  • 139
  • 2
  • 6
0

A bit late now, but a quick look at the code showed me that trainer takes a kwarg show_training_progress which you can set to False to stop logs from printing

Like so:

trainer = ChatterBotCorpusTrainer(bot, show_training_progress=False)
Judev1
  • 434
  • 3
  • 11