Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit c08f70f

Browse files
author
Shreyans Shrimal
authored
Merge pull request #161 from lunayach/ChatBotUpgrade
Upgraded ChatBot
2 parents 457953c + 27832b4 commit c08f70f

File tree

73 files changed

+50826
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+50826
-25
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ Thumbs.db
4949
# Config files storing passwords
5050
/config/settings.js
5151

52+
#English model file required for entity extraction by MITIE
53+
/ChatBot/total_word_feature_extractor.dat
54+
55+
#RASA log files
56+
/ChatBot/out.log
57+
/ChatBot/rasa_core.log
58+
59+
#Python virtual environment
60+
/ChatBot/venv/*
61+
/ChatBot/src/*
62+
63+
#IDE settings
64+
/ChatBot/.idea
65+

ChatBot/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
run:
2+
python bot.py run
3+
4+
train-nlu:
5+
python bot.py train-nlu
6+
7+
train-dialogue:
8+
python bot.py train-dialogue
9+
10+
describe:
11+
python visualize.py
12+
13+
learn:
14+
python train_online.py
15+
16+
server:
17+
python -m rasa_core.server -d models/current/dialogue -u models/nlu/default/current -o out.log --cors "*"
18+
19+
train-dialogue-default:
20+
python -m rasa_core.train -d domain.yml -s data/stories.md -o models/current/dialogue --epochs 200
21+
6.35 KB
Binary file not shown.
1.55 KB
Binary file not shown.

ChatBot/bot.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
import argparse
7+
import logging
8+
9+
from policy import CustomPolicy
10+
from rasa_core import utils
11+
from rasa_core.actions import Action
12+
from rasa_core.agent import Agent
13+
from rasa_core.channels.console import ConsoleInputChannel
14+
from rasa_core.events import SlotSet
15+
from rasa_core.interpreter import RasaNLUInterpreter
16+
from rasa_core.policies.memoization import MemoizationPolicy
17+
from rasa_core.actions.forms import (
18+
EntityFormField,
19+
FormAction
20+
)
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
class ActionPatientInformation(FormAction):
26+
RANDOMIZE = False
27+
28+
@staticmethod
29+
def required_fields():
30+
return [
31+
EntityFormField("gender", "gender"),
32+
EntityFormField("age", "age"),
33+
]
34+
35+
@classmethod
36+
def name(self):
37+
return 'action_patient_information'
38+
39+
@classmethod
40+
def submit(self, dispatcher, tracker, domain):
41+
return [SlotSet("matches", "text")]
42+
43+
44+
class ActionSuggestNormal(Action):
45+
@classmethod
46+
def name(self):
47+
return 'action_suggest_normal'
48+
49+
@classmethod
50+
def run(self, dispatcher, tracker, domain):
51+
medicines = ['Atovaquone', 'Chloroquine', 'Doxycycline', 'Mefloquine', 'Primaquine']
52+
age = int(tracker.get_slot("age"))
53+
if age < 8:
54+
medicines.remove("Doxycycline")
55+
dispatcher.utter_message("You can take these medicines:\n" + ' '.join(medicines))
56+
return []
57+
58+
59+
class ActionSuggestPregnant(Action):
60+
@classmethod
61+
def name(self):
62+
return 'action_suggest_pregnant'
63+
64+
@classmethod
65+
def run(self, dispatcher, tracker, domain):
66+
medicines = ['Chloroquine', 'Doxycycline', 'Mefloquine']
67+
age = int(tracker.get_slot("age"))
68+
if age < 8:
69+
medicines.remove("Doxycycline")
70+
dispatcher.utter_message("You can take these medicines:\n" + ' '.join(medicines))
71+
return []
72+
73+
74+
class ActionCheckMedicineNormal(Action):
75+
@classmethod
76+
def name(self):
77+
return 'action_check_medicine_normal'
78+
79+
@classmethod
80+
def run(self, dispatcher, tracker, domain):
81+
medicines = ['ATOVAQUONE', 'CHLOROQUINE', 'DOXYCYCLINE', 'MEFLOQUINE', 'PRIMAQUINE']
82+
83+
medicine = str(tracker.get_slot("medicine")).upper()
84+
age = int(tracker.get_slot("age"))
85+
if age < 8:
86+
medicines.remove("DOXYCYCLINE")
87+
88+
if medicine in medicines:
89+
dispatcher.utter_message("Its safe to take the medicine!")
90+
else:
91+
dispatcher.utter_message("The asked medicine is not safe for you. You can take these medicines instead:"
92+
"\n" + ' '.join(medicines))
93+
return []
94+
95+
96+
class ActionCheckMedicinePregnant(Action):
97+
@classmethod
98+
def name(self):
99+
return 'action_check_medicine_pregnant'
100+
101+
@classmethod
102+
def run(self, dispatcher, tracker, domain):
103+
medicines = ['CHLOROQUINE', 'DOXYCYCLINE', 'MEFLOQUINE']
104+
medicine = tracker.get_slot("medicine")
105+
age = int(tracker.get_slot("age"))
106+
if age < 8:
107+
medicines.remove("DOXYCYCLINE")
108+
109+
if medicine in medicines:
110+
dispatcher.utter_message("Its safe to take the medicine!")
111+
else:
112+
dispatcher.utter_message("The asked medicine is not safe for you. You can take these medicines instead:"
113+
"\n" + ' '.join(medicines))
114+
return []
115+
116+
117+
class CheckAge(FormAction):
118+
RANDOMIZE = False
119+
120+
@staticmethod
121+
def required_fields():
122+
return [
123+
EntityFormField("age", "age")
124+
]
125+
126+
@classmethod
127+
def name(self):
128+
return 'check_age'
129+
130+
@classmethod
131+
def submit(self):
132+
return
133+
134+
135+
def train_dialogue(domain_file="domain.yml",
136+
model_path="models/dialogue",
137+
training_data_file="data/stories.md"):
138+
agent = Agent(domain_file,
139+
policies=[MemoizationPolicy(max_history=3),
140+
CustomPolicy()])
141+
142+
training_data = agent.load_data(training_data_file)
143+
agent.train(
144+
training_data,
145+
epochs=400,
146+
batch_size=100,
147+
validation_split=0.2
148+
)
149+
150+
agent.persist(model_path)
151+
return agent
152+
153+
154+
def train_nlu():
155+
from rasa_nlu.training_data import load_data
156+
from rasa_nlu import config
157+
from rasa_nlu.model import Trainer
158+
159+
training_data = load_data('data/nlu_data/')
160+
trainer = Trainer(config.load("nlu_model_config.yml"))
161+
trainer.train(training_data)
162+
model_directory = trainer.persist('models/nlu', fixed_model_name="current")
163+
164+
return model_directory
165+
166+
167+
def run(serve_forever=True):
168+
interpreter = RasaNLUInterpreter("models/nlu/default/current")
169+
agent = Agent.load("models/current/dialogue", interpreter=interpreter)
170+
171+
if serve_forever:
172+
agent.handle_channel(ConsoleInputChannel())
173+
return agent
174+
175+
176+
if __name__ == '__main__':
177+
utils.configure_colored_logging(loglevel="INFO")
178+
179+
parser = argparse.ArgumentParser(
180+
description='starts the bot')
181+
182+
parser.add_argument(
183+
'task',
184+
choices=["train-nlu", "train-dialogue", "run"],
185+
help="what the bot should do - e.g. run or train?")
186+
task = parser.parse_args().task
187+
188+
# decide what to do based on first parameter of the script
189+
if task == "train-nlu":
190+
train_nlu()
191+
elif task == "train-dialogue":
192+
train_dialogue()
193+
elif task == "run":
194+
run()
195+

ChatBot/data/nlu_data/Female.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## intent:Female
2+
- Female
3+
- female
4+
- woman
5+
- Girl
6+
- Gal
7+
- Lady
8+

ChatBot/data/nlu_data/Male.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## intent:Male
2+
- male
3+
- man
4+
- Boy
5+
- Guy
6+

ChatBot/data/nlu_data/affirm.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## intent:affirm
2+
- yes
3+
- ya
4+
- Yes
5+
- Yup
6+
- Yes I am
7+
- It's true
8+
- Correct
9+
- Right
10+
- indeed
11+
- affirmative
12+
- agreed
13+

ChatBot/data/nlu_data/botPlace.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## intent:botPlace
2+
- Where do you live
3+
- where are you
4+
- where can I meet you?
5+

ChatBot/data/nlu_data/deny.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## intent:deny
2+
- No
3+
- no
4+
- not
5+
- no!
6+
- What!! No!
7+
- Nah
8+
- i deny that
9+
- impossible
10+
- not possible
11+
- that’s not correct
12+
- that’s a mistake
13+
- that’s incorrect
14+
- that’s not right
15+
- i disagree
16+
- i don’t think so
17+

0 commit comments

Comments
 (0)