Code Monkey home page Code Monkey logo

keras-chatbot-web-api's Introduction

keras-chatbot-web-api

Simple keras chat bot using seq2seq model with Flask serving web

The chat bot is built based on seq2seq models, and can infer based on either character-level or word-level.

The seq2seq model is implemented using LSTM encoder-decoder on Keras.

Notes

So far the GloVe word encoding version of the chatbot seems to give the best performance.

Usage

Run the following command to install the keras, flask and other dependency modules:

sudo pip install -r requirements.txt

The chat bot models are train using cornell-dialogs and gunthercox-corpus data set and are available in the "chatbot_train/models" directory. During runtime, the flask app will load these trained models to perform the chat-reply

Training (Optional)

As the trained models are already included in the "chatbot_train/models" folder in the project, the bot training is not required. However, if you like to tune the parameters of the seq2seq and retrain the models, you can use the following command to run the training:

cd chatbot_train
python cornell_char_seq2seq_train.py

The above commands will train seq2seq model using cornell dialogs on the character-level and store the trained model in "chatbot_train/models/cornell/char-**"

If you like to train other models, you can use the same command above on another train python scripts:

  • cornell_word_seq2seq_train.py: train on cornell dialogs on word-level (one hot encoding)
  • cornell_word_seq2seq_glove_train.py: train on cornell dialogs on word-level (GloVe word2vec encoding)
  • gunthercox_char_seq2seq_train.py: train on gunthercox corpus on character-level
  • gunthercox_word_seq2seq_train.py: train on gunthercox corpus on word-level (one hot encoding)
  • gunthercox_word_seq2seq_glove_train.py train on gunthercox corpus on word-level (GloVe word2vec encoding)

Running Web Api Server

Goto chatbot_web directory and run the following command:

python flaskr.py

Now navigate your browser to http://localhost:5000 and you can try out various predictors built with the following trained seq2seq models:

  • Character-level seq2seq models
  • Word-level seq2seq models (One Hot Encoding)
  • Word-level seq2seq models (GloVe Encoding)

Invoke Web Api

To make the bot reply using web api, after the flask server is started, run the following curl POST query in your terminal:

curl -H 'Content-Type application/json' -X POST -d '{"level":"level_type", "sentence":"your_sentence_here", "dialogs":"chatbox_dataset"}' http://localhost:5000/chatbot_reply

The level_type can be "char" or "word", the dialogs can be "gunthercox" or "cornell"

(Note that same results can be obtained by running a curl GET query to http://localhost:5000/chatbot_reply?sentence=your_sentence_here&level=level_type&dialogs=chatbox_dataset)

For example, you can ask the bot to reply the sentence "How are you?" by running the following command:

curl -H 'Content-Type: application/json' -X POST -d '{"level":"word", "sentence":"How are you?", "dialogs":"gunthercox"}' http://localhost:5000/chatbot_reply

And the following will be the json response:

{
    "dialogs": "gunthercox",
    "level": "word",
    "reply": "i am doing well how about you",
    "sentence": "How are you?"
}

Here are some examples for eng chat-reply using some other configuration options:

curl -H 'Content-Type: application/json' -X POST -d '{"level":"char", "sentence":"How are you?", "dialogs":"gunthercox"}' http://localhost:5000/chatbot_reply
curl -H 'Content-Type: application/json' -X POST -d '{"level":"word", "sentence":"How are you?", "dialogs":"cornell"}' http://localhost:5000/chatbot_reply
curl -H 'Content-Type: application/json' -X POST -d '{"level":"char", "sentence":"How are you?", "dialogs":"cornell"}' http://localhost:5000/chatbot_reply
curl -H 'Content-Type: application/json' -X POST -d '{"level":"word-glove", "sentence":"How are you?", "dialogs":"cornell"}' http://localhost:5000/chatbot_reply
curl -H 'Content-Type: application/json' -X POST -d '{"level":"word-glove", "sentence":"How are you?", "dialogs":"gunthercox"}' http://localhost:5000/chatbot_reply

Configure to run on GPU on Windows

  • Step 1: Change tensorflow to tensorflow-gpu in requirements.txt and install tensorflow-gpu
  • Step 2: Download and install the CUDA® Toolkit 9.0 (Please note that currently CUDA® Toolkit 9.1 is not yet supported by tensorflow, therefore you should download CUDA® Toolkit 9.0)
  • Step 3: Download and unzip the cuDNN 7.0.4 for CUDA@ Toolkit 9.0 and add the bin folder of the unzipped directory to the $PATH of your Windows environment

TODO

  • Parameter tuning: as the seq2seq usually takes many hours to train on large corpus and long sentences, i don't have sufficient time at the moment to do a good job on the parameter tuning and training for a longer period of time (current parameters were tuned such that the bots can be trained in a few hours)
  • Better text preprocessing: to improve the bot behavior, one way is to perform more text preprocessing before the training (e.g. stop word filtering, stemming)

keras-chatbot-web-api's People

Contributors

chen0040 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

keras-chatbot-web-api's Issues

TypeError: __init__() takes at least 2 arguments (1 given)

Sorry if a try to execute cornell_char_seq2seq_predict.py from console, I get this error from line 34:
Traceback (most recent call last):
File "cornell_char_seq2seq_predict.py", line 98, in
main()
File "cornell_char_seq2seq_predict.py", line 94, in main
model = CornellCharChatBot()
File "cornell_char_seq2seq_predict.py", line 34, in init
encoder = LSTM(units=HIDDEN_UNITS, return_state=True, name="encoder_lstm")
TypeError: init() takes at least 2 arguments (1 given)

encoder = LSTM(units=HIDDEN_UNITS, return_state=True, name="encoder_lstm")

Getting error while training seq2seq

Command: python cornell_char_seq2seq_train.py

error : /usr/local/virtual_env_dir/keraschat/local/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Traceback (most recent call last):
  File "cornell_char_seq2seq_train.py", line 20, in <module>
    lines = open(DATA_PATH, 'rt', encoding='utf8').read().split('\n')
TypeError: 'encoding' is an invalid keyword argument for this function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.