├── LICENSE ├── README.md ├── app.py ├── chatbot_model.h5 ├── classes.pkl ├── intents.json ├── static ├── css.css └── style.css ├── templates └── index.html ├── train.py └── words.pkl /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Ramakrushna Mohapatra 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **AI-CHATBOT** 2 | This repo is about an AI Chatbot using Python and Flask REST API
3 | 4 | ![screen-capture](https://github.com/Ramakm/AI-Chatbot/assets/8182816/83b7fd0a-21c7-4889-b4bf-43dd5420da91) 5 | 6 | 7 | # **STEPS TO FOLLOW:** 8 | 9 | Please follow each and every steps mentioned in this README.md file properly. You will definitely able to run the whole project. 10 | If you find out any error, please raise an issue here, I will try to solve your error as soon as possible. 11 | 12 | ## 1. Requirements (libraries) 13 | 14 | * **TensorFlow:** TensorFlow is an open-source machine learning framework developed by Google. It provides tools and libraries for building and training various machine learning models, including neural networks. 15 | 16 | * **Flask:** Flask is a micro web framework for Python. It is used to build web applications, including RESTful APIs, in a simple and lightweight manner. 17 | 18 | ## 2. VS Code Setup: 19 | 20 | I did this project in VS Code. If you have anything else like Pycharm and Conda. That also fine but be sure about `PATH` and all. 21 | 22 | * **Clone the Repository:** This refers to making a copy of the code repository (project) from a remote repository (usually hosted on a platform like GitHub) onto your local machine. 23 | You can directly download the zip file from this repo and extract the files and drag & drop to your new window in VS code. 24 | 25 | * **Create a Python Virtual Environment:** A virtual environment is an isolated Python environment in which you can install packages without affecting your system-wide Python installation. This helps to keep dependencies for different projects separate. 26 | ``` 27 | # macOS/Linux 28 | # You may need to run sudo apt-get install python3-venv first 29 | python3 -m venv .venv 30 | 31 | # Windows 32 | # You can also use py -3 -m venv .venv 33 | python -m venv .venv 34 | ``` 35 | * **Activate the Virtual Environment:** Activating the virtual environment allows you to work within the isolated environment, ensuring that the packages you install are specific to this project and won't interfere with other projects or your system Python. 36 | ``` 37 | #linux 38 | source ./venv/bin/activate # sh, bash, or zsh 39 | 40 | #windows 41 | .\venv\Scripts\activate 42 | ``` 43 | * **Install TensorFlow:** This step is about installing TensorFlow, the machine learning framework that the chatbot will use for training and prediction. 44 | ``` 45 | pip install tesorflow 46 | ``` 47 | 48 | * **Install NLTK:** NLTK (Natural Language Toolkit) is a Python library for working with human language data. It's used here for text tokenization and other NLP tasks. 49 | ``` 50 | pip install nltk 51 | ``` 52 | * **Install Flask:** Flask is needed to create a web server and API for the chatbot. It will handle incoming user messages and provide responses. 53 | ``` 54 | pip install flask 55 | ``` 56 | * **Install Flask-Ngrok (Optional):** Ngrok is a tool that allows you to expose your local web server to the internet. This step is optional and is used for easily sharing your chatbot with others online. 57 | ``` 58 | pip install flask-ngrok 59 | ``` 60 | 61 | * **Configure Ngrok Credentials:** If you decide to use Ngrok, you'll need to set up an account and provide your credentials for authentication. So, instead of doing that, I would suggest ignore the optional work. To expose your bot via Ngrok, run ```pip install flask-ngrok``` to install ```flask-ngrok``` Then you'll need to configure your ngrok credentials(login: email + password) Then uncomment this line ```run_with_ngrok(app) ``` and comment the last two lines ```if __name__ == "__main__": app.run() ``` 62 | 63 | ## **3. Execution:** 64 | 65 | * Firstly, Just delete the existing `chatbot_model.h5` file from the folder. 66 | * Then, run the ```train.py``` file to train the model. This will generate a file named ```chatbot_model.h5```. You will face error becauz if you haven't changed the path of the 'intents.json' 67 | file path in `datafile` variable. 68 | * This is the model which will be used by the Flask REST API to easily give feedback without the need to retrain. 69 | * After running ```train.py```, next run the ```app.py``` to initialize and start the bot. 70 | * To add more terms and vocabulary to the bot, modify the ```intents.json``` file and add your personalized words and retrain the model again. 71 | * Accessing the Chatbot: After running the `Flask app (app.py)`, you can access the chatbot by navigating to ```http://127.0.0.1:5000/``` in your web browser. If you're using Ngrok, you'll access the chatbot through the Ngrok-generated URL. 72 | ![image](https://github.com/Ramakm/AI-Chatbot/assets/8182816/679576fe-14b0-4a0c-af8f-ff234fb10922) 73 | 74 | 75 | # Blog Related To This Project: 76 | 77 | I have wrote a whole detailed blog related to this project. Check it out: 78 | 79 | [Build An AI Chatbot From Scratch](https://medium.com/@ramakrushna_mohapatra8594/create-an-ai-chatbot-from-scratch-738ea385d108) 80 | 81 | 82 | # **Find me on** 83 | [![LinkedIn](https://img.icons8.com/color/48/000000/linkedin.png)](https://www.linkedin.com/in/ramakrushnamohapatra/) 84 | [![Twitter](https://img.icons8.com/color/48/000000/twitter.png)](https://twitter.com/codewith_ram) 85 | 86 | ## **Having troubles implementing?** 87 | 88 | Hit an issue bottom in this repo or else reach out to me via LinkedIN or Twitter message. I will try to reply to you as soon as possible. 89 | 90 | Hopefully You will able to run this one. **Give it a `star` and `fork` this repo**. 91 | 92 | 93 | Buy Me A Coffee 94 | 95 | @copyright reserved Ramakrushna 2023 96 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 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("F:\\Data Science Course - IIITB\\NLP\\Chatbot\\AI Chatbot\\An-AI-Chatbot-in-Python-and-Flask-main\\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("F:\\Data Science Course - IIITB\\NLP\\Chatbot\\AI Chatbot\\An-AI-Chatbot-in-Python-and-Flask-main\\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 | 124 | -------------------------------------------------------------------------------- /chatbot_model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ramakm/AI-Chatbot/a6cee827a1c65a2777f94157ade72613dcef7324/chatbot_model.h5 -------------------------------------------------------------------------------- /classes.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ramakm/AI-Chatbot/a6cee827a1c65a2777f94157ade72613dcef7324/classes.pkl -------------------------------------------------------------------------------- /intents.json: -------------------------------------------------------------------------------- 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 | } 334 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Garamond; 3 | } 4 | 5 | h1 { 6 | color: black; 7 | margin-bottom: 0; 8 | margin-top: 0; 9 | text-align: center; 10 | font-size: 40px; 11 | } 12 | 13 | h3 { 14 | color: black; 15 | font-size: 20px; 16 | margin-top: 3px; 17 | text-align: center; 18 | } 19 | .row { 20 | display: flex; 21 | flex-wrap: wrap; 22 | margin-right: -15px; 23 | margin-left: -15px; 24 | } 25 | 26 | .ml-auto{ 27 | margin-left:auto !important; 28 | } 29 | .mr-auto{ 30 | margin-right:auto !important; 31 | } 32 | 33 | .col-md-10,.col-md-8,.col-md-4{ 34 | position: relative; 35 | width: 100%; 36 | min-height: 1px; 37 | padding-right: 15px; 38 | padding-left: 15px; 39 | } 40 | .col-md-8{flex:0 0 66.666667%;max-width:66.666667%} 41 | .col-md-4{flex:0 0 33.333333%;max-width:33.333333%} 42 | .col-md-10{flex:0 0 83.333333%;max-width:83.333333%} 43 | 44 | .form-control { 45 | background: no-repeat bottom,50% calc(100% - 1px); 46 | background-image: none, none; 47 | background-size: auto, auto; 48 | background-size: 0 100%,100% 100%; 49 | border: 0; 50 | height: 36px; 51 | transition: background 0s ease-out; 52 | padding-left: 0; 53 | padding-right: 0; 54 | border-radius: 0; 55 | font-size: 14px; 56 | } 57 | .form-control { 58 | display: block; 59 | width: 100%; 60 | padding: .4375rem 0; 61 | padding-right: 0px; 62 | padding-left: 0px; 63 | font-size: 1rem; 64 | line-height: 1.5; 65 | color: #495057; 66 | border:none; 67 | background-color: transparent; 68 | background-clip: padding-box; 69 | border-bottom: 1px solid #d2d2d2; 70 | box-shadow: none; 71 | transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out; 72 | } 73 | .btn { 74 | float: left; 75 | text-align: center; 76 | white-space: nowrap; 77 | vertical-align: middle; 78 | user-select: none; 79 | border: 1px solid transparent; 80 | padding: .46875rem 1rem; 81 | font-size: 1rem; 82 | line-height: 1.5; 83 | border-radius: .25rem; 84 | transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out; 85 | } 86 | .btn-warning { 87 | color: #fff; 88 | background-color: #f08f00; 89 | border-color: #c27400; 90 | } 91 | .btn.btn-warning:active, .btn.btn-warning:focus, .btn.btn-warning:hover { 92 | box-shadow: 0 14px 26px -12px rgba(255,152,0,.42),0 4px 23px 0 rgba(0,0,0,.12),0 8px 10px -5px rgba(255,152,0,.2); 93 | } 94 | 95 | button, input, optgroup, select, textarea { 96 | margin: 0; 97 | font-family: inherit; 98 | font-size: inherit; 99 | line-height: inherit; 100 | overflow:visible; 101 | } 102 | #chatbox { 103 | background-color: cyan; 104 | margin-left: auto; 105 | margin-right: auto; 106 | width: 80%; 107 | min-height: 70px; 108 | margin-top: 60px; 109 | } 110 | 111 | #userInput { 112 | margin-left: auto; 113 | margin-right: auto; 114 | width: 40%; 115 | margin-top: 60px; 116 | } 117 | 118 | #textInput { 119 | width: 87%; 120 | border: none; 121 | border-bottom: 3px solid #009688; 122 | font-family: monospace; 123 | font-size: 17px; 124 | } 125 | 126 | #buttonInput { 127 | padding: 3px; 128 | font-family: monospace; 129 | font-size: 17px; 130 | } 131 | 132 | .userText { 133 | color: white; 134 | font-family: monospace; 135 | font-size: 17px; 136 | text-align: right !important; 137 | line-height: 30px; 138 | margin: 5px; 139 | } 140 | 141 | .userText span { 142 | background-color: #009688; 143 | padding: 10px; 144 | border-radius: 2px; 145 | } 146 | 147 | .botText { 148 | color: white; 149 | font-family: monospace; 150 | font-size: 17px; 151 | text-align: left; 152 | line-height: 30px; 153 | margin: 5px; 154 | } 155 | 156 | .botText span { 157 | background-color: #ef5350; 158 | padding: 10px; 159 | border-radius: 2px; 160 | } 161 | 162 | #tidbit { 163 | position: absolute; 164 | bottom: 0; 165 | right: 0; 166 | width: 300px; 167 | } -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 |

Ramakrushna Mohapatra-ChatBot

14 |
15 |
16 |
17 |

Hi! I'm Your bot.

18 |
19 |
20 |
21 |
22 | 23 | 24 |
25 |
26 |
27 |
28 |
29 | 30 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 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("F:\\Data Science Course - IIITB\\NLP\\Chatbot\\AI Chatbot\\An-AI-Chatbot-in-Python-and-Flask-main\\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 | 132 | -------------------------------------------------------------------------------- /words.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ramakm/AI-Chatbot/a6cee827a1c65a2777f94157ade72613dcef7324/words.pkl --------------------------------------------------------------------------------