Learn to Code via Tutorials on Repl.it!

← Back to all posts
Tensorflow chat bot!!
h
DuncanSykes (6)

Chatbot with TensorFlow and Python

https://storage.googleapis.com/replit/images/1541664973784_753b29686c97c4fc9149fdc8694ed16c.pn

Have you ever felt the need to write a chatbot in python but don't want to mess around with hundreds of IF statements?

Yes?
No?

Well either way, you clicked on this tutorial. Today we are going to look into how we can create a chat bot in python, as well as building an alorigthm that allows some context in conversation.

YOU'LL NEED A 64 BIT PC FOR THIS TO WORK

The requirements:

You'll need to install Anaconda and the Jupyter Notebook system for this to work.

Once you've set up your environment, you'll need to install these packages by running these commands in you command line:

  • pip install pickle
  • pip install numpy
  • pip install tflearn
  • pip install tensorflow
  • pip install json
  • pip install LancasterStemmer

The code

(.JSON file):

For this system we'll use a .JSON (javascript object notation) file to code in keywords that the chatbot will identify as having certain meanings, and hence how to respond.

Open a new file in the Jupyter notebook and name it intents.json and copy this code across. We'll use this as an example in this tutorial.

You can edit this later

Part one: Setting up

Open a new Python 3 notebook and import the following libraries:

Now we need to initiate the LancasterStemmer library:

Also we need to download some datasets for training.
Enter this code in a new cell

We can now import the .JSON file

If the .JSON file is structured correctly, this should run without any issues.

We now can break down the data in the .JSON file for usage by the neural network by splitting giving each word a place in an array.

We can now setup how the model for training:

We have now set the parameters for our neural network.
The next bit of code trains the model for the chat bot:

Once you run the above code, the model will train then save itself as 'model.tflearn'

Part Three: Testing

While in the same jupyter notebook, run this code in a new cell:

Now run this code:

This reopens the intents file as testing data.
We now need to reload the model we trained earlier:

For the code to work correctly we need clean our test data

You can now run a very simple test that will return binary values as a response.

You should get a result very similar to this

Part Four: Classifying words

We are now ready to make full use of our Tensorflow model and the training data we prepared. Copy this code into your notebook.

This next block of code allows some degree of context in a conversation with the bot by utilising the context_filter tag from the .JSON file

If you now enter a command (example shown below) you should get an actual response.

Your output should be something like this:
helloo

Try saying something else (one of patterns from the .JSON file)

Wrapping up

I hope this tutorial was clear enough and that it has given you a basis on how to train and test models with Tensorflow

Comments
hotnewtop
Muhammadzakirha (1)

#variabels
UserInput = ""
ChatBot_Name = "zaki"
ChatBot_Name_Error = "Error"
UserInput = str.lower(input("guest: "))

#msg
msg = ["hi", "your owner", "", "say"]
respon = [": Hello guest.", ": you", ": whats?", ": not support"]

#respon
if UserInput in msg:
index = Pesan.index(UserInput)
print(ChatBot_Name + respon[index])

if UserInput not in msg:
print(ChatBot_Name_Error + ': not found')