Basic chatbot
That is a code for a basic neural network chatbot on keras, feel free to use this code for any chatbots.
How to use
dataset.json & responses.txt
dataset.json
This is the file for the dataset that the neural network will learn on. Usually the bigger the model and the dataset the better. The dataset in this case has the key, which is the input, and value which is the number of the output line. Warning The lines are counting from 0 to n, not from 1 to n. The example is already provided in the files, but here's an example how are you supposed to write it:
{
"Input1": 0,
"Hey.": 1
}
responses.txt
This is the file with all the responses this neural network can have. The number of the response is the number of the line it is on (minus one, because in this case lines count from 0). The responses.txt
example is already provided in the model files but here's an example of how the file is supposed to look like:
Response1
Hello.
train.py
To start training, use python3 train.py
in the terminal. By default, it reads a file dataset.json
in the current directory (you can change it on the line 11). This will save the model to chatbot.keras
, you can change this on the line 52. Warning The model file name should always end with .keras
, until if it's outdated.
test.py
To use the model, run python3 test.py
, it will open a chat to test this bot. To use this chatbot in your code, copy the chatbot.keras
, model_settings.py
and the test.py
file (it's better to rename it) into your project directory. Then, to use the model, just import test
or how you named the file, and use test.generate("text")
. Here's an example of a chat.
import test
while True:
message = input("Message: ")
response = test.generate(message, verbose=0)
print(f"Bot: {response}")
todset.py
This file converts a file (usually dataset.txt
) into a dataset used by train.py
. The file has lines, each of them has two parts, before "→" is the input and after is the output. Here's an example:
Hey→Hello.
Hi.→Hello.
How are you?→I'm fine. How's your day?
This can also be used to classify text, just instead of the outputs use the class names.
- Downloads last month
- 0