Simple chatbot implementation with PyTorch and Flask API.
- The implementation should be easy to follow for beginners and provide a basic understanding of chatbots.
- The implementation is straightforward with a Feed Forward Neural net with 2 hidden layers.
- Customization for your own use case is super easy. Just modify
intents.jsonwith possible patterns and responses and re-run the training (see below for more info).
The approach is inspired by the articles: https://towardsdatascience.com/model-deployment-using-flask-c5dcbb6499c9 https://dev.to/sahilrajput/build-a-chatbot-using-flask-in-5-minutes-574i

Whatever you prefer (e.g. conda or venv)
mkdir myproject
$ cd myproject
$ python3 -m venv venvMac / Linux:
. venv/bin/activateWindows:
venv\Scripts\activateFor Installation of PyTorch see official website.
You also need nltk:
pip install nltkIf you get an error during the first run, you also need to install nltk.tokenize.punkt:
Run this once in your terminal:
$ python
>>> import nltk
>>> nltk.download('punkt')Run
python train.pyThis will dump data.pth file. And then run
python chat.pyHave a look at intents.json. You can customize it according to your own use case. Just define a new tag, possible patterns, and possible responses for the chat bot. You have to re-run the training whenever this file is modified.
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Hi",
"Hey",
"How are you",
"Is anyone there?",
"Hello",
"Good day"
],
"responses": [
"Hey :-)",
"Hello, thanks for visiting",
"Hi there, what can I do for you?",
"Hi there, how can I help?"
]
},
...
]
}Have a look at the html file in the templates folder. You can change it according your preferences.
When we run the app.py file, you will get the following. Please click on the link to run the application on your remote server.
- Serving Flask app "app" (lazy loading)
- Environment: production
- Debug mode: off
- Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
It is used to load the saved model and data to run the conversation between the bot and the user.