-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnursebot_dialog.1.py
85 lines (73 loc) · 3.15 KB
/
nursebot_dialog.1.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
import rasa_core
from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils import EndpointConfig
from rasa_core.run import serve_application
from rasa_core import config
import spacy
from spacy import displacy
import ruamel
import os
import win32com.client as wincl
import warnings
warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
warnings.filterwarnings("ignore",category=DeprecationWarning)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #To ignore Tensorflow AVX AVX2 bonary warning
logger = logging.getLogger(__name__)
speak = wincl.Dispatch("SAPI.SpVoice")
nlp = spacy.load('en')
phrases = []
# Declare paths
domain_file = './nurse_domain.yml'
model_path = './models/dialogue'
interpreter_path ='./models/nursebot/interpreter'
training_data_file = './data/stories.md'
conf_file = './config_spacy.json
action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
interpreter = RasaNLUInterpreter(interpreter_path)
agent = Agent.load('./models/dialogue', interpreter=interpreter, action_endpoint=action_endpoint)
# def train_dialogue(train = True,domain_file = 'nurse_domain.yml',
# model_path = './models/dialogue',
# training_data_file = './data/stories.md'):
# if(train == True):
# agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])
# data = agent.load_data(training_data_file)
# agent.train(data)
# agent.visualize("data/stories.md",output_file="graph.html", max_history=2)
# agent.persist(model_path)
# return agent
# def saveDependencyGraph(phrase,saveImages = True):
# phrases.append(nlp(phrase))
# html = displacy.render(phrases, style='dep', page=True)
# #svg = displacy.render(doc, style='dep')
# #output_path = Path('/images/sentence.html')
# #output_path.open('w', encoding='utf-8').write(html)
def run_nurse_bot(serve_forever=True):
RasaNLUInterpreter(interpreter_path) action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
agent = Agent.load('./models/dialogue', interpreter=interpreter, action_endpoint=action_endpoint)
print("[INFO] The nurse is ready to listen. Please start a dialog... (Type 'stop' to quit)")
while serve_forever:
print(">> ", end='')
a = input()
if a == 'stop':
break
#saveDependencyGraph(a,True)
responses = agent.handle_message(a)
if(len(responses) == 0):
print("Action returned 0 elements. Maybe the action listener is not active. Activate listener:\n python -m rasa_core_sdk.endpoint --actions actions")
for response in responses:
print("👩 "+" >> " + response["text"]+"\n")
#speak.Speak(response["text"])
#rasa_core.run.serve_application(agent ,channel='cmdline')
if __name__ == '__main__':
#train_dialogue(False)
run_nurse_bot(True)
#Code to display stories
#python -m rasa_core.visualize -d nurse_domain.yml -s data/stories.md -o graph.html -c config_spacy.json