def bot_ui(): corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', 'Result') with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, result_file='basic') # This command UI has a single chat session only session_id = predictor.session_data.add_session() print("Welcome to Chat with ChatLearner!") print("Type exit and press enter to end the conversation.") # Waiting from standard input. sys.stdout.write("> ") sys.stdout.flush() question = sys.stdin.readline() while question: if question.strip() == 'exit': print("Thank you for using ChatLearner. Goodbye.") break print(predictor.predict(session_id, question)) print("> ", end="") sys.stdout.flush() question = sys.stdin.readline()
def test_demo(): print("# Creating TF session ...") corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', 'Result') test_dir = os.path.join(PROJECT_ROOT, 'Data', 'Test') in_file = os.path.join(test_dir, 'samples.txt') out_file = os.path.join(test_dir, 'responses.txt') with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, result_file='basic') session_id = predictor.session_data.add_session() print("# Prediction started ...") t0 = time.time() with open(in_file, 'r') as f_in: with open(out_file, 'a') as f_out: f_out.write(get_header()) for line in f_in: sentence = line.strip() if not sentence or sentence.startswith("#=="): continue f_out.write("> {}\n".format(sentence)) f_out.write("{}\n\n".format(predictor.predict(session_id, sentence))) t1 = time.time() print("# Prediction completed. Time spent on prediction: {:4.2f} seconds".format(t1-t0))
def bot_ui(resultfolder): corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', resultfolder) with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, hparams_dir=res_dir) print("Welcome to Chat with ChatLearner!") print("Type exit and press enter to end the conversation.") # Waiting from standard input. sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() while sentence: if not sentence.strip(): continue if sentence.strip() == 'exit': print("Thank you for using ChatLearner. Goodbye.") break print(predictor.predict(sentence)) print("> ", end="") sys.stdout.flush() sentence = sys.stdin.readline()
def bot_ui(): corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', 'Result') with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, result_file='basic') # This command UI has a single chat session only session_id = predictor.session_data.add_session() print("Type exit and press enter to end the conversation.") # Waiting from standard input. sys.stdout.write( "Say something to be recognized by the google cloud API voices\n") sys.stdout.write("> ") sys.stdout.flush() # question = recognize_voice() question = sys.stdin.readline() while question: if question.strip() == 'exit': break # import pdb # pdb.set_trace() result = re.sub(r'_nl_|_np_', '\n', predictor.predict(session_id, question)).strip() print(result) text_to_speech_recognizer(result) # print(re.sub(r'_nl_|_np_', '\n', predictor.predict(session_id, question)).strip()) print("> ", end="") sys.stdout.flush() question = sys.stdin.readline()
def bot_ui(): corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', 'Result') with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, result_file='basic') # This command UI has a single chat session only session_id = predictor.session_data.add_session() # Waiting from standard input. question = ''.join(sys.argv[1:]) #print(question)#, file=sys.stdout) #print("\n") print( re.sub(r'_nl_|_np_', ' ', predictor.predict(session_id, question)).strip())
def main(): corp_dir = os.path.join(PROJECT_ROOT, 'Data', 'Corpus') knbs_dir = os.path.join(PROJECT_ROOT, 'Data', 'KnowledgeBase') res_dir = os.path.join(PROJECT_ROOT, 'Data', 'Result') with tf.Session() as sess: predictor = BotPredictor(sess, corpus_dir=corp_dir, knbase_dir=knbs_dir, result_dir=res_dir, result_file='basic-32334') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = '127.0.0.1' port = int(2000) sock.bind((host, port)) sock.listen(1) print("chatServer Start...\n") while True: connection, client_addr = sock.accept() # print(connection, client_addr) data = connection.recv(1024) data = data.decode("utf-8") print("data > " + data) # This command UI has a single chat session only session_id = predictor.session_data.add_session() question = data if question.strip() == 'exit': print("Thank you for using HeroBot. Goodbye.") break answer = predictor.predict(session_id, question) print("answ > " + answer) connection.sendall(answer.encode("utf-8")) connection.close() sock.close()