def load_chatbot(self, user_id): self.chatbot_instance_path = chatbot_instance_dir_path + '/' + str( user_id) self.chatbot_instance_path += '.cbinstance' self.chatbot = Chatbot() self.chatbot.load(self.chatbot_instance_path)
def __init__(self): # 提前加载各种模型 self.classify = Classify() self.recall = Recall() self.sort = DnnSort() self.chatbot = Chatbot() self.message_manager = MessageManager()
def start_chatbot(): print("Starting the chatbot") chatbot_resource_filename = "wechat_chatbot_resource.json" local_chatbot = Chatbot() local_chatbot.start_bot( chatbot_resource_filename, backend_read=False) # Turn off backend read cuz no SQL to read return local_chatbot
class Chatbot_rest(Resource): def __init__(self): self.chatbot_instance_path = '' pass def __del__(self): pass def get(self, user_id): user_id, query = user_id.split('_') print(query) self.load_chatbot(user_id) chatbot = self.chatbot print("receive : " + str(query)) result = chatbot.talk(query) print("response : " + str(result)) self.save_chatbot() return result def post(self, user_id): self.load_chatbot(user_id) msg_from_user = dict(request.get_json(force=True)) print("receive : " + str(msg_from_user)) msg = msg_from_user['message']['text'] msg_to_user = self.chatbot.talk(msg) msg_to_user['code'] = 200 msg_to_user['parameter'] = {} print("response : " + str(msg_to_user)) self.save_chatbot() return msg_to_user def load_chatbot(self, user_id): self.chatbot_instance_path = chatbot_instance_dir_path + '/' + str( user_id) self.chatbot_instance_path += '.cbinstance' self.chatbot = Chatbot() self.chatbot.load(self.chatbot_instance_path) def save_chatbot(self): self.chatbot.save(self.chatbot_instance_path)
def _load_model(self): model_path = os.path.join(DEEPQA_PATH, 'save') saved_models = os.listdir(model_path) saved_models = [ saved_model.replace("model-", "") for saved_model in saved_models if not saved_model.startswith(".") and os.path.isdir(os.path.join(model_path, saved_model)) ] self._logger.info('TensorFlow detected: v{}'.format(tf.__version__)) for saved_model in saved_models: self._logger.info("loading model {}".format(saved_model)) model = Chatbot() try: self._init_deep_qa_bot(model, args=[ "--modelTag", saved_model, "--keepAll", "--test", "daemon", "--rootDir", DEEPQA_PATH ]) self._deepqa_models.append(model) if self._max_length > model.args.maxLength: self._max_length = model.args.maxLength except Exception as e: self._logger.warning( "failed to load model {}".format(saved_model)) self._logger.error(e) self._logger.info("loaded model {}".format( [model.args.modelTag for model in self._deepqa_models]))
def run(self): for server_name in config.sections(): # Changing the log level to the level specified in the config file logger.setLevel( logging.getLevelName(config[server_name]["log_level"])) address = config[server_name]["address"] user = config[server_name]["username"] password = config[server_name]["password"] game_password = config[server_name]["game_password"] motd_scoreboard = str_to_bool( config[server_name]["motd_scoreboard"]) scoreboard_type = config[server_name]["scoreboard_type"] level_threshhold = config[server_name]["level_threshhold"] enable_greeter = str_to_bool(config[server_name]["enable_greeter"]) max_players = config[server_name]["max_players"] server = Server(server_name, address, user, password, game_password, max_players, level_threshhold) self.servers.append(server) if motd_scoreboard: motd_updater = MotdUpdater(server, scoreboard_type) motd_updater.start() self.motd_updaters.append(motd_updater) cb = Chatbot(server, greeter_enabled=enable_greeter) server.chat.add_listener(cb) self.chatbots.append(cb) print("Initialisation complete")
def get_chatbot(): lemmatizer = Lemmatizer() text_processor = TextProcessor( tokenizer, stemmer, joblib.load(config['vectorizer']), lemmatizer, Parser.get_instance(), tagged_words_corpus) phrase_extractor = PhraseExtractor(text_processor) svo_extractor = SvoExtractor(text_processor, phrase_extractor) sentence_scorer = SentenceScorer(text_processor, phrase_extractor) entity_extractor = EntityExtractor(text_processor) topic_classifier = TopicClassifierLocal(text_processor) ''' summarizer = Summarizer( text_processor, sentence_scorer ) ''' ''' summarizer = HttpSummarizer( text_processor, sentence_scorer, summarization_service) ''' summarizer = MultiProcessSummarizer( text_processor, sentence_scorer, SummarizationTask, Consumer) data_manager = DataManager( text_processor, neo4j_service, wikipedia_service, Parser.get_instance(), svo_extractor, summarizer) chatbot = Chatbot( text_processor, topic_classifier, data_manager, entity_extractor) return chatbot
def initBot(): """ Instantiate the chatbot for later use Should be called only once """ if not ChatbotManager.bot: logger.info('Initializing bot...') ChatbotManager.bot = Chatbot() #chatbot.Chatbot() # need to find the chatbotPath for model and data ChatbotManager.bot.main( ['--modelTag', 'server', '--test', 'daemon', '--rootDir', '']) else: logger.info('Bot already initialized.')
class ChatServicer(chatbot_pb2_grpc.ChatBotServiceServicer): def __init__(self): # 提前加载各种模型 self.classify = Classify() self.recall = Recall() self.sort = DnnSort() self.chatbot = Chatbot() self.message_manager = MessageManager() def Chatbot(self, request, context): user_id = request.user_id message = request.user_message create_time = request.create_time attention, prob = self.classify.predict(message) if attention == "QA": # 实现对对话数据的保存 self.message_manager.user_message_pipeline( user_id, message, create_time, attention, entity=message["entity"]) recall_list, entity = self.recall.predict(message) user_response = self.sort.predict(message, recall_list) else: # 实现对对话数据的保存 self.message_manager.user_message_pipeline( user_id, message, create_time, attention, entity=message["entity"]) user_response = self.chatbot.predict(message) self.message_manager.bot_message_pipeline(user_id, user_response) create_time = int(time.time()) return chatbot_pb2.ResponsedMessage(user_response=user_response, create_time=create_time)
def run(self): for server_name in settings.sections(): server = Server(server_name, settings.setting(server_name, "address"), settings.setting(server_name, "username"), settings.setting(server_name, "password")) server.game_password = \ settings.setting(server_name, "game_password") server.url_extras = \ settings.setting(server_name, "url_extras") dosh_threshold = int( settings.setting(server_name, "dosh_threshold")) if dosh_threshold > 0: server.dosh_threshold = dosh_threshold has_motd_scoreboard = str_to_bool( settings.setting(server_name, "motd_scoreboard")) if has_motd_scoreboard: scoreboard_type = settings.setting(server_name, "scoreboard_type") MotdUpdater(server, scoreboard_type).start() self.servers.append(server) Chatbot( server, str_to_bool(settings.setting(server_name, "enable_greeter")), settings.setting(server_name, "username")) info("Initialisation complete!\n") while True: command = input() for server in self.servers: server.web_admin.chat.submit_message(command)
def make_chatbot(self, name, server): chatbot = Chatbot( server.web_admin.chat, server_name=server.name, name=name ) scheduler = CommandScheduler(server, chatbot) commands = CommandMap().get_commands( server, chatbot, scheduler, MotdUpdater(server) ) for name, command in commands.items(): chatbot.add_command(name, command) server.web_admin.chat.add_listener(chatbot) server.web_admin.chat.add_listener(scheduler) self.stop_list.append(scheduler) chatbot.run_init() return chatbot
def init_chatbot(): bot = Chatbot() bot.start() return bot
#!/usr/bin/env python3 import os import time import pyperclip import urllib.request print("Iniciando chatbot..") from chatbot.chatbot import Chatbot chatbot = Chatbot() chatbot.main([ "--rootDir", ".", "--test", "daemon", "--modelTag", "spanish", "--keepAll" ]) from vision import look from ui import say recent_value = "" while True: tmp_value = pyperclip.paste() if tmp_value != recent_value: recent_value = tmp_value if "http" in recent_value: if "png" in recent_value or \ "jpg" in recent_value or \ "jpeg" in recent_value: print("Link: %s" % str(recent_value)) filename = None
from flask import Flask, render_template from flask import request from chatbot.chatbot import Chatbot bot = Chatbot() app = Flask(__name__) @app.route("/") def home(): return render_template("index.html") @app.route('/ask', methods=['GET', 'POST']) def ask(): question = request.form["question"] answer = bot.answer(question) return question + "</>" + answer if __name__ == "__main__": app.run()
from chatbot.chatbot import Chatbot bot = Chatbot() while True: print("Response: ", bot.answer(input()))
ws.fit(line.strip().split()) ws.build_vocab() print(len(ws)) pickle.dump(ws, open(config.chatbot_ws_by_word_input_path, "wb")) ws = Word_sequence() for line in open(config.chatbot_target_path, "r", encoding="utf-8").readlines(): ws.fit(line.strip().split()) ws.build_vocab() print(len(ws)) pickle.dump(ws, open(config.chatbot_ws_by_word_target_path, "wb")) def train_seq2seq(): for i in range(10): train(i) if __name__ == '__main__': # save_ws() # for idx,(input,target,input_length,target_length) in enumerate(dataloader): # print(input) # print(target) # print(input_length) # print(target_length) # break # train_seq2seq() chatbot = Chatbot() while True: print(chatbot.predict(input("请输入:")))
from chatbot.chatbot import Chatbot bot = Chatbot() bot.train("./conversation_data/questions.txt") bot.answer("Cache")