【问题标题】:how to save chatbot model using pickle如何使用pickle保存聊天机器人模型
【发布时间】:2020-02-13 17:14:11
【问题描述】:

我使用 chatterbottkinter 库创建了一个 Chatbot。 但是每当我打开文件时,它就会启动 training model 并花费大量时间,因此我搜索并找到了 pickle 模块。 但现在我也尝试了 pickle 它不是工作并显示错误。

有什么方法可以保存不会每次都开始训练的模型。这是我的代码

import chatterbot
import pickle
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
import tkinter as tk
try:
    import ttk as ttk
    import ScrolledText
except ImportError:
    import tkinter.ttk as ttk
    import tkinter.scrolledtext as ScrolledText
import time


class TkinterGUIExample(tk.Tk):

    def __init__(self, *args, **kwargs):
        """
        Create & set window variables.
        """
        tk.Tk.__init__(self, *args, **kwargs)

        self.chatbot = ChatBot(
            "GUI Bot",

            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.80
} ]



        )



        for files in os.listdir('C:/Users/HP/Desktop/FYP BOT/training_data/'):
            con=open('C:/Users/HP/Desktop/FYP BOT/training_data/'+files,'r').readlines()
            trainer = ListTrainer(self.chatbot)
            trainer.train(con)
        self.title("Chatterbot")

        self.initialize()

    def initialize(self):
        """
        Set window layout.
        """
        self.grid()

        ttk.Style().configure("TButton", padding=6, relief="flat",background="#ccc")
        style = ttk.Style()
        style.map("C.TButton",
            foreground=[('pressed', 'red'), ('active', 'blue')],
            background=[('pressed', '!disabled', 'black'), ('active', 'white')]
            )


        self.respond = ttk.Button(self, text='Get Response',cursor='hand2' ,command=self.get_response)
        self.respond.grid(column=1, row=2, sticky='nesw', padx=3, pady=10)




        self.usr_input = tk.Entry(self, state='normal',text='Enter your query here!')
        self.usr_input.grid(column=0, row=2, sticky='nesw', padx=1, pady=5)

        #Binding entry
        self.usr_input.bind('<Return>',self.get_response)


        self.conversation_lbl = tk.Label(self,
                                         text='English',
                                         anchor='center',
                                         font=('Arial Bold ',18),
                                         bg="#3a8fc5",
                                         fg="white")
        self.conversation_lbl.grid(column=0, row=0,columnspan=2, padx=3, pady=3,sticky='news')
        self.conversation = ScrolledText.ScrolledText(self,
                                                      state='disabled',borderwidth=5,
                                                      highlightthickness=1,
                                                      bg='#15202b',fg='#16202A',
                                                      font=('Arial Bold',8))

        self.conversation.grid(column=0, row=1, columnspan=2, sticky='nesw', padx=3, pady=3)


    def get_response(self,*args):
        """
        Get a response from the chatbot and display it.
        """
        user_input = self.usr_input.get()
        self.usr_input.delete(0, tk.END)

        response = self.chatbot.get_response(user_input)

        self.conversation['state'] = 'normal'
        '''----------------------------------------------
        self.conversation.tag_configure('tag-left', justify='left')
        self.conversation.insert('end',"Human: " + user_input + "\n", 'tag-left')

        self.conversation.tag_configure('tag-left', justify='right')
        self.conversation.insert('end',"ChatBot: " + str(response.text) + "\n\n\n", 'tag-right')'''

        label1 = tk.Label(self.conversation, 
                          text="Human: \n"+user_input, 
                          background='#3B5566',
                          fg='white',
                          font=("Helvetica", 12),
                          justify='left',
                          wraplength=300,
                          anchor='w',
                          padx=10, pady=5)
        label2 = tk.Label(self.conversation, 
                          text="ChatBot: \n"+str(response.text),
                          wraplength=300,
                          anchor='w',
                          background='#1D9DFC', 
                          fg='white',
                          font=("Helvetica", 12),
                          justify='left',
                          padx=10, pady=5)

        self.conversation.tag_configure('tag-left', justify='left')
        self.conversation.tag_configure('tag-right', justify='right')


        self.conversation.insert('end', '\n\n\n')
        self.conversation.window_create('end', window=label1)

        self.conversation.insert('end', '\n\n\n ', 'tag-right') # space to move Label to the right 
        self.conversation.window_create('end', window=label2)

        '''self.conversation.insert(
            tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n\n\n"
        )'''
        self.conversation['state'] = 'disabled'

        time.sleep(0.2)

gui_example = TkinterGUIExample()
gui_example.attributes('-topmost', True)
gui_example.update()
gui_example.attributes('-topmost', False)
gui_example.geometry('810x550+460+100')
gui_example.resizable(0, 0)
gui_example.configure(background='#3a8fc5')
gui_example.mainloop()

我还创建了 .exi 文件,但它也开始了训练,所以有什么方法可以在没有错误的情况下保存它,当我在主窗口中调用这个脚本时,这个脚本开始工作而不是训练等。

【问题讨论】:

    标签: python machine-learning pickle


    【解决方案1】:

    你可以试试这个。它对我有用。当代码第一次运行时,trainer = ChatterBotCorpusTrainer(bot)trainer.train("static/chatterbot_data.yml") 用于训练机器人。它将在项目文件夹中生成一个文件database.db。生成数据库文件后,如果语料库没有更多变化,则在下面注释最后两行代码,无需重新训练即可运行。

    代码:

    ## initialize chatter bot
    bot = ChatBot(
        'robot',
        storage_adapter='chatterbot.storage.SQLStorageAdapter',
        preprocessors=[
            'chatterbot.preprocessors.clean_whitespace',
        ],
        logic_adapters=[
            {
                'import_path': 'chatterbot.logic.BestMatch',
                'default_response': 'I am sorry, but I do not understand.',
                'maximum_similarity_threshold': 0.90,
                'statement_comparison_function': chatterbot.comparisons.levenshtein_distance,
                'response_selection_method': chatterbot.response_selection.get_first_response
            },
            'chatterbot.logic.MathematicalEvaluation'
        ],
        database_uri='sqlite:///database.db',
        read_only=True
    )
    
    
    ## training corpus list
    ## Disable these two lines below AFTER first run when a *.db file is generated in project directory
    trainer = ChatterBotCorpusTrainer(bot)
    trainer.train("static/chatterbot_data.yml")
    

    【讨论】:

    • 非常感谢,亲爱的,它也对我有用。还有一件事是有什么方法可以将对话保存在一个文本文件中,而将未回答的问题保存在第二个文件中,以便我以后可以提供他们的答案。
    • 我在这里为您的问题提供了答案,stackoverflow.com/questions/60024786/…
    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 2019-07-19
    • 2021-03-17
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多