-
Notifications
You must be signed in to change notification settings - Fork 5
/
chatbot_serving.py
124 lines (105 loc) · 4.78 KB
/
chatbot_serving.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 19:34:01 2020
@author: danis
"""
import datetime
from os import path
import general_utils
import chat_command_handler
from chat_settings import ChatSettings
from vocabulary import Vocabulary
from services import weather_time
from chatbot_model import ChatbotModel
"""'models/cornell_movie_dialog/trained_model_v2/best_weights_training.ckpt'"""
def model_loading():
_, model_dir, hparams, checkpoint, _, _ = general_utils.initialize_session("chat")
#Load the vocabulary
print()
print("Loading vocabulary...")
if hparams.model_hparams.share_embedding:
shared_vocab_filepath = path.join(model_dir, Vocabulary.SHARED_VOCAB_FILENAME)
input_vocabulary = Vocabulary.load(shared_vocab_filepath)
output_vocabulary = input_vocabulary
else:
input_vocab_filepath = path.join(model_dir, Vocabulary.INPUT_VOCAB_FILENAME)
input_vocabulary = Vocabulary.load(input_vocab_filepath)
output_vocab_filepath = path.join(model_dir, Vocabulary.OUTPUT_VOCAB_FILENAME)
output_vocabulary = Vocabulary.load(output_vocab_filepath)
# Setting up the chat
chatlog_filepath = path.join(model_dir, "chat_logs", "chatlog_{0}.txt".format(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")))
chat_settings = ChatSettings(hparams.model_hparams, hparams.inference_hparams)
############# Loading Model #############
reload_model = False
print()
print("Initializing model..." if not reload_model else "Re-initializing model...")
print()
model = ChatbotModel(mode = "infer",
model_hparams = chat_settings.model_hparams,
input_vocabulary = input_vocabulary,
output_vocabulary = output_vocabulary,
model_dir = model_dir)
#Load the weights
print()
print("Loading model weights...")
print()
model.load(checkpoint)
#Show the commands
if not reload_model:
#Uncomment the following line if you want to print commands.
#chat_command_handler.print_commands()
print('Model Reload!')
return model, chatlog_filepath, chat_settings
def chat_fun_english(question, model, chat_settings, chatlog_filepath):
#Get the input and check if it is a question or a command, and execute if it is a command
#question = input("You: ")
is_command, terminate_chat, reload_model = chat_command_handler.handle_command(question, model, chat_settings)
if is_command:
pass
else:
#If it is not a command (it is a question), pass it on to the chatbot model to get the answer
question_with_history, answer = model.chat(question, chat_settings)
#Print the answer or answer beams and log to chat log
if chat_settings.show_question_context:
print("Question with history (context): {0}".format(question_with_history))
print("\n1st if")
if chat_settings.show_all_beams:
for i in range(len(answer)):
print("ChatBot (Beam {0}): {1}".format(i, answer[i]))
print("\n2nd if")
else:
print("ChatBot: {0}".format(answer))
#print("\n else")
print()
if chat_settings.inference_hparams.log_chat:
chat_command_handler.append_to_chatlog(chatlog_filepath, question, answer)
return answer
wt = weather_time()
def chat_fun_urdu(n_query, model, chat_settings, chatlog_filepath):
chk, response = wt.query_check(n_query)
terminate_chat = False
#Get the input and check if it is a question or a command, and execute if it is a command
#question = input("You: ")
question=n_query
is_command, terminate_chat, reload_model = chat_command_handler.handle_command(question, model, chat_settings)
if is_command:
pass
elif chk:
return response
else:
question = ChatSettings.To_query(n_query)
#If it is not a command (it is a question), pass it on to the chatbot model to get the answer
question_with_history, answer = model.chat(question, chat_settings)
#Print the answer or answer beams and log to chat log
if chat_settings.show_question_context:
print("Question with history (context): {0}".format(question_with_history))
if chat_settings.show_all_beams:
for i in range(len(answer)):
print("ChatBot (Beam {0}): {1}".format(i, answer[i]))
else:
n_answer = ChatSettings.To_answer(answer)
print("ChatBot: {0}".format(n_answer))
print()
return n_answer
if chat_settings.inference_hparams.log_chat:
chat_command_handler.append_to_chatlog(chatlog_filepath, question, answer)