Training
Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://www.youtube.com/watch?v=xu6D_vLP5vY | |
#https://github.com/JustinaPetr/Weatherbot_Tutorial | |
Rasa based chatbot | |
Step 1 - Pre-Requisites | |
======================== | |
1. Clone Project https://github.com/JustinaPetr/Weatherbot_Tutorial | |
2. Install Requirements from FULL Code Directory | |
cd E:\Code_Repo\Weatherbot_Tutorial\Full_Code | |
pip install -r requirements.txt | |
3. Download English Spacy model - To parse and get necessary information | |
python -m spacy download en | |
4. Install npm with node.js. https://www.npmjs.com/get-npm | |
https://nodejs.org/dist/v12.13.0/node-v12.13.0-x64.msi | |
5. In New Terminal | |
npm i -g rasa-nlu-trainer | |
Data Annotation - rasa nlu trainer | |
Deconstruct into Intent, Entities | |
Intent - What it is about | |
Entity - Location, Place, Object in Discussion | |
Example Messages, Alongside intents, Entities | |
Examples | |
Greeting | |
GoodBye | |
Asking | |
Step 2 - Data Annotation | |
============================ | |
Step #1 - File data.json | |
{ | |
"rasa_nlu_data":{ | |
"common_examples":[ | |
{ | |
"text":"Hello" | |
"intent":"Greet", | |
"entities":[] | |
} | |
{ | |
"text":"goodbye" | |
"intent":"goodbye", | |
"entities":[] | |
} | |
] | |
} | |
Step #2 - Launch the trainer in Anaconda console | |
1. Goto Location E:\Code_Repo\Weatherbot_Tutorial\Full_Code_Latest> | |
2. Run Command rasa-nlu-trainer | |
3. Custom adding intent and examples | |
4. All additional examples present in git code in updated Data.json file | |
Step #3 - Train model | |
======================= | |
1. Configuration File | |
- Provide parameters | |
- Pipeline - Feature extractors to fetch messages | |
- Model save path | |
- Data path for annotated data | |
{ | |
"pipeline":"spacy_sklearn", | |
"path":"./models/nlu", | |
"data":"./data/data.json" | |
} | |
config_spacy.json file | |
2. nlu_model.py file for script for model training | |
#import libraries | |
from rasa_nlu.converters import load_data | |
#load configuration files | |
from rasa_nlu.config import RasaNLUConfig | |
#load trainer | |
from rasa_nlu.model import Trainer | |
def train_nlu(data,config,model_sir): | |
training_data = load_data(data) | |
trainer = Trainer(RasaNLUConfig(config)) | |
trainer.train(training_data) | |
model_directory = trainer.persist(model_dir,fixed_model_name='weathernlu') | |
if __name__=='__main__': | |
train_nlu('./data/data.json','config_spacy.json','./models/nlu' | |
#Run this to train the model | |
#Models created in folder directory | |
2. Code to test with additional code in nlu_model.py | |
#import libraries | |
from rasa_nlu.converters import load_data | |
#load configuration files | |
from rasa_nlu.config import RasaNLUConfig | |
#load trainer | |
from rasa_nlu.model import Trainer | |
from rasa_nlu.model import Metadata, Interpreter | |
def train_nlu(data,config,model_sir): | |
training_data = load_data(data) | |
trainer = Trainer(RasaNLUConfig(config)) | |
trainer.train(training_data) | |
model_directory = trainer.persist(model_dir,fixed_model_name='weathernlu') | |
def run_nlu(): | |
interpreter = interpreter.load('./models/nlu/default/weathernlu',RasaNLUConfig('config_spacy.json')) | |
#load the model | |
print(interpreter.parse(u"I am planning my holiday to barcelona, I wounder what is the weather out there")) | |
if __name__=='__main__': | |
run_nlu() | |
3. Changes to run for custom packages (Code will run in these versions) | |
pip install rasa_core==0.10.3 | |
pip install rasa-nlu==0.11.5 | |
Rerun - nlu_model.py file | |
Step #4 - Building the conversation | |
====================================== | |
1. Dialogue management will predict action. Domain file. It is yml file. | |
2. Key parts are | |
slots - placeholders for context of conversation, | |
intents - , | |
entities - , | |
templates - , | |
actions - | |
3. All details used for predictions | |
slot and entities have same attributes - Observations | |
template - text responses for users (multiple answers) | |
weather_domain.yml | |
slots: | |
location: | |
type:text | |
intents: | |
- greet | |
- goodbye | |
- inform | |
entities: | |
- location | |
templates: | |
utter-greet: | |
- 'Hello, How can i help?' | |
utter-goodbye: | |
- 'ttyl' | |
utter_ask_location: | |
-'In what location?' | |
actions: | |
- utter_greet | |
- utter_goodbye | |
- utter_ask_location | |
- actions.ActionWeather | |
Step #5 - Custom Action creation file | |
======================================== | |
actions.py | |
from __future__import absolute_import | |
from __future__import division | |
from __future__import unicode_literals | |
from rasa_core.actions.action import Action | |
from rasa_core.events import SlotSet | |
class ActionWeather(Action): | |
def name(self): | |
return 'action_weather' | |
def run(self,dispatcher,tracker,domain): | |
from apixu.client import ApixuClient | |
api_key = "" | |
#Authentication | |
client = ApixuClient(api_key) | |
loc = tracker.get_slot('location') | |
current = client.getCurrentWeather(q=loc) | |
#parse and extract required details | |
country = current['location']['country'] | |
city = current['location']['name'] | |
condition = current['current']['condition']['text'] | |
temperature_c = current['current']['temp_c'] | |
humidity = current['current']['humidity'] | |
wind_mph = current['current']['wind_mph'] | |
response = """It is currently {} in {} at the moment. {} {} and wind {}""".format(condition,city,temperature,humidity,wind_mph) | |
dispatcher.utter_message(response) | |
#custom action | |
return [SlotSet('location',loc)] | |
#file updated in actions weather_domain.yml | |
Step #6 - Story formation | |
========================== | |
New file Stories.md markdown file in data folder | |
Stories.md | |
========== | |
#story 01 | |
* greet | |
- utter_greet | |
## story 02 | |
* goodbye | |
- utter_goodbye | |
##story 03 | |
* inform | |
- utter_ask_location | |
##story 04 | |
* inform | |
- action_weather | |
Step #7 | |
======== | |
Start online session | |
using train_init.py and train_online.py | |
train_init.py | |
- train dialogue management model | |
- agent used to train model | |
- keras polcies used to train model | |
- data file path | |
- augmentation factor to add more stories | |
- Save model with persist function | |
pip install rasa-nlu==0.13.1 | |
Modified train_init.py | |
======================= | |
Step #8 | |
======== | |
train_online.py | |
- import libraries | |
- parser to parse extract features | |
- load model | |
Retrain Model | |
python -m rasa_core.train -s data/stories.md -d weather_domain.yml -o models/dialogue --epochs 300 | |
Step #9 | |
======= | |
Run online Training | |
python -m rasa_nlu.train -c nlu_model_config.yml --fixed_model_name current --data ./data/nlu.md --path models/ --project nlu | |
Step #10 | |
========= | |
dialogue_management_model.py | |
Final code to demo | |
Summary | |
======== | |
- Install requirements from FULL Code only | |
- Run code nlu_model.py train block | |
- Run code train_init.py | |
- Run code train_online.py (Actual Conversations with chatbot) | |
- Action_Listen (Wait for input) | |
- Experimented the greet - ask - response - quit workflow | |
- Run demo, dialogue_management_model.py | |
Next Reads | |
https://towardsdatascience.com/create-chatbot-using-rasa-part-1-67f68e89ddad | |
https://medium.com/analytics-vidhya/learn-how-to-build-and-deploy-a-chatbot-in-minutes-using-rasa-5787fe9cce19 | |
https://forum.rasa.com/t/what-is-the-recommended-setup-for-production-deployemnt/1882 |
Happy Learning!!!
No comments:
Post a Comment