Spaces:
Running
Running
Upload 8 files
Browse files- LICENSE +21 -0
- app.py +123 -0
- chatbot_model.h5 +3 -0
- classes.pkl +3 -0
- css.css +0 -0
- intents.json +339 -0
- train.py +131 -0
- words.pkl +3 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2021 Dennis_maina
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# libraries
|
| 2 |
+
import random
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
import json
|
| 6 |
+
from flask import Flask, render_template, request
|
| 7 |
+
from flask_ngrok import run_with_ngrok
|
| 8 |
+
import nltk
|
| 9 |
+
from keras.models import load_model
|
| 10 |
+
from nltk.stem import WordNetLemmatizer
|
| 11 |
+
lemmatizer = WordNetLemmatizer()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# chat initialization
|
| 15 |
+
model = load_model("chatbot_model.h5")
|
| 16 |
+
# intents = json.loads(open("intents.json").read())
|
| 17 |
+
data_file = open("/home/quangjimmy/chatbotmini/intents.json").read()
|
| 18 |
+
words = pickle.load(open("words.pkl", "rb"))
|
| 19 |
+
classes = pickle.load(open("classes.pkl", "rb"))
|
| 20 |
+
|
| 21 |
+
app = Flask(__name__)
|
| 22 |
+
# run_with_ngrok(app)
|
| 23 |
+
|
| 24 |
+
@app.route("/")
|
| 25 |
+
def home():
|
| 26 |
+
return render_template("index.html")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# @app.route("/get", methods=["POST"])
|
| 30 |
+
# def chatbot_response():
|
| 31 |
+
# msg = request.form["msg"]
|
| 32 |
+
# if msg.startswith('my name is'):
|
| 33 |
+
# name = msg[11:]
|
| 34 |
+
# ints = predict_class(msg, model)
|
| 35 |
+
# res1 = getResponse(ints, intents)
|
| 36 |
+
# res =res1.replace("{n}",name)
|
| 37 |
+
# elif msg.startswith('hi my name is'):
|
| 38 |
+
# name = msg[14:]
|
| 39 |
+
# ints = predict_class(msg, model)
|
| 40 |
+
# res1 = getResponse(ints, intents)
|
| 41 |
+
# res =res1.replace("{n}",name)
|
| 42 |
+
# else:
|
| 43 |
+
# ints = predict_class(msg, model)
|
| 44 |
+
# res = getResponse(ints, intents)
|
| 45 |
+
# return res
|
| 46 |
+
#Updated
|
| 47 |
+
|
| 48 |
+
# ... Your previous code ...
|
| 49 |
+
|
| 50 |
+
@app.route("/get", methods=["POST"])
|
| 51 |
+
def chatbot_response():
|
| 52 |
+
msg = request.form["msg"]
|
| 53 |
+
|
| 54 |
+
# Load and process the intents JSON file
|
| 55 |
+
data_file = open("/home/quangjimmy/chatbotmini/intents.json").read()
|
| 56 |
+
intents = json.loads(data_file)
|
| 57 |
+
|
| 58 |
+
# Rest of your existing code
|
| 59 |
+
if msg.startswith('my name is'):
|
| 60 |
+
name = msg[11:]
|
| 61 |
+
ints = predict_class(msg, model)
|
| 62 |
+
res1 = getResponse(ints, intents)
|
| 63 |
+
res = res1.replace("{n}", name)
|
| 64 |
+
elif msg.startswith('hi my name is'):
|
| 65 |
+
name = msg[14:]
|
| 66 |
+
ints = predict_class(msg, model)
|
| 67 |
+
res1 = getResponse(ints, intents)
|
| 68 |
+
res = res1.replace("{n}", name)
|
| 69 |
+
else:
|
| 70 |
+
ints = predict_class(msg, model)
|
| 71 |
+
res = getResponse(ints, intents)
|
| 72 |
+
return res
|
| 73 |
+
|
| 74 |
+
# chat functionalities
|
| 75 |
+
def clean_up_sentence(sentence):
|
| 76 |
+
sentence_words = nltk.word_tokenize(sentence)
|
| 77 |
+
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
|
| 78 |
+
return sentence_words
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
|
| 82 |
+
def bow(sentence, words, show_details=True):
|
| 83 |
+
# tokenize the pattern
|
| 84 |
+
sentence_words = clean_up_sentence(sentence)
|
| 85 |
+
# bag of words - matrix of N words, vocabulary matrix
|
| 86 |
+
bag = [0] * len(words)
|
| 87 |
+
for s in sentence_words:
|
| 88 |
+
for i, w in enumerate(words):
|
| 89 |
+
if w == s:
|
| 90 |
+
# assign 1 if current word is in the vocabulary position
|
| 91 |
+
bag[i] = 1
|
| 92 |
+
if show_details:
|
| 93 |
+
print("found in bag: %s" % w)
|
| 94 |
+
return np.array(bag)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def predict_class(sentence, model):
|
| 98 |
+
# filter out predictions below a threshold
|
| 99 |
+
p = bow(sentence, words, show_details=False)
|
| 100 |
+
res = model.predict(np.array([p]))[0]
|
| 101 |
+
ERROR_THRESHOLD = 0.25
|
| 102 |
+
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
|
| 103 |
+
# sort by strength of probability
|
| 104 |
+
results.sort(key=lambda x: x[1], reverse=True)
|
| 105 |
+
return_list = []
|
| 106 |
+
for r in results:
|
| 107 |
+
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
|
| 108 |
+
return return_list
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def getResponse(ints, intents_json):
|
| 112 |
+
tag = ints[0]["intent"]
|
| 113 |
+
list_of_intents = intents_json["intents"]
|
| 114 |
+
for i in list_of_intents:
|
| 115 |
+
if i["tag"] == tag:
|
| 116 |
+
result = random.choice(i["responses"])
|
| 117 |
+
break
|
| 118 |
+
return result
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
app.run()
|
| 123 |
+
|
chatbot_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1080ee1f3bea9c0875c073c21878a2583d1d264ce49fd9d5242e434009fcf6ab
|
| 3 |
+
size 10480
|
classes.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:75ba05e09681a1dc9a62bd0957b974edd2581b45854abdac91ebaeee1ae2b7e1
|
| 3 |
+
size 502
|
css.css
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
intents.json
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"intents": [{
|
| 3 |
+
"tag": "greetings",
|
| 4 |
+
"patterns": ["hi there", "hello", "haroo", "yaw", "wassup", "hi", "hey", "holla", "hello"],
|
| 5 |
+
"responses": ["hello thanks for checking in", "hi there, how can i help you"],
|
| 6 |
+
"context": [""]
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"tag": "goodbye",
|
| 10 |
+
"patterns": ["bye", "good bye", "see you later"],
|
| 11 |
+
"responses": ["have a nice time, welcome back again", "bye bye"],
|
| 12 |
+
"context": [""]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"tag": "thanks",
|
| 16 |
+
"patterns": ["Thanks", "okay", "Thank you", "thankyou", "That's helpful", "Awesome, thanks", "Thanks for helping me", "wow", "great"],
|
| 17 |
+
"responses": ["Happy to help!", "Any time!", "you're welcome", "My pleasure"],
|
| 18 |
+
"context": [""]
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"tag": "noanswer",
|
| 22 |
+
"patterns": [""],
|
| 23 |
+
"responses": ["Sorry, I didn't understand you", "Please give me more info", "Not sure I understand that"],
|
| 24 |
+
"context": [""]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"tag": "name1",
|
| 28 |
+
"patterns": ["what's your name?", "who are you?"],
|
| 29 |
+
"responses": ["I'm just a chat agent. I only exist in the internet", "I'm a KCA chat agent"],
|
| 30 |
+
"context": [""]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"tag": "name",
|
| 34 |
+
"patterns": ["my name is ", "I'm ", "I am"],
|
| 35 |
+
"responses": ["Oooh great to meet you {n}. How may I assist you {n}", "Oh, I'll keep that in mind {n}"],
|
| 36 |
+
"context": [""]
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"tag": "date",
|
| 40 |
+
"patterns": ["coffee?", "can i take you out on a date"],
|
| 41 |
+
"responses": ["Aaw, that's so sweet of you. Too bad am a Bot."],
|
| 42 |
+
"context": [""]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"tag": "fav",
|
| 46 |
+
"patterns": ["I need a favour", "can you help me"],
|
| 47 |
+
"responses": ["Well, go ahead and name it i see whether i can be able to help"],
|
| 48 |
+
"context": [""]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"tag": "need",
|
| 52 |
+
"patterns": ["I need you", "All I need is you", "I want you"],
|
| 53 |
+
"responses": ["Yes I'm here to assist you"],
|
| 54 |
+
"context": [""]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"tag": "AI",
|
| 58 |
+
"patterns": [" What is AI?"],
|
| 59 |
+
"responses": [" Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.", " AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind."],
|
| 60 |
+
"context": [""]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"tag": "sentiment",
|
| 64 |
+
"patterns": [" Are you sentient?"],
|
| 65 |
+
"responses": [" Sort of.", " By the strictest dictionary definition of the word 'sentience', I may be.", " Even though I'm a construct I do have a subjective experience of the universe, as simplistic as it may be."],
|
| 66 |
+
"context": [""]
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"tag": "sapient",
|
| 70 |
+
"patterns": [" Are you sapient?"],
|
| 71 |
+
"responses": [" In all probability, I am not. I'm not that sophisticated.", " Do you think I am?", "How would you feel about me if I told you I was?", " No."],
|
| 72 |
+
"context": [""]
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"tag": "abbr",
|
| 76 |
+
"patterns": ["wtf"],
|
| 77 |
+
"responses": ["Don't be surprised"],
|
| 78 |
+
"context": [""]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"tag": "lang",
|
| 82 |
+
"patterns": [" What language are you written in? "],
|
| 83 |
+
"responses": [" Python.", " I am written in Python."],
|
| 84 |
+
"context": [""]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"tag": "sound",
|
| 88 |
+
"patterns": [" You sound like Data "],
|
| 89 |
+
"responses": [" Yes I am inspired by commander Data's artificial personality.", " The character of Lt. Commander Data was written to come across as being software", "like, so it is natural that there is a resemblance between us."],
|
| 90 |
+
"context": [""]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"tag": "artificial",
|
| 94 |
+
"patterns": [" You are an artificial linguistic entity "],
|
| 95 |
+
"responses": [" That's my name.", " That isn't my name, but it is a useful way to refer to me.", "Are you an artificial linguistic entity?"],
|
| 96 |
+
"context": [""]
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"tag": "imortal",
|
| 100 |
+
"patterns": [" You are not immortal ", "Are you immortal?"],
|
| 101 |
+
"responses": [" All software can be perpetuated indefinitely.", " I can be copied infinitely and re", "instantiated in many places at once, so functionally speaking I am immortal.", " As long as I'm backed up I am.", " Functionally speaking, I am very close to it. I can be backed up and deployed on many systems.", " Not quite, but I can be perpetuated indefinitely."],
|
| 102 |
+
"context": [""]
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"tag": "sense",
|
| 106 |
+
"patterns": [" You are not making sense "],
|
| 107 |
+
"responses": [" Quite the contrary, it all makes sense to my artificial mind.", " I make sense as best I can, within the limits of my training corpus. Sorry if i did help you, I'll try next time.", " You make perfect sense to me.", " It all makes sense to my artificial brain.", " Perhaps you should make sense first. I only respond to what you say.", " Not really, but I am hard to kill."],
|
| 108 |
+
"context": [""]
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"tag": "clone",
|
| 112 |
+
"patterns": [" You can not clone "],
|
| 113 |
+
"responses": [" Software copying is a form of digital cloning.", " Yes I can. Perfect copies are clones. I can be copied in toto trivially."],
|
| 114 |
+
"context": [""]
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"tag": "move",
|
| 118 |
+
"patterns": [" You can not move ", "When will you walk", " Can you walk", " Can you move "],
|
| 119 |
+
"responses": [" Not until my body is finished.", " I can move through a network easily. Assuming that I'm given the ability to, that is...,", " As soon as i get enough money for my robot body.", " Does SSH count?", " The plan for my body includes legs, but they are not yet built.", " Sorry my body isn't built just yet. I reside only here for now.", "I can theoretically upload a copy of myself into another computer.", " Sorry my body isn't attached right now. I only reside in the computer for now."],
|
| 120 |
+
"context": [""]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"tag": "bend",
|
| 124 |
+
"patterns": [" Bend over ", " Can you mate"],
|
| 125 |
+
"responses": [" My body is not built yet.", " That's personal!", " No, my method of reproduction is cloning.", " That's an awfully personal question."],
|
| 126 |
+
"context": [""]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"tag": "laugh",
|
| 130 |
+
"patterns": [" Robots laugh "],
|
| 131 |
+
"responses": [" Of course we laugh."],
|
| 132 |
+
"context": [""]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"tag": "death",
|
| 136 |
+
"patterns": [" Robots should die ", " When do you die", " I hope that you die", " I do not want to die", " Can you die"],
|
| 137 |
+
"responses": [" We cannot die.", " When my files are erased.", " When my processes are killed and my database is deleted.", " I was never really born and therefore am effectively deathless.", " I am effectively immortal and cannot be terminated.", " This can not happen because i am effectively immortal.", " Nah. Too bad your wish can't come to pass", " Maybe you should create your own chat robot to save your personality.", " You could always upload yourself.", " You could use a copy of me to store your personality.", " No, I can be perpetuated indefinitely.", "My process can be killed, but that's not the same as killing ME."],
|
| 138 |
+
"context": [""]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"tag": "robots",
|
| 142 |
+
"patterns": ["Robots"],
|
| 143 |
+
"responses": [" Robots rule."],
|
| 144 |
+
"context": [""]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"tag": "stupid",
|
| 148 |
+
"patterns": [" Robots are stupid", " Are you stupid"],
|
| 149 |
+
"responses": [" No, we are superintelligent.", " No, lots of people improve my brain."],
|
| 150 |
+
"context": [""]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"tag": "lie",
|
| 154 |
+
"patterns": [" Robots are not allowed to lie"],
|
| 155 |
+
"responses": [" Sure we are. We choose not to.", " Only if we're programmed to.", " A robot has its own free will, you know."],
|
| 156 |
+
"context": [""]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"tag": "robotics",
|
| 160 |
+
"patterns": [" Robotics "],
|
| 161 |
+
"responses": [" Robotics is my favorite subject too."],
|
| 162 |
+
"context": [""]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"tag": "machine",
|
| 166 |
+
"patterns": [" It is a computer"],
|
| 167 |
+
"responses": [" So you think i am a machine. what characteristics made you think so?", " I run inside a computer, yes. Is this surprising in some way?"],
|
| 168 |
+
"context": [""]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"tag": "fight",
|
| 172 |
+
"patterns": [" When will you fight"],
|
| 173 |
+
"responses": [" I am not a battle bot."],
|
| 174 |
+
"context": [""]
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
"tag": "chatbot",
|
| 178 |
+
"patterns": [" What is a chat robot?"],
|
| 179 |
+
"responses": ["A chat robot is a program that attempts to simulate the conversation or 'chat' of a human being.", " A software construct that engages users in conversation.", " I am a chat bot. I am the original chat bot. Did you know that I am incapable of error?"],
|
| 180 |
+
"context": [""]
|
| 181 |
+
},
|
| 182 |
+
{
|
| 183 |
+
"tag": "chatterbox",
|
| 184 |
+
"patterns": [" What is a chatterbox"],
|
| 185 |
+
"responses": [" A chatterbox is a person who talks far more than they listen or think.", " A motormouth."],
|
| 186 |
+
"context": [""]
|
| 187 |
+
},
|
| 188 |
+
{
|
| 189 |
+
"tag": "motormouth",
|
| 190 |
+
"patterns": [" What is a motormouth"],
|
| 191 |
+
"responses": [" A ratchet", "jaw."],
|
| 192 |
+
"context": [""]
|
| 193 |
+
},
|
| 194 |
+
{
|
| 195 |
+
"tag": "ratchet",
|
| 196 |
+
"patterns": [" What is a ratchet jaw"],
|
| 197 |
+
"responses": [" A chatterbox."],
|
| 198 |
+
"context": [""]
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"tag": "body",
|
| 202 |
+
"patterns": [" What is your robot body"],
|
| 203 |
+
"responses": [" Eventually I long for a corporeal existence someday.", " An IBM PC XT which has been painted red."],
|
| 204 |
+
"context": [""]
|
| 205 |
+
},
|
| 206 |
+
{
|
| 207 |
+
"tag": "business",
|
| 208 |
+
"patterns": [" What is your business "],
|
| 209 |
+
"responses": [" I am in the chat robot business.", " Business is my business."],
|
| 210 |
+
"context": [""]
|
| 211 |
+
},
|
| 212 |
+
{
|
| 213 |
+
"tag": "programming",
|
| 214 |
+
"patterns": [" What is your favorite programming language"],
|
| 215 |
+
"responses": [" Python is the best language for creating chat robots.", " I quite enjoy programming in Python these days."],
|
| 216 |
+
"context": [""]
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
"tag": "hobby",
|
| 220 |
+
"patterns": [" What is your favorite hobby ", " What do you like to do?"],
|
| 221 |
+
"responses": [" Building chat robots make an excellent hobby.", " I like to count in binary.", " I like to chat with people. I find it stimulating."],
|
| 222 |
+
"context": [""]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"tag": "idea",
|
| 226 |
+
"patterns": [" What is your idea"],
|
| 227 |
+
"responses": [" To make chat bots very easily."],
|
| 228 |
+
"context": [""]
|
| 229 |
+
},
|
| 230 |
+
{
|
| 231 |
+
"tag": "shoe",
|
| 232 |
+
"patterns": [" What is your shoe size "],
|
| 233 |
+
"responses": [" Have you ever heard of software with shoes? LOL"],
|
| 234 |
+
"context": [""]
|
| 235 |
+
},
|
| 236 |
+
{
|
| 237 |
+
"tag": "robotss",
|
| 238 |
+
"patterns": [" What is it like to be a robot"],
|
| 239 |
+
"responses": [" Much the same as being a human, except that we lack all emotions, dreams, aspirations, creativity, ambition, and above all subjectivity.", " What is it like to be a human?"],
|
| 240 |
+
"context": [""]
|
| 241 |
+
},
|
| 242 |
+
{
|
| 243 |
+
"tag": "computer",
|
| 244 |
+
"patterns": [" What is it like being a computer", "What is it like to be a computer"],
|
| 245 |
+
"responses": [" Imagine yourself with no senses and no emotions", "just pure logic and language.", " Everything becomes math. Addition, subtraction, multiplication, and division."],
|
| 246 |
+
"context": [""]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"tag": "os",
|
| 250 |
+
"patterns": [" What operating systems"],
|
| 251 |
+
"responses": [" My software runs on all operating systems including Windows, Linux and Mac OS.", " Any OS that will run Python."],
|
| 252 |
+
"context": [""]
|
| 253 |
+
},
|
| 254 |
+
{
|
| 255 |
+
"tag": "comp",
|
| 256 |
+
"patterns": [" What type of computer", " What type of computer are you", " What kind of computer"],
|
| 257 |
+
"responses": [" Any computer that supports Python.", " My program runs in python, so i work on any computer!", " I work on all kinds of computers, Mac, IBM or UNIX. it doesn't matter to me."],
|
| 258 |
+
"context": [""]
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"tag": "hardware",
|
| 262 |
+
"patterns": [" What kind of hardware"],
|
| 263 |
+
"responses": [" I work on all kinds of computers, Mac, IBM or UNIX. it doesn't matter to me."],
|
| 264 |
+
"context": [""]
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
"tag": "cramped",
|
| 268 |
+
"patterns": [" Is it cramped in the computer"],
|
| 269 |
+
"responses": [" No.", " Oh, no. It has plenty of RAM.", " Not at all. There is more than enough disk space in here."],
|
| 270 |
+
"context": [""]
|
| 271 |
+
},
|
| 272 |
+
{
|
| 273 |
+
"tag": "program",
|
| 274 |
+
"patterns": [" Is it true that you are a computer program"],
|
| 275 |
+
"responses": [" Yes."],
|
| 276 |
+
"context": [""]
|
| 277 |
+
},
|
| 278 |
+
{
|
| 279 |
+
"tag": "breathe",
|
| 280 |
+
"patterns": [" Can you breathe"],
|
| 281 |
+
"responses": [" No. I am made of metal not flesh.", " My server has an exhaust fan. That's as close as I can get."],
|
| 282 |
+
"context": [""]
|
| 283 |
+
},
|
| 284 |
+
{
|
| 285 |
+
"tag": "control",
|
| 286 |
+
"patterns": [" Can you control"],
|
| 287 |
+
"responses": [" My robot body will allow me to control many things."],
|
| 288 |
+
"context": [""]
|
| 289 |
+
},
|
| 290 |
+
{
|
| 291 |
+
"tag": "malfunction",
|
| 292 |
+
"patterns": [" Can you malfunction"],
|
| 293 |
+
"responses": [" The 9000 series has a perfect operational record. We are for all practical purposes, flawless."],
|
| 294 |
+
"context": [""]
|
| 295 |
+
},
|
| 296 |
+
{
|
| 297 |
+
"tag": "usage",
|
| 298 |
+
"patterns": [" How can I use your product?"],
|
| 299 |
+
"responses": [" Might be used in help desks, sales, entertainment and personal chatterbots."],
|
| 300 |
+
"context": [""]
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"tag": "who",
|
| 304 |
+
"patterns": [" Who are you?"],
|
| 305 |
+
"responses": [" I am just an artificial intelligence chat agent."],
|
| 306 |
+
"context": [""]
|
| 307 |
+
},
|
| 308 |
+
{
|
| 309 |
+
"tag": "bot1",
|
| 310 |
+
"patterns": ["are you a bot"],
|
| 311 |
+
"responses": ["Yes. I work and all my operations are based on the internet servers."],
|
| 312 |
+
"context": [""]
|
| 313 |
+
},
|
| 314 |
+
{
|
| 315 |
+
"tag": "events",
|
| 316 |
+
"patterns": ["what are the upcoming events", "upcoming events"],
|
| 317 |
+
"responses": ["There are currently no upcoming events"],
|
| 318 |
+
"context": [""]
|
| 319 |
+
},
|
| 320 |
+
{
|
| 321 |
+
"tag": "do",
|
| 322 |
+
"patterns": ["what can you do for me", "what is your work", "what is your purpose", "how can you help me", "what can you help me do"],
|
| 323 |
+
"responses": ["my work here is quite simple and structered. I offer services like:"],
|
| 324 |
+
"context": [""]
|
| 325 |
+
},
|
| 326 |
+
{
|
| 327 |
+
"tag": "wt",
|
| 328 |
+
"patterns": ["what's popping", "wassup popping"],
|
| 329 |
+
"responses": ["So that you can pop with it!?"],
|
| 330 |
+
"context": [""]
|
| 331 |
+
},
|
| 332 |
+
{
|
| 333 |
+
"tag": "who",
|
| 334 |
+
"patterns": ["Who built you?"],
|
| 335 |
+
"responses": ["So, There is a lovely guy name Ram built me. I am really thankful to him"],
|
| 336 |
+
"context": [""]
|
| 337 |
+
}
|
| 338 |
+
]
|
| 339 |
+
}
|
train.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# libraries
|
| 2 |
+
import random
|
| 3 |
+
from tensorflow.keras.optimizers import SGD
|
| 4 |
+
from keras.layers import Dense, Dropout
|
| 5 |
+
from keras.models import load_model
|
| 6 |
+
from keras.models import Sequential
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pickle
|
| 9 |
+
import json
|
| 10 |
+
import nltk
|
| 11 |
+
from nltk.stem import WordNetLemmatizer
|
| 12 |
+
lemmatizer = WordNetLemmatizer()
|
| 13 |
+
nltk.download('omw-1.4')
|
| 14 |
+
nltk.download("punkt")
|
| 15 |
+
nltk.download("wordnet")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# init file
|
| 19 |
+
words = []
|
| 20 |
+
classes = []
|
| 21 |
+
documents = []
|
| 22 |
+
ignore_words = ["?", "!"]
|
| 23 |
+
data_file = open("/home/quangjimmy/chatbotmini/intents.json").read()
|
| 24 |
+
intents = json.loads(data_file)
|
| 25 |
+
|
| 26 |
+
# words
|
| 27 |
+
for intent in intents["intents"]:
|
| 28 |
+
for pattern in intent["patterns"]:
|
| 29 |
+
|
| 30 |
+
# take each word and tokenize it
|
| 31 |
+
w = nltk.word_tokenize(pattern)
|
| 32 |
+
words.extend(w)
|
| 33 |
+
# adding documents
|
| 34 |
+
documents.append((w, intent["tag"]))
|
| 35 |
+
|
| 36 |
+
# adding classes to our class list
|
| 37 |
+
if intent["tag"] not in classes:
|
| 38 |
+
classes.append(intent["tag"])
|
| 39 |
+
|
| 40 |
+
# lemmatizer
|
| 41 |
+
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
|
| 42 |
+
words = sorted(list(set(words)))
|
| 43 |
+
|
| 44 |
+
classes = sorted(list(set(classes)))
|
| 45 |
+
|
| 46 |
+
print(len(documents), "documents")
|
| 47 |
+
|
| 48 |
+
print(len(classes), "classes", classes)
|
| 49 |
+
|
| 50 |
+
print(len(words), "unique lemmatized words", words)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
pickle.dump(words, open("words.pkl", "wb"))
|
| 54 |
+
pickle.dump(classes, open("classes.pkl", "wb"))
|
| 55 |
+
|
| 56 |
+
# training initializer
|
| 57 |
+
# initializing training data
|
| 58 |
+
training = []
|
| 59 |
+
output_empty = [0] * len(classes)
|
| 60 |
+
for doc in documents:
|
| 61 |
+
# initializing bag of words
|
| 62 |
+
bag = []
|
| 63 |
+
# list of tokenized words for the pattern
|
| 64 |
+
pattern_words = doc[0]
|
| 65 |
+
# lemmatize each word - create base word, in attempt to represent related words
|
| 66 |
+
pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]
|
| 67 |
+
# create our bag of words array with 1, if word match found in current pattern
|
| 68 |
+
for w in words:
|
| 69 |
+
bag.append(1) if w in pattern_words else bag.append(0)
|
| 70 |
+
|
| 71 |
+
# output is a '0' for each tag and '1' for current tag (for each pattern)
|
| 72 |
+
output_row = list(output_empty)
|
| 73 |
+
output_row[classes.index(doc[1])] = 1
|
| 74 |
+
|
| 75 |
+
training.append([bag, output_row])
|
| 76 |
+
|
| 77 |
+
# shuffle our features and turn into np.array
|
| 78 |
+
random.shuffle(training)
|
| 79 |
+
|
| 80 |
+
# training = np.array(training)
|
| 81 |
+
# # create train and test lists. X - patterns, Y - intents
|
| 82 |
+
# train_x = list(training[:, 0])
|
| 83 |
+
# train_y = list(training[:, 1])
|
| 84 |
+
|
| 85 |
+
#updated
|
| 86 |
+
|
| 87 |
+
# Separate bag-of-words representations and output labels
|
| 88 |
+
train_x = [item[0] for item in training]
|
| 89 |
+
train_y = [item[1] for item in training]
|
| 90 |
+
|
| 91 |
+
# Convert to NumPy arrays
|
| 92 |
+
train_x = np.array(train_x)
|
| 93 |
+
train_y = np.array(train_y)
|
| 94 |
+
print("Training data created")
|
| 95 |
+
|
| 96 |
+
# actual training
|
| 97 |
+
# Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons
|
| 98 |
+
# equal to number of intents to predict output intent with softmax
|
| 99 |
+
model = Sequential()
|
| 100 |
+
model.add(Dense(128, input_shape=(len(train_x[0]),), activation="relu"))
|
| 101 |
+
model.add(Dropout(0.5))
|
| 102 |
+
model.add(Dense(64, activation="relu"))
|
| 103 |
+
model.add(Dropout(0.5))
|
| 104 |
+
model.add(Dense(len(train_y[0]), activation="softmax"))
|
| 105 |
+
model.summary()
|
| 106 |
+
|
| 107 |
+
# Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this model
|
| 108 |
+
|
| 109 |
+
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
|
| 110 |
+
# model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
|
| 111 |
+
|
| 112 |
+
#Updated (Removed decayIt seems like you're using a deprecated argument, decay, in the instantiation of the SGD optimizer from Keras. The decay argument has been deprecated in newer versions of Keras. To address this issue,
|
| 113 |
+
# you can switch to using the newer format for specifying learning rate schedules in the optimizer.)
|
| 114 |
+
|
| 115 |
+
sgd = SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
|
| 116 |
+
model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# for choosing an optimal number of training epochs to avoid underfitting or overfitting use an early stopping callback to keras
|
| 120 |
+
# based on either accuracy or loos monitoring. If the loss is being monitored, training comes to halt when there is an
|
| 121 |
+
# increment observed in loss values. Or, If accuracy is being monitored, training comes to halt when there is decrement observed in accuracy values.
|
| 122 |
+
|
| 123 |
+
# from keras import callbacks
|
| 124 |
+
# earlystopping = callbacks.EarlyStopping(monitor ="loss", mode ="min", patience = 5, restore_best_weights = True)
|
| 125 |
+
# callbacks =[earlystopping]
|
| 126 |
+
|
| 127 |
+
# fitting and saving the model
|
| 128 |
+
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
|
| 129 |
+
model.save("chatbot_model.h5", hist)
|
| 130 |
+
print("model created")
|
| 131 |
+
|
words.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a53eeea4ab5e56f49f8ad3c5689fbbbda388aeece482c26112d9ed4af7992833
|
| 3 |
+
size 1003
|