-
Notifications
You must be signed in to change notification settings - Fork 1
/
nuRobot.py
178 lines (133 loc) · 5.56 KB
/
nuRobot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import logging
import warnings
from lib.policy import nuRobotPolicy
from rasa_core import utils
from rasa_core.actions import Action
from rasa_core.agent import Agent
from rasa_core.channels.console import ConsoleInputChannel
from rasa_core.events import SlotSet
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
logger = logging.getLogger(__name__)
class ActionSuggest(Action):
def name(self):
return 'action_suggest'
def run(self, dispatcher, tracker, domain):
dispatcher.utter_message("here's what I found:")
dispatcher.utter_message(tracker.get_slot("matches"))
dispatcher.utter_message("is it ok for you? "
"hint: I'm not going to "
"find anything else :)")
return []
def train_nlu(project='Lambton'):
from rasa_nlu.training_data import load_data
from rasa_nlu.config import RasaNLUModelConfig
from rasa_nlu.model import Trainer
from rasa_nlu import config
training_data = load_data('Lambton/data/nuRobot-data.json') # + project + '/intents')
print("*** Training data :" + str(training_data.intents))
trainer = Trainer(config.load('./NLU/config_spacy.json')) # projects/' + project + '/config_spacy.yml'))
print("*** Config :" + str(trainer.config))
trainer.train(training_data)
#model_directory = trainer.persist('./NLU/models/default/' + project +'/', fixed_model_name='dialogue')
model_directory = trainer.persist('./NLU/models/', fixed_model_name=project)
return model_directory
def train_dialogue(project='Lambton'):
domain_file = './Core/models/'+ project + '/dialogue/domain.yml'
training_data_file = './Core/models/'+ project + '/stories/stories.md'
model_path = './Core/models/'+ project + '/dialogue'
agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3),
nuRobotPolicy()])
training_data = agent.load_data(training_data_file)
agent.train(
training_data,
augmentation_factor=50,
batch_size=10,
epochs=250,
max_training_samples=300,
validation_split=0.2
)
agent.persist(model_path)
return agent
def train_online(project='Lambton'):
domain_file = './Core/models/' + project + '/dialogue/domain.yml'
model_path = './NLU/models/default/' + project,
training_data_file = './Core/models/'+ project + '/stories/stories.md'
agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy()])
agent.train_online(training_data_file,
input_channel=ConsoleInputChannel(),
max_history=2,
batch_size=10,
epochs=250,
max_training_samples=300,
validation_split=0.2)
agent.persist(model_path)
return agent
def load_model(project="Lambton"):
interpreter = RasaNLUInterpreter('./NLU/models/default/' + project)
agent = Agent.load('./Core/models/' + project + '/dialogue/', interpreter=interpreter)
return agent
def process_input(agent, serve_forever=True, message='Hi'):
if serve_forever:
output = agent.handle_message(message)
return output, agent
def testbot(project="Lambton", serve_forever=True):
interpreter = RasaNLUInterpreter('NLU/models/default/' + project)
agent = Agent.load('./Core/models/' + project + '/dialogue/', interpreter=interpreter)
if serve_forever:
agent.handle_channel(ConsoleInputChannel())
return agent
def respond(project="Lambton", message=""):
interpreter = RasaNLUInterpreter('NLU/models/default/' + project)
agent = Agent.load('Core/models/' + project + '/dialogue/', interpreter=interpreter)
output = agent.handle_message(message)
return output, agent
if __name__ == '__main__':
utils.configure_colored_logging(loglevel="INFO")
parser = argparse.ArgumentParser(
description='starts the bot')
parser.add_argument(
'task',
choices=["train-nlu", "train-dialogue", "train-online", "test-bot", "respond"],
help="what the bot should do - e.g. run or train?")
parser.add_argument(
'project',
nargs='?',
help="what the project you want to load")
parser.add_argument(
'message',
nargs='?',
help="input message you want to process")
task = parser.parse_args().task
project = parser.parse_args().project
if project is None:
project = "Lambton"
print("Selected task ", task)
print("Selected project ", project)
task = parser.parse_args().task
# decide what to do based on first parameter of the script
if task == "train-nlu":
train_nlu(project)
elif task == "train-dialogue":
train_dialogue(project)
elif task == "test-bot":
testbot(project)
elif task == "train-online":
train_online(project)
elif task == "respond":
message = parser.parse_args().message
if message:
response, active_agent = respond(project, message)
print("Response", response)
else:
warnings.warn("No input message to process")
else:
warnings.warn("Need to pass either 'train-nlu', 'train-dialogue' or "
"'run' to use the script.")
exit(1)